2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-07-05 13:10:57 +00:00

Pricing bug fix (#4422)

* Control when a new PartPricing object can be created

- Prevent this when calling from an on_delete signal
- There is an edge case where deleting a part triggers a series of on_delete signals inside an atomic transaction
- When the new PartPricing object is created,

* Add unit testing:

- Ensure PartPricing gets created when a new StockItem is added
- Part.delete() works without error
- PartPricing instances are deleted also

* style fixes
This commit is contained in:
Oliver
2023-02-26 16:36:11 +11:00
committed by GitHub
parent 0c5dc2865c
commit b657fb4405
7 changed files with 72 additions and 15 deletions

View File

@ -1678,17 +1678,33 @@ class Part(InvenTreeBarcodeMixin, MetadataMixin, MPTTModel):
return pricing
def schedule_pricing_update(self):
def schedule_pricing_update(self, create: bool = False):
"""Helper function to schedule a pricing update.
Importantly, catches any errors which may occur during deletion of related objects,
in particular due to post_delete signals.
Ref: https://github.com/inventree/InvenTree/pull/3986
Arguments:
create: Whether or not a new PartPricing object should be created if it does not already exist
"""
pricing = self.pricing
pricing.schedule_for_update()
try:
self.refresh_from_db()
except Part.DoesNotExist:
return
try:
pricing = self.pricing
if create or pricing.pk:
pricing.schedule_for_update()
except IntegrityError:
# If this part instance has been deleted,
# some post-delete or post-save signals may still be fired
# which can cause issues down the track
pass
def get_price_info(self, quantity=1, buy=True, bom=True, internal=False):
"""Return a simplified pricing string for this part.
@ -2261,6 +2277,10 @@ class PartPricing(common.models.MetaMixin):
def schedule_for_update(self, counter: int = 0):
"""Schedule this pricing to be updated"""
if not self.part or not self.part.pk or not Part.objects.filter(pk=self.part.pk).exists():
logger.warning("Referenced part instance does not exist - skipping pricing update.")
return
try:
if self.pk:
self.refresh_from_db()
@ -3710,7 +3730,7 @@ def update_pricing_after_edit(sender, instance, created, **kwargs):
# Update part pricing *unless* we are importing data
if InvenTree.ready.canAppAccessDatabase() and not InvenTree.ready.isImportingData():
instance.part.schedule_pricing_update()
instance.part.schedule_pricing_update(create=True)
@receiver(post_delete, sender=BomItem, dispatch_uid='post_delete_bom_item')
@ -3721,7 +3741,7 @@ def update_pricing_after_delete(sender, instance, **kwargs):
# Update part pricing *unless* we are importing data
if InvenTree.ready.canAppAccessDatabase() and not InvenTree.ready.isImportingData():
instance.part.schedule_pricing_update()
instance.part.schedule_pricing_update(create=False)
class BomItemSubstitute(models.Model):