bornhack-website/src/tickets/models.py

153 lines
3.8 KiB
Python
Raw Normal View History

2017-08-17 15:51:24 +00:00
import io
import logging
import hashlib
import base64
import qrcode
from django.conf import settings
2018-01-07 18:59:53 +00:00
from django.utils import timezone
from django.dispatch import receiver
2017-08-17 15:51:24 +00:00
from django.core.urlresolvers import reverse_lazy
from django.utils.translation import ugettext_lazy as _
from utils.models import (
UUIDModel,
2018-01-07 18:59:53 +00:00
CampRelatedModel,
2017-08-17 15:51:24 +00:00
CreatedUpdatedModel
)
from utils.pdf import generate_pdf_letter
2017-08-19 23:05:30 +00:00
from django.db import models
2018-01-07 18:59:53 +00:00
from django.db.models.signals import post_save
from datetime import (
timedelta,
datetime
)
from django.db.models import Count
from ircbot.models import OutgoingIrcMessage
2017-08-17 15:51:24 +00:00
logger = logging.getLogger("bornhack.%s" % __name__)
# TicketType can be full week, one day. etc.
2017-08-19 23:05:30 +00:00
class TicketType(CampRelatedModel, UUIDModel):
2017-08-17 15:51:24 +00:00
name = models.TextField()
camp = models.ForeignKey('camps.Camp')
2017-08-17 15:51:24 +00:00
def __str__(self):
return '{} ({})'.format(self.name, self.camp.title)
2017-08-17 15:51:24 +00:00
class BaseTicket(CampRelatedModel, UUIDModel):
2017-08-17 15:51:24 +00:00
ticket_type = models.ForeignKey('TicketType')
2017-08-18 11:04:13 +00:00
checked_in = models.BooleanField(default=False)
badge_handed_out = models.BooleanField(default=False)
class Meta:
abstract = True
2017-08-17 15:51:24 +00:00
@property
def camp(self):
return self.ticket_type.camp
2017-08-17 15:51:24 +00:00
def _get_token(self):
return hashlib.sha256(
'{_id}{secret_key}'.format(
_id=self.pk,
secret_key=settings.SECRET_KEY,
).encode('utf-8')
).hexdigest()
2017-08-19 21:10:13 +00:00
def get_qr_code_base64(self):
2017-08-17 15:51:24 +00:00
qr = qrcode.make(
self._get_token(),
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H
).resize((250, 250))
file_like = io.BytesIO()
qr.save(file_like, format='png')
qrcode_base64 = base64.b64encode(file_like.getvalue())
return qrcode_base64
def get_qr_code_url(self):
2018-01-07 18:59:53 +00:00
return 'data:image/png;base64,{}'.format(
self.get_qr_code_base64().decode('utf-8')
)
2017-08-17 15:51:24 +00:00
def generate_pdf(self):
return generate_pdf_letter(
2017-08-19 22:25:48 +00:00
filename='{}_ticket_{}.pdf'.format(self.shortname, self.pk),
2017-08-17 15:51:24 +00:00
formatdict={'ticket': self},
template='pdf/ticket.html'
)
class SponsorTicket(BaseTicket):
2017-08-17 15:51:24 +00:00
sponsor = models.ForeignKey('sponsors.Sponsor')
def __str__(self):
return 'SponsorTicket: {}'.format(self.pk)
2017-08-19 22:25:48 +00:00
@property
def shortname(self):
return "sponsor"
2017-08-17 15:51:24 +00:00
class DiscountTicket(BaseTicket):
2017-08-17 15:51:24 +00:00
price = models.IntegerField(
help_text=_('Price of the discounted ticket (in DKK, including VAT).')
)
def __str__(self):
return 'DiscountTicket: {}'.format(self.pk)
2017-08-19 22:25:48 +00:00
@property
def shortname(self):
return "discount"
2017-08-17 15:51:24 +00:00
class ShopTicket(BaseTicket):
2017-08-17 15:51:24 +00:00
order = models.ForeignKey('shop.Order', related_name='shoptickets')
product = models.ForeignKey('shop.Product')
name = models.CharField(
max_length=100,
help_text=(
'Name of the person this ticket belongs to. '
'This can be different from the buying user.'
),
null=True,
blank=True,
)
email = models.EmailField(
null=True,
blank=True,
)
# overwrite the _get_token method because old tickets use the user_id
def _get_token(self):
return hashlib.sha256(
'{_id}{user_id}{secret_key}'.format(
_id=self.pk,
user_id=self.order.user.pk,
secret_key=settings.SECRET_KEY,
).encode('utf-8')
).hexdigest()
def __str__(self):
return 'Ticket {user} {product}'.format(
user=self.order.user,
product=self.product
)
def get_absolute_url(self):
return str(
reverse_lazy('tickets:shopticket_edit', kwargs={'pk': self.pk})
)
2017-08-19 22:25:48 +00:00
@property
def shortname(self):
return "shop"
2018-01-07 18:59:53 +00:00