Skip to content

Commit

Permalink
fix(Prices): on price delete, also update any related PriceTag status (
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn authored Jan 19, 2025
1 parent 18b74b1 commit 2b46cbb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
8 changes: 8 additions & 0 deletions open_prices/prices/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,14 @@ def price_post_create_increment_counts(sender, instance, created, **kwargs):
)


@receiver(signals.pre_delete, sender=Price)
def price_pre_delete_update_price_tag(sender, instance, **kwargs):
instance.price_tags.update(
status=None,
# price_id=None, # will be done in the CASCADE
)


@receiver(signals.post_delete, sender=Price)
def price_post_delete_decrement_counts(sender, instance, **kwargs):
if instance.owner:
Expand Down
15 changes: 14 additions & 1 deletion open_prices/proofs/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ def test_select_proof_image_dir_existing_dir_create_new_dir(self):
self.assertEqual(selected_dir, images_dir / "0002")


class PriceTagCreationTest(TestCase):
class PriceTagTest(TestCase):
def test_create_price_tag_invalid_bounding_box_length(self):
with self.assertRaises(ValidationError) as cm:
PriceTagFactory(
Expand Down Expand Up @@ -747,3 +747,16 @@ def test_create_price_tag_invalid_proof_type(self):
str(cm.exception),
"{'proof': ['Proof should have type PRICE_TAG.']}",
)

def test_delete_price_should_update_price_tag(self):
proof = ProofFactory(type=proof_constants.TYPE_PRICE_TAG)
price_tag = PriceTagFactory(proof=proof)
price = PriceFactory(proof=proof, location=proof.location)
price_tag.price_id = price.id
price_tag.status = proof_constants.PriceTagStatus.linked_to_price.value
price_tag.save()
# delete price
price.delete()
price_tag.refresh_from_db()
self.assertIsNone(price_tag.price_id)
self.assertIsNone(price_tag.status)

0 comments on commit 2b46cbb

Please sign in to comment.