This commit is contained in:
Stephan Telling 2018-01-07 19:59:53 +01:00
parent 84f187709b
commit 328d6aa803
No known key found for this signature in database
GPG key ID: D4892289F36ADA9B
3 changed files with 69 additions and 44 deletions

View file

@ -1,13 +1,7 @@
from django.apps import AppConfig from django.apps import AppConfig
from .signal_handlers import ticket_created
from django.db.models.signals import post_save
import logging import logging
logger = logging.getLogger("bornhack.%s" % __name__) logger = logging.getLogger("bornhack.%s" % __name__)
class TicketsConfig(AppConfig): class TicketsConfig(AppConfig):
name = 'tickets' name = 'tickets'
def ready(self):
post_save.connect(ticket_created, sender='tickets.ShopTicket')

View file

@ -4,16 +4,25 @@ import hashlib
import base64 import base64
import qrcode import qrcode
from utils.models import CreatedUpdatedModel, CampRelatedModel
from django.conf import settings from django.conf import settings
from django.utils import timezone
from django.dispatch import receiver
from django.core.urlresolvers import reverse_lazy from django.core.urlresolvers import reverse_lazy
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from utils.models import ( from utils.models import (
UUIDModel, UUIDModel,
CampRelatedModel,
CreatedUpdatedModel CreatedUpdatedModel
) )
from utils.pdf import generate_pdf_letter from utils.pdf import generate_pdf_letter
from django.db import models 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__) logger = logging.getLogger("bornhack.%s" % __name__)
@ -56,7 +65,9 @@ class BaseTicket(CreatedUpdatedModel, UUIDModel):
return qrcode_base64 return qrcode_base64
def get_qr_code_url(self): 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): def generate_pdf(self):
return generate_pdf_letter( return generate_pdf_letter(
@ -134,3 +145,59 @@ class ShopTicket(BaseTicket):
def shortname(self): def shortname(self):
return "shop" 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)
)

View file

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