From 58037e1065151f3cbdfb6fbbc4379597e0b1ecde Mon Sep 17 00:00:00 2001 From: Kostiantyn Stoliarskyi Date: Mon, 22 May 2023 14:17:52 +0300 Subject: [PATCH] done --- app/templates/book/favorite_books.html | 57 ++++++- app/templates/book/my_contributions.html | 183 +++++++++++++++-------- app/templates/book/my_library.html | 5 +- app/templates/book/qa_view.html | 2 +- app/views/book/book.py | 61 +++++++- 5 files changed, 230 insertions(+), 78 deletions(-) diff --git a/app/templates/book/favorite_books.html b/app/templates/book/favorite_books.html index 4c0ad90..3f685d9 100644 --- a/app/templates/book/favorite_books.html +++ b/app/templates/book/favorite_books.html @@ -11,7 +11,7 @@ {% endif %} - {% if current_user.is_authenticated and current_user.stars|length== 0%} + {% if current_user.is_authenticated and not books %}
@@ -20,7 +20,7 @@

Fav books

- {% for book in current_user.stars %} + {% for book in books %}
{{book.label}}
@@ -47,6 +47,59 @@
{% endfor %} + {% if current_user.is_authenticated and page.pages > 1 %} +
+ +
+ {% endif %}
diff --git a/app/templates/book/my_contributions.html b/app/templates/book/my_contributions.html index 246f18b..ae5c4cc 100644 --- a/app/templates/book/my_contributions.html +++ b/app/templates/book/my_contributions.html @@ -20,75 +20,128 @@

My contributions

- {% for interpretation in current_user.contributions %} + {% for interpretation in interpretations %}
-
-
- {% if interpretation.user_id != current_user.id %} -
- -
- {% endif %} - - - {{ interpretation.vote_count }} - - - {% if interpretation.user_id != current_user.id %} -
- -
- {% endif %} -
- -
-
- {% set local_breadcrumbs = interpretation.section.breadcrumbs_path %} - {% include 'book/local_breadcrumbs_navigation.html'%} -

{{ interpretation.section.label }}

- -
-

{{ interpretation.text|safe }}

-
-
-
-
- - {{interpretation.user.username}} on {{interpretation.created_at.strftime('%B %d, %Y')}} -
- -
-
+
+
+ {% if interpretation.user_id != current_user.id %} +
+
-
+ {% endif %} + + + {{ interpretation.vote_count }} + + + {% if interpretation.user_id != current_user.id %} +
+ +
+ {% endif %} +
+ +
+
+ {% set local_breadcrumbs = interpretation.section.breadcrumbs_path %} + {% include 'book/local_breadcrumbs_navigation.html'%} +

{{ interpretation.section.label }}

+ +
+

{{ interpretation.text|safe }}

+
+
+
+
+ + {{interpretation.user.username}} on {{interpretation.created_at.strftime('%B %d, %Y')}} +
+ +
+
+ + {% endfor %} +{% if current_user.is_authenticated and page.pages > 1 %} +
+ +
+ {% endif %} diff --git a/app/templates/book/my_library.html b/app/templates/book/my_library.html index ef19ee1..262380e 100644 --- a/app/templates/book/my_library.html +++ b/app/templates/book/my_library.html @@ -47,7 +47,7 @@ {% endfor %} - + {% if current_user.is_authenticated and page.pages > 1 %}
@@ -102,7 +102,8 @@
{% endif %} - + + {% endblock %} diff --git a/app/templates/book/qa_view.html b/app/templates/book/qa_view.html index 81f9493..f044092 100644 --- a/app/templates/book/qa_view.html +++ b/app/templates/book/qa_view.html @@ -61,7 +61,7 @@
Comments:
- {% for comment in interpretation.comments if not comment.is_deleted %} + {% for comment in interpretation.comments if not comment.is_deleted and not comment.parent_id%}
diff --git a/app/views/book/book.py b/app/views/book/book.py index 44dcba1..961da14 100644 --- a/app/views/book/book.py +++ b/app/views/book/book.py @@ -6,6 +6,7 @@ from flask import ( request, ) from flask_login import login_required, current_user +from sqlalchemy import and_, or_ from app.controllers import ( create_pagination, @@ -40,19 +41,14 @@ def get_all(): @bp.route("/my_library", methods=["GET"]) def my_library(): if current_user.is_authenticated: - q = request.args.get("q", type=str, default=None) books: m.Book = m.Book.query.order_by(m.Book.id) books = books.filter_by(user_id=current_user.id, is_deleted=False) - if q: - books = books.filter(m.Book.label.like(f"{q}")) - pagination = create_pagination(total=books.count()) return render_template( "book/my_library.html", books=books.paginate(page=pagination.page, per_page=pagination.per_page), page=pagination, - search_query=q, ) return render_template( "book/my_library.html", @@ -141,13 +137,62 @@ def statistic_view(book_id: int): @bp.route("/favorite_books", methods=["GET"]) def favorite_books(): - return render_template( - "book/favorite_books.html", - ) + if current_user.is_authenticated: + books = ( + db.session.query( + m.Book, + ) + .filter( + and_( + m.Book.id == m.BookStar.book_id, + m.BookStar.user_id == current_user.id, + m.Book.is_deleted.is_(False), + ) + ) + .order_by(m.Book.created_at.desc()) + ) + + books = books.filter_by(is_deleted=False) + pagination = create_pagination(total=books.count()) + + return render_template( + "book/favorite_books.html", + books=books.paginate(page=pagination.page, per_page=pagination.per_page), + page=pagination, + ) + return render_template("book/favorite_books.html", books=[]) @bp.route("/my_contributions", methods=["GET"]) def my_contributions(): + interpretations = ( + db.session.query( + m.Interpretation, + ) + .filter( + or_( + and_( + m.Interpretation.id == m.Comment.interpretation_id, + m.Comment.user_id == current_user.id, + m.Comment.is_deleted.is_(False), + m.Interpretation.is_deleted.is_(False), + ), + and_( + m.Interpretation.user_id == current_user.id, + m.Interpretation.is_deleted.is_(False), + ), + ) + ) + .group_by(m.Interpretation.id) + .order_by(m.Interpretation.created_at.desc()) + ) + + pagination = create_pagination(total=interpretations.count()) + return render_template( "book/my_contributions.html", + interpretations=interpretations.paginate( + page=pagination.page, per_page=pagination.per_page + ), + page=pagination, )