2017-09-15 16:30:20 -04:00
|
|
|
from django.db.models import Q
|
|
|
|
from django.core import mail
|
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
from django.utils import timezone
|
|
|
|
from django.utils.html import strip_tags
|
|
|
|
|
|
|
|
from OACPL import settings
|
2017-09-30 15:25:06 -04:00
|
|
|
from OACPL.utils import url_fix_render_to_string
|
2017-09-15 16:30:20 -04:00
|
|
|
from newsletters.models import Newsletter, Subscriber
|
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = 'Send out newsletter emails to subscribers'
|
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
newsletters = Newsletter.objects.filter(Q(sent=False) & Q(publish__lte=timezone.now()))
|
|
|
|
subscribers = Subscriber.objects.all().values_list('email', flat=True)
|
2017-09-29 16:21:21 -04:00
|
|
|
print('Found %s unsent newsletters' % len(newsletters))
|
|
|
|
print('Found %s subscribers' % len(subscribers))
|
2017-09-15 16:30:20 -04:00
|
|
|
for newsletter in newsletters:
|
2017-09-29 20:07:05 -04:00
|
|
|
print('Sending newsletter: "%s"' % newsletter.subject)
|
2018-01-14 17:15:28 -05:00
|
|
|
mail.send_mail(newsletter.subject, strip_tags(newsletter.body), settings.EMAIL_HOST_USER, subscribers, html_message=url_fix_render_to_string('email.html', {'content': newsletter.body, 'unsubscribe': True}))
|
2017-09-15 16:30:20 -04:00
|
|
|
newsletter.sent = True
|
|
|
|
newsletter.save()
|
|
|
|
print('Complete!')
|