Merge pull request #14 from Simple2B/svyat/feat/sections_view

Fix displaying sections on different levels
This commit is contained in:
Svyatoslav Artymovych 2023-05-01 17:29:06 +03:00 committed by GitHub
commit 727b793e48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 92 additions and 63 deletions

View File

@ -9,7 +9,7 @@
{{ book.owner.username }}/{{ book.label }} {{ book.owner.username }}/{{ book.label }}
</h1> </h1>
<h1 class="text-2xl font-extrabold dark:text-white ml-4"> <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> </h1>
</div> </div>
</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"> <dl class="w-md md:w-full text-gray-900 divide-y divide-gray-200 dark:text-white dark:divide-gray-700">
<!-- prettier-ignore --> <!-- prettier-ignore -->
{% for interpretation in section.interpretations %} {% for interpretation in section.interpretations %}
{% 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)}}" > <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"> <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"> <div class="flex flex-col pb-3 p-3 w-full">
<!-- prettier-ignore --> <!-- prettier-ignore -->

View File

@ -23,7 +23,7 @@
<div class="flex p-2"> <div class="flex p-2">
<div> <div>
<h1 class="text-l font-extrabold dark:text-white ml-4"> {{ book.owner.username }}/{{ book.label }} </h1> <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>
</div> </div>
<!-- prettier-ignore --> <!-- prettier-ignore -->
@ -54,10 +54,14 @@
<dl <dl
class="w-md md:w-full text-gray-900 divide-y divide-gray-200 dark:text-white dark:divide-gray-700"> class="w-md md:w-full text-gray-900 divide-y divide-gray-200 dark:text-white dark:divide-gray-700">
<!-- prettier-ignore --> <!-- prettier-ignore -->
{% for section in sub_collection.sections %} {% for section in sections %}
<!-- prettier-ignore --> <!-- prettier-ignore -->
{% 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)}}"> <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"> <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"> <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"> <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" id="about"
role="tabpanel" role="tabpanel"
aria-labelledby="about-tab"> aria-labelledby="about-tab">
<p class="text-sm text-gray-500 dark:text-gray-400"> <p class="text-sm text-gray-500 dark:text-gray-400">This is about</p>
This is about of {{sub_collection.label}}
</p>
</div> </div>
</div> </div>

View File

@ -69,7 +69,7 @@
<!-- prettier-ignore --> <!-- prettier-ignore -->
<h1 class="text-l font-extrabold dark:text-white my-2">{{section.path}}</h1> <h1 class="text-l font-extrabold dark:text-white my-2">{{section.path}}</h1>
{% endfor %} {% endfor %}
<a type="button" href="{{ url_for('section.get_all') }}" class="mt-4 text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center inline-flex items-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800"> Explore all sections... <svg aria-hidden="true" class="w-5 h-5 ml-2 -mr-1" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M10.293 3.293a1 1 0 011.414 0l6 6a1 1 0 010 1.414l-6 6a1 1 0 01-1.414-1.414L14.586 11H3a1 1 0 110-2h11.586l-4.293-4.293a1 1 0 010-1.414z" clip-rule="evenodd"></path></svg> </a> <a type="button" href="{{ url_for('book.get_all') }}" class="mt-4 text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center inline-flex items-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800"> Explore all sections... <svg aria-hidden="true" class="w-5 h-5 ml-2 -mr-1" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M10.293 3.293a1 1 0 011.414 0l6 6a1 1 0 010 1.414l-6 6a1 1 0 01-1.414-1.414L14.586 11H3a1 1 0 110-2h11.586l-4.293-4.293a1 1 0 010-1.414z" clip-rule="evenodd"></path></svg> </a>
</div> </div>
</div> </div>
{% endblock %} {% endblock %}

View File

@ -76,7 +76,7 @@ def create():
return redirect(url_for("book.my_books")) 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): def collection_view(book_id: int):
book = db.session.get(m.Book, book_id) book = db.session.get(m.Book, book_id)
if not book or book.is_deleted: 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) 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): def sub_collection_view(book_id: int, collection_id: int):
book: m.Book = db.session.get(m.Book, book_id) book: m.Book = db.session.get(m.Book, book_id)
if not book or book.is_deleted: 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") flash("Collection not found", "danger")
return redirect(url_for("book.collection_view", book_id=book_id)) return redirect(url_for("book.collection_view", book_id=book_id))
if collection.is_leaf: if collection.is_leaf:
return render_template( return redirect(
"book/section_view.html", url_for("book.section_view", book_id=book.id, collection_id=collection.id)
book=book,
collection=collection,
sub_collection=collection,
) )
else: else:
return render_template( 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"]) @bp.route("/<int:book_id>/<int:collection_id>/sections", 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( @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"], methods=["GET"],
) )
def interpretation_view( def section_view(
book_id: int, collection_id: int, sub_collection_id: int, section_id: int book_id: int, collection_id: int, sub_collection_id: int | None = None
): ):
book: m.Book = db.session.get(m.Book, book_id) book: m.Book = db.session.get(m.Book, book_id)
if not book or book.is_deleted: if not book or book.is_deleted:
@ -164,13 +130,70 @@ def interpretation_view(
flash("Collection not found", "danger") flash("Collection not found", "danger")
return redirect(url_for("book.collection_view", book_id=book_id)) return redirect(url_for("book.collection_view", book_id=book_id))
sub_collection = None
if sub_collection_id:
sub_collection: m.Collection = db.session.get(m.Collection, sub_collection_id) sub_collection: m.Collection = db.session.get(m.Collection, sub_collection_id)
if not sub_collection or sub_collection.is_deleted: if not sub_collection or sub_collection.is_deleted:
log(log.WARNING, "Sub_collection with id [%s] not found", sub_collection_id) log(log.WARNING, "Sub_collection with id [%s] not found", sub_collection_id)
flash("Sub_collection not found", "danger") flash("Sub_collection not found", "danger")
return redirect( return redirect(
url_for( url_for(
"book.sub_collection_view", book_id=book_id, collection_id=collection_id "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,
) )
) )
@ -191,7 +214,7 @@ def interpretation_view(
"book/interpretation_view.html", "book/interpretation_view.html",
book=book, book=book,
collection=collection, collection=collection,
sub_collection=sub_collection, sub_collection=sub_collection if sub_collection_id else None,
section=section, section=section,
) )