prevalidate agenda items on assign

This commit is contained in:
FinnStutzenstein 2018-11-29 08:42:16 +01:00
parent cfd5cf3c7e
commit 80fce9a173

View File

@ -386,33 +386,35 @@ class ItemViewSet(ListModelMixin, RetrieveModelMixin, UpdateModelMixin, GenericV
raise ValidationError({'detail': 'Parent item {} does not exist'.format(request.data['parent_id'])}) raise ValidationError({'detail': 'Parent item {} does not exist'.format(request.data['parent_id'])})
# Collect ancestors # Collect ancestors
ancestors = [] ancestors = [parent.pk]
grandparent = parent.parent grandparent = parent.parent
while grandparent is not None: while grandparent is not None:
ancestors.append(grandparent.pk) ancestors.append(grandparent.pk)
grandparent = grandparent.parent grandparent = grandparent.parent
item_result = [] # First validate all items before changeing them.
items = []
for item_id in request.data['items']: for item_id in request.data['items']:
# Prevent hierarchical loops. # Prevent hierarchical loops.
if item_id == parent.pk or item_id in ancestors: if item_id in ancestors:
raise ValidationError({'detail': 'Assigning item {} to one of its children is not possible.'.format(item_id)}) raise ValidationError({'detail': 'Assigning item {} to one of its children is not possible.'.format(item_id)})
# Check every item # Check every item
try: try:
item = Item.objects.get(pk=item_id) items.append(Item.objects.get(pk=item_id))
except Item.DoesNotExist: except Item.DoesNotExist:
raise ValidationError({'detail': 'Item {} does not exist'.format(item_id)}) raise ValidationError({'detail': 'Item {} does not exist'.format(item_id)})
# OK, assign new parents.
for item in items:
# Assign new parent. # Assign new parent.
item.parent = parent item.parent = parent
item.save(skip_autoupdate=True) item.save(skip_autoupdate=True)
item_result.append(item)
# Now inform all clients. # Now inform all clients.
inform_changed_data(item_result) inform_changed_data(items)
# Send response. # Send response.
return Response({ return Response({
'detail': _('{number} items successfully assigned.').format(number=len(item_result)), 'detail': _('{number} items successfully assigned.').format(number=len(items)),
}) })