diff --git a/app/controllers/next_prev_section.py b/app/controllers/next_prev_section.py index 3f9d8ed..4c0b728 100644 --- a/app/controllers/next_prev_section.py +++ b/app/controllers/next_prev_section.py @@ -26,3 +26,33 @@ def recursive_move_down(collection: m.Collection): current = parent parent = parent.parent + + +# + + +def get_prev_section(collection: m.Collection): + if collection.active_sections: + return collection.active_sections[-1] + + # find on current level in next by order collections + for child in collection.active_children[::-1]: + if section := get_prev_section(child): + return section + + +def recursive_move_up(collection: m.Collection): + parent: m.Collection = collection.parent + current: m.Collection = collection + while True: + index = parent.active_children.index(current) + if parent.active_children[:index]: + for child in parent.active_children[:index][::-1]: + if section := get_prev_section(child): + return section + + if current.is_root: + return None + + current = parent + parent = parent.parent diff --git a/app/models/section.py b/app/models/section.py index 441ace7..b194a99 100644 --- a/app/models/section.py +++ b/app/models/section.py @@ -7,7 +7,7 @@ from app.controllers import create_breadcrumbs from .interpretation import Interpretation from .comment import Comment from .interpretation_vote import InterpretationVote -from app.controllers.next_prev_section import recursive_move_down +from app.controllers.next_prev_section import recursive_move_down, recursive_move_up class Section(BaseModel): @@ -130,10 +130,14 @@ class Section(BaseModel): @property def next_section(self): - section = Section.query.filter( - Section.collection_id == self.collection_id, - Section.position > self.position, - ).first() + section = ( + Section.query.filter( + Section.collection_id == self.collection_id, + Section.position > self.position, + ) + .order_by(Section.position) + .first() + ) if section: return section @@ -142,8 +146,19 @@ class Section(BaseModel): @property def previous_section(self): - previous_section = None - return previous_section + section = ( + Section.query.filter( + Section.collection_id == self.collection_id, + Section.position < self.position, + ) + .order_by(Section.position.desc()) + .first() + ) + if section: + return section + + section = recursive_move_up(self.collection) + return section def __repr__(self): return f"<{self.id}: {self.label}>" diff --git a/app/templates/book/interpretation_view.html b/app/templates/book/interpretation_view.html index ef1130c..8fefbac 100644 --- a/app/templates/book/interpretation_view.html +++ b/app/templates/book/interpretation_view.html @@ -208,17 +208,23 @@
Book label
{{ next_section.label }}