diff options
Diffstat (limited to 'app/lttr/models.py')
-rw-r--r-- | app/lttr/models.py | 95 |
1 files changed, 45 insertions, 50 deletions
diff --git a/app/lttr/models.py b/app/lttr/models.py index 029022c..ec5e897 100644 --- a/app/lttr/models.py +++ b/app/lttr/models.py @@ -28,6 +28,7 @@ def make_activation_code(): class Newsletter(models.Model): """ A model for Newletters. Might I one day have two? I might. """ title = models.CharField(max_length=250) + slug = models.SlugField(db_index=True, unique=True) def __str__(self): return self.title @@ -36,7 +37,7 @@ class Newsletter(models.Model): return reverse("lttr:detail", kwargs={"slug": self.slug}) def subscribe_url(self): - return reverse('newsletter_subscribe_request', kwargs={'newsletter_slug': self.slug}) + return reverse('lttr:newsletter_subscribe_request', kwargs={'newsletter_slug': self.slug}) def unsubscribe_url(self): return reverse('newsletter_unsubscribe_request', kwargs={'newsletter_slug': self.slug}) @@ -50,6 +51,44 @@ class Newsletter(models.Model): def get_subscriptions(self): return Subscriber.objects.filter(newsletter=self, subscribed=True) + def get_templates(self, action): + """ + Return a subject, text, HTML tuple with e-mail templates for + a particular action. Returns a tuple with subject, text and e-mail + template. + """ + + assert action in ACTIONS + ('message', ), 'Unknown action: %s' % action + + # Common substitutions for filenames + tpl_subst = { + 'action': action, + 'newsletter': self.slug + } + + # Common root path for all the templates + tpl_root = 'lttr/message/' + + subject_template = select_template([ + tpl_root + '%(newsletter)s/%(action)s_subject.txt' % tpl_subst, + tpl_root + '%(action)s_subject.txt' % tpl_subst, + ]) + + text_template = select_template([ + tpl_root + '%(newsletter)s/%(action)s.txt' % tpl_subst, + tpl_root + '%(action)s.txt' % tpl_subst, + ]) + + html_template = select_template([ + tpl_root + '%(newsletter)s/%(action)s.html' % tpl_subst, + tpl_root + '%(action)s.html' % tpl_subst, + ]) + + return (subject_template, text_template, html_template) + + def get_sender(self): + return 'Scott Gilbertson <sng@luxagraf.net>' + @classmethod def get_default(cls): try: @@ -85,45 +124,6 @@ class NewsletterMailing(models.Model): self.date_created = timezone.now() super(NewsletterMailing, self).save() - def get_templates(self, action): - """ - Return a subject, text, HTML tuple with e-mail templates for - a particular action. Returns a tuple with subject, text and e-mail - template. - """ - - assert action in ACTIONS + ('message', ), 'Unknown action: %s' % action - - # Common substitutions for filenames - tpl_subst = { - 'action': action, - 'newsletter': self.slug - } - - # Common root path for all the templates - tpl_root = 'newsletter/message/' - - subject_template = select_template([ - tpl_root + '%(newsletter)s/%(action)s_subject.txt' % tpl_subst, - tpl_root + '%(action)s_subject.txt' % tpl_subst, - ]) - - text_template = select_template([ - tpl_root + '%(newsletter)s/%(action)s.txt' % tpl_subst, - tpl_root + '%(action)s.txt' % tpl_subst, - ]) - - if self.send_html: - html_template = select_template([ - tpl_root + '%(newsletter)s/%(action)s.html' % tpl_subst, - tpl_root + '%(action)s.html' % tpl_subst, - ]) - else: - # HTML templates are not required - html_template = None - - return (subject_template, text_template, html_template) - class Subscriber(models.Model): """ A model for Newletter Subscriber """ @@ -131,9 +131,7 @@ class Subscriber(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) date_created = models.DateTimeField(blank=True, auto_now_add=True, editable=False) date_updated = models.DateTimeField(blank=True, auto_now=True, editable=False) - ip = models.GenericIPAddressField(blank=True, null=True) newsletter = models.ForeignKey(Newsletter, on_delete=models.CASCADE) - create_date = models.DateTimeField(editable=False, default=datetime.datetime.now) activation_code = models.CharField(max_length=40, default=make_activation_code) subscribed = models.BooleanField(default=False, db_index=True) subscribe_date = models.DateTimeField(null=True, blank=True) @@ -278,25 +276,22 @@ class Subscriber(models.Model): message.send() def subscribe_activate_url(self): - return reverse('newsletter_update_activate', kwargs={ - 'newsletter_slug': self.newsletter.slug, - 'email': self.email, - 'action': 'subscribe', + return reverse('lttr:newsletter_activate', kwargs={ + 'slug': self.newsletter.slug, 'activation_code': self.activation_code }) def unsubscribe_activate_url(self): return reverse('newsletter_update_activate', kwargs={ - 'newsletter_slug': self.newsletter.slug, + 'slug': self.newsletter.slug, 'email': self.email, 'action': 'unsubscribe', 'activation_code': self.activation_code }) def update_activate_url(self): - return reverse('newsletter_update_activate', kwargs={ - 'newsletter_slug': self.newsletter.slug, - 'email': self.email, + return reverse('lttr:newsletter_update_activate', kwargs={ + 'slug': self.newsletter.slug, 'action': 'update', 'activation_code': self.activation_code }) |