Add needs_price_update field to Purchase model
Django CI/CD / test (push) Successful in 22s
Django CI/CD / build-and-push (push) Has been skipped

Replace fragile price change detection in Purchase.save() with a
lazy dirty flag approach. A pre_save/post_save signal pair detects
price/currency changes without extra DB queries, and convert_prices()
uses the flag to determine which purchases need conversion.

- Add needs_price_update BooleanField with db_index
- Add pre_save signal to store old price/currency values
- Add post_save signal to set needs_price_update=True when price/currency changes
- Simplify Purchase.save() to remove DB reload + comparison logic
- Remove price_or_currency_differ_from() method
- Update convert_prices() to filter on needs_price_update flag
- Extract _get_exchange_rate() and _save_converted_price() helpers
- Add tests for the new behavior
This commit is contained in:
2026-05-12 13:57:59 +02:00
parent a4e697a274
commit e3b53cd4a9
5 changed files with 209 additions and 66 deletions
+23
View File
@@ -17,6 +17,29 @@ from games.models import Game, GameStatusChange, Purchase, Session
logger = logging.getLogger("games")
@receiver(pre_save, sender=Purchase)
def store_purchase_price_snapshot(sender, instance, **kwargs):
"""Store old price values before save so we can detect changes."""
if instance.pk is not None:
try:
old_instance = sender.objects.get(pk=instance.pk)
instance._old_price = old_instance.price
instance._old_currency = old_instance.price_currency
except sender.DoesNotExist:
pass
@receiver(post_save, sender=Purchase)
def mark_needs_price_update(sender, instance, created, **kwargs):
"""Mark purchase for price update if price or currency changed."""
if not created and hasattr(instance, "_old_price"):
if (
instance.price != instance._old_price
or instance.price_currency != instance._old_currency
):
sender.objects.filter(pk=instance.pk).update(needs_price_update=True)
@receiver(m2m_changed, sender=Purchase.games.through)
def update_num_purchases(sender, instance, action, reverse, **kwargs):
if not reverse and action.startswith("post_"):