From ac19a6db4bbe24ac2a6016269847ef8494f68a91 Mon Sep 17 00:00:00 2001 From: Thomas Steen Rasmussen Date: Thu, 8 Aug 2019 09:44:47 +0200 Subject: [PATCH] add management command to email sponsor tickets --- src/sponsors/admin.py | 2 +- src/sponsors/email.py | 25 +++++++++++++ .../commands/email_sponsor_tickets.py | 36 +++++++++++++++++++ .../migrations/0012_auto_20190807_2229.py | 28 +++++++++++++++ src/sponsors/models.py | 6 ++++ .../templates/emails/sponsorticket_email.html | 11 ++++++ .../templates/emails/sponsorticket_email.txt | 11 ++++++ 7 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 src/sponsors/email.py create mode 100644 src/sponsors/management/commands/email_sponsor_tickets.py create mode 100644 src/sponsors/migrations/0012_auto_20190807_2229.py create mode 100644 src/sponsors/templates/emails/sponsorticket_email.html create mode 100644 src/sponsors/templates/emails/sponsorticket_email.txt diff --git a/src/sponsors/admin.py b/src/sponsors/admin.py index 9f5e4ff5..74772526 100644 --- a/src/sponsors/admin.py +++ b/src/sponsors/admin.py @@ -5,7 +5,7 @@ from .models import Sponsor, SponsorTier @admin.register(Sponsor) class SponsorAdmin(admin.ModelAdmin): - list_display = ("name", "tier") + list_display = ("name", "tier", "ticket_email", "ticket_ready", "tickets_sent") list_filter = ("tier__camp",) diff --git a/src/sponsors/email.py b/src/sponsors/email.py new file mode 100644 index 00000000..db0a8537 --- /dev/null +++ b/src/sponsors/email.py @@ -0,0 +1,25 @@ +from utils.email import add_outgoing_email + + +def add_sponsorticket_email(sponsor): + # put formatdict together + formatdict = { + "sponsor": sponsor, + } + + subject = "BornHack %s Sponsor Tickets" % sponsor.camp.title + attachments = [] + for ticket in sponsor.sponsorticket_set.all(): + path = "sponsor_ticket_%s" % ticket.uuid + attachments.append() + + # add email to outgoing email queue + return add_outgoing_email( + text_template="emails/sponsorticket_email.txt", + html_template="emails/sponsorticket_email.html", + to_recipients=sponsor.ticket_email, + formatdict=formatdict, + subject=subject, + attachments=attachments + ) + diff --git a/src/sponsors/management/commands/email_sponsor_tickets.py b/src/sponsors/management/commands/email_sponsor_tickets.py new file mode 100644 index 00000000..e186ee05 --- /dev/null +++ b/src/sponsors/management/commands/email_sponsor_tickets.py @@ -0,0 +1,36 @@ +# coding: utf-8 +from django.core.management.base import BaseCommand +from django.utils import timezone + +from camps.models import Camp +from sponsors.models import Sponsor +from sponsors.email import add_sponsorticket_email +from tickets.models import SponsorTicket, TicketType + + +class Command(BaseCommand): + help = "Emails sponsor tickets" + + def add_arguments(self, parser): + parser.add_argument("camp_slug", type=str) + + def output(self, message): + self.stdout.write( + "{}: {}".format(timezone.now().strftime("%Y-%m-%d %H:%M:%S"), message) + ) + + def handle(self, *args, **options): + camp = Camp.objects.get(slug=options["camp_slug"]) + sponsors = Sponsor.objects.filter(tier__camp=camp, tickets_generated=False) + + for sponsor in sponsors: + if sponsor.tier.tickets and sponsor.ticket_email and sponsor.ticket_ready and not sponsor.tickets_sent: + self.output("# Generating outgoing email to send tickets for {}:".format(sponsor)) + # send the email + if add_sponsorticket_email(sponsor=sponsor): + logger.info("OK: email to %s added" % sponsor) + sponsor.tickets_sent = True + sponsor.save() + else: + logger.error("Unable to send sponsor ticket email to %s" % sponsor) + diff --git a/src/sponsors/migrations/0012_auto_20190807_2229.py b/src/sponsors/migrations/0012_auto_20190807_2229.py new file mode 100644 index 00000000..b194b01d --- /dev/null +++ b/src/sponsors/migrations/0012_auto_20190807_2229.py @@ -0,0 +1,28 @@ +# Generated by Django 2.2.3 on 2019-08-07 20:29 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('sponsors', '0011_auto_20181118_1513'), + ] + + operations = [ + migrations.AddField( + model_name='sponsor', + name='ticket_email', + field=models.EmailField(blank=True, help_text='The email to send the tickets to', max_length=254, null=True), + ), + migrations.AddField( + model_name='sponsor', + name='ticket_ready', + field=models.BooleanField(default=False, help_text='Check when we are ready to send tickets to this sponsor.'), + ), + migrations.AddField( + model_name='sponsor', + name='tickets_sent', + field=models.BooleanField(default=False, help_text='True when the tickets have been emailed to the sponsor'), + ), + ] diff --git a/src/sponsors/models.py b/src/sponsors/models.py index 8ed52c0e..d8e310e6 100644 --- a/src/sponsors/models.py +++ b/src/sponsors/models.py @@ -23,6 +23,12 @@ class Sponsor(CampRelatedModel): tickets_generated = models.BooleanField(default=False) + ticket_email = models.EmailField(null=True, blank=True, help_text="The email to send the tickets to") + + ticket_ready = models.BooleanField(default=False, help_text="Check when we are ready to send tickets to this sponsor.") + + tickets_sent = models.BooleanField(default=False, help_text="True when the tickets have been emailed to the sponsor") + def __str__(self): return "{} ({})".format(self.name, self.tier.camp) diff --git a/src/sponsors/templates/emails/sponsorticket_email.html b/src/sponsors/templates/emails/sponsorticket_email.html new file mode 100644 index 00000000..5eeca3bc --- /dev/null +++ b/src/sponsors/templates/emails/sponsorticket_email.html @@ -0,0 +1,11 @@ +Hello!
+
+This email contains the {{ sponsor.name }} sponsor tickets for {{ sponsor.camp.title }}
+Thank you for helping out! :)
+
+Best regards,
+
+BornHack
+
+(This email is automatically generated)
+
diff --git a/src/sponsors/templates/emails/sponsorticket_email.txt b/src/sponsors/templates/emails/sponsorticket_email.txt new file mode 100644 index 00000000..8b2882db --- /dev/null +++ b/src/sponsors/templates/emails/sponsorticket_email.txt @@ -0,0 +1,11 @@ +Hello! + +This email contains the {{ sponsor.name }} sponsor tickets for {{ sponsor.camp.title }} +Thank you for helping out! :) + +Best regards, + +BornHack + +(This email is automatically generated) +