From 65440ab2ef1d36f5bb96e84bed816b79a7cd39bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Floure?= Date: Fri, 8 May 2020 16:47:32 +0200 Subject: [PATCH] Add depends_on relation on orders --- .../migrations/0013_auto_20200508_1446.py | 24 +++++++++++++++++++ uncloud_pay/models.py | 8 +++++++ uncloud_pay/serializers.py | 4 ++-- uncloud_vm/views.py | 3 ++- 4 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 uncloud_pay/migrations/0013_auto_20200508_1446.py diff --git a/uncloud_pay/migrations/0013_auto_20200508_1446.py b/uncloud_pay/migrations/0013_auto_20200508_1446.py new file mode 100644 index 0000000..dcf7675 --- /dev/null +++ b/uncloud_pay/migrations/0013_auto_20200508_1446.py @@ -0,0 +1,24 @@ +# Generated by Django 3.0.6 on 2020-05-08 14:46 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('uncloud_pay', '0012_billnico'), + ] + + operations = [ + migrations.AddField( + model_name='order', + name='depends_on', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='parent_of', to='uncloud_pay.Order'), + ), + migrations.AlterField( + model_name='order', + name='replaced_by', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='supersede', to='uncloud_pay.Order'), + ), + ] diff --git a/uncloud_pay/models.py b/uncloud_pay/models.py index d7a9fc8..89ec917 100644 --- a/uncloud_pay/models.py +++ b/uncloud_pay/models.py @@ -609,9 +609,17 @@ class Order(models.Model): validators=[MinValueValidator(0)]) replaced_by = models.ForeignKey('self', + related_name='supersede', on_delete=models.PROTECT, blank=True, null=True) + + depends_on = models.ForeignKey('self', + related_name='parent_of', + on_delete=models.PROTECT, + blank=True, + null=True) + @property def is_terminated(self): return self.ending_date != None and self.ending_date < timezone.now() diff --git a/uncloud_pay/serializers.py b/uncloud_pay/serializers.py index 3f91b04..edccd07 100644 --- a/uncloud_pay/serializers.py +++ b/uncloud_pay/serializers.py @@ -70,9 +70,9 @@ class OrderSerializer(serializers.ModelSerializer): class Meta: model = Order + read_only_fields = ['replaced_by', 'depends_on'] fields = ['uuid', 'owner', 'description', 'creation_date', 'starting_date', 'ending_date', - 'bill', 'recurring_period', 'recurring_price', 'one_time_price', 'replaced_by'] - read_only_fields = ['replaced_by'] + 'bill', 'recurring_period', 'recurring_price', 'one_time_price'] + read_only_fields ### diff --git a/uncloud_vm/views.py b/uncloud_vm/views.py index c196407..67f8656 100644 --- a/uncloud_vm/views.py +++ b/uncloud_vm/views.py @@ -174,7 +174,8 @@ class VMProductViewSet(ProductViewSet): disk_order = Order.from_product( disk, recurring_period=order_recurring_period, - starting_date=timezone.now() + starting_date=timezone.now(), + depends_on=vm_order )