From 162d0ce55bd23dc1e11d741a7f204ed8cdc3728c Mon Sep 17 00:00:00 2001 From: SvyatoslavArtymovych Date: Thu, 15 Jun 2023 12:52:51 +0300 Subject: [PATCH] hotfix queries --- app/templates/book/favorite_books.html | 4 +-- app/templates/book/my_contributions.html | 2 +- app/templates/book/my_library.html | 25 +++++++++--------- app/views/book/book.py | 33 +++++++++++++++++------- app/views/home.py | 8 +++--- app/views/search.py | 12 ++++----- 6 files changed, 50 insertions(+), 34 deletions(-) diff --git a/app/templates/book/favorite_books.html b/app/templates/book/favorite_books.html index 8ecde31..077ddc3 100644 --- a/app/templates/book/favorite_books.html +++ b/app/templates/book/favorite_books.html @@ -6,7 +6,7 @@ {% block content %}
+ class="pt-1 relative shadow-md sm:rounded-lg mt-1 h-box flex"> {% if not current_user.is_authenticated %}
@@ -31,7 +31,7 @@ {% endif %} {% for book in books %} -
+
{{book.label}}
{% if book.versions %} diff --git a/app/templates/book/my_contributions.html b/app/templates/book/my_contributions.html index 7ef51ee..e158792 100644 --- a/app/templates/book/my_contributions.html +++ b/app/templates/book/my_contributions.html @@ -6,7 +6,7 @@ {% block content %}
+ class="pt-1 relative shadow-md sm:rounded-lg mt-1 h-box flex"> {% if not current_user.is_authenticated %}
diff --git a/app/templates/book/my_library.html b/app/templates/book/my_library.html index fe46e70..7ddb5f2 100644 --- a/app/templates/book/my_library.html +++ b/app/templates/book/my_library.html @@ -11,7 +11,7 @@ {% endif %} -
+
{% if not current_user.is_authenticated %}
{% endif %} -
- {% if current_user.is_authenticated and books.total %} -
-

My library

- {% if current_user.is_authenticated %} - {% include 'book/components/header_buttons.html' %} - {% endif %} + {% if current_user.is_authenticated and books.total %} +
+
+

My library

+ {% if current_user.is_authenticated %} + {% include 'book/components/header_buttons.html' %} + {% endif %} +
- {% endif %} + {% endif %} {% for book in books if not book.is_deleted %} -
+
{% if book.original_book %} diff --git a/app/views/book/book.py b/app/views/book/book.py index 0471734..35f5f90 100644 --- a/app/views/book/book.py +++ b/app/views/book/book.py @@ -1,6 +1,6 @@ from flask import render_template, flash, redirect, url_for, request from flask_login import login_required, current_user -from sqlalchemy import and_, or_, func +from sqlalchemy import and_, or_, func, distinct from app.controllers import ( register_book_verify_route, @@ -32,8 +32,10 @@ def my_library(): db.session.query( m.Book, m.Book.created_at.label("created_at"), - func.count(m.Interpretation.id).label("interpretations_count"), - func.count(m.BookStar.id).label("stars_count"), + func.count(distinct(m.Interpretation.id)).label( + "interpretations_count" + ), + func.count(distinct(m.BookStar.id)).label("stars_count"), ) .join( m.BookStar, @@ -49,6 +51,7 @@ def my_library(): m.BookVersion.book_id == m.Book.id, m.BookVersion.is_deleted == False, # noqa: E712 ), + full=True, ) .join( m.Section, @@ -210,8 +213,10 @@ def favorite_books(): db.session.query( m.Book, m.Book.created_at.label("created_at"), - func.count(m.Interpretation.id).label("interpretations_count"), - func.count(m.BookStar.id).label("stars_count"), + func.count(distinct(m.Interpretation.id)).label( + "interpretations_count" + ), + func.count(distinct(m.BookStar.id)).label("stars_count"), ) .join( m.BookStar, @@ -275,14 +280,24 @@ def my_contributions(): m.Interpretation, m.Interpretation.score.label("score"), m.Interpretation.created_at.label("created_at"), - func.count(m.Comment.interpretation_id).label("comments_count"), + func.count(distinct(m.Comment.interpretation_id)).label( + "comments_count" + ), ) .join( - m.Comment, m.Comment.interpretation_id == m.Interpretation.id, full=True + m.Comment, + and_( + m.Comment.interpretation_id == m.Interpretation.id, + m.Comment.is_deleted == False, # noqa: E712 + ), + full=True, ) .join( m.InterpretationVote, - m.InterpretationVote.interpretation_id == m.Interpretation.id, + and_( + m.InterpretationVote.interpretation_id == m.Interpretation.id, + m.Interpretation.is_deleted == False, # noqa: E712 + ), full=True, ) .filter( @@ -301,7 +316,7 @@ def my_contributions(): ), ), m.Interpretation.is_deleted == False, # noqa: E712 - m.Interpretation.copy_of == None, # noqa: E711 + m.Interpretation.copy_of == 0, ) .group_by(m.Interpretation.id) ) diff --git a/app/views/home.py b/app/views/home.py index 99ad2c2..8d9f01f 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_, func +from sqlalchemy import and_, func, distinct from app import models as m, db from app.logger import log from app.controllers.sorting import sort_by @@ -20,7 +20,7 @@ def get_all(): m.Interpretation, m.Interpretation.score.label("score"), m.Interpretation.created_at.label("created_at"), - func.count(m.Comment.interpretation_id).label("comments_count"), + func.count(distinct(m.Comment.interpretation_id)).label("comments_count"), ) .join( m.Comment, @@ -53,8 +53,8 @@ def explore_books(): db.session.query( m.Book, m.Book.created_at.label("created_at"), - func.count(m.Interpretation.id).label("interpretations_count"), - func.count(m.BookStar.id).label("stars_count"), + func.count(distinct(m.Interpretation.id)).label("interpretations_count"), + func.count(distinct(m.BookStar.id)).label("stars_count"), ) .join( m.BookStar, diff --git a/app/views/search.py b/app/views/search.py index 468a30e..106bc64 100644 --- a/app/views/search.py +++ b/app/views/search.py @@ -18,7 +18,7 @@ def search_interpretations(): log(log.INFO, "Starting to build query for interpretations") interpretations = m.Interpretation.query.order_by(m.Interpretation.id).filter( (func.lower(m.Interpretation.plain_text).like(f"%{q}%")), - m.Interpretation.copy_of == None, # noqa: E711 + m.Interpretation.copy_of == 0, ) log(log.INFO, "Get count of interpretations") count = interpretations.count() @@ -55,18 +55,18 @@ def search_books(): and_( func.lower(m.Collection.label).like(f"%{q}%"), m.Collection.is_deleted == False, # noqa: E712 - m.Collection.copy_of == None, # noqa: E711 + m.Collection.copy_of == 0, m.Collection.is_root == False, # noqa: E712 ), and_( func.lower(m.Section.label).like(f"%{q}%"), - m.Section.copy_of == None, # noqa: E711 + m.Section.copy_of == 0, m.Section.is_deleted == False, # noqa: E712 ), and_( func.lower(m.Interpretation.plain_text).like(f"%{q}%"), m.Interpretation.is_deleted == False, # noqa: E712 - m.Interpretation.copy_of == None, # noqa: E711 + m.Interpretation.copy_of == 0, m.Section.is_deleted == False, # noqa: E712 ), ), @@ -157,7 +157,7 @@ def tag_search_interpretations(): func.lower(m.Tag.name) == (tag_name), m.InterpretationTag.tag_id == m.Tag.id, m.Interpretation.id == m.InterpretationTag.interpretation_id, - m.Interpretation.copy_of == None, # noqa: E711 + m.Interpretation.copy_of == 0, m.Interpretation.is_deleted == False, # noqa: E712 ) ) @@ -221,7 +221,7 @@ def quick_search(): m.Interpretation.query.order_by(m.Interpretation.id) .filter( (func.lower(m.Interpretation.plain_text).like(f"%{search_query}%")), - m.Interpretation.copy_of == None, # noqa: E711 + m.Interpretation.copy_of == 0, m.Interpretation.is_deleted == False, # noqa: E712, ) .limit(2)