diff options
Diffstat (limited to 'app/lttr/models.py')
-rw-r--r-- | app/lttr/models.py | 76 |
1 files changed, 74 insertions, 2 deletions
diff --git a/app/lttr/models.py b/app/lttr/models.py index 33a6735..91d5e22 100644 --- a/app/lttr/models.py +++ b/app/lttr/models.py @@ -3,6 +3,7 @@ from django.contrib.gis.db import models from django.contrib.sites.models import Site from django.template.loader import select_template from django.core.mail import EmailMultiAlternatives +from django.utils.translation import ugettext_lazy as _ from django.utils import timezone from django.utils.text import slugify from django.urls import reverse @@ -14,6 +15,7 @@ from taggit.managers import TaggableManager from utils.util import render_images, parse_video, markdown_to_html from taxonomy.models import TaggedItems from photos.models import LuxImage, LuxImageSize +from books.models import Book # Possible actions that user can perform @@ -101,7 +103,9 @@ class Newsletter(models.Model): class NewsletterMailing(models.Model): """ A model for Newletter Mailings, the things actually sent out """ + newsletter = models.ForeignKey(Newsletter, on_delete=models.CASCADE) title = models.CharField(max_length=250) + subtitle = models.CharField(max_length=250, null=True, blank=True) slug = models.SlugField(unique_for_date='pub_date', blank=True) body_html = models.TextField(blank=True) body_markdown = models.TextField() @@ -113,7 +117,9 @@ class NewsletterMailing(models.Model): (1, 'Published'), ) status = models.IntegerField(choices=PUB_STATUS, default=0) + issue = models.PositiveIntegerField() date_created = models.DateTimeField(blank=True, auto_now_add=True, editable=False) + books = models.ManyToManyField(Book, related_name="mailing_books", blank=True) class Meta: ordering = ('-title', '-date_created') @@ -122,7 +128,36 @@ class NewsletterMailing(models.Model): return self.title def get_absolute_url(self): - return reverse("newsletter:mailing-detail", kwargs={"slug": self.slug}) + return reverse("lttr:detail", kwargs={"slug": self.newsletter.slug, "issue": self.get_issue_str(), "mailing":self.slug}) + + def get_issue_str(self): + issue = self.issue + if self.issue < 100: + issue = "0%s" % self.issue + if self.issue < 10: + issue = "00%s" % self.issue + return issue + + @property + def get_previous_published(self): + return self.get_previous_by_pub_date(status__exact=1,newsletter=self.newsletter) + + @property + def get_previous_admin_url(self): + n = self.get_previous_by_pub_date() + return reverse('admin:%s_%s_change' %(self._meta.app_label, self._meta.model_name), args=[n.id] ) + + @property + def get_next_published(self): + return self.get_next_by_pub_date(status__exact=1, newsletter=self.newsletter) + + @property + def get_next_admin_url(self): + model = apps.get_model(app_label=self._meta.app_label, model_name=self._meta.model_name) + try: + return reverse('admin:%s_%s_change' %(self._meta.app_label, self._meta.model_name), args=[self.get_next_by_pub_date().pk] ) + except model.DoesNotExist: + return '' def save(self, *args, **kwargs): created = self.pk is None @@ -134,7 +169,7 @@ class NewsletterMailing(models.Model): self.featured_image = LuxImage.objects.latest() old = type(self).objects.get(pk=self.pk) if self.pk else None if old and old.featured_image != self.featured_image: # Field has changed - s = LuxImageSize.objects.get(name="featured_jrnl") + s = LuxImageSize.objects.get(name="navigation_thumb") ss = LuxImageSize.objects.get(name="picwide-med") self.featured_image.sizes.add(s) self.featured_image.sizes.add(ss) @@ -319,3 +354,40 @@ def get_address(name, email): return u'%s <%s>' % (name, email) else: return u'%s' % email + + +class MailingStatus(models.Model): + """Status of the reception""" + SENT_TEST = -1 + SENT = 0 + ERROR = 1 + INVALID = 2 + OPENED = 4 + OPENED_ON_SITE = 5 + LINK_OPENED = 6 + UNSUBSCRIPTION = 7 + + STATUS_CHOICES = ((SENT_TEST, _('sent in test')), + (SENT, _('sent')), + (ERROR, _('error')), + (INVALID, _('invalid email')), + (OPENED, _('opened')), + (OPENED_ON_SITE, _('opened on site')), + (LINK_OPENED, _('link opened')), + (UNSUBSCRIPTION, _('unsubscription')), + ) + + newsletter_mailing = models.ForeignKey(NewsletterMailing, on_delete=models.CASCADE, verbose_name=_('newsletter')) + subscriber = models.ForeignKey(Subscriber, on_delete=models.CASCADE, verbose_name=_('subscriber')) + status = models.IntegerField(_('status'), choices=STATUS_CHOICES) + creation_date = models.DateTimeField(_('creation date'), auto_now_add=True) + + def __str__(self): + return '%s : %s : %s' % (self.newsletter.__str__(), + self.contact.__str__(), + self.get_status_display()) + + class Meta: + ordering = ('-creation_date',) + verbose_name = _('subscriber mailing status') + verbose_name_plural = _('subscriber mailing statuses') |