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.days)
37 def run(self, time=None, verbose=False, dry_run=False):
38 state = self.get_state()
39 recipients = state(time=time, offset=self.days).get_recipients()
40 hash_values = set(recipient.hash_value for recipient in recipients)
41 sent = set(EmailSent.objects.filter(
42 template=self, hash_value__in=hash_values
43 ).values_list('hash_value', flat=True))
44 for recipient in recipients:
45 if recipient.hash_value in sent:
47 self.send(recipient, verbose=verbose, dry_run=dry_run)
51 if s.slug == self.state:
53 raise ValueError('Unknown state', s.state)
55 def send(self, recipient, verbose=False, dry_run=False):
56 subject = Template(self.subject).render(Context(recipient.context))
57 body = Template(self.body).render(Context(recipient.context))
59 print(recipient.email, subject)
62 send_mail(subject, body, settings.CONTACT_EMAIL, [recipient.email], fail_silently=False)
66 self.emailsent_set.create(
67 hash_value=recipient.hash_value,
68 email=recipient.email,
74 class EmailSent(models.Model):
75 template = models.ForeignKey(EmailTemplate, models.CASCADE)
76 hash_value = models.CharField(max_length=1024)
77 timestamp = models.DateTimeField(auto_now_add=True)
78 email = models.CharField(_('e-mail'), max_length=1024)
79 subject = models.CharField(_('subject'), max_length=1024)
80 body = models.TextField(_('body'))
83 verbose_name = _('email sent')
84 verbose_name_plural = _('emails sent')
85 ordering = ('-timestamp',)
88 return '%s %s' % (self.email, self.timestamp)
91 class Contact(models.Model):
98 email = models.EmailField(unique=True)
99 level = models.PositiveSmallIntegerField(
101 (TRIED, _('Would-be donor')),
102 (SINGLE, _('One-time donor')),
103 (RECURRING, _('Recurring donor')),
105 (OPT_OUT, _('Opt out')),
107 since = models.DateTimeField()
108 expires_at = models.DateTimeField(null=True, blank=True)
111 def update(cls, email, level, since, expires_at=None):
112 obj, created = cls.objects.get_or_create(email=email, defaults={
115 "expires_at": expires_at
118 obj.ascend(level, since, expires_at)
120 def ascend(self, level, since, expires_at=None):
121 if level < self.level:
123 if level == self.level:
124 self.since = min(since, self.since)
126 if expires_at and self.expires_at:
127 self.expires_at = max(expires_at, self.expires_at)
129 self.expires_at = expires_at
133 self.expires_at = expires_at