Add a command to generate sponsor tickets.

This commit is contained in:
Víðir Valberg Guðmundsson 2018-08-15 11:30:33 +02:00
parent 32f4ae13e3
commit 47bf0fcdbf
5 changed files with 69 additions and 0 deletions

View file

View file

@ -0,0 +1,38 @@
# 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 tickets.models import SponsorTicket, TicketType
class Command(BaseCommand):
help = 'Creates sponsor tickets'
def add_arguments(self, parser):
parser.add_argument('camp_slug', type=str,)
parser.add_argument('ticket_type_pk', 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):
ticket_type = TicketType.objects.get(pk=options['ticket_type_pk'])
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:
self.output("# Generating tickets for {}:".format(sponsor))
for _ in range(sponsor.tier.tickets):
ticket = SponsorTicket(sponsor=sponsor, ticket_type=ticket_type)
ticket.save()
ticket.generate_pdf()
self.output("- {}_ticket_{}.pdf".format(ticket.shortname, ticket.pk))
else:
self.output("{} is in tier {}, which has no tickets set.".format(sponsor, sponsor.tier))

View file

@ -0,0 +1,23 @@
# Generated by Django 2.1 on 2018-08-15 09:19
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('sponsors', '0007_auto_20180318_0906'),
]
operations = [
migrations.AddField(
model_name='sponsor',
name='tickets_generated',
field=models.BooleanField(default=False),
),
migrations.AddField(
model_name='sponsortier',
name='tickets',
field=models.IntegerField(blank=True, help_text='If set this is the number of tickets generated for a sponsor in this tier.', null=True),
),
]

View file

@ -36,6 +36,8 @@ class Sponsor(CampRelatedModel):
help_text="An URL to the sponsor." help_text="An URL to the sponsor."
) )
tickets_generated = models.BooleanField(default=False)
def __str__(self): def __str__(self):
return '{} ({})'.format(self.name, self.tier.camp) return '{} ({})'.format(self.name, self.tier.camp)
@ -70,5 +72,11 @@ class SponsorTier(CampRelatedModel):
gold should have a lower value than silver.""" gold should have a lower value than silver."""
) )
tickets = models.IntegerField(
null=True,
blank=True,
help_text="If set this is the number of tickets generated for a sponsor in this tier."
)
def __str__(self): def __str__(self):
return '{} ({})'.format(self.name, self.camp) return '{} ({})'.format(self.name, self.camp)