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.

oscar_cleanup_alerts.py 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import logging
  2. from optparse import make_option
  3. from datetime import timedelta
  4. from django.db.models import get_model
  5. from django.utils.timezone import now
  6. from django.core.management.base import BaseCommand
  7. ProductAlert = get_model('customer', 'ProductAlert')
  8. logger = logging.getLogger(__name__)
  9. class Command(BaseCommand):
  10. """
  11. Command to remove all stale unconfirmed alerts
  12. """
  13. help = "Check unconfirmed alerts and clean them up"
  14. option_list = BaseCommand.option_list + (
  15. make_option('--days',
  16. dest='days',
  17. default=0,
  18. help='cleanup alerts older then DAYS from now.'),
  19. make_option('--hours',
  20. dest='hours',
  21. default=0,
  22. help='cleanup alerts older then HOURS from now.'),
  23. )
  24. def handle(self, *args, **options):
  25. """
  26. Generate a threshold date from the input options or 24 hours
  27. if no options specified. All alerts that have the
  28. status ``UNCONFIRMED`` and have been created before the
  29. threshold date will be removed assuming that the emails
  30. are wrong or the customer changed their mind.
  31. """
  32. delta = timedelta(days=int(options['days']),
  33. hours=int(options['hours']))
  34. if not delta:
  35. delta = timedelta(hours=24)
  36. threshold_date = now() - delta
  37. logger.info('Deleting unconfirmed alerts older than %s',
  38. threshold_date.strftime("%Y-%m-%d %H:%M"))
  39. qs = ProductAlert.objects.filter(
  40. status=ProductAlert.UNCONFIRMED,
  41. date_created__lt=threshold_date
  42. )
  43. logger.info("Found %d stale alerts to delete", qs.count())
  44. qs.delete()