Просмотр статей по тэгам

main
Сергей Ванюшкин 2023-09-25 15:53:34 +03:00
parent 7c6fc02529
commit 6163505ca0
7 changed files with 116 additions and 40 deletions

View File

@ -1,4 +1,4 @@
from flask import abort, g, redirect, request, url_for
from flask import abort, redirect, request, url_for
from flask_admin.contrib import sqla
from flask_ckeditor import CKEditorField
from flask_security import current_user

View File

@ -1,8 +1,10 @@
import locale
from flask import render_template, render_template_string, request
from flask import (redirect, render_template, render_template_string, request,
session, url_for)
from ..dbase.database import get_paginated_posts, get_post
from ..dbase.database import (get_all_posts_by_tag, get_paginated_posts,
get_post, get_tags)
from .blog import bp
locale.setlocale(locale.LC_ALL, "")
@ -11,14 +13,14 @@ locale.setlocale(locale.LC_ALL, "")
@bp.route("/", methods=["GET"], defaults={"page": 1})
@bp.route("/<int:page>", methods=["GET"])
def index(page=1):
per_page = 3
session["back_url"] = request.url
per_page = 2
posts, total_pages = get_paginated_posts(page, per_page)
list_pages = [
x for x in range(1, total_pages + 1) if x >= page - 2 and x <= page + 2
]
return render_template(
"blog/index.html",
request=request,
posts=posts,
title="pyproger - разговоры про питон",
menu_title="pyproger",
@ -31,13 +33,48 @@ def index(page=1):
@bp.route("/post/")
@bp.route("/post/<path:slug>")
def post(slug=None):
if slug:
back_url = session.get("back_url")
if slug is not None:
current_post = get_post(slug)
return render_template(
"blog/postview.html",
title=f"pyproger - {current_post.Post.title}",
menu_title="pyproger",
post=current_post,
back_url=back_url,
)
else:
return render_template_string("noup")
@bp.route("/tags/")
def get_all_tags():
tags = get_tags()
return render_template(
"blog/tags.html",
tags=tags,
title="pyproger - поиск по тэгу",
menu_title="pyproger",
)
@bp.route("/tag/", methods=["GET"], defaults={"page": 1})
@bp.route("/tag/<path:tag>", methods=["GET"])
def get_posts_by_tag(page=1):
tag = request.args.get("tag")
if tag is None:
return redirect(url_for(".get_all_tags"))
per_page = 2
posts, total_pages = get_all_posts_by_tag(tag, page, per_page)
list_pages = [
x for x in range(1, total_pages + 1) if x >= page - 2 and x <= page + 2
]
return render_template(
"blog/index.html",
posts=posts,
title=f"pyproger - посты по {tag}",
menu_title="pyproger",
page=page,
total_pages=total_pages,
list_pages=list_pages,
)

View File

@ -1,17 +1,21 @@
from logging import error
from . import db
from .models import Post, User
from .models import Post, Tag, User
def get_paginated_posts(page, per_page):
quer = (
all_post_query = (
db.session.query(Post, User)
.join(User, Post.author == User.id)
.order_by(Post.create_datetime.desc())
.paginate(page=page, per_page=per_page, error_out=False)
.paginate(page=page, per_page=per_page, error_out=True)
)
total_pages = (
all_post_query.total // per_page + [0, 1][all_post_query.total % per_page != 0]
)
total_pages = quer.total // per_page + [0, 1][quer.total % per_page != 0]
return quer, total_pages
return all_post_query, total_pages
def get_post(slug):
@ -22,3 +26,23 @@ def get_post(slug):
.first()
)
return post_query
def get_tags():
tags_query = db.session.query(Tag).order_by(Tag.tag)
return tags_query
def get_all_posts_by_tag(tag, page, per_page):
posts_query = (
db.session.query(Post, User)
.join(User, Post.author == User.id)
.filter(Post.tags.any(Tag.tag == tag))
.order_by(Post.create_datetime.desc())
.paginate(page=page, per_page=per_page, error_out=True)
)
total_pages = (
posts_query.total // per_page + [0, 1][posts_query.total % per_page != 0]
)
print(posts_query)
return posts_query, total_pages

View File

@ -9,10 +9,6 @@
{% endblock %}
{% block content %}
<div class="conteiner">
<br>
</div>
<div class="container px-4 px-lg-5">
<div class="list-group justify-content-center">
@ -22,6 +18,12 @@
{{ p.Post.description | safe}}
</a>
<small>
{% for t in p.Post.tags %}
<a class="link-offset-2 link-offset-3-hover link-underline link-underline-opacity-0 link-underline-opacity-75-hover" href="{{url_for(".get_posts_by_tag", tag=t.tag )}}">
#{{t.tag}}
</a>
{% endfor %}<br>
Опубликовал
<a href="mailto:{{p.User.email}}">
{{ p.User.username}}

View File

@ -15,5 +15,8 @@
<h4>{{ post.Post.title | safe }}</h4>
{{ post.Post.text | safe }}
</div>
<a class="link-offset-2 link-offset-3-hover link-underline link-underline-opacity-0 link-underline-opacity-75-hover" href="{{ back_url }}">
Назад
</a>
{% endblock %}

View File

@ -0,0 +1,28 @@
{% extends 'blog/base.html' %}
{% block title %}
{{title}}
{% endblock %}
{% block menu_title %}
{{ menu_title }}
{% endblock %}
{% block content %}
<!-- Main Content-->
<div class="conteiner">
<br>
<h4>Тэги статей:</h4>
{% for t in tags %}
<li>
<a class="link-offset-2 link-offset-3-hover link-underline link-underline-opacity-0 link-underline-opacity-75-hover" href="{{url_for(".get_posts_by_tag", tag=t.tag )}}">
#{{t.tag}}
</a>
</li>
{% endfor %}
</div>
<a class="link-offset-2 link-offset-3-hover link-underline link-underline-opacity-0 link-underline-opacity-75-hover" href="{{ back_url }}">
Назад
</a>
{% endblock %}

View File

@ -5,7 +5,7 @@ from flask_babel import Babel
def get_locale():
translations = ["en", "ru"]
translations = ["ru"]
return request.accept_languages.best_match(translations)
@ -14,22 +14,4 @@ babel = Babel()
bp = Blueprint(
"bp_translation",
__name__,
cli_group="translate",
)
@bp.cli.command("update")
def update():
"""Update all languages."""
if os.system("pybabel extract -F babel.cfg -k _l -o messages.pot ."):
raise RuntimeError("extract command failed")
if os.system("pybabel update -i messages.pot -d pi3code/translations"):
raise RuntimeError("update command failed")
os.remove("messages.pot")
@bp.cli.command("compile")
def compile():
"""Compile all languages."""
if os.system("pybabel compile -d pi3code/translations"):
raise RuntimeError("compile command failed")