Locale
[wolnelektury.git] / src / messaging / models.py
index a8c481f..6f158e9 100644 (file)
@@ -1,9 +1,11 @@
+from django.apps import apps
 from django.conf import settings
 from django.core.mail import send_mail
 from django.db import models
 from django.template import Template, Context
 from django.utils.translation import ugettext_lazy as _
 from sentry_sdk import capture_exception
+from .recipient import Recipient
 from .states import states
 
 
@@ -31,8 +33,7 @@ class EmailTemplate(models.Model):
         verbose_name_plural = _('email templates')
 
     def __str__(self):
-        return '%s (%+d)' % (self.get_state_display(), self.days)
-        return self.subject
+        return '%s (%+d)' % (self.get_state_display(), self.min_days_since or 0)
 
     def run(self, time=None, verbose=False, dry_run=False):
         state = self.get_state()
@@ -52,7 +53,7 @@ class EmailTemplate(models.Model):
                 return s
         raise ValueError('Unknown state', s.state)
 
-    def send(self, recipient, verbose=False, dry_run=False):
+    def send(self, recipient, verbose=False, dry_run=False, test=False):
         subject = Template(self.subject).render(Context(recipient.context))
         body = Template(self.body).render(Context(recipient.context))
         if verbose:
@@ -63,12 +64,18 @@ class EmailTemplate(models.Model):
             except:
                 capture_exception()
             else:
-                self.emailsent_set.create(
-                    hash_value=recipient.hash_value,
-                    email=recipient.email,
-                    subject=subject,
-                    body=body,
-                )
+                if not test:
+                    self.emailsent_set.create(
+                        hash_value=recipient.hash_value,
+                        email=recipient.email,
+                        subject=subject,
+                        body=body,
+                    )
+
+    def send_test_email(self, email):
+        state = self.get_state()()
+        recipient = state.get_example_recipient(email)
+        self.send(recipient, test=True)
 
 
 class EmailSent(models.Model):