Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

receivers.py 1018B

1234567891011121314151617181920212223242526272829
  1. from django.dispatch import receiver
  2. from django.db.models.signals import m2m_changed, post_save
  3. from oscar.core.loading import import_module
  4. import_module('offer.models', ['Voucher'], locals())
  5. import_module('order.models', ['OrderDiscount'], locals())
  6. @receiver(m2m_changed)
  7. def receive_basket_voucher_change(sender, **kwargs):
  8. if kwargs['model'] == Voucher and kwargs['action'] == 'post_add':
  9. voucher_id = list(kwargs['pk_set'])[0]
  10. voucher = Voucher._default_manager.get(pk=voucher_id)
  11. voucher.num_basket_additions += 1
  12. voucher.save()
  13. @receiver(post_save, sender=OrderDiscount)
  14. def receive_order_discount_save(sender, instance, **kwargs):
  15. # Record the amount of discount against the appropriate offers
  16. # and vouchers
  17. discount = instance
  18. if discount.voucher:
  19. discount.voucher.total_discount += discount.amount
  20. discount.voucher.save()
  21. discount.offer.total_discount += discount.amount
  22. discount.offer.save()