2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-28 11:36:44 +00:00

Exception handling for BulkDeleteMixin (#8205) (#8206)

* Exception handling for BulkDeleteMixin

* Fix unit test

(cherry picked from commit 33499d61bd522fb1fe24fb296fa09b361436574c)

Co-authored-by: Oliver <oliver.henry.walters@gmail.com>
This commit is contained in:
github-actions[bot] 2024-09-29 15:38:28 +10:00 committed by GitHub
parent b12bd3bb4b
commit 0a0da7b65b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 3 deletions

View File

@ -383,11 +383,26 @@ class BulkDeleteMixin:
# 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)

View File

@ -1713,7 +1713,7 @@ class StockTestResultTest(StockAPITestCase):
# 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)