Fix displaying sections on different levels

This commit is contained in:
SvyatoslavArtymovych 2023-05-01 15:31:48 +03:00
parent 5bdaa1e0b1
commit adf0c14907
3 changed files with 88 additions and 59 deletions

View File

@ -9,7 +9,7 @@
{{ book.owner.username }}/{{ book.label }}
</h1>
<h1 class="text-2xl font-extrabold dark:text-white ml-4">
{{collection.label}}/{{sub_collection.label}}/{{section.label}}
{{collection.label}} {% if sub_collection %}/ {{sub_collection.label}} {% endif %} /{{section.label}}
</h1>
</div>
</div>
@ -32,7 +32,11 @@
<dl class="w-md md:w-full text-gray-900 divide-y divide-gray-200 dark:text-white dark:divide-gray-700">
<!-- prettier-ignore -->
{% for interpretation in section.interpretations %}
<a href="{{url_for('book.interpretation_view',book_id=book.id,collection_id=collection.id,sub_collection_id=sub_collection.id, section_id=section.id)}}" >
{% if sub_collection %}
<a href="{{url_for('book.interpretation_view', book_id=book.id, collection_id=collection.id, sub_collection_id=sub_collection.id, section_id=section.id)}}" >
{% else %}
<a href="{{url_for('book.interpretation_view', book_id=book.id, collection_id=collection.id, section_id=section.id)}}" >
{% endif %}
<dl class="bg-white dark:bg-gray-900 max-w-full p-3 text-gray-900 divide-y divide-gray-200 dark:text-white dark:divide-gray-700 m-3 border-2 border-gray-200 border-solid rounded-lg dark:border-gray-700">
<div class="flex flex-col pb-3 p-3 w-full">
<!-- prettier-ignore -->

View File

@ -23,7 +23,7 @@
<div class="flex p-2">
<div>
<h1 class="text-l font-extrabold dark:text-white ml-4"> {{ book.owner.username }}/{{ book.label }} </h1>
<h1 class="text-2xl font-extrabold dark:text-white ml-4">{{collection.label}}/{{sub_collection.label}}</h1>
<h1 class="text-2xl font-extrabold dark:text-white ml-4">{{collection.label}}{% if sub_collection %}/ {{sub_collection.label}} {% endif %} </h1>
</div>
</div>
<!-- prettier-ignore -->
@ -54,10 +54,14 @@
<dl
class="w-md md:w-full text-gray-900 divide-y divide-gray-200 dark:text-white dark:divide-gray-700">
<!-- prettier-ignore -->
{% for section in sub_collection.sections %}
{% for section in sections %}
<!-- prettier-ignore -->
<a href="{{url_for('book.interpretation_view',book_id=book.id,collection_id=collection.id,sub_collection_id=sub_collection.id, section_id=section.id)}}">
{% if sub_collection %}
<a href="{{url_for('book.interpretation_view', book_id=book.id, collection_id=collection.id, sub_collection_id=sub_collection.id, section_id=section.id)}}">
{% else %}
<a href="{{url_for('book.interpretation_view', book_id=book.id, collection_id=collection.id, section_id=section.id)}}">
{% endif %}
<dl class="bg-white dark:bg-gray-900 max-w-full p-3 text-gray-900 divide-y divide-gray-200 dark:text-white dark:divide-gray-700 m-3 border-2 border-gray-200 border-solid rounded-lg dark:border-gray-700">
<div class="flex flex-col pb-3 p-3 w-full">
<dt class="flex w-full mb-1 text-gray-500 md:text-lg dark:text-gray-400 flex-col">
@ -87,9 +91,7 @@
id="about"
role="tabpanel"
aria-labelledby="about-tab">
<p class="text-sm text-gray-500 dark:text-gray-400">
This is about of {{sub_collection.label}}
</p>
<p class="text-sm text-gray-500 dark:text-gray-400">This is about</p>
</div>
</div>

View File

@ -76,7 +76,7 @@ def create():
return redirect(url_for("book.my_books"))
@bp.route("/<int:book_id>", methods=["GET"])
@bp.route("/<int:book_id>/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("/<int:book_id>/<int:collection_id>", methods=["GET"])
@bp.route("/<int:book_id>/<int:collection_id>/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("/<int:book_id>/<int:collection_id>/<int:sub_collection_id>", 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("/<int:book_id>/<int:collection_id>/sections", methods=["GET"])
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/<int:section_id>",
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/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(
"/<int:book_id>/<int:collection_id>/<int:section_id>/interpretations",
methods=["GET"],
)
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/<int:section_id>/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,
)