From adf0c14907265c401b26177c01bf6f947f0b558d Mon Sep 17 00:00:00 2001 From: SvyatoslavArtymovych Date: Mon, 1 May 2023 15:31:48 +0300 Subject: [PATCH 1/2] Fix displaying sections on different levels --- app/templates/book/interpretation_view.html | 8 +- app/templates/book/section_view.html | 14 ++- app/views/book.py | 125 ++++++++++++-------- 3 files changed, 88 insertions(+), 59 deletions(-) diff --git a/app/templates/book/interpretation_view.html b/app/templates/book/interpretation_view.html index bbdafef..1fb585b 100644 --- a/app/templates/book/interpretation_view.html +++ b/app/templates/book/interpretation_view.html @@ -9,7 +9,7 @@ {{ book.owner.username }}/{{ book.label }}

- {{collection.label}}/{{sub_collection.label}}/{{section.label}} + {{collection.label}} {% if sub_collection %}/ {{sub_collection.label}} {% endif %} /{{section.label}}

@@ -32,7 +32,11 @@
{% for interpretation in section.interpretations %} - + {% if sub_collection %} + + {% else %} + + {% endif %}
diff --git a/app/views/book.py b/app/views/book.py index e2946f5..d20cb1b 100644 --- a/app/views/book.py +++ b/app/views/book.py @@ -76,7 +76,7 @@ def create(): return redirect(url_for("book.my_books")) -@bp.route("/", methods=["GET"]) +@bp.route("//collections", methods=["GET"]) def collection_view(book_id: int): book = db.session.get(m.Book, book_id) if not book or book.is_deleted: @@ -87,7 +87,7 @@ def collection_view(book_id: int): return render_template("book/collection_view.html", book=book) -@bp.route("//", methods=["GET"]) +@bp.route("///subcollections", methods=["GET"]) def sub_collection_view(book_id: int, collection_id: int): book: m.Book = db.session.get(m.Book, book_id) if not book or book.is_deleted: @@ -101,11 +101,8 @@ def sub_collection_view(book_id: int, collection_id: int): flash("Collection not found", "danger") return redirect(url_for("book.collection_view", book_id=book_id)) if collection.is_leaf: - return render_template( - "book/section_view.html", - book=book, - collection=collection, - sub_collection=collection, + return redirect( + url_for("book.section_view", book_id=book.id, collection_id=collection.id) ) else: return render_template( @@ -113,44 +110,13 @@ def sub_collection_view(book_id: int, collection_id: int): ) -@bp.route("///", methods=["GET"]) -def section_view(book_id: int, collection_id: int, sub_collection_id: int): - book: m.Book = db.session.get(m.Book, book_id) - if not book or book.is_deleted: - log(log.WARNING, "Book with id [%s] not found", book_id) - flash("Book not found", "danger") - return redirect(url_for("book.my_books")) - - collection: m.Collection = db.session.get(m.Collection, collection_id) - if not collection or collection.is_deleted: - log(log.WARNING, "Collection with id [%s] not found", collection_id) - flash("Collection not found", "danger") - return redirect(url_for("book.collection_view", book_id=book_id)) - - sub_collection: m.Collection = db.session.get(m.Collection, sub_collection_id) - if not sub_collection or sub_collection.is_deleted: - log(log.WARNING, "Sub_collection with id [%s] not found", sub_collection_id) - flash("Sub_collection not found", "danger") - return redirect( - url_for( - "book.sub_collection_view", book_id=book_id, collection_id=collection_id - ) - ) - else: - return render_template( - "book/section_view.html", - book=book, - collection=collection, - sub_collection=sub_collection, - ) - - +@bp.route("///sections", methods=["GET"]) @bp.route( - "////", + "////sections", methods=["GET"], ) -def interpretation_view( - book_id: int, collection_id: int, sub_collection_id: int, section_id: int +def section_view( + book_id: int, collection_id: int, sub_collection_id: int | None = None ): book: m.Book = db.session.get(m.Book, book_id) if not book or book.is_deleted: @@ -164,15 +130,72 @@ def interpretation_view( flash("Collection not found", "danger") return redirect(url_for("book.collection_view", book_id=book_id)) - sub_collection: m.Collection = db.session.get(m.Collection, sub_collection_id) - if not sub_collection or sub_collection.is_deleted: - log(log.WARNING, "Sub_collection with id [%s] not found", sub_collection_id) - flash("Sub_collection not found", "danger") - return redirect( - url_for( - "book.sub_collection_view", book_id=book_id, collection_id=collection_id + sub_collection = None + if sub_collection_id: + sub_collection: m.Collection = db.session.get(m.Collection, sub_collection_id) + if not sub_collection or sub_collection.is_deleted: + log(log.WARNING, "Sub_collection with id [%s] not found", sub_collection_id) + flash("Sub_collection not found", "danger") + return redirect( + url_for( + "book.sub_collection_view", + book_id=book_id, + collection_id=collection_id, + ) + ) + + if sub_collection: + sections = sub_collection.sections + else: + sections = collection.sections + + return render_template( + "book/section_view.html", + book=book, + collection=collection, + sections=sections, + sub_collection=sub_collection, + ) + + +@bp.route( + "////interpretations", + methods=["GET"], +) +@bp.route( + "/////interpretations", + methods=["GET"], +) +def interpretation_view( + book_id: int, + collection_id: int, + section_id: int, + sub_collection_id: int | None = None, +): + book: m.Book = db.session.get(m.Book, book_id) + if not book or book.is_deleted: + log(log.WARNING, "Book with id [%s] not found", book_id) + flash("Book not found", "danger") + return redirect(url_for("book.my_books")) + + collection: m.Collection = db.session.get(m.Collection, collection_id) + if not collection or collection.is_deleted: + log(log.WARNING, "Collection with id [%s] not found", collection_id) + flash("Collection not found", "danger") + return redirect(url_for("book.collection_view", book_id=book_id)) + + if sub_collection_id: + sub_collection: m.Collection = db.session.get(m.Collection, sub_collection_id) + if not sub_collection or sub_collection.is_deleted: + log(log.WARNING, "Sub_collection with id [%s] not found", sub_collection_id) + flash("Sub_collection not found", "danger") + return redirect( + url_for( + "book.sub_collection_view", + book_id=book_id, + collection_id=collection_id, + ) ) - ) section: m.Section = db.session.get(m.Section, section_id) if not section: @@ -191,7 +214,7 @@ def interpretation_view( "book/interpretation_view.html", book=book, collection=collection, - sub_collection=sub_collection, + sub_collection=sub_collection if sub_collection_id else None, section=section, ) From 816425fc89438e8b2c5fe483f5294b04528975df Mon Sep 17 00:00:00 2001 From: SvyatoslavArtymovych Date: Mon, 1 May 2023 15:39:59 +0300 Subject: [PATCH 2/2] fix error with url_for on home page --- app/templates/home/index.html | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/templates/home/index.html b/app/templates/home/index.html index 234d4f0..64376db 100644 --- a/app/templates/home/index.html +++ b/app/templates/home/index.html @@ -65,11 +65,11 @@ {% endblock %}