"""send the weekly email""" from datetime import timedelta 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) print( f"Sending weekly emails to {len(subscribers)} subscribers at {timezone.now()}" ) cutoff = timezone.now() - timedelta(days=7) blogs = models.Blog.objects.filter(approved=True, updateddate__gte=cutoff) articles = models.Article.objects.filter(pubdate__gte=cutoff) events = models.Event.objects.filter(approved=True, pub_date__gte=cutoff) cfps = models.CallForPapers.objects.filter( conference__approved=True, closing_date__gte=timezone.now().date() ) newsletters = models.Newsletter.objects.filter( approved=True, pub_date__gte=cutoff ) groups = models.Group.objects.filter(approved=True, pub_date__gte=cutoff) new_blogs = "" for blog in blogs: title_string = f"

{blog.title}

" author_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 = ( "

New Blogs

" + new_blogs + "
" ) new_articles = "" for post in articles: title_string = f"

{post.title}

" author_string = ( f"

{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 = ( "

New Articles

" + new_articles + "
" ) coming_events = "" for event in events: s_date = event.start_date title_string = f"

{event.name}

" date_string = ( f"

{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 = ( "

Upcoming Events

" + coming_events + "
" ) open_cfps = "" for instance in cfps: c_date = instance.closing_date title_string = ( f"

{instance.name}

" ) dates_string = f"

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 = ( "

Open Calls

" + open_cfps + "
" ) new_newsletters = "" for instance in newsletters: title_string = f"

{instance.name}

" author_string = ( f"

{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 = ( "

New Newsletters

" + new_newsletters + "
" ) new_groups = "" for instance in groups: group_type = GroupType(instance.type).label title_string = f"

{instance.name}

" register_string = f"

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 = ( "

New Groups

" + new_groups + "
" ) # Now let's put it all together... dt = timezone.now() choices = [ "🍓", "🍒", "🍎", "🍊", "🍍", "🍋", "🍉", "🥝", "🥦", "🥒", "🥕", "🍏", "🍅", "🥬", "🫐", "🍐", "🥗", "☕️", "🚚", "📬", "🍣", ] emoji = random.choice(choices) subject = f"{emoji} Fresh Aus GLAMR updates for the week of {dt.day} {dt:%B} {dt.year}" sections = [ new_articles, new_blogs, new_newsletters, new_groups, open_cfps, coming_events, ] body = "".join(sections) 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.

" end = "" parts = [start, body, footer, end] message = "".join(parts) send_email(subject, message, subscriber.email) print(f"Weekly emails completed {timezone.now()}")