From 8493087f662b3f777925cb1eb76a5cec6c069950 Mon Sep 17 00:00:00 2001 From: Kostiantyn Stoliarskyi Date: Wed, 14 Jun 2023 11:09:02 +0300 Subject: [PATCH] done with first kind of sorting --- .../book/components/header_buttons.html | 4 +- app/views/home.py | 50 ++++++++++++------- migrations/versions/f104cc0131c5_score.py | 34 +++++++++++++ 3 files changed, 68 insertions(+), 20 deletions(-) create mode 100644 migrations/versions/f104cc0131c5_score.py diff --git a/app/templates/book/components/header_buttons.html b/app/templates/book/components/header_buttons.html index e1d0cf4..b03e331 100644 --- a/app/templates/book/components/header_buttons.html +++ b/app/templates/book/components/header_buttons.html @@ -22,9 +22,9 @@ {% if selected_tab=='my_library' or selected_tab=='favorite_books' or selected_tab=='explore_books'%}
  • Most favored
  • {% endif %} -
  • Most recent
  • +
  • Most recent
  • {% if selected_tab=='latest_interpretations' or selected_tab=='my_contributions' %} -
  • Most comments
  • +
  • Most comments
  • {% endif %} {% if selected_tab=='my_library' or selected_tab=='favorite_books' or selected_tab=='explore_books'%}
  • Most interpretations
  • diff --git a/app/views/home.py b/app/views/home.py index 14de716..5bc37e3 100644 --- a/app/views/home.py +++ b/app/views/home.py @@ -3,7 +3,7 @@ from flask import ( render_template, request, ) -from sqlalchemy import and_ +from sqlalchemy import and_, func, text from app import models as m, db from app.logger import log from app.controllers import create_pagination @@ -15,24 +15,36 @@ bp = Blueprint("home", __name__, url_prefix="/home") @bp.route("/", methods=["GET"]) def get_all(): sort = request.args.get("sort") - interpretations = db.session.query( - m.Interpretation, - ).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 + interpretations = ( + db.session.query( + m.Interpretation, + func.count(m.Comment.interpretation_id).label("comments_count"), ) + .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) ) match sort: case "upvoted": - interpretations = interpretations.order_by(m.Interpretation.score) + 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")) case _: interpretations = interpretations.order_by( m.Interpretation.created_at.desc() @@ -44,11 +56,13 @@ def get_all(): log(log.INFO, "Returns data for front end") log(log.INFO, "Returning data to front end") + interpretations = interpretations.paginate( + page=pagination.page, per_page=pagination.per_page + ) + interpretations.items = [item[0] for item in interpretations.items] return render_template( "home/index.html", - interpretations=interpretations.paginate( - page=pagination.page, per_page=pagination.per_page - ), + interpretations=interpretations, page=pagination, ) diff --git a/migrations/versions/f104cc0131c5_score.py b/migrations/versions/f104cc0131c5_score.py new file mode 100644 index 0000000..b294629 --- /dev/null +++ b/migrations/versions/f104cc0131c5_score.py @@ -0,0 +1,34 @@ +"""score + +Revision ID: f104cc0131c5 +Revises: ad0ed27f417f +Create Date: 2023-06-13 17:04:08.590895 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = "f104cc0131c5" +down_revision = "ad0ed27f417f" +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table("interpretations", schema=None) as batch_op: + batch_op.add_column( + sa.Column("score", sa.Integer(), nullable=True, server_default="0") + ) + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table("interpretations", schema=None) as batch_op: + batch_op.drop_column("score") + + # ### end Alembic commands ###