mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
BOM delete fix (#7586)
* Fix order of operations for BOM item bulk delete * Improve error messaging * Add "validate_delete" method
This commit is contained in:
parent
11e70c9ba0
commit
633873365d
@ -311,8 +311,26 @@ class BulkDeleteMixin:
|
|||||||
- Speed (single API call and DB query)
|
- Speed (single API call and DB query)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
def validate_delete(self, queryset, request) -> None:
|
||||||
|
"""Perform validation right before deletion.
|
||||||
|
|
||||||
|
Arguments:
|
||||||
|
queryset: The queryset to be deleted
|
||||||
|
request: The request object
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
None
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ValidationError: If the deletion should not proceed
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
def filter_delete_queryset(self, queryset, request):
|
def filter_delete_queryset(self, queryset, request):
|
||||||
"""Provide custom filtering for the queryset *before* it is deleted."""
|
"""Provide custom filtering for the queryset *before* it is deleted.
|
||||||
|
|
||||||
|
The default implementation does nothing, just returns the queryset.
|
||||||
|
"""
|
||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
def delete(self, request, *args, **kwargs):
|
def delete(self, request, *args, **kwargs):
|
||||||
@ -371,6 +389,9 @@ class BulkDeleteMixin:
|
|||||||
if filters:
|
if filters:
|
||||||
queryset = queryset.filter(**filters)
|
queryset = queryset.filter(**filters)
|
||||||
|
|
||||||
|
# Run a final validation step (should raise an error if the deletion should not proceed)
|
||||||
|
self.validate_delete(queryset, request)
|
||||||
|
|
||||||
n_deleted = queryset.count()
|
n_deleted = queryset.count()
|
||||||
queryset.delete()
|
queryset.delete()
|
||||||
|
|
||||||
|
@ -1875,14 +1875,12 @@ class BomList(BomMixin, DataExportViewMixin, ListCreateDestroyAPIView):
|
|||||||
'pricing_updated': 'sub_part__pricing_data__updated',
|
'pricing_updated': 'sub_part__pricing_data__updated',
|
||||||
}
|
}
|
||||||
|
|
||||||
def filter_delete_queryset(self, queryset, request):
|
def validate_delete(self, queryset, request) -> None:
|
||||||
"""Ensure that there are no 'locked' items."""
|
"""Ensure that there are no 'locked' items."""
|
||||||
for bom_item in queryset:
|
for bom_item in queryset:
|
||||||
# Note: Calling check_part_lock may raise a ValidationError
|
# Note: Calling check_part_lock may raise a ValidationError
|
||||||
bom_item.check_part_lock(bom_item.part)
|
bom_item.check_part_lock(bom_item.part)
|
||||||
|
|
||||||
return super().filter_delete_queryset(queryset, request)
|
|
||||||
|
|
||||||
|
|
||||||
class BomDetail(BomMixin, RetrieveUpdateDestroyAPI):
|
class BomDetail(BomMixin, RetrieveUpdateDestroyAPI):
|
||||||
"""API endpoint for detail view of a single BomItem object."""
|
"""API endpoint for detail view of a single BomItem object."""
|
||||||
|
@ -4101,16 +4101,16 @@ class BomItem(
|
|||||||
"""
|
"""
|
||||||
# TODO: Perhaps control this with a global setting?
|
# TODO: Perhaps control this with a global setting?
|
||||||
|
|
||||||
msg = _('BOM item cannot be modified - assembly is locked')
|
|
||||||
|
|
||||||
if assembly.locked:
|
if assembly.locked:
|
||||||
raise ValidationError(msg)
|
raise ValidationError(_('BOM item cannot be modified - assembly is locked'))
|
||||||
|
|
||||||
# If this BOM item is inherited, check all variants of the assembly
|
# If this BOM item is inherited, check all variants of the assembly
|
||||||
if self.inherited:
|
if self.inherited:
|
||||||
for part in assembly.get_descendants(include_self=False):
|
for part in assembly.get_descendants(include_self=False):
|
||||||
if part.locked:
|
if part.locked:
|
||||||
raise ValidationError(msg)
|
raise ValidationError(
|
||||||
|
_('BOM item cannot be modified - variant assembly is locked')
|
||||||
|
)
|
||||||
|
|
||||||
# A link to the parent part
|
# A link to the parent part
|
||||||
# Each part will get a reverse lookup field 'bom_items'
|
# Each part will get a reverse lookup field 'bom_items'
|
||||||
|
Loading…
Reference in New Issue
Block a user