1 from django.conf import settings
2 from django.core.mail import send_mail
3 from django.db import models
4 from django.template import Template, Context
5 from django.utils.translation import ugettext_lazy as _
6 from sentry_sdk import capture_exception
7 from .states import states
10 class EmailTemplate(models.Model):
11 state = models.CharField(_('state'), max_length=128, choices=[(s.slug, s.name) for s in states], help_text='?')
12 subject = models.CharField(_('subject'), max_length=1024)
13 body = models.TextField(_('body'))
14 min_days_since = models.SmallIntegerField(_('min days since'), null=True, blank=True)
15 max_days_since = models.SmallIntegerField(_('max days since'), null=True, blank=True)
16 min_hour = models.PositiveSmallIntegerField(_('min hour'), null=True, blank=True)
17 max_hour = models.PositiveSmallIntegerField(_('max hour'), null=True, blank=True)
18 min_day_of_month = models.PositiveSmallIntegerField(_('min day of month'), null=True, blank=True)
19 max_day_of_month = models.PositiveSmallIntegerField(_('max day of month'), null=True, blank=True)
20 dow_1 = models.BooleanField(_('Monday'), default=True)
21 dow_2 = models.BooleanField(_('Tuesday'), default=True)
22 dow_3 = models.BooleanField(_('Wednesday'), default=True)
23 dow_4 = models.BooleanField(_('Thursday'), default=True)
24 dow_5 = models.BooleanField(_('Friday'), default=True)
25 dow_6 = models.BooleanField(_('Saturday'), default=True)
26 dow_7 = models.BooleanField(_('Sunday'), default=True)
27 is_active = models.BooleanField(_('active'), default=False)
30 verbose_name = _('email template')
31 verbose_name_plural = _('email templates')
34 return '%s (%+d)' % (self.get_state_display(), self.min_days_since or 0)
36 def run(self, time=None, verbose=False, dry_run=False):
37 state = self.get_state()
38 recipients = state(time=time, offset=self.days).get_recipients()
39 hash_values = set(recipient.hash_value for recipient in recipients)
40 sent = set(EmailSent.objects.filter(
41 template=self, hash_value__in=hash_values
42 ).values_list('hash_value', flat=True))
43 for recipient in recipients:
44 if recipient.hash_value in sent:
46 self.send(recipient, verbose=verbose, dry_run=dry_run)
50 if s.slug == self.state:
52 raise ValueError('Unknown state', s.state)
54 def send(self, recipient, verbose=False, dry_run=False):
55 subject = Template(self.subject).render(Context(recipient.context))
56 body = Template(self.body).render(Context(recipient.context))
58 print(recipient.email, subject)
61 send_mail(subject, body, settings.CONTACT_EMAIL, [recipient.email], fail_silently=False)
65 self.emailsent_set.create(
66 hash_value=recipient.hash_value,
67 email=recipient.email,
73 class EmailSent(models.Model):
74 template = models.ForeignKey(EmailTemplate, models.CASCADE)
75 hash_value = models.CharField(max_length=1024)
76 timestamp = models.DateTimeField(auto_now_add=True)
77 email = models.CharField(_('e-mail'), max_length=1024)
78 subject = models.CharField(_('subject'), max_length=1024)
79 body = models.TextField(_('body'))
82 verbose_name = _('email sent')
83 verbose_name_plural = _('emails sent')
84 ordering = ('-timestamp',)
87 return '%s %s' % (self.email, self.timestamp)
90 class Contact(models.Model):
97 email = models.EmailField(unique=True)
98 level = models.PositiveSmallIntegerField(
100 (TRIED, _('Would-be donor')),
101 (SINGLE, _('One-time donor')),
102 (RECURRING, _('Recurring donor')),
104 (OPT_OUT, _('Opt out')),
106 since = models.DateTimeField()
107 expires_at = models.DateTimeField(null=True, blank=True)
110 def update(cls, email, level, since, expires_at=None):
111 obj, created = cls.objects.get_or_create(email=email, defaults={
114 "expires_at": expires_at
117 obj.ascend(level, since, expires_at)
119 def ascend(self, level, since, expires_at=None):
120 if level < self.level:
122 if level == self.level:
123 self.since = min(since, self.since)
125 if expires_at and self.expires_at:
126 self.expires_at = max(expires_at, self.expires_at)
128 self.expires_at = expires_at
132 self.expires_at = expires_at