From 178a0342c5025d64bc65e1c3fc4b17f5987a44b0 Mon Sep 17 00:00:00 2001 From: Mohamed Hamza Date: Tue, 12 Mar 2019 00:15:45 +0200 Subject: [PATCH] fixing on_delete signal for django2.0 and recreating the migrations --- coupons/migrations/0001_initial.py | 50 +++++++++++++++---- coupons/migrations/0002_coupon_valid_until.py | 20 -------- coupons/migrations/0003_auto_20150416_0617.py | 39 --------------- coupons/migrations/0004_auto_20151105_1456.py | 43 ---------------- coupons/migrations/0005_auto_20151105_1502.py | 27 ---------- coupons/migrations/0006_auto_20151105_1509.py | 22 -------- coupons/migrations/0007_auto_20151105_2328.py | 18 ------- coupons/models.py | 7 +-- 8 files changed, 43 insertions(+), 183 deletions(-) delete mode 100644 coupons/migrations/0002_coupon_valid_until.py delete mode 100644 coupons/migrations/0003_auto_20150416_0617.py delete mode 100644 coupons/migrations/0004_auto_20151105_1456.py delete mode 100644 coupons/migrations/0005_auto_20151105_1502.py delete mode 100644 coupons/migrations/0006_auto_20151105_1509.py delete mode 100644 coupons/migrations/0007_auto_20151105_2328.py diff --git a/coupons/migrations/0001_initial.py b/coupons/migrations/0001_initial.py index 56e2883..675d0c4 100644 --- a/coupons/migrations/0001_initial.py +++ b/coupons/migrations/0001_initial.py @@ -1,33 +1,61 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals +# Generated by Django 2.1.1 on 2019-03-11 21:50 -from django.db import models, migrations from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion class Migration(migrations.Migration): + initial = True + dependencies = [ migrations.swappable_dependency(settings.AUTH_USER_MODEL), ] operations = [ + migrations.CreateModel( + name='Campaign', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=255, unique=True, verbose_name='Name')), + ('description', models.TextField(blank=True, verbose_name='Description')), + ], + options={ + 'verbose_name_plural': 'Campaigns', + 'verbose_name': 'Campaign', + 'ordering': ['name'], + }, + ), migrations.CreateModel( name='Coupon', fields=[ - ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('value', models.IntegerField(help_text='Arbitrary coupon value', verbose_name='Value')), - ('code', models.CharField(help_text='Leaving this field empty will generate a random code.', unique=True, max_length=30, verbose_name='Code', blank=True)), - ('type', models.CharField(max_length=20, verbose_name='Type', choices=[(b'monetary', b'Money based coupon'), (b'percentage', b'Percentage discount'), (b'virtual_currency', b'Virtual currency')])), + ('code', models.CharField(blank=True, help_text='Leaving this field empty will generate a random code.', max_length=30, unique=True, verbose_name='Code')), + ('type', models.CharField(choices=[('monetary', 'Money based coupon'), ('percentage', 'Percentage discount'), ('virtual_currency', 'Virtual currency')], max_length=20, verbose_name='Type')), + ('user_limit', models.PositiveIntegerField(default=1, verbose_name='User limit')), ('created_at', models.DateTimeField(auto_now_add=True, verbose_name='Created at')), - ('redeemed_at', models.DateTimeField(null=True, verbose_name='Redeemed at', blank=True)), - ('user', models.ForeignKey(blank=True, to=settings.AUTH_USER_MODEL, help_text='You may specify a user you want to restrict this coupon to.', null=True, verbose_name='User')), + ('valid_until', models.DateTimeField(blank=True, help_text='Leave empty for coupons that never expire', null=True, verbose_name='Valid until')), + ('campaign', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='coupons', to='coupons.Campaign', verbose_name='Campaign')), ], options={ - 'ordering': ['created_at'], - 'verbose_name': 'Coupon', 'verbose_name_plural': 'Coupons', + 'verbose_name': 'Coupon', + 'ordering': ['created_at'], }, - bases=(models.Model,), + ), + migrations.CreateModel( + name='CouponUser', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('redeemed_at', models.DateTimeField(blank=True, null=True, verbose_name='Redeemed at')), + ('coupon', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='users', to='coupons.Coupon')), + ('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, verbose_name='User')), + ], + ), + migrations.AlterUniqueTogether( + name='couponuser', + unique_together={('coupon', 'user')}, ), ] diff --git a/coupons/migrations/0002_coupon_valid_until.py b/coupons/migrations/0002_coupon_valid_until.py deleted file mode 100644 index ae43e8f..0000000 --- a/coupons/migrations/0002_coupon_valid_until.py +++ /dev/null @@ -1,20 +0,0 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - -from django.db import models, migrations - - -class Migration(migrations.Migration): - - dependencies = [ - ('coupons', '0001_initial'), - ] - - operations = [ - migrations.AddField( - model_name='coupon', - name='valid_until', - field=models.DateTimeField(null=True, verbose_name='Valid until', blank=True), - preserve_default=True, - ), - ] diff --git a/coupons/migrations/0003_auto_20150416_0617.py b/coupons/migrations/0003_auto_20150416_0617.py deleted file mode 100644 index 1b3cce4..0000000 --- a/coupons/migrations/0003_auto_20150416_0617.py +++ /dev/null @@ -1,39 +0,0 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - -from django.db import models, migrations - - -class Migration(migrations.Migration): - - dependencies = [ - ('coupons', '0002_coupon_valid_until'), - ] - - operations = [ - migrations.CreateModel( - name='Campaign', - fields=[ - ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), - ('name', models.CharField(unique=True, max_length=255, verbose_name='Name')), - ('description', models.TextField(verbose_name='Description', blank=True)), - ], - options={ - 'ordering': ['name'], - 'verbose_name': 'Campaign', - 'verbose_name_plural': 'Campaigns', - }, - bases=(models.Model,), - ), - migrations.AddField( - model_name='coupon', - name='campaign', - field=models.ForeignKey(related_name=b'coupons', verbose_name='Campaign', blank=True, to='coupons.Campaign', null=True), - preserve_default=True, - ), - migrations.AlterField( - model_name='coupon', - name='valid_until', - field=models.DateTimeField(help_text='Leave empty for coupons that never expire', null=True, verbose_name='Valid until', blank=True), - ), - ] diff --git a/coupons/migrations/0004_auto_20151105_1456.py b/coupons/migrations/0004_auto_20151105_1456.py deleted file mode 100644 index 5fbbc0e..0000000 --- a/coupons/migrations/0004_auto_20151105_1456.py +++ /dev/null @@ -1,43 +0,0 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - -from django.db import migrations, models -from django.conf import settings - - -class Migration(migrations.Migration): - - dependencies = [ - migrations.swappable_dependency(settings.AUTH_USER_MODEL), - ('coupons', '0003_auto_20150416_0617'), - ] - - operations = [ - migrations.CreateModel( - name='CouponUser', - fields=[ - ('id', models.AutoField(serialize=False, auto_created=True, primary_key=True, verbose_name='ID')), - ('redeemed_at', models.DateTimeField(blank=True, verbose_name='Redeemed at', null=True)), - ], - ), - migrations.AddField( - model_name='coupon', - name='user_limit', - field=models.PositiveIntegerField(verbose_name='User limit', default=1), - ), - migrations.AlterField( - model_name='coupon', - name='type', - field=models.CharField(choices=[('monetary', 'Money based coupon'), ('percentage', 'Percentage discount'), ('virtual_currency', 'Virtual currency')], verbose_name='Type', max_length=20), - ), - migrations.AddField( - model_name='couponuser', - name='coupon', - field=models.ForeignKey(related_name='users', to='coupons.Coupon'), - ), - migrations.AddField( - model_name='couponuser', - name='user', - field=models.ForeignKey(null=True, to=settings.AUTH_USER_MODEL, blank=True, verbose_name='User'), - ), - ] diff --git a/coupons/migrations/0005_auto_20151105_1502.py b/coupons/migrations/0005_auto_20151105_1502.py deleted file mode 100644 index d8fd9e1..0000000 --- a/coupons/migrations/0005_auto_20151105_1502.py +++ /dev/null @@ -1,27 +0,0 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - -from django.db import migrations - - -def migrate_user_coupons(apps, schema_editor): - Coupon = apps.get_model('coupons', 'Coupon') - UserCoupon = apps.get_model('coupons', 'CouponUser') - for coupon in Coupon.objects.all(): - if coupon.user is not None or coupon.redeemed_at is not None: - UserCoupon.objects.create( - coupon=coupon, - user=coupon.user, - redeemed_at=coupon.redeemed_at - ) - - -class Migration(migrations.Migration): - - dependencies = [ - ('coupons', '0004_auto_20151105_1456'), - ] - - operations = [ - migrations.RunPython(migrate_user_coupons), - ] diff --git a/coupons/migrations/0006_auto_20151105_1509.py b/coupons/migrations/0006_auto_20151105_1509.py deleted file mode 100644 index dfb5216..0000000 --- a/coupons/migrations/0006_auto_20151105_1509.py +++ /dev/null @@ -1,22 +0,0 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('coupons', '0005_auto_20151105_1502'), - ] - - operations = [ - migrations.RemoveField( - model_name='coupon', - name='redeemed_at', - ), - migrations.RemoveField( - model_name='coupon', - name='user', - ), - ] diff --git a/coupons/migrations/0007_auto_20151105_2328.py b/coupons/migrations/0007_auto_20151105_2328.py deleted file mode 100644 index f928855..0000000 --- a/coupons/migrations/0007_auto_20151105_2328.py +++ /dev/null @@ -1,18 +0,0 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('coupons', '0006_auto_20151105_1509'), - ] - - operations = [ - migrations.AlterUniqueTogether( - name='couponuser', - unique_together=set([('coupon', 'user')]), - ), - ] diff --git a/coupons/models.py b/coupons/models.py index 2b69741..124e68f 100644 --- a/coupons/models.py +++ b/coupons/models.py @@ -3,6 +3,7 @@ from django.conf import settings from django.db import IntegrityError from django.db import models +from django.db.models import CASCADE from django.dispatch import Signal from django.utils.encoding import python_2_unicode_compatible from django.utils import timezone @@ -76,7 +77,7 @@ class Coupon(models.Model): valid_until = models.DateTimeField( _("Valid until"), blank=True, null=True, help_text=_("Leave empty for coupons that never expire")) - campaign = models.ForeignKey('Campaign', verbose_name=_("Campaign"), blank=True, null=True, related_name='coupons') + campaign = models.ForeignKey('Campaign', verbose_name=_("Campaign"), on_delete=CASCADE, blank=True, null=True, related_name='coupons') objects = CouponManager() @@ -149,8 +150,8 @@ def __str__(self): @python_2_unicode_compatible class CouponUser(models.Model): - coupon = models.ForeignKey(Coupon, related_name='users') - user = models.ForeignKey(user_model, verbose_name=_("User"), null=True, blank=True) + coupon = models.ForeignKey(Coupon, on_delete=CASCADE, related_name='users') + user = models.ForeignKey(user_model, verbose_name=_("User"), on_delete=CASCADE, null=True, blank=True) redeemed_at = models.DateTimeField(_("Redeemed at"), blank=True, null=True) class Meta: