"""send the weekly email""" from datetime import timedelta import logging import random from django.conf import settings from django.utils import timezone from django.core.management.base import BaseCommand from blogs import models from blogs.utilities import send_email from blogs.models.utils import GroupType class Command(BaseCommand): """the announce command""" # we could add arguments but we don't really need any def handle(self, *args, **options): """find subscribers and send an update""" subscribers = models.Subscriber.objects.filter(confirmed=True) logging.info( f"Sending weekly emails to {len(subscribers)} subscribers at {timezone.now()}" ) cutoff = timezone.now() - timedelta(days=7) blogs = models.Blog.objects.filter( approved=True, active=True, added__gte=cutoff ) articles = models.Article.objects.filter(pubdate__gte=cutoff) events = models.Event.objects.filter(approved=True, pubdate__gte=cutoff) cfps = models.CallForPapers.objects.filter( event__approved=True, closing_date__gte=timezone.now().date() ) newsletters = models.Newsletter.objects.filter( approved=True, pubdate__gte=cutoff ) editions = models.Edition.objects.filter(pubdate__gte=cutoff) groups = models.Group.objects.filter(approved=True, pubdate__gte=cutoff) new_blogs = "" for blog in blogs: title_string = f"
{blog.author_name}
" if blog.author_name else "" ) description_string = ( f"{blog.description}
" ) string_list = [title_string, author_string, description_string] string = "".join(string_list) new_blogs = new_blogs + string if new_blogs != "": new_blogs = ( "{post.author_name}
" if post.author_name else "" ) description_string = ( f"{post.description}
" ) string_list = [title_string, author_string, description_string] string = "".join(string_list) new_articles = new_articles + string if new_articles != "": new_articles = ( "{edition.author_name}
" if edition.author_name else "" ) description_string = ( f"{edition.description}
" ) string_list = [title_string, author_string, description_string] string = "".join(string_list) new_editions = new_editions + string if new_editions != "": new_editions = ( "{s_date:%a} {s_date.day} {s_date:%B} {s_date:%Y}
" ) description_string = ( f"{event.description}
" ) string_list = [title_string, date_string, description_string] string = "".join(string_list) coming_events = coming_events + string if coming_events != "": coming_events = ( "Closes:{c_date:%a} {c_date.day} {c_date:%B}
" description_string = ( f"{instance.details}
" ) string_list = [title_string, dates_string, description_string] string = "".join(string_list) open_cfps = open_cfps + string if open_cfps != "": open_cfps = ( "{instance.author}
" if instance.author else "" ) description_string = ( f"{instance.description}
" ) string_list = [title_string, author_string, description_string] string = "".join(string_list) new_newsletters = new_newsletters + string if new_newsletters != "": new_newsletters = ( "Register to join this {group_type}
" description_string = ( f"{instance.description}
" ) string_list = [title_string, register_string, description_string] string = "".join(string_list) new_groups = new_groups + string if new_groups != "": new_groups = ( "No new updates this week.
Why not spend some time publishing your own blog post instead?
" for subscriber in subscribers: opt_out = f"https://{settings.DOMAIN}/unsubscribe-email/{subscriber.token}/{subscriber.id}" start = "" footer = f"This email was sent to {subscriber.email} because you subscribed to email updates from Aus GLAMR.
You can unsubscribe at any time.