calculate qrcode_base64 on the fly

This commit is contained in:
Thomas Steen Rasmussen 2017-08-19 23:10:13 +02:00
parent bf56a1624c
commit e6ca8a5c71
2 changed files with 29 additions and 7 deletions

View file

@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-08-19 21:09
from __future__ import unicode_literals
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('tickets', '0002_auto_20170819_2222'),
]
operations = [
migrations.RemoveField(
model_name='discountticket',
name='qrcode_base64',
),
migrations.RemoveField(
model_name='shopticket',
name='qrcode_base64',
),
migrations.RemoveField(
model_name='sponsorticket',
name='qrcode_base64',
),
]

View file

@ -28,17 +28,12 @@ class TicketType(CreatedUpdatedModel, UUIDModel):
class BaseTicket(CreatedUpdatedModel, UUIDModel):
qrcode_base64 = models.TextField(null=True, blank=True)
ticket_type = models.ForeignKey('TicketType')
checked_in = models.BooleanField(default=False)
class Meta:
abstract = True
def save(self, **kwargs):
self.qrcode_base64 = self.get_qr_code()
super().save(**kwargs)
def _get_token(self):
return hashlib.sha256(
'{_id}{secret_key}'.format(
@ -47,7 +42,7 @@ class BaseTicket(CreatedUpdatedModel, UUIDModel):
).encode('utf-8')
).hexdigest()
def get_qr_code(self):
def get_qr_code_base64(self):
qr = qrcode.make(
self._get_token(),
version=1,
@ -59,7 +54,7 @@ class BaseTicket(CreatedUpdatedModel, UUIDModel):
return qrcode_base64
def get_qr_code_url(self):
return 'data:image/png;base64,{}'.format(self.qrcode_base64)
return 'data:image/png;base64,{}'.format(self.get_qr_code_base64)
def generate_pdf(self):
generate_pdf_letter(