Apply inputs from pullrequest

This commit is contained in:
Lorenz Padberg 2023-07-17 14:58:40 +02:00
parent 2e826f81d5
commit 13239ef27e
1 changed files with 8 additions and 15 deletions

View File

@ -9,34 +9,30 @@ from wagtail.models import Page
def set_page_position(request, page_to_move_id): def set_page_position(request, page_to_move_id):
page_to_move = get_object_or_404(Page, id=page_to_move_id) page_to_move = get_object_or_404(Page, id=page_to_move_id)
parent_page = page_to_move.get_parent() parent_page = page_to_move.get_parent()
position = None
if not parent_page.permissions_for_user(request.user).can_reorder_children(): if not parent_page.permissions_for_user(request.user).can_reorder_children():
raise PermissionDenied raise PermissionDenied
if request.method == "POST": if request.method == "POST":
# Get position parameter # Get target_position parameter
visible_target_position = request.GET.get("position", None) visible_target_position = request.GET.get("position", None)
# Get position within all children, the frontend does not count the unpublished pages # Get position within all children. the frontend position is determined explorer_page queryset.
if visible_target_position is not None: if visible_target_position is not None:
visible_target_position = int(visible_target_position) current_page = get_visible_children(parent_page, request)[int(visible_target_position)]
position = list(parent_page.get_children()).index(get_visible_children(parent_page, request)[visible_target_position]) position = list(parent_page.get_children()).index(current_page)
print(f"Visible target position: {visible_target_position} -> {position}")
# Find page that's already in this position # Find page that's already in this position
position_page = None position_page = None
if position is not None: if position is not None:
#position = int(position) + 1
try: try:
position_page = parent_page.get_children()[int(position)] position_page = parent_page.get_children()[position]
except IndexError: except IndexError:
pass # No page in this position pass # No page in this position
# Move page # Move page
# any invalid moves *should* be caught by the permission check above, # any invalid moves *should* be caught by the permission check above,
# so don't bother to catch InvalidMoveToDescendant # so don't bother to catch InvalidMoveToDescendant
@ -44,12 +40,10 @@ def set_page_position(request, page_to_move_id):
# If the page has been moved to the right, insert it to the # If the page has been moved to the right, insert it to the
# right. If left, then left. # right. If left, then left.
old_position = list(parent_page.get_children()).index(page_to_move) old_position = list(parent_page.get_children()).index(page_to_move)
if int(position) < old_position: if position < old_position:
page_to_move.move(position_page, pos="left", user=request.user) page_to_move.move(position_page, pos="left", user=request.user)
elif int(position) > old_position: elif position > old_position:
page_to_move.move(position_page, pos="right", user=request.user) page_to_move.move(position_page, pos="right", user=request.user)
print(f"Moved {old_position} to {position}")
else: else:
# Move page to end # Move page to end
page_to_move.move(parent_page, pos="last-child", user=request.user) page_to_move.move(parent_page, pos="last-child", user=request.user)
@ -65,4 +59,3 @@ def get_visible_children(parent_page, request):
for hook in hooks.get_hooks("construct_explorer_page_queryset"): for hook in hooks.get_hooks("construct_explorer_page_queryset"):
pages = hook(parent_page, pages, request) pages = hook(parent_page, pages, request)
return pages return pages