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 @@
{% if current_user.is_authenticated %}
+ {% if sub_collection.is_leaf and not sub_collection.children %}
-
+ {% else %}
-
+ {% endif %}
-
diff --git a/app/templates/book/local_breadcrumbs_navigation.html b/app/templates/book/local_breadcrumbs_navigation.html
index 963a078..6bb5de9 100644
--- a/app/templates/book/local_breadcrumbs_navigation.html
+++ b/app/templates/book/local_breadcrumbs_navigation.html
@@ -1,26 +1,33 @@
{% for breadcrumb in local_breadcrumbs if breadcrumb.type != "MyBookList" and breadcrumb.type != "AuthorBookList" %}
--
- {% if not loop.index==local_breadcrumbs|length-1 %}
-
- {% else %}
-
- {% endif %}
-
-
- {{ breadcrumb.label }}
- {% if not loop.index==local_breadcrumbs|length %}
-
- {% else %}
-
- {% endif %}
- {% if not loop.index==local_breadcrumbs|length-1 %}
-
- {% endif %}
-
+ -
+ {% if not loop.index==local_breadcrumbs|length-1 %}
+
+ {% else %}
+
+ {% endif %}
+
+ {% if breadcrumb.type != "Splitter" %}
+
+ {% endif %}
+
+
+ {{ breadcrumb.label }}
+
+ {% if not loop.index==local_breadcrumbs|length %}
+
+ {% else %}
+
+ {% endif %}
+
+
+ {% if not loop.index==local_breadcrumbs|length-1 %}
+
+ {% endif %}
+
{% endfor %}
\ No newline at end of file
diff --git a/app/views/book/collection.py b/app/views/book/collection.py
index 2ff5f6d..9b64114 100644
--- a/app/views/book/collection.py
+++ b/app/views/book/collection.py
@@ -21,7 +21,7 @@ from .bp import bp
@bp.route("//collections", methods=["GET"])
def collection_view(book_id: int):
book = db.session.get(m.Book, book_id)
- breadcrumbs = create_breadcrumbs(book_id=book_id, collection_path=())
+ breadcrumbs = create_breadcrumbs(book_id=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")
diff --git a/app/views/book/interpretation.py b/app/views/book/interpretation.py
index 4108f6d..9664a1f 100644
--- a/app/views/book/interpretation.py
+++ b/app/views/book/interpretation.py
@@ -38,20 +38,14 @@ def interpretation_view(
book_id=book_id,
)
)
- # FIXME breadcrumbs
- # breadcrumbs = create_breadcrumbs(
- # book_id=book_id,
- # collection_path=(
- # collection_id,
- # sub_collection_id,
- # ),
- # section_id=section_id,
- # )
+ breadcrumbs = create_breadcrumbs(
+ book_id=book_id, section_id=section_id, collection_id=section.collection.id
+ )
return render_template(
"book/interpretation_view.html",
book=book,
section=section,
- breadcrumbs=[],
+ breadcrumbs=breadcrumbs,
)
@@ -191,20 +185,15 @@ def qa_view(book_id: int, interpretation_id: int):
flash("Interpretation not found", "danger")
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_id=interpretation.section.collection.id,
+ section_id=interpretation.section.id,
+ )
return render_template(
"book/qa_view.html",
book=book,
section=interpretation.section,
interpretation=interpretation,
- breadcrumbs=[],
+ breadcrumbs=breadcrumbs,
)
diff --git a/tests/test_book.py b/tests/test_book.py
index 197129c..5e7441d 100644
--- a/tests/test_book.py
+++ b/tests/test_book.py
@@ -458,7 +458,7 @@ def test_crud_subcollection(client: FlaskClient, runner: FlaskCliRunner):
label="Test SubCollection #1 Label"
).first()
assert sub_collection
- assert sub_collection.is_leaf
+ assert not sub_collection.is_leaf
assert sub_collection.parent_id == collection.id
m.Collection(
diff --git a/tests/test_breadcrumbs.py b/tests/test_breadcrumbs.py
index 3dc4ca3..7fbb00b 100644
--- a/tests/test_breadcrumbs.py
+++ b/tests/test_breadcrumbs.py
@@ -6,13 +6,13 @@ from app import models as m, db
def test_breadcrumbs(runner: FlaskCliRunner, app):
runner.invoke(args=["db-populate"])
with app.app_context(), app.test_request_context():
- res = create_breadcrumbs(1, (1,), 1)
+ res = create_breadcrumbs(book_id=1, collection_id=1, section_id=1)
assert len(res) == 4
book: m.Book = db.session.get(m.Book, 1)
assert book
assert book.owner.username in res[0].label
assert res[1].label == book.label
with app.app_context(), app.test_request_context():
- res = create_breadcrumbs(1, (), 1)
+ res = create_breadcrumbs(book_id=1, section_id=1)
assert res
- assert len(res) == 2
+ assert len(res) == 4