remove the old Ticket model, move irc notification signal handler to the new Tickets app, various cleanup, fixes #168

This commit is contained in:
Thomas Steen Rasmussen 2017-10-04 00:06:49 +02:00
parent e0e65212bc
commit 66014811a4
7 changed files with 37 additions and 94 deletions

View file

@ -49,11 +49,6 @@ class ProductInline(admin.TabularInline):
model = models.OrderProductRelation model = models.OrderProductRelation
class TicketInline(admin.TabularInline):
model = models.Ticket
exclude = ['qrcode_base64']
@admin.register(models.Order) @admin.register(models.Order)
class OrderAdmin(admin.ModelAdmin): class OrderAdmin(admin.ModelAdmin):
change_form_template = 'admin/change_order_form.html' change_form_template = 'admin/change_order_form.html'
@ -91,7 +86,7 @@ class OrderAdmin(admin.ModelAdmin):
exclude = ['products'] exclude = ['products']
inlines = [ProductInline, TicketInline] inlines = [ProductInline]
actions = ['mark_order_as_paid', 'mark_order_as_refunded'] actions = ['mark_order_as_paid', 'mark_order_as_refunded']
@ -109,24 +104,3 @@ class OrderAdmin(admin.ModelAdmin):
def get_user_email(obj): def get_user_email(obj):
return obj.order.user.email return obj.order.user.email
@admin.register(models.Ticket)
class TicketModelAdmin(admin.ModelAdmin):
list_display = [
'order',
'product',
'name',
'email',
get_user_email,
'get_token',
'checked_in'
]
list_filter = ['product', 'checked_in']
actions = ['mark_as_arrived']
def mark_as_arrived(self, request, queryset):
queryset.update(checked_in=True)

View file

@ -1,12 +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 ShopConfig(AppConfig): class ShopConfig(AppConfig):
name = 'shop' name = 'shop'
def ready(self):
post_save.connect(ticket_created, sender='shop.Ticket')

View file

@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-10-03 22:05
from __future__ import unicode_literals
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('shop', '0051_creditnote_danish_vat'),
]
operations = [
migrations.RemoveField(
model_name='ticket',
name='order',
),
migrations.RemoveField(
model_name='ticket',
name='product',
),
migrations.DeleteModel(
name='Ticket',
),
]

View file

@ -534,62 +534,3 @@ class CoinifyAPIRequest(CreatedUpdatedModel):
def __str__(self): def __str__(self):
return 'order %s api request %s' % (self.order.id, self.method) return 'order %s api request %s' % (self.order.id, self.method)
class Ticket(CreatedUpdatedModel, UUIDModel):
order = models.ForeignKey('shop.Order', related_name='tickets')
product = models.ForeignKey('shop.Product', related_name='tickets')
qrcode_base64 = models.TextField(null=True, blank=True)
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,
)
checked_in = models.BooleanField(default=False)
def __str__(self):
return 'Ticket {user} {product}'.format(
user=self.order.user,
product=self.product
)
def save(self, **kwargs):
super(Ticket, self).save(**kwargs)
self.qrcode_base64 = self.get_qr_code()
super(Ticket, self).save(**kwargs)
def get_token(self):
return hashlib.sha256(
'{ticket_id}{user_id}{secret_key}'.format(
ticket_id=self.pk,
user_id=self.order.user.pk,
secret_key=settings.SECRET_KEY,
).encode('utf-8')
).hexdigest()
def get_qr_code(self):
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):
return 'data:image/png;base64,{}'.format(self.qrcode_base64)
def get_absolute_url(self):
return str(reverse_lazy('shop:ticket_detail', kwargs={'pk': self.pk}))

View file

@ -29,7 +29,6 @@ from shop.models import (
EpayPayment, EpayPayment,
CoinifyAPIInvoice, CoinifyAPIInvoice,
CoinifyAPICallback, CoinifyAPICallback,
Ticket,
CreditNote, CreditNote,
) )
from .forms import AddToOrderForm from .forms import AddToOrderForm

View file

@ -1,5 +1,13 @@
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
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

@ -15,8 +15,8 @@ def ticket_created(sender, instance, created, **kwargs):
target = settings.IRCBOT_CHANNELS['orga'] if 'orga' in settings.IRCBOT_CHANNELS else settings.IRCBOT_CHANNELS['default'] 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 # get ticket stats, FIXME: Camp name is hardcoded here for now
from shop.models import Ticket from tickets.models import ShopTicket
stats = ", ".join(["%s: %s" % (tickettype['product__name'].replace("BornHack 2017 ", ""), tickettype['total']) for tickettype in Ticket.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')]) 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() 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() onedaychildstats = Ticket.objects.filter(product__name__startswith="BornHack 2017 One Day Children").count()