Skip to content

Commit

Permalink
refactored pages
Browse files Browse the repository at this point in the history
  • Loading branch information
gunlinux committed May 3, 2024
1 parent c9de1e1 commit 65979f4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
28 changes: 17 additions & 11 deletions blog/post/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,43 @@
from sqlalchemy import or_
from blog import cache, db
from blog.post.models import Post
from functools import wraps


post = Blueprint("postb", __name__)


def pages_gen(f):
@wraps(f)
def decorated_function(*args, **kwargs):
page_category = current_app.config["PAGE_CATEGORY"]
pages_query = sa.select(Post).where(Post.category_id == page_category)
pages = db.session.scalars(pages_query).all()
return f(pages=pages, *args, **kwargs)
return decorated_function


@post.route('/')
@cache.cached(timeout=50)
def index():
page_category = current_app.config['PAGE_CATEGORY']
@pages_gen
def index(**kwargs):
post_query = sa.select(Post).where(Post.publishedon != None, # noqa: E711
Post.category_id == None) # noqa: E711
posts = db.session.scalars(post_query).all()
pages_query = sa.select(Post).where(Post.category_id == page_category)
pages = db.session.scalars(pages_query).all()

return render_template("posts.html", posts=posts, pages=pages)
return render_template("posts.html", posts=posts, **kwargs)


@post.route('/<alias>')
@cache.cached(timeout=50)
def view(alias=None):
@pages_gen
def view(alias=None, **kwargs):
page_category = current_app.config["PAGE_CATEGORY"]
post_query = sa.select(Post).where(
or_(Post.publishedon != None, Post.category_id == page_category), # noqa: E711
Post.alias == alias, # noqa: E711
)
post = db.first_or_404(post_query)
pages_query = sa.select(Post).where(Post.category_id == page_category)
pages = db.session.scalars(pages_query).all()

return render_template('post.html', post=post, pages=pages)
return render_template('post.html', post=post, **kwargs)


@post.route('/md/', methods=["POST", "GET"])
Expand Down
29 changes: 11 additions & 18 deletions blog/tags/views.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
import sqlalchemy as sa
from flask import render_template, Blueprint, current_app
from blog.post.models import Post
from flask import render_template, Blueprint
from blog.post.views import pages_gen
from blog.tags.models import Tag
from blog import db

tagsb = Blueprint("tagsb", __name__, url_prefix='/tags')
tagsb = Blueprint("tagsb", __name__, url_prefix="/tags")

PAGE_STATUS = 1
PAGE_SPECIAL = 3


@tagsb.route('/')
def index():
page_category = current_app.config['PAGE_CATEGORY']
@tagsb.route("/")
@pages_gen
def index(**kwargs):
tags = db.session.scalars(sa.select(Tag)).all()
pages_query = sa.select(Post).where(Post.category_id == page_category)
pages = db.session.scalars(pages_query).all()
return render_template("tags.html", tags=tags, pages=pages)
return render_template("tags.html", tags=tags, **kwargs)


@tagsb.route('/<alias>')
def view(alias=None):
page_category = current_app.config['PAGE_CATEGORY']
pages_query = sa.select(Post).where(Post.category_id == page_category)
pages = db.session.scalars(pages_query).all()
@tagsb.route("/<alias>")
@pages_gen
def view(alias=None, **kwargs):
tag_query = sa.select(Tag).where(Tag.alias == alias)
tag = db.first_or_404(tag_query)
return render_template('posts.html', posts=tag.posts, pages=pages, tag=tag)
return render_template("posts.html", posts=tag.posts, tag=tag, **kwargs)

0 comments on commit 65979f4

Please sign in to comment.