add management command to email sponsor tickets

This commit is contained in:
Thomas Steen Rasmussen 2019-08-08 09:44:47 +02:00
parent 99977759ee
commit ac19a6db4b
7 changed files with 118 additions and 1 deletions

View file

@ -5,7 +5,7 @@ from .models import Sponsor, SponsorTier
@admin.register(Sponsor) @admin.register(Sponsor)
class SponsorAdmin(admin.ModelAdmin): class SponsorAdmin(admin.ModelAdmin):
list_display = ("name", "tier") list_display = ("name", "tier", "ticket_email", "ticket_ready", "tickets_sent")
list_filter = ("tier__camp",) list_filter = ("tier__camp",)

25
src/sponsors/email.py Normal file
View file

@ -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
)

View file

@ -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)

View file

@ -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'),
),
]

View file

@ -23,6 +23,12 @@ class Sponsor(CampRelatedModel):
tickets_generated = models.BooleanField(default=False) 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): def __str__(self):
return "{} ({})".format(self.name, self.tier.camp) return "{} ({})".format(self.name, self.tier.camp)

View file

@ -0,0 +1,11 @@
Hello!<br>
<br>
This email contains the {{ sponsor.name }} sponsor tickets for {{ sponsor.camp.title }}<br>
Thank you for helping out! :)<br>
<br>
Best regards,<br>
<br>
BornHack<br>
<br>
(This email is automatically generated)<br>
<br>

View file

@ -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)