Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Prices): on price delete, also update any related PriceTag status #684

Merged
merged 1 commit into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading