2023-04-21 14:40:20 +03:00
|
|
|
from flask import (
|
|
|
|
Blueprint,
|
|
|
|
render_template,
|
2023-06-13 17:03:36 +03:00
|
|
|
request,
|
2023-04-21 14:40:20 +03:00
|
|
|
)
|
2023-06-14 11:09:02 +03:00
|
|
|
from sqlalchemy import and_, func, text
|
2023-05-12 09:55:10 +03:00
|
|
|
from app import models as m, db
|
2023-05-26 16:56:04 +03:00
|
|
|
from app.logger import log
|
2023-06-13 17:03:36 +03:00
|
|
|
from app.controllers import create_pagination
|
2023-05-26 16:56:04 +03:00
|
|
|
|
2023-04-21 14:40:20 +03:00
|
|
|
|
|
|
|
bp = Blueprint("home", __name__, url_prefix="/home")
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/", methods=["GET"])
|
|
|
|
def get_all():
|
2023-06-13 17:03:36 +03:00
|
|
|
sort = request.args.get("sort")
|
2023-06-14 11:09:02 +03:00
|
|
|
interpretations = (
|
|
|
|
db.session.query(
|
|
|
|
m.Interpretation,
|
|
|
|
func.count(m.Comment.interpretation_id).label("comments_count"),
|
2023-05-12 09:55:10 +03:00
|
|
|
)
|
2023-06-14 11:09:02 +03:00
|
|
|
.join(m.Comment, isouter=True)
|
|
|
|
.filter(
|
|
|
|
and_(
|
|
|
|
m.Section.id == m.Interpretation.section_id,
|
|
|
|
m.Collection.id == m.Section.collection_id,
|
|
|
|
m.BookVersion.id == m.Section.version_id,
|
|
|
|
m.Book.id == m.BookVersion.book_id,
|
|
|
|
m.Book.is_deleted == False, # noqa: E712
|
|
|
|
m.BookVersion.is_deleted == False, # noqa: E712
|
|
|
|
m.Interpretation.is_deleted == False, # noqa: E712
|
|
|
|
m.Section.is_deleted == False, # noqa: E712
|
|
|
|
m.Collection.is_deleted == False, # noqa: E712
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.group_by(m.Interpretation.id)
|
2023-05-12 09:55:10 +03:00
|
|
|
)
|
2023-06-13 17:03:36 +03:00
|
|
|
match sort:
|
|
|
|
case "upvoted":
|
2023-06-14 11:09:02 +03:00
|
|
|
interpretations = interpretations.order_by(m.Interpretation.score.desc())
|
|
|
|
case "recent":
|
|
|
|
interpretations = interpretations.order_by(
|
|
|
|
m.Interpretation.created_at.desc()
|
|
|
|
)
|
|
|
|
case "commented":
|
|
|
|
interpretations = interpretations.order_by(text("comments_count DESC"))
|
2023-06-13 17:03:36 +03:00
|
|
|
case _:
|
|
|
|
interpretations = interpretations.order_by(
|
|
|
|
m.Interpretation.created_at.desc()
|
|
|
|
)
|
|
|
|
|
|
|
|
log(log.INFO, "Creating pagination for interpretations")
|
|
|
|
|
|
|
|
pagination = create_pagination(total=interpretations.count())
|
|
|
|
log(log.INFO, "Returns data for front end")
|
2023-04-21 14:40:20 +03:00
|
|
|
|
2023-06-13 17:03:36 +03:00
|
|
|
log(log.INFO, "Returning data to front end")
|
2023-06-14 11:09:02 +03:00
|
|
|
interpretations = interpretations.paginate(
|
|
|
|
page=pagination.page, per_page=pagination.per_page
|
|
|
|
)
|
|
|
|
interpretations.items = [item[0] for item in interpretations.items]
|
2023-04-21 14:40:20 +03:00
|
|
|
return render_template(
|
|
|
|
"home/index.html",
|
2023-06-14 11:09:02 +03:00
|
|
|
interpretations=interpretations,
|
2023-06-13 17:03:36 +03:00
|
|
|
page=pagination,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/explore_books", methods=["GET"])
|
|
|
|
def explore_books():
|
|
|
|
log(log.INFO, "Create query for home page for books")
|
|
|
|
|
|
|
|
books: m.Book = m.Book.query.filter_by(is_deleted=False).order_by(m.Book.id)
|
|
|
|
log(log.INFO, "Creating pagination for books")
|
|
|
|
|
|
|
|
pagination = create_pagination(total=books.count())
|
|
|
|
log(log.INFO, "Returns data for front end")
|
|
|
|
|
|
|
|
return render_template(
|
|
|
|
"home/explore_books.html",
|
|
|
|
books=books.paginate(page=pagination.page, per_page=pagination.per_page),
|
|
|
|
page=pagination,
|
2023-04-21 14:40:20 +03:00
|
|
|
)
|