diff --git a/src/tickets/apps.py b/src/tickets/apps.py index 307172a7..6418e2fe 100644 --- a/src/tickets/apps.py +++ b/src/tickets/apps.py @@ -1,13 +1,7 @@ from django.apps import AppConfig -from .signal_handlers import ticket_created -from django.db.models.signals import post_save import logging logger = logging.getLogger("bornhack.%s" % __name__) class TicketsConfig(AppConfig): name = 'tickets' - - def ready(self): - post_save.connect(ticket_created, sender='tickets.ShopTicket') - diff --git a/src/tickets/models.py b/src/tickets/models.py index 52c84e0b..279d2543 100644 --- a/src/tickets/models.py +++ b/src/tickets/models.py @@ -4,16 +4,25 @@ import hashlib import base64 import qrcode -from utils.models import CreatedUpdatedModel, CampRelatedModel from django.conf import settings +from django.utils import timezone +from django.dispatch import receiver from django.core.urlresolvers import reverse_lazy from django.utils.translation import ugettext_lazy as _ from utils.models import ( UUIDModel, + CampRelatedModel, CreatedUpdatedModel ) from utils.pdf import generate_pdf_letter from django.db import models +from django.db.models.signals import post_save +from datetime import ( + timedelta, + datetime +) +from django.db.models import Count +from ircbot.models import OutgoingIrcMessage logger = logging.getLogger("bornhack.%s" % __name__) @@ -56,7 +65,9 @@ class BaseTicket(CreatedUpdatedModel, UUIDModel): return qrcode_base64 def get_qr_code_url(self): - return 'data:image/png;base64,{}'.format(self.get_qr_code_base64().decode('utf-8')) + return 'data:image/png;base64,{}'.format( + self.get_qr_code_base64().decode('utf-8') + ) def generate_pdf(self): return generate_pdf_letter( @@ -134,3 +145,59 @@ class ShopTicket(BaseTicket): def shortname(self): return "shop" + +@receiver(post_save, sender=ShopTicket) +def ticket_created(sender, instance, created, **kwargs): + # only send a message when a ticket is created + if not created: + return + + # queue an IRC message to the orga channel if defined, + # otherwise for the default channel + target = settings.IRCBOT_CHANNELS['orga'] if 'orga' in settings.IRCBOT_CHANNELS else settings.IRCBOT_CHANNELS['default'] + + # get ticket stats + ticket_prefix = "BornHack {}".format(datetime.now().year) + + stats = ", ".join( + [ + "{}: {}".format( + tickettype['product__name'].replace( + "{} ".format(ticket_prefix), + "" + ), + tickettype['total'] + ) for tickettype in ShopTicket.objects.filter( + product__name__startswith=ticket_prefix + ).exclude( + product__name__startswith="{} One Day".format(ticket_prefix) + ).values( + 'product__name' + ).annotate( + total=Count('product__name') + ).order_by('-total') + ] + ) + + onedaystats = ShopTicket.objects.filter( + product__name__startswith="{} One Day Ticket".format(ticket_prefix) + ).count() + onedaychildstats = ShopTicket.objects.filter( + product__name__startswith="{} One Day Children".format(ticket_prefix) + ).count() + + # queue the messages + OutgoingIrcMessage.objects.create( + target=target, + message="%s sold!" % instance.product.name, + timeout=timezone.now()+timedelta(minutes=10) + ) + OutgoingIrcMessage.objects.create( + target=target, + message="Totals: {}, 1day: {}, 1day child: {}".format( + stats, + onedaystats, + onedaychildstats + )[:200], + timeout=timezone.now()+timedelta(minutes=10) + ) diff --git a/src/tickets/signal_handlers.py b/src/tickets/signal_handlers.py deleted file mode 100644 index de588c60..00000000 --- a/src/tickets/signal_handlers.py +++ /dev/null @@ -1,36 +0,0 @@ -from django.utils import timezone -from django.conf import settings -from datetime import timedelta -from django.db.models import Count -import logging -logger = logging.getLogger("bornhack.%s" % __name__) - - -def ticket_created(sender, instance, created, **kwargs): - # only send a message when a ticket is created - if not created: - return - - # queue an IRC message to the orga channel if any is defined, otherwise for the default channel - target = settings.IRCBOT_CHANNELS['orga'] if 'orga' in settings.IRCBOT_CHANNELS else settings.IRCBOT_CHANNELS['default'] - - # get ticket stats, FIXME: Camp name is hardcoded here for now - from tickets.models import ShopTicket - stats = ", ".join(["%s: %s" % (tickettype['product__name'].replace("BornHack 2017 ", ""), tickettype['total']) for tickettype in ShopTicket.objects.filter(product__name__startswith="BornHack 2017").exclude(product__name__startswith="BornHack 2017 One Day").values('product__name').annotate(total=Count('product__name')).order_by('-total')]) - - onedaystats = Ticket.objects.filter(product__name__startswith="BornHack 2017 One Day Ticket").count() - onedaychildstats = Ticket.objects.filter(product__name__startswith="BornHack 2017 One Day Children").count() - - # queue the messages - from ircbot.models import OutgoingIrcMessage - OutgoingIrcMessage.objects.create( - target=target, - message="%s sold!" % instance.product.name, - timeout=timezone.now()+timedelta(minutes=10) - ) - OutgoingIrcMessage.objects.create( - target=target, - message="Totals: %s, 1day: %s, 1day child: %s" % (stats, onedaystats, onedaychildstats)[:200], - timeout=timezone.now()+timedelta(minutes=10) - ) - diff --git a/src/utils/management/commands/bootstrap-devsite.py b/src/utils/management/commands/bootstrap-devsite.py index 250f11c2..b3331837 100644 --- a/src/utils/management/commands/bootstrap-devsite.py +++ b/src/utils/management/commands/bootstrap-devsite.py @@ -321,29 +321,29 @@ class Command(BaseCommand): camp=camp ) - name = 'Standard ticket {}'.format(year) + name = 'BornHack {} Standard ticket'.format(year) ticket1 = Product.objects.create( name=name, description='A ticket', price=1200, category=tickets, available_in=( - timezone.datetime(2017, 3, 1, 12, 0, tzinfo=timezone.utc), - timezone.datetime(2017, 8, 20, 12, 0, tzinfo=timezone.utc), + timezone.datetime(year, 1, 1, 12, 0, tzinfo=timezone.utc), + timezone.datetime(year, 12, 20, 12, 0, tzinfo=timezone.utc), ), slug='{}'.format(slugify(name)), ticket_type=adult_full_week ) - name = 'Hacker ticket {}'.format(year) + name = 'BornHack {} Hacker ticket'.format(year) ticket2 = Product.objects.create( name=name, description='Another ticket', price=1337, category=tickets, available_in=( - timezone.datetime(2017, 3, 1, 12, 0, tzinfo=timezone.utc), - timezone.datetime(2017, 8, 20, 12, 0, tzinfo=timezone.utc), + timezone.datetime(year, 1, 1, 12, 0, tzinfo=timezone.utc), + timezone.datetime(year, 12, 20, 12, 0, tzinfo=timezone.utc), ), slug='{}'.format(slugify(name)), ticket_type=adult_full_week