You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

receivers.py 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from django.conf import settings
  2. from django.db.models import get_model
  3. from django.db.models.signals import post_save
  4. from django.db import connection
  5. from oscar.core.compat import get_user_model
  6. User = get_user_model()
  7. def send_product_alerts(sender, instance, created, **kwargs):
  8. if kwargs.get('raw', False):
  9. return
  10. from oscar.apps.customer.alerts import utils
  11. utils.send_product_alerts(instance.product)
  12. def migrate_alerts_to_user(sender, instance, created, **kwargs):
  13. """
  14. Transfer any active alerts linked to a user's email address to the newly
  15. registered user.
  16. """
  17. if not created:
  18. return
  19. ProductAlert = get_model('customer', 'ProductAlert')
  20. # This signal will be raised when creating a superuser as part of syncdb,
  21. # at which point only a subset of tables will be created. Thus, we test if
  22. # the alert table exists before trying to exercise the ORM.
  23. table = ProductAlert._meta.db_table
  24. if table in connection.introspection.table_names():
  25. alerts = ProductAlert.objects.filter(
  26. email=instance.email, status=ProductAlert.ACTIVE)
  27. alerts.update(user=instance, key=None, email=None)
  28. post_save.connect(migrate_alerts_to_user, sender=User)
  29. if settings.OSCAR_EAGER_ALERTS:
  30. StockRecord = get_model('partner', 'StockRecord')
  31. post_save.connect(send_product_alerts, sender=StockRecord)