хранение статик страниц в бд и их редактировани из админки. автоматическое создание ссылок на них на страницах

main
Сергей Ванюшкин 2023-10-02 23:32:19 +03:00
parent 9d92bcac90
commit 03cf3ffd19
7 changed files with 120 additions and 33 deletions

View File

@ -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 ###

View File

@ -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"

View File

@ -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

View File

@ -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("/<path:slug>")
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,
)

View File

@ -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

View File

@ -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)

View File

@ -44,14 +44,12 @@
<a class="link-offset-2 link-underline link-underline-opacity-0 text-white"
href="{{ url_for('bp_blog.get_all_tags')}}">Статьи по темам</a>
</li>
{% for m in menu_items %}
<li>
<a class="link-offset-2 link-underline link-underline-opacity-0 text-white"
href="{{ url_for('bp_blog.about') }}">О сайте</a>
</li>
<li>
<a class="link-offset-2 link-underline link-underline-opacity-0 text-white"
href="{{ url_for('bp_blog.contacts') }}">Контакты</a>
href="/{{m.slug}}">{{m.name}}</a>
</li>
{% endfor %}
</ul>
</div>
</div>
@ -124,14 +122,13 @@
<a class="link-offset-2 link-underline link-underline-opacity-0 text-white"
href="{{ url_for('bp_blog.get_all_tags')}}">Статьи по темам</a>
</li>
{% for m in menu_items %}
<li>
<a class="link-offset-2 link-underline link-underline-opacity-0 text-white"
href="{{ url_for('bp_blog.about') }}">О сайте</a>
</li>
<li>
<a class="link-offset-2 link-underline link-underline-opacity-0 text-white"
href="{{ url_for('bp_blog.contacts') }}">Контакты</a>
href="/{{m.slug}}">{{m.name}}</a>
</li>
{% endfor %}
</ul>
</div>
<!--Grid column-->