recursive_move_up

This commit is contained in:
SvyatoslavArtymovych 2023-06-06 18:04:10 +03:00
parent 4fd687e26c
commit 50df1a98c7
3 changed files with 72 additions and 29 deletions

View File

@ -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

View File

@ -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}>"

View File

@ -208,17 +208,23 @@
</div>
<div class="flex p-10 justify-between">
<!-- prettier-ignore -->
<button
type="button"
class="{% if not section.previous_section %}cursor-not-allowed{% endif %} text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center inline-flex items-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800"
{% if not section.previous_section %}disabled{% endif %}>
<!-- prettier-ignore -->
<svg aria-hidden="true" class="w-5 h-5 mr-2 -ml-1 rotate-180" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"> <path fill-rule="evenodd" d="M10.293 3.293a1 1 0 011.414 0l6 6a1 1 0 010 1.414l-6 6a1 1 0 01-1.414-1.414L14.586 11H3a1 1 0 110-2h11.586l-4.293-4.293a1 1 0 010-1.414z" clip-rule="evenodd"></path> </svg>
<div class="flex flex-col items-end ml-5">
<p class="p-3">Book label</p>
<p class="p-3">Previous section</p>
</div>
</button>
{% set previous_section = section.previous_section %}
{% if previous_section %}
<button
type="button"
class="{% if not section.previous_section %}cursor-not-allowed{% endif %} text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center inline-flex items-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800"
{% if not section.previous_section %}disabled{% endif %}>
<!-- prettier-ignore -->
<svg aria-hidden="true" class="w-5 h-5 mr-2 -ml-1 rotate-180" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"> <path fill-rule="evenodd" d="M10.293 3.293a1 1 0 011.414 0l6 6a1 1 0 010 1.414l-6 6a1 1 0 01-1.414-1.414L14.586 11H3a1 1 0 110-2h11.586l-4.293-4.293a1 1 0 010-1.414z" clip-rule="evenodd"></path> </svg>
<div class="flex flex-col items-end ml-5">
<p class="p-3">Book label</p>
<p class="p-3">{{ previous_section.label }}</p>
</div>
</button>
{% else %}
<div></div>
{% endif %}
<!-- prettier-ignore -->
{% set next_section = section.next_section %}
{% if next_section %}
@ -230,18 +236,10 @@
<p class="p-3">Book label</p>
<p class="p-3">{{ next_section.label }}</p>
</div>
<svg
aria-hidden="true"
class="w-5 h-5 ml-2 -mr-1"
fill="currentColor"
viewBox="0 0 20 20"
xmlns="http://www.w3.org/2000/svg">
<path
fill-rule="evenodd"
d="M10.293 3.293a1 1 0 011.414 0l6 6a1 1 0 010 1.414l-6 6a1 1 0 01-1.414-1.414L14.586 11H3a1 1 0 110-2h11.586l-4.293-4.293a1 1 0 010-1.414z"
clip-rule="evenodd"></path>
</svg>
<svg aria-hidden="true" class="w-5 h-5 ml-2 -mr-1" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"> <path fill-rule="evenodd" d="M10.293 3.293a1 1 0 011.414 0l6 6a1 1 0 010 1.414l-6 6a1 1 0 01-1.414-1.414L14.586 11H3a1 1 0 110-2h11.586l-4.293-4.293a1 1 0 010-1.414z" clip-rule="evenodd"></path> </svg>
</button>
{% else %}
<div></div>
{% endif %}
</div>
</div>