From a07e74180e0c52fbbc66683b7f01033dbaef4a0d Mon Sep 17 00:00:00 2001 From: Thomas Steen Rasmussen Date: Sat, 19 Aug 2017 22:22:54 +0200 Subject: [PATCH 1/5] redo migrations for Tickets app again, fix BaseTicket to be abstract --- .../migrations/0048_product_ticket_type.py | 2 +- src/tickets/migrations/0001_initial.py | 92 ++++++++++--------- .../migrations/0002_auto_20170819_2222.py | 36 ++++++++ src/tickets/models.py | 3 + 4 files changed, 89 insertions(+), 44 deletions(-) create mode 100644 src/tickets/migrations/0002_auto_20170819_2222.py diff --git a/src/shop/migrations/0048_product_ticket_type.py b/src/shop/migrations/0048_product_ticket_type.py index a9b9ae5f..f473f593 100644 --- a/src/shop/migrations/0048_product_ticket_type.py +++ b/src/shop/migrations/0048_product_ticket_type.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Generated by Django 1.10.5 on 2017-08-19 19:32 +# Generated by Django 1.10.5 on 2017-08-19 20:21 from __future__ import unicode_literals from django.db import migrations, models diff --git a/src/tickets/migrations/0001_initial.py b/src/tickets/migrations/0001_initial.py index 7c59deaa..2c350430 100644 --- a/src/tickets/migrations/0001_initial.py +++ b/src/tickets/migrations/0001_initial.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Generated by Django 1.10.5 on 2017-08-19 19:32 +# Generated by Django 1.10.5 on 2017-08-19 20:21 from __future__ import unicode_literals from django.db import migrations, models @@ -12,20 +12,52 @@ class Migration(migrations.Migration): initial = True dependencies = [ - ('shop', '0047_auto_20170522_1942'), - ('sponsors', '0006_auto_20170715_1110'), ('camps', '0022_camp_colour'), + ('sponsors', '0006_auto_20170715_1110'), + ('shop', '0047_auto_20170522_1942'), ] operations = [ migrations.CreateModel( - name='BaseTicket', + name='DiscountTicket', fields=[ ('uuid', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), ('created', models.DateTimeField(auto_now_add=True)), ('updated', models.DateTimeField(auto_now=True)), ('qrcode_base64', models.TextField(blank=True, null=True)), ('checked_in', models.BooleanField(default=False)), + ('price', models.IntegerField(help_text='Price of the discounted ticket (in DKK, including VAT).')), + ], + options={ + 'abstract': False, + }, + ), + migrations.CreateModel( + name='ShopTicket', + fields=[ + ('uuid', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), + ('created', models.DateTimeField(auto_now_add=True)), + ('updated', models.DateTimeField(auto_now=True)), + ('qrcode_base64', models.TextField(blank=True, null=True)), + ('checked_in', models.BooleanField(default=False)), + ('name', models.CharField(blank=True, help_text='Name of the person this ticket belongs to. This can be different from the buying user.', max_length=100, null=True)), + ('email', models.EmailField(blank=True, max_length=254, null=True)), + ('order', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='shoptickets', to='shop.Order')), + ('product', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='shop.Product')), + ], + options={ + 'abstract': False, + }, + ), + migrations.CreateModel( + name='SponsorTicket', + fields=[ + ('uuid', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), + ('created', models.DateTimeField(auto_now_add=True)), + ('updated', models.DateTimeField(auto_now=True)), + ('qrcode_base64', models.TextField(blank=True, null=True)), + ('checked_in', models.BooleanField(default=False)), + ('sponsor', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='sponsors.Sponsor')), ], options={ 'abstract': False, @@ -38,51 +70,25 @@ class Migration(migrations.Migration): ('created', models.DateTimeField(auto_now_add=True)), ('updated', models.DateTimeField(auto_now=True)), ('name', models.TextField()), - ('camp', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='camps.Camp')), + ('camp', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='camps.Camp')), ], options={ 'abstract': False, }, ), - migrations.CreateModel( - name='DiscountTicket', - fields=[ - ('baseticket_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='tickets.BaseTicket')), - ('price', models.IntegerField(help_text='Price of the discounted ticket (in DKK, including VAT).')), - ], - options={ - 'abstract': False, - }, - bases=('tickets.baseticket',), - ), - migrations.CreateModel( - name='ShopTicket', - fields=[ - ('baseticket_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='tickets.BaseTicket')), - ('name', models.CharField(blank=True, help_text='Name of the person this ticket belongs to. This can be different from the buying user.', max_length=100, null=True)), - ('email', models.EmailField(blank=True, max_length=254, null=True)), - ('order', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='shoptickets', to='shop.Order')), - ('product', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='shop.Product')), - ], - options={ - 'abstract': False, - }, - bases=('tickets.baseticket',), - ), - migrations.CreateModel( - name='SponsorTicket', - fields=[ - ('baseticket_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='tickets.BaseTicket')), - ('sponsor', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='sponsors.Sponsor')), - ], - options={ - 'abstract': False, - }, - bases=('tickets.baseticket',), - ), migrations.AddField( - model_name='baseticket', + model_name='sponsorticket', name='ticket_type', - field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='tickets.TicketType'), + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='tickets.TicketType'), + ), + migrations.AddField( + model_name='shopticket', + name='ticket_type', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='tickets.TicketType'), + ), + migrations.AddField( + model_name='discountticket', + name='ticket_type', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='tickets.TicketType'), ), ] diff --git a/src/tickets/migrations/0002_auto_20170819_2222.py b/src/tickets/migrations/0002_auto_20170819_2222.py new file mode 100644 index 00000000..decabb8a --- /dev/null +++ b/src/tickets/migrations/0002_auto_20170819_2222.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.5 on 2017-08-19 20:22 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('tickets', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='discountticket', + name='ticket_type', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='tickets.TicketType'), + ), + migrations.AlterField( + model_name='shopticket', + name='ticket_type', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='tickets.TicketType'), + ), + migrations.AlterField( + model_name='sponsorticket', + name='ticket_type', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='tickets.TicketType'), + ), + migrations.AlterField( + model_name='tickettype', + name='camp', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='camps.Camp'), + ), + ] diff --git a/src/tickets/models.py b/src/tickets/models.py index 5f4246b4..32aca309 100644 --- a/src/tickets/models.py +++ b/src/tickets/models.py @@ -32,6 +32,9 @@ class BaseTicket(CreatedUpdatedModel, UUIDModel): ticket_type = models.ForeignKey('TicketType') checked_in = models.BooleanField(default=False) + class Meta: + abstract = True + def save(self, **kwargs): super(BaseTicket, self).save(**kwargs) self.qrcode_base64 = self.get_qr_code() From b790b84d6c4d134ec411bde61cbe5782235d1f5f Mon Sep 17 00:00:00 2001 From: Thomas Steen Rasmussen Date: Sat, 19 Aug 2017 23:00:49 +0200 Subject: [PATCH 2/5] not so super --- src/tickets/models.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/tickets/models.py b/src/tickets/models.py index 32aca309..3ac78db4 100644 --- a/src/tickets/models.py +++ b/src/tickets/models.py @@ -36,7 +36,6 @@ class BaseTicket(CreatedUpdatedModel, UUIDModel): abstract = True def save(self, **kwargs): - super(BaseTicket, self).save(**kwargs) self.qrcode_base64 = self.get_qr_code() super(BaseTicket, self).save(**kwargs) From bf56a1624c59440b266d3335dc05540c44efc8d5 Mon Sep 17 00:00:00 2001 From: Thomas Steen Rasmussen Date: Sat, 19 Aug 2017 23:01:16 +0200 Subject: [PATCH 3/5] py3 syntax --- src/tickets/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tickets/models.py b/src/tickets/models.py index 3ac78db4..0919d7e5 100644 --- a/src/tickets/models.py +++ b/src/tickets/models.py @@ -37,7 +37,7 @@ class BaseTicket(CreatedUpdatedModel, UUIDModel): def save(self, **kwargs): self.qrcode_base64 = self.get_qr_code() - super(BaseTicket, self).save(**kwargs) + super().save(**kwargs) def _get_token(self): return hashlib.sha256( From e6ca8a5c713f0c8a5cdf70b77ecc0cb114913782 Mon Sep 17 00:00:00 2001 From: Thomas Steen Rasmussen Date: Sat, 19 Aug 2017 23:10:13 +0200 Subject: [PATCH 4/5] calculate qrcode_base64 on the fly --- .../migrations/0003_auto_20170819_2309.py | 27 +++++++++++++++++++ src/tickets/models.py | 9 ++----- 2 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 src/tickets/migrations/0003_auto_20170819_2309.py diff --git a/src/tickets/migrations/0003_auto_20170819_2309.py b/src/tickets/migrations/0003_auto_20170819_2309.py new file mode 100644 index 00000000..64a1cef1 --- /dev/null +++ b/src/tickets/migrations/0003_auto_20170819_2309.py @@ -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', + ), + ] diff --git a/src/tickets/models.py b/src/tickets/models.py index 0919d7e5..dac2169f 100644 --- a/src/tickets/models.py +++ b/src/tickets/models.py @@ -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( From 5f4e9d16e9575293e0770e64461b36197e27941b Mon Sep 17 00:00:00 2001 From: Thomas Steen Rasmussen Date: Sat, 19 Aug 2017 23:14:01 +0200 Subject: [PATCH 5/5] remove save method frm ShopTicket --- src/tickets/models.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/tickets/models.py b/src/tickets/models.py index dac2169f..df8a1969 100644 --- a/src/tickets/models.py +++ b/src/tickets/models.py @@ -115,11 +115,6 @@ class ShopTicket(BaseTicket): product=self.product ) - def save(self, **kwargs): - super(ShopTicket, self).save(**kwargs) - self.qrcode_base64 = self.get_qr_code() - super(ShopTicket, self).save(**kwargs) - def get_absolute_url(self): return str(reverse_lazy('shop:ticket_detail', kwargs={'pk': self.pk}))