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..0cb43ac 100644 --- a/app/controllers/breadcrumbs.py +++ b/app/controllers/breadcrumbs.py @@ -5,11 +5,25 @@ 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,39 +66,32 @@ 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, - ) - ] - - if section_id and collection_path: + section: m.Section = None + if section_id: section: m.Section = db.session.get(m.Section, section_id) + + if collection_id and not section: + collections_crumbs = [] + collection: m.Collection = db.session.get(m.Collection, collection_id) + if not collection.is_root: + create_collections_breadcrumb(collections_crumbs, collection) + collections_crumbs.reverse() + crumples += collections_crumbs + + if section: + collections_crumbs = [] + collection: m.Collection = db.session.get(m.Collection, section.collection_id) + if not collection.is_root: + create_collections_breadcrumb(collections_crumbs, collection) + collections_crumbs.reverse() + crumples += collections_crumbs + crumples += [ s.BreadCrumb( type=s.BreadCrumbType.Section, 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/models/section.py b/app/models/section.py index e00cffd..ac7cb9d 100644 --- a/app/models/section.py +++ b/app/models/section.py @@ -6,6 +6,7 @@ from app.controllers import create_breadcrumbs from .interpretation import Interpretation from .comment import Comment from .interpretation_vote import InterpretationVote +from app import schema as s class Section(BaseModel): @@ -46,16 +47,15 @@ class Section(BaseModel): @property def breadcrumbs_path(self): - parent = self.collection - grand_parent = parent.parent - if grand_parent.is_root: - collection_path = (parent.id,) - else: - collection_path = ( - grand_parent.id, - parent.id, + breadcrumbs_path = create_breadcrumbs( + book_id=self.book_id, collection_id=self.collection.id + ) + if len(breadcrumbs_path) > 5: + breadcrumbs_path = ( + breadcrumbs_path[:3] + + [s.BreadCrumb(type=s.BreadCrumbType.Splitter, url="", label="...")] + + breadcrumbs_path[-2:] ) - breadcrumbs_path = create_breadcrumbs(self.book_id, collection_path) return breadcrumbs_path @property diff --git a/app/schema/breadcrumbs.py b/app/schema/breadcrumbs.py index fb87758..11e4c1d 100644 --- a/app/schema/breadcrumbs.py +++ b/app/schema/breadcrumbs.py @@ -11,6 +11,7 @@ class BreadCrumbType(enum.StrEnum): Collection = "Collection" Section = "Section" Interpretation = "Interpretation" + Splitter = "Splitter" class BreadCrumb(BaseModel): diff --git a/app/templates/book/components/sub_collection_tab_content.html b/app/templates/book/components/sub_collection_tab_content.html index 62594e6..ff93ee5 100644 --- a/app/templates/book/components/sub_collection_tab_content.html +++ b/app/templates/book/components/sub_collection_tab_content.html @@ -32,12 +32,15 @@