diff --git a/migrations/versions/e24055e16b26_.py b/migrations/versions/e24055e16b26_.py new file mode 100644 index 0000000..4943b16 --- /dev/null +++ b/migrations/versions/e24055e16b26_.py @@ -0,0 +1,42 @@ +"""empty message + +Revision ID: e24055e16b26 +Revises: 8c415e462cb3 +Create Date: 2023-10-02 09:30:10.566908 + +""" +import flask_security +import sqlalchemy as sa +from alembic import op + +# revision identifiers, used by Alembic. +revision = "e24055e16b26" +down_revision = "8c415e462cb3" +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table( + "page", + sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), + sa.Column("name", sa.String(length=20), nullable=True), + sa.Column("slug", sa.String(length=50), nullable=False), + sa.Column("text", sa.Text(), nullable=True), + sa.PrimaryKeyConstraint("id"), + sa.UniqueConstraint("id"), + ) + with op.batch_alter_table("post", schema=None) as batch_op: + batch_op.create_unique_constraint(None, ["id"]) + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table("post", schema=None) as batch_op: + batch_op.drop_constraint(None, type_="unique") + + op.drop_table("page") + # ### end Alembic commands ### diff --git a/pyproger/admin/views.py b/pyproger/admin/views.py index af2ad0a..077ebbe 100644 --- a/pyproger/admin/views.py +++ b/pyproger/admin/views.py @@ -87,3 +87,15 @@ class PostView(MyAdminView): ) create_template = "admin/edit.html" edit_template = "admin/edit.html" + + +class PageView(MyAdminView): + column_labels = dict( + name="Название страницы", + slug="URL страницы", + text="Содержимое страницы", + ) + + form_overrides = dict(text=CKEditorField) + create_template = "admin/edit.html" + edit_template = "admin/edit.html" diff --git a/pyproger/app.py b/pyproger/app.py index 07dbf9d..8c27951 100644 --- a/pyproger/app.py +++ b/pyproger/app.py @@ -5,7 +5,8 @@ from flask_migrate import Migrate from flask_security.core import Security from pyproger.dbase import Role, User, db, user_datastore -from pyproger.dbase.models import Post, Tag +from pyproger.dbase.database import get_menu_items +from pyproger.dbase.models import Page, Post, Tag def create_app(test_config=None): @@ -37,7 +38,7 @@ def create_app(test_config=None): admin.init_app(app) - from pyproger.admin.views import PostView, RoleView, TagView, UserView + from pyproger.admin.views import PageView, PostView, RoleView, TagView, UserView admin.add_view( RoleView( @@ -71,6 +72,14 @@ def create_app(test_config=None): name="Посты", ) ) + admin.add_view( + PageView( + Page, + db.session, + category=" Страницы", + name="Страницы блога", + ) + ) from pyproger.cli.commands import bp_cli diff --git a/pyproger/blog/urls.py b/pyproger/blog/urls.py index 5c16c19..ce6e3ed 100644 --- a/pyproger/blog/urls.py +++ b/pyproger/blog/urls.py @@ -12,6 +12,8 @@ from flask import ( from ..dbase.database import ( get_all_posts_by_tag, + get_menu_items, + get_page, get_paginated_posts, get_post, get_tags, @@ -31,11 +33,13 @@ def index(page=1): list_pages = [ x for x in range(1, total_pages + 1) if x >= page - 2 and x <= page + 2 ] + menu_items = get_menu_items() return render_template( "blog/index.html", - posts=posts, title="pyproger - разговоры про питон", menu_title="pyproger", + menu_items=menu_items, + posts=posts, page=page, total_pages=total_pages, list_pages=list_pages, @@ -51,11 +55,13 @@ def post(slug=None): if current_post is None: return abort(404) + menu_items = get_menu_items() return render_template( "blog/postview.html", title=f"pyproger - {current_post.Post.title}", menu_title="pyproger", + menu_items=menu_items, post=current_post, back_url=back_url, ) @@ -66,11 +72,13 @@ def post(slug=None): @bp.route("/tags/") def get_all_tags(): tags = get_tags() + menu_items = get_menu_items() return render_template( "blog/tags.html", - tags=tags, title="pyproger - поиск по тэгу", menu_title="pyproger", + tags=tags, + menu_items=menu_items, ) @@ -89,34 +97,30 @@ def get_posts_by_tag(page=1, tag=None): list_pages = [ x for x in range(1, total_pages + 1) if x >= page - 2 and x <= page + 2 ] + menu_items = get_menu_items() + return render_template( "blog/index.html", - posts=posts, title=f"pyproger - посты по {tag}", menu_title="pyproger", + menu_items=menu_items, + posts=posts, page=page, total_pages=total_pages, list_pages=list_pages, ) -@bp.route("/about") -def about(): +@bp.route("/") +def page(slug=None): + page = get_page(slug) + if page is None: + abort(404) + menu_items = get_menu_items() return render_template( "blog/page.html", - title="pyproger - О сайте", + title=f"pyproger - {page.name}", menu_title="pyproger", - content_head="О сайте", - content_body="описание", - ) - - -@bp.route("/contacts") -def contacts(): - return render_template( - "blog/page.html", - title="pyproger - Контакты", - menu_title="pyproger", - content_head="Контакты", - content_body="описание", + menu_items=menu_items, + content_body=page.text, ) diff --git a/pyproger/dbase/database.py b/pyproger/dbase/database.py index 35c0a11..7c6e443 100644 --- a/pyproger/dbase/database.py +++ b/pyproger/dbase/database.py @@ -1,5 +1,5 @@ from . import db -from .models import Post, Tag, User +from .models import Page, Post, Tag, User def get_paginated_posts(page, per_page): @@ -45,3 +45,13 @@ def get_all_posts_by_tag(tag, page, per_page): if posts_query.total == 0: return None, None return posts_query, total_pages + + +def get_page(slug): + page_query = db.session.query(Page).filter(Page.slug == slug).one_or_none() + return page_query + + +def get_menu_items(): + menu_items = db.session.query(Page.name, Page.slug).all() + return menu_items diff --git a/pyproger/dbase/models.py b/pyproger/dbase/models.py index adf56eb..c51dc8c 100644 --- a/pyproger/dbase/models.py +++ b/pyproger/dbase/models.py @@ -1,6 +1,5 @@ import datetime -from flask_security import current_user from flask_security.models import fsqla from sqlalchemy import Boolean, Column, DateTime, Integer, String, Text @@ -87,3 +86,17 @@ class Post(db.Model): onupdate=datetime.datetime.utcnow(), ) text = Column(Text) + + +class Page(db.Model): + __tablename__ = "page" + id = Column( + Integer, + primary_key=True, + nullable=False, + unique=True, + autoincrement=True, + ) + name = Column(String(20)) + slug = Column(String(50), nullable=False) + text = Column(Text) diff --git a/pyproger/templates/blog/base.html b/pyproger/templates/blog/base.html index dcaeda9..a435a5d 100644 --- a/pyproger/templates/blog/base.html +++ b/pyproger/templates/blog/base.html @@ -44,14 +44,12 @@ Статьи по темам + {% for m in menu_items %}
  • О сайте -
  • -
  • - Контакты + href="/{{m.slug}}">{{m.name}}
  • + {% endfor %} @@ -124,14 +122,13 @@ Статьи по темам + + {% for m in menu_items %}
  • О сайте -
  • -
  • - Контакты + href="/{{m.slug}}">{{m.name}}
  • + {% endfor %}