2023-05-01 14:01:03 +00:00
|
|
|
from flask import url_for
|
2023-05-01 08:15:57 +00:00
|
|
|
from flask_login import current_user
|
2023-05-01 14:29:54 +00:00
|
|
|
|
2023-05-01 08:15:57 +00:00
|
|
|
from app import models as m, db
|
2023-05-01 14:01:03 +00:00
|
|
|
from app import schema as s
|
2023-05-01 08:15:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
def create_breadcrumbs(
|
|
|
|
book_id: int,
|
2023-05-01 14:01:03 +00:00
|
|
|
collection_path: tuple[int],
|
2023-05-01 08:15:57 +00:00
|
|
|
section_id: int = 0,
|
2023-05-01 14:01:03 +00:00
|
|
|
interpretation_id: int = 0,
|
|
|
|
) -> list[s.BreadCrumb]:
|
2023-05-02 07:26:33 +00:00
|
|
|
"""
|
|
|
|
How breadcrumbs look like:
|
|
|
|
|
|
|
|
Book List -> Book Name -> Top Level Collection -> SubCollection -> Section -> Interpretation
|
|
|
|
|
|
|
|
- If i am not owner of a book
|
|
|
|
John's books -> Book Name -> Top Level Collection -> SubCollection -> Section -> Interpretation
|
|
|
|
|
|
|
|
- If i am owner
|
|
|
|
My Books -> Book Title -> Part I -> Chapter X -> Paragraph 1.7 -> By John
|
|
|
|
"""
|
|
|
|
|
2023-05-01 14:01:03 +00:00
|
|
|
crumples: list[s.BreadCrumb] = []
|
|
|
|
book: m.Book = db.session.get(m.Book, book_id)
|
|
|
|
if current_user.is_authenticated and book.user_id == current_user.id:
|
|
|
|
# My Book
|
|
|
|
crumples += [
|
|
|
|
s.BreadCrumb(
|
|
|
|
type=s.BreadCrumbType.MyBookList,
|
|
|
|
url=url_for("book.my_books"),
|
|
|
|
label="My Books",
|
|
|
|
)
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
# Not mine book
|
|
|
|
crumples += [
|
|
|
|
s.BreadCrumb(
|
|
|
|
type=s.BreadCrumbType.AuthorBookList,
|
|
|
|
url="#",
|
|
|
|
label=book.owner.username + "'s books",
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
crumples += [
|
|
|
|
s.BreadCrumb(
|
|
|
|
type=s.BreadCrumbType.Collection,
|
|
|
|
url=url_for("book.collection_view", book_id=book_id),
|
|
|
|
label=book.label,
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
for collection_id in collection_path:
|
2023-05-02 07:21:24 +00:00
|
|
|
if collection_id is None:
|
|
|
|
continue
|
2023-05-01 14:01:03 +00:00
|
|
|
collection: m.Collection = db.session.get(m.Collection, collection_id)
|
|
|
|
crumples += [
|
|
|
|
s.BreadCrumb(
|
|
|
|
type=s.BreadCrumbType.Collection,
|
|
|
|
url=url_for(
|
|
|
|
"book.sub_collection_view",
|
|
|
|
book_id=book_id,
|
|
|
|
collection_id=collection_id,
|
|
|
|
),
|
|
|
|
label=collection.label,
|
|
|
|
)
|
|
|
|
]
|
2023-05-01 14:29:54 +00:00
|
|
|
if section_id and collection_path:
|
2023-05-01 14:01:03 +00:00
|
|
|
section: m.Section = db.session.get(m.Section, section_id)
|
|
|
|
if len(collection_path) == 2:
|
|
|
|
crumples += [
|
|
|
|
s.BreadCrumb(
|
|
|
|
type=s.BreadCrumbType.Section,
|
|
|
|
url=url_for(
|
|
|
|
"book.section_view",
|
|
|
|
book_id=book_id,
|
|
|
|
collection_id=collection_path[0],
|
|
|
|
sub_collection_id=collection_path[-1],
|
|
|
|
),
|
|
|
|
label=section.label,
|
|
|
|
)
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
crumples += [
|
|
|
|
s.BreadCrumb(
|
|
|
|
type=s.BreadCrumbType.Section,
|
|
|
|
url=url_for(
|
|
|
|
"book.section_view",
|
|
|
|
book_id=book_id,
|
|
|
|
collection_id=collection_path[0],
|
|
|
|
sub_collection_id=collection_path[0],
|
|
|
|
),
|
|
|
|
label=section.label,
|
|
|
|
)
|
|
|
|
]
|
2023-05-01 14:29:54 +00:00
|
|
|
if interpretation_id:
|
2023-05-01 14:01:03 +00:00
|
|
|
interpretation: m.Interpretation = db.session.get(
|
|
|
|
m.Interpretation, interpretation_id
|
|
|
|
)
|
|
|
|
if len(collection_path) == 2:
|
|
|
|
crumples += [
|
|
|
|
s.BreadCrumb(
|
|
|
|
type=s.BreadCrumbType.Interpretation,
|
|
|
|
url=url_for(
|
|
|
|
"book.interpretation_view",
|
|
|
|
book_id=book_id,
|
|
|
|
collection_id=collection_path[0],
|
|
|
|
sub_collection_id=collection_path[-1],
|
|
|
|
section_id=section_id,
|
|
|
|
),
|
|
|
|
label=interpretation.label,
|
|
|
|
)
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
crumples += [
|
|
|
|
s.BreadCrumb(
|
|
|
|
type=s.BreadCrumbType.Interpretation,
|
|
|
|
url=url_for(
|
|
|
|
"book.interpretation_view",
|
|
|
|
book_id=book_id,
|
|
|
|
collection_id=collection_path[0],
|
|
|
|
sub_collection_id=collection_path[0],
|
|
|
|
section_id=section_id,
|
|
|
|
),
|
|
|
|
label=interpretation.label,
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
return crumples
|