diff --git a/pyproger/admin/views.py b/pyproger/admin/views.py index 80a788e..af2ad0a 100644 --- a/pyproger/admin/views.py +++ b/pyproger/admin/views.py @@ -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 diff --git a/pyproger/blog/urls.py b/pyproger/blog/urls.py index 738bcd0..59f7906 100644 --- a/pyproger/blog/urls.py +++ b/pyproger/blog/urls.py @@ -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("/", 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/") 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/", 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, + ) diff --git a/pyproger/dbase/database.py b/pyproger/dbase/database.py index 196b1e9..0f51234 100644 --- a/pyproger/dbase/database.py +++ b/pyproger/dbase/database.py @@ -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 diff --git a/pyproger/templates/blog/index.html b/pyproger/templates/blog/index.html index 0de2d79..d29b929 100644 --- a/pyproger/templates/blog/index.html +++ b/pyproger/templates/blog/index.html @@ -9,11 +9,7 @@ {% endblock %} {% block content %} -
-
-
- -
+
{% for p in posts %} @@ -22,11 +18,17 @@ {{ p.Post.description | safe}} - Опубликовал - - {{ p.User.username}} - - {{ p.Post.create_datetime.strftime('%d %B, %Y') }} + + {% for t in p.Post.tags %} + + #{{t.tag}} + + {% endfor %}
+ Опубликовал + + {{ p.User.username}} + + {{ p.Post.create_datetime.strftime('%d %B, %Y') }}

{% endfor %} diff --git a/pyproger/templates/blog/postview.html b/pyproger/templates/blog/postview.html index f1e098b..575f60b 100644 --- a/pyproger/templates/blog/postview.html +++ b/pyproger/templates/blog/postview.html @@ -15,5 +15,8 @@

{{ post.Post.title | safe }}

{{ post.Post.text | safe }}
+ + Назад + {% endblock %} diff --git a/pyproger/templates/blog/tags.html b/pyproger/templates/blog/tags.html new file mode 100644 index 0000000..fd8e50a --- /dev/null +++ b/pyproger/templates/blog/tags.html @@ -0,0 +1,28 @@ +{% extends 'blog/base.html' %} + +{% block title %} +{{title}} +{% endblock %} + +{% block menu_title %} +{{ menu_title }} +{% endblock %} + +{% block content %} + +
+
+

Тэги статей:

+ {% for t in tags %} +
  • + + #{{t.tag}} + +
  • + {% endfor %} +
    + + Назад + + +{% endblock %} diff --git a/pyproger/translations/translation.py b/pyproger/translations/translation.py index 5b722a7..ae6e8fd 100644 --- a/pyproger/translations/translation.py +++ b/pyproger/translations/translation.py @@ -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")