Skip to content

Commit

Permalink
Exception handling for BulkDeleteMixin (#8205)
Browse files Browse the repository at this point in the history
* Exception handling for BulkDeleteMixin

* Fix unit test
  • Loading branch information
SchrodingersGat authored Sep 29, 2024
1 parent 390fec3 commit 33499d6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
19 changes: 17 additions & 2 deletions src/backend/InvenTree/InvenTree/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,26 @@ def delete(self, request, *args, **kwargs):

# Filter by provided item ID values
if items:
queryset = queryset.filter(id__in=items)
try:
queryset = queryset.filter(id__in=items)
except Exception:
raise ValidationError({
'non_field_errors': _('Invalid items list provided')
})

# Filter by provided filters
if filters:
queryset = queryset.filter(**filters)
try:
queryset = queryset.filter(**filters)
except Exception:
raise ValidationError({
'non_field_errors': _('Invalid filters provided')
})

if queryset.count() == 0:
raise ValidationError({
'non_field_errors': _('No items found to delete')
})

# Run a final validation step (should raise an error if the deletion should not proceed)
self.validate_delete(queryset, request)
Expand Down
2 changes: 1 addition & 1 deletion src/backend/InvenTree/stock/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1874,7 +1874,7 @@ def test_bulk_delete(self):
# Now, let's delete all the newly created items with a single API request
# However, we will provide incorrect filters
response = self.delete(
url, {'items': tests, 'filters': {'stock_item': 10}}, expected_code=204
url, {'items': tests, 'filters': {'stock_item': 10}}, expected_code=400
)

self.assertEqual(StockItemTestResult.objects.count(), n + 50)
Expand Down

0 comments on commit 33499d6

Please sign in to comment.