Merge pull request #198 from bornhack/st/fix-174

work on #174
This commit is contained in:
Stephan Telling 2018-01-07 20:15:51 +01:00 committed by GitHub
commit cdc35eb114
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 75 additions and 50 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)
)

View file

@ -321,29 +321,29 @@ class Command(BaseCommand):
camp=camp camp=camp
) )
name = 'Standard ticket {}'.format(year) name = 'BornHack {} Standard ticket'.format(year)
ticket1 = Product.objects.create( ticket1 = Product.objects.create(
name=name, name=name,
description='A ticket', description='A ticket',
price=1200, price=1200,
category=tickets, category=tickets,
available_in=( available_in=(
timezone.datetime(2017, 3, 1, 12, 0, tzinfo=timezone.utc), timezone.datetime(year, 1, 1, 12, 0, tzinfo=timezone.utc),
timezone.datetime(2017, 8, 20, 12, 0, tzinfo=timezone.utc), timezone.datetime(year, 12, 20, 12, 0, tzinfo=timezone.utc),
), ),
slug='{}'.format(slugify(name)), slug='{}'.format(slugify(name)),
ticket_type=adult_full_week ticket_type=adult_full_week
) )
name = 'Hacker ticket {}'.format(year) name = 'BornHack {} Hacker ticket'.format(year)
ticket2 = Product.objects.create( ticket2 = Product.objects.create(
name=name, name=name,
description='Another ticket', description='Another ticket',
price=1337, price=1337,
category=tickets, category=tickets,
available_in=( available_in=(
timezone.datetime(2017, 3, 1, 12, 0, tzinfo=timezone.utc), timezone.datetime(year, 1, 1, 12, 0, tzinfo=timezone.utc),
timezone.datetime(2017, 8, 20, 12, 0, tzinfo=timezone.utc), timezone.datetime(year, 12, 20, 12, 0, tzinfo=timezone.utc),
), ),
slug='{}'.format(slugify(name)), slug='{}'.format(slugify(name)),
ticket_type=adult_full_week ticket_type=adult_full_week