diff options
Diffstat (limited to 'app/lttr/models.py')
-rw-r--r-- | app/lttr/models.py | 77 |
1 files changed, 47 insertions, 30 deletions
diff --git a/app/lttr/models.py b/app/lttr/models.py index 91d5e22..5f80321 100644 --- a/app/lttr/models.py +++ b/app/lttr/models.py @@ -1,8 +1,8 @@ import datetime + 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 @@ -10,6 +10,10 @@ from django.urls import reverse from django.conf import settings from django.utils.crypto import get_random_string +from django.template import Context, Template + +from bs4 import BeautifulSoup + from taggit.managers import TaggableManager from utils.util import render_images, parse_video, markdown_to_html @@ -22,6 +26,18 @@ from books.models import Book ACTIONS = ('subscribe', 'unsubscribe', 'update') +def markdown_to_emailhtml(base_html): + soup = BeautifulSoup(base_html, "lxml") + for p in soup.find_all('p'): + p.attrs['style']="margin-top:0;margin-right:0;margin-left:0;padding-top:0;padding-bottom:0;padding-right:0;padding-left:0;font-weight:normal;margin-bottom:1.4em;font-size:17px;line-height:1.5;hyphens:auto;color:#222222;font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif !important;" + for i in soup.find_all('img'): + i.attrs['width']="720" + i.attrs['style']="margin-top:0;margin-right:0;margin-left:0;padding-top:0;padding-bottom:0;padding-right:0;padding-left:0;display:inline;margin-bottom:0;width:100% !important;max-width:100% !important;height:auto !important;max-height:auto !important;" + for h in soup.find_all('hr'): + h.attrs['style']="padding-top:0;padding-bottom:0;padding-right:0;padding-left:0;width:20%;margin-top:40px;margin-bottom:40px;margin-right:0px;margin-left:0px;border-width:0;border-top-width:1px;border-top-style:solid;border-top-color:#ddd;" + return str(soup)[12:-14] + + def make_activation_code(): """ Generate a unique activation code. """ @@ -108,6 +124,7 @@ class NewsletterMailing(models.Model): 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_email_html = models.TextField(blank=True) body_markdown = models.TextField() pub_date = models.DateTimeField() featured_image = models.ForeignKey(LuxImage, on_delete=models.CASCADE, null=True, blank=True) @@ -137,6 +154,10 @@ class NewsletterMailing(models.Model): if self.issue < 10: issue = "00%s" % self.issue return issue + + def email_encode(self): + + return self.body_markdown @property def get_previous_published(self): @@ -164,13 +185,14 @@ class NewsletterMailing(models.Model): if not created: md = render_images(self.body_markdown) self.body_html = markdown_to_html(md) + self.body_email_html = markdown_to_emailhtml(self.body_html) self.date_created = timezone.now() if created and not self.featured_image: 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="navigation_thumb") - ss = LuxImageSize.objects.get(name="picwide-med") + ss = LuxImageSize.objects.get(name="picwide-sm") self.featured_image.sizes.add(s) self.featured_image.sizes.add(ss) self.featured_image.save() @@ -180,7 +202,7 @@ class NewsletterMailing(models.Model): class Subscriber(models.Model): """ A model for Newletter Subscriber """ email_field = models.EmailField(db_column='email', db_index=True, blank=True, null=True) - user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) + user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, blank=True, null=True) date_created = models.DateTimeField(blank=True, auto_now_add=True, editable=False) date_updated = models.DateTimeField(blank=True, auto_now=True, editable=False) newsletter = models.ForeignKey(Newsletter, on_delete=models.CASCADE) @@ -191,7 +213,9 @@ class Subscriber(models.Model): unsubscribe_date = models.DateTimeField(null=True, blank=True) def __str__(self): - return self.user.username + if self.user: + return self.user.username + return self.email_field def get_name(self): if self.user: @@ -334,10 +358,8 @@ class Subscriber(models.Model): }) def unsubscribe_activate_url(self): - return reverse('newsletter_update_activate', kwargs={ + return reverse('lttr:newsletter_unsubscribe', kwargs={ 'slug': self.newsletter.slug, - 'email': self.email, - 'action': 'unsubscribe', 'activation_code': self.activation_code }) @@ -356,38 +378,33 @@ def get_address(name, email): 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')), - ) +class StatusType(models.IntegerChoices): + INIT = 0, ('Initialized') + SENT = 1, ('Sent') + ERROR = 2, ('Error') + +class MailingStatus(models.Model): 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) + status = models.IntegerField(choices=StatusType.choices, null=True) creation_date = models.DateTimeField(_('creation date'), auto_now_add=True) def __str__(self): - return '%s : %s : %s' % (self.newsletter.__str__(), - self.contact.__str__(), + return '%s : %s : %s' % (self.newsletter_mailing, + self.subscriber, self.get_status_display()) class Meta: ordering = ('-creation_date',) verbose_name = _('subscriber mailing status') verbose_name_plural = _('subscriber mailing statuses') + + +''' +from lttr.mailer import SendShit +mailing = NewsletterMailing.objects.get(pk=1) +newsletter = Newsletter.objects.get(pk=3) +n = SendShit(newsletter, mailing, 1) +n.send_mailings() +''' |