From a7e9f3c09d809a223d32974aeb487adbfab6b6f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Floure?= Date: Mon, 9 Mar 2020 17:25:02 +0100 Subject: [PATCH] Move Order.add_record to save hook in abstract Product --- .../uncloud/uncloud_pay/models.py | 21 ++++++++++++------- .../uncloud/uncloud_vm/views.py | 10 --------- .../uncloud/ungleich_service/views.py | 14 ------------- 3 files changed, 14 insertions(+), 31 deletions(-) diff --git a/uncloud_django_based/uncloud/uncloud_pay/models.py b/uncloud_django_based/uncloud/uncloud_pay/models.py index d4e46b2..6e075a9 100644 --- a/uncloud_django_based/uncloud/uncloud_pay/models.py +++ b/uncloud_django_based/uncloud/uncloud_pay/models.py @@ -476,13 +476,6 @@ class Order(models.Model): def recurring_price(self): return reduce(lambda acc, record: acc + record.recurring_price, self.records, 0) - def add_record(self, one_time_price, recurring_price, description): - OrderRecord.objects.create(order=self, - one_time_price=one_time_price, - recurring_price=recurring_price, - description=description) - - class OrderRecord(models.Model): """ Order records store billing informations for products: the actual product @@ -543,6 +536,20 @@ class Product(UncloudModel): # Default period for all products default_recurring_period = RecurringPeriod.PER_MONTH + # Used to save records. + def save(self, *args, **kwargs): + # _state.adding is switched to false after super(...) call. + being_created = self._state.adding + + super(Product, self).save(*args, **kwargs) + + # Make sure we only create records on creation. + if being_created: + record = OrderRecord( + one_time_price=self.one_time_price, + recurring_price=self.recurring_price(self.recurring_period), + description=self.description) + self.order.orderrecord_set.add(record, bulk=False) @property def recurring_price(self, recurring_period=RecurringPeriod.PER_MONTH): diff --git a/uncloud_django_based/uncloud/uncloud_vm/views.py b/uncloud_django_based/uncloud/uncloud_vm/views.py index a29ecd7..c601c5b 100644 --- a/uncloud_django_based/uncloud/uncloud_vm/views.py +++ b/uncloud_django_based/uncloud/uncloud_vm/views.py @@ -127,11 +127,6 @@ class VMProductViewSet(ProductViewSet): # Create VM. vm = serializer.save(owner=request.user, order=order) - # Add Product record to order (VM is mutable, allows to keep history in order). - # XXX: Move this to some kind of on_create hook in parent Product class? - order.add_record(vm.one_time_price, - vm.recurring_price(order.recurring_period), vm.description) - return Response(serializer.data) @@ -215,9 +210,4 @@ class DCLCreateVMProductViewSet(ProductViewSet): # Create VM. vm = serializer.save(owner=request.user, order=order) - # Add Product record to order (VM is mutable, allows to keep history in order). - # XXX: Move this to some kind of on_create hook in parent Product class? - order.add_record(vm.one_time_price, - vm.recurring_price(order.recurring_period), vm.description) - return Response(serializer.data) diff --git a/uncloud_django_based/uncloud/ungleich_service/views.py b/uncloud_django_based/uncloud/ungleich_service/views.py index 47c15e2..9a7ff28 100644 --- a/uncloud_django_based/uncloud/ungleich_service/views.py +++ b/uncloud_django_based/uncloud/ungleich_service/views.py @@ -38,24 +38,10 @@ class MatrixServiceProductViewSet(ProductViewSet): vm_data['order'] = order vm = VMProduct.objects.create(**vm_data) - # XXX: Move this to some kind of on_create hook in parent - # Product class? - order.add_record( - vm.one_time_price, - vm.recurring_price(order.recurring_period), - vm.description) - # Create service. service = serializer.save( order=order, owner=self.request.user, vm=vm) - # XXX: Move this to some kind of on_create hook in parent - # Product class? - order.add_record( - service.one_time_price, - service.recurring_price(order.recurring_period), - service.description) - return Response(serializer.data)