From c835c874d5e17da2a49d6ed1360556081244d945 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Sat, 2 May 2020 22:48:05 +0200 Subject: [PATCH] [BREAKING] make Order a stand-alone version I think that while the idea of an Orderrecord is good, we might get away / have a simpler implementation if we only use orders and reference them where needed. I saved the previous Order model for easy rollback, if my assumption is wrong. --- .../migrations/0009_auto_20200502_2047.py | 47 ++++++++++++++ .../uncloud/uncloud_pay/models.py | 65 ++++++++++++++++++- 2 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 uncloud_django_based/uncloud/uncloud_pay/migrations/0009_auto_20200502_2047.py diff --git a/uncloud_django_based/uncloud/uncloud_pay/migrations/0009_auto_20200502_2047.py b/uncloud_django_based/uncloud/uncloud_pay/migrations/0009_auto_20200502_2047.py new file mode 100644 index 0000000..cb9cd78 --- /dev/null +++ b/uncloud_django_based/uncloud/uncloud_pay/migrations/0009_auto_20200502_2047.py @@ -0,0 +1,47 @@ +# Generated by Django 3.0.5 on 2020-05-02 20:47 + +from django.conf import settings +import django.core.validators +from django.db import migrations, models +import django.db.models.deletion +import django.utils.timezone +import uuid + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('uncloud_pay', '0008_auto_20200502_1921'), + ] + + operations = [ + migrations.AddField( + model_name='order', + name='one_time_price', + field=models.DecimalField(decimal_places=2, default=0.0, max_digits=10, validators=[django.core.validators.MinValueValidator(0)]), + ), + migrations.AddField( + model_name='order', + name='recurring_price', + field=models.DecimalField(decimal_places=2, default=0.0, max_digits=10, validators=[django.core.validators.MinValueValidator(0)]), + ), + migrations.AddField( + model_name='order', + name='replaced_by', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='uncloud_pay.Order'), + ), + migrations.CreateModel( + name='OrderTimothee', + fields=[ + ('uuid', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), + ('creation_date', models.DateTimeField(auto_now_add=True)), + ('starting_date', models.DateTimeField(default=django.utils.timezone.now)), + ('ending_date', models.DateTimeField(blank=True, null=True)), + ('recurring_period', models.CharField(choices=[('ONCE', 'Onetime'), ('YEAR', 'Per Year'), ('MONTH', 'Per Month'), ('WEEK', 'Per Week'), ('DAY', 'Per Day'), ('HOUR', 'Per Hour'), ('MINUTE', 'Per Minute'), ('SECOND', 'Per Second')], default='MONTH', max_length=32)), + ('bill', models.ManyToManyField(blank=True, editable=False, to='uncloud_pay.Bill')), + ('billing_address', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='uncloud_pay.BillingAddress')), + ('owner', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/uncloud_django_based/uncloud/uncloud_pay/models.py b/uncloud_django_based/uncloud/uncloud_pay/models.py index a326810..9a8a49a 100644 --- a/uncloud_django_based/uncloud/uncloud_pay/models.py +++ b/uncloud_django_based/uncloud/uncloud_pay/models.py @@ -809,6 +809,64 @@ class Order(models.Model): editable=False) billing_address = models.ForeignKey(BillingAddress, on_delete=models.CASCADE) + # TODO: enforce ending_date - starting_date to be larger than recurring_period. + creation_date = models.DateTimeField(auto_now_add=True) + starting_date = models.DateTimeField(default=timezone.now) + ending_date = models.DateTimeField(blank=True, + null=True) + + bill = models.ManyToManyField(Bill, + editable=False, + blank=True) + + recurring_period = models.CharField(max_length=32, + choices = RecurringPeriod.choices, + default = RecurringPeriod.PER_MONTH) + + one_time_price = models.DecimalField(default=0.0, + max_digits=AMOUNT_MAX_DIGITS, + decimal_places=AMOUNT_DECIMALS, + validators=[MinValueValidator(0)]) + + recurring_price = models.DecimalField(default=0.0, + max_digits=AMOUNT_MAX_DIGITS, + decimal_places=AMOUNT_DECIMALS, + validators=[MinValueValidator(0)]) + + replaced_by = models.ForeignKey('self', + on_delete=models.PROTECT, + blank=True, + null=True) + + # Trigger initial bill generation at order creation. + def save(self, *args, **kwargs): + if self.ending_date and self.ending_date < self.starting_date: + raise ValidationError("End date cannot be before starting date") + + super().save(*args, **kwargs) + + Bill.generate_for(self.starting_date.year, self.starting_date.month, self.owner) + + # Used by uncloud_pay tests. + @property + def bills(self): + return Bill.objects.filter(order=self) + + def __str__(self): + return "Order {} created at {}, {}->{}, recurring period {}. Price one time {}, recurring {}".format( + self.uuid, self.creation_date, + self.starting_date, self.ending_date, + self.recurring_period, + self.one_time_price, + self.recurring_price) + +class OrderTimothee(models.Model): + uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) + owner = models.ForeignKey(get_user_model(), + on_delete=models.CASCADE, + editable=False) + billing_address = models.ForeignKey(BillingAddress, on_delete=models.CASCADE) + # TODO: enforce ending_date - starting_date to be larger than recurring_period. creation_date = models.DateTimeField(auto_now_add=True) starting_date = models.DateTimeField(default=timezone.now) @@ -856,10 +914,13 @@ class Order(models.Model): description=description) def __str__(self): - return "Order {} created at {}, {}->{}, recurring period {}".format( + return "Order {} created at {}, {}->{}, recurring period {}. Price one time {}, recurring {}".format( self.uuid, self.creation_date, self.starting_date, self.ending_date, - self.recurring_period) + self.recurring_period, + self.one_time_price, + self.recurring_price) + class OrderRecord(models.Model):