From d6c55661dca284ba4ecffea4bf2e68a011c40c9f Mon Sep 17 00:00:00 2001 From: SvyatoslavArtymovych Date: Tue, 30 May 2023 14:10:04 +0300 Subject: [PATCH] start refactor breadcrumbs --- .vscode/settings.json | 1 + app/controllers/breadcrumbs.py | 46 +++++++++++++++----------------- app/views/book/interpretation.py | 23 +++++++--------- 3 files changed, 31 insertions(+), 39 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 8e68863..9d4f8ba 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -11,6 +11,7 @@ "**/.venv/*/**": true }, "cSpell.words": [ + "backref", "bookname", "Btns", "CLEANR", diff --git a/app/controllers/breadcrumbs.py b/app/controllers/breadcrumbs.py index 250c90a..2c994ff 100644 --- a/app/controllers/breadcrumbs.py +++ b/app/controllers/breadcrumbs.py @@ -5,11 +5,27 @@ from app import models as m, db from app import schema as s +def create_collections_breadcrumb( + bread_crumbs: list[s.BreadCrumb], collection: m.Collection +) -> list[s.BreadCrumb]: + bread_crumbs += [ + s.BreadCrumb( + type=s.BreadCrumbType.Collection, + url="", + label=collection.label, + ) + ] + + if collection.parent and not collection.parent.is_root: + create_collections_breadcrumb(bread_crumbs, collection.parent) + + def create_breadcrumbs( book_id: int, collection_path: tuple[int], section_id: int = 0, interpretation_id: int = 0, + collection_id: int = 0, ) -> list[s.BreadCrumb]: """ How breadcrumbs look like: @@ -52,28 +68,12 @@ def create_breadcrumbs( ) ] - for index, collection_id in enumerate(collection_path): - if collection_id is None: - continue - collection: m.Collection = db.session.get(m.Collection, collection_id) - if index == 0: - crumples += [ - s.BreadCrumb( - type=s.BreadCrumbType.Collection, - url="", - label=collection.label, - ) - ] - elif index == 1: - crumples += [ - s.BreadCrumb( - type=s.BreadCrumbType.Section, - url="", - label=collection.label, - ) - ] + collections_crumbs = [] + collection: m.Collection = db.session.get(m.Collection, collection_id) + create_collections_breadcrumb(collections_crumbs, collection) + crumples += collections_crumbs.reverse() - if section_id and collection_path: + if section_id and collection_id: section: m.Section = db.session.get(m.Section, section_id) crumples += [ s.BreadCrumb( @@ -81,10 +81,6 @@ def create_breadcrumbs( url=url_for( "book.interpretation_view", book_id=book_id, - collection_id=collection_path[0], - sub_collection_id=collection_path[-1] - if len(collection_path) == 2 - else collection_path[0], section_id=section_id, ), label=section.label, diff --git a/app/views/book/interpretation.py b/app/views/book/interpretation.py index 4108f6d..f4ec30c 100644 --- a/app/views/book/interpretation.py +++ b/app/views/book/interpretation.py @@ -41,10 +41,7 @@ def interpretation_view( # FIXME breadcrumbs # breadcrumbs = create_breadcrumbs( # book_id=book_id, - # collection_path=( - # collection_id, - # sub_collection_id, - # ), + # collection_path=(section.collection.id,), # section_id=section_id, # ) return render_template( @@ -192,19 +189,17 @@ def qa_view(book_id: int, interpretation_id: int): return redirect(url_for("book.collection_view", book_id=book_id)) # FIXME - # breadcrumbs = create_breadcrumbs( - # book_id=book_id, - # collection_path=( - # collection_id, - # sub_collection_id, - # ), - # section_id=section_id, - # interpretation_id=interpretation.id, - # ) + breadcrumbs = create_breadcrumbs( + book_id=book_id, + collection_path=(interpretation.section.collection.id,), + section_id=interpretation.section.id, + interpretation_id=interpretation.id, + _collection_id=interpretation.section.collection.id, + ) return render_template( "book/qa_view.html", book=book, section=interpretation.section, interpretation=interpretation, - breadcrumbs=[], + breadcrumbs=breadcrumbs, )