1 from django.apps import apps
2 from django.conf import settings
3 from django.core.mail import send_mail
4 from django.db import models
5 from django.template import Template, Context
6 from django.utils.translation import ugettext_lazy as _
7 from sentry_sdk import capture_exception
8 from .recipient import Recipient
9 from .states import states
12 class EmailTemplate(models.Model):
13 state = models.CharField(_('state'), max_length=128, choices=[(s.slug, s.name) for s in states], help_text='?')
14 subject = models.CharField(_('subject'), max_length=1024)
15 body = models.TextField(_('body'))
16 min_days_since = models.SmallIntegerField(_('min days since'), null=True, blank=True)
17 max_days_since = models.SmallIntegerField(_('max days since'), null=True, blank=True)
18 min_hour = models.PositiveSmallIntegerField(_('min hour'), null=True, blank=True)
19 max_hour = models.PositiveSmallIntegerField(_('max hour'), null=True, blank=True)
20 min_day_of_month = models.PositiveSmallIntegerField(_('min day of month'), null=True, blank=True)
21 max_day_of_month = models.PositiveSmallIntegerField(_('max day of month'), null=True, blank=True)
22 dow_1 = models.BooleanField(_('Monday'), default=True)
23 dow_2 = models.BooleanField(_('Tuesday'), default=True)
24 dow_3 = models.BooleanField(_('Wednesday'), default=True)
25 dow_4 = models.BooleanField(_('Thursday'), default=True)
26 dow_5 = models.BooleanField(_('Friday'), default=True)
27 dow_6 = models.BooleanField(_('Saturday'), default=True)
28 dow_7 = models.BooleanField(_('Sunday'), default=True)
29 is_active = models.BooleanField(_('active'), default=False)
32 verbose_name = _('email template')
33 verbose_name_plural = _('email templates')
36 return '%s (%+d)' % (self.get_state_display(), self.min_days_since or 0)
38 def run(self, time=None, verbose=False, dry_run=False):
39 state = self.get_state()
40 recipients = state(time=time, offset=self.days).get_recipients()
41 hash_values = set(recipient.hash_value for recipient in recipients)
42 sent = set(EmailSent.objects.filter(
43 template=self, hash_value__in=hash_values
44 ).values_list('hash_value', flat=True))
45 for recipient in recipients:
46 if recipient.hash_value in sent:
48 self.send(recipient, verbose=verbose, dry_run=dry_run)
52 if s.slug == self.state:
54 raise ValueError('Unknown state', s.state)
56 def send(self, recipient, verbose=False, dry_run=False, test=False):
57 subject = Template(self.subject).render(Context(recipient.context))
58 body = Template(self.body).render(Context(recipient.context))
60 print(recipient.email, subject)
63 send_mail(subject, body, settings.CONTACT_EMAIL, [recipient.email], fail_silently=False)
68 self.emailsent_set.create(
69 hash_value=recipient.hash_value,
70 email=recipient.email,
75 def send_test_email(self, email):
76 state = self.get_state()()
77 recipient = state.get_example_recipient(email)
78 self.send(recipient, test=True)
81 class EmailSent(models.Model):
82 template = models.ForeignKey(EmailTemplate, models.CASCADE)
83 hash_value = models.CharField(max_length=1024)
84 timestamp = models.DateTimeField(auto_now_add=True)
85 email = models.CharField(_('e-mail'), max_length=1024)
86 subject = models.CharField(_('subject'), max_length=1024)
87 body = models.TextField(_('body'))
90 verbose_name = _('email sent')
91 verbose_name_plural = _('emails sent')
92 ordering = ('-timestamp',)
95 return '%s %s' % (self.email, self.timestamp)
98 class Contact(models.Model):
105 email = models.EmailField(unique=True)
106 level = models.PositiveSmallIntegerField(
108 (TRIED, _('Would-be donor')),
109 (SINGLE, _('One-time donor')),
110 (RECURRING, _('Recurring donor')),
112 (OPT_OUT, _('Opt out')),
114 since = models.DateTimeField()
115 expires_at = models.DateTimeField(null=True, blank=True)
118 def update(cls, email, level, since, expires_at=None):
119 obj, created = cls.objects.get_or_create(email=email, defaults={
122 "expires_at": expires_at
125 obj.ascend(level, since, expires_at)
127 def ascend(self, level, since, expires_at=None):
128 if level < self.level:
130 if level == self.level:
131 self.since = min(since, self.since)
133 if expires_at and self.expires_at:
134 self.expires_at = max(expires_at, self.expires_at)
136 self.expires_at = expires_at
140 self.expires_at = expires_at