🐛(backend) Prevent moving document to its own descendant or self

When attempting to move a document to itself or to any of its
descendants, the server would crash with a 500 Internal Server
Error.
    
Signed-off-by: Mohamed El Amine BOUKERFA <boukerfa.ma@gmail.com>
This commit is contained in:
Mohamed El Amine BOUKERFA
2026-04-28 14:13:42 +00:00
committed by GitHub
parent 99764b8e3e
commit ed663f2e1e
3 changed files with 98 additions and 1 deletions
+8 -1
View File
@@ -44,6 +44,7 @@ from rest_framework import filters, status, viewsets
from rest_framework import response as drf_response
from rest_framework.permissions import AllowAny
from rest_framework.views import APIView
from treebeard.exceptions import InvalidMoveToDescendant
from core import authentication, choices, enums, models
from core.api.filters import remove_accents
@@ -961,7 +962,13 @@ class DocumentViewSet(
status=status.HTTP_400_BAD_REQUEST,
)
document.move(target_document, pos=position)
try:
document.move(target_document, pos=position)
except InvalidMoveToDescendant:
return drf.response.Response(
{"target_document_id": "Cannot move a document to its own descendant."},
status=status.HTTP_400_BAD_REQUEST,
)
# Make sure we have at least one owner
if (