send_expiration_reminders.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. """ Send reminders for contribution that are about to expire
  2. It offers a way for contributors to opt-in keeping the data one year more (or
  3. deleting it right now).
  4. Reminders are sent when the script is called exactly `n` days before the
  5. expiration date.
  6. This `n` can be configured via `DATA_EXPIRATION_REMINDERS` setting.
  7. """
  8. from django.conf import settings
  9. from django.core.mail import send_mail
  10. from django.core.management.base import BaseCommand, CommandError
  11. from django.template.loader import get_template
  12. from django.utils.translation import activate
  13. from ...models import Contrib
  14. from ... emails import send_contributor_email
  15. from ...tokens import ContribTokenManager
  16. class Command(BaseCommand):
  17. help = __doc__
  18. def add_arguments(self, parser):
  19. parser.add_argument(
  20. '--dry-run', default=False, action='store_true',
  21. help="Do not actually send emails, just pretend to")
  22. def handle(self, dry_run, *args, **options):
  23. if dry_run:
  24. self.stderr.write('DRY RUN MODE: we do not actually send emails.')
  25. for ndays in settings.DATA_EXPIRATION_REMINDERS:
  26. expirating_contribs = Contrib.objects.expires_in_days(ndays)
  27. for contrib in expirating_contribs:
  28. if not contrib.email:
  29. self._warn_no_email(contrib, ndays)
  30. else:
  31. if not dry_run:
  32. self.send_expiration_reminder(contrib)
  33. self._log_sent_email(contrib, ndays)
  34. def _warn_no_email(self, contrib, ndays):
  35. self.stderr.write(
  36. 'WARNING : the contribution {} is about to expire in {} days but'
  37. + 'do not define a contact email.'.format(
  38. contrib, ndays))
  39. def _log_sent_email(self, contrib, ndays):
  40. self.stderr.write(
  41. "Sent reminder email to for {} expiration in {} days".format(
  42. contrib, ndays))
  43. def send_expiration_reminder(self, contrib):
  44. subject = get_template(
  45. 'contribmap/mails/expiration_reminder.subject')
  46. body = get_template(
  47. 'contribmap/mails/expiration_reminder.txt')
  48. mgmt_token = ContribTokenManager().mk_token(contrib)
  49. send_contributor_email(contrib, subject, body, mgmt_token)