Add Idea for the wagtail fix

This commit is contained in:
Lorenz Padberg 2023-07-17 10:41:12 +02:00
parent 3f308d9a32
commit 1de888b5cd
2 changed files with 57 additions and 0 deletions

View File

@ -64,6 +64,7 @@
{% elif show_bulk_actions %}
{% include "wagtailadmin/bulk_actions/listing_checkbox_cell.html" with obj_type="page" obj=page aria_labelledby_prefix="page_" aria_labelledby=page.pk|unlocalize aria_labelledby_suffix="_title" %}
{% endif %}
<td id="page_{{ page.pk|unlocalize }}_title" class="title" valign="top" data-listing-page-title>
{% block page_title %}

View File

@ -0,0 +1,56 @@
from django.core.exceptions import PermissionDenied
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from wagtail.models import Page
def set_page_position(request, page_to_move_id):
page_to_move = get_object_or_404(Page, id=page_to_move_id)
parent_page = page_to_move.get_parent()
if not parent_page.permissions_for_user(request.user).can_reorder_children():
raise PermissionDenied
if request.method == "POST":
# Get position parameter
visible_target_position = request.GET.get("position", None)
# Get position within all children, the frontend does not count the unpublished pages
if visible_target_position is not None:
visible_target_position = int(visible_target_position)
position = list(parent_page.get_children()).index(
get_visible_children(parent_page)[visible_target_position])
print(f"Visible target position: {visible_target_position} -> {position}")
# Find page that's already in this position
position_page = None
if position is not None:
# position = int(position) + 1
try:
position_page = parent_page.get_children()[int(position)]
except IndexError:
pass # No page in this position
# Move page
# any invalid moves *should* be caught by the permission check above,
# so don't bother to catch InvalidMoveToDescendant
if position_page:
# If the page has been moved to the right, insert it to the
# right. If left, then left.
old_position = list(parent_page.get_children()).index(page_to_move)
if int(position) < old_position:
page_to_move.move(position_page, pos="left", user=request.user)
elif int(position) > old_position:
page_to_move.move(position_page, pos="right", user=request.user)
print(f"Moved {old_position} to {position}")
else:
# Move page to end
page_to_move.move(parent_page, pos="last-child", user=request.user)
return HttpResponse("")
def get_visible_children(parent_page):
visible_children = parent_page.get_children().exclude(has_unpublished_changes=True)
print_children(visible_children)
return visible_children
def print_children(children, level=0):
for ii, sibling in enumerate(children):
print(ii, sibling.has_unpublished_changes, sibling)