From 81bd54116a8d6d078a22d20df19aedbbc5cf3177 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Floure?= Date: Mon, 2 Mar 2020 09:25:03 +0100 Subject: [PATCH] Add records to orders --- uncloud/uncloud_pay/models.py | 28 +++++++++++++++------------- uncloud/uncloud_pay/serializers.py | 9 ++++++++- uncloud/uncloud_vm/models.py | 6 ++++-- uncloud/uncloud_vm/views.py | 7 +++++++ 4 files changed, 34 insertions(+), 16 deletions(-) diff --git a/uncloud/uncloud_pay/models.py b/uncloud/uncloud_pay/models.py index 2862940..8b19c37 100644 --- a/uncloud/uncloud_pay/models.py +++ b/uncloud/uncloud_pay/models.py @@ -65,16 +65,6 @@ class BillEntry(): # confusing: the order is a 'contract' with the customer, were both parts # agree on deal => That's what we want to keep archived. # -# SOON: -# -# We'll need to add some kind of OrderEntry table (each order might have -# multiple entries) storing: recurring_price, recurring_period, setup_fee, description -# -# FOR NOW: -# -# We dynamically get pricing from linked product, as they are not updated in -# this stage of development. -# # /!\ BIG FAT WARNING /!\ # class Order(models.Model): uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) @@ -95,11 +85,23 @@ class Order(models.Model): choices = RecurringPeriod.choices, default = RecurringPeriod.PER_MONTH) + @property + def records(self): + return OrderRecord.objects.filter(order=self) @property - def amount(self): - records = OrderRecord.objects.filter(order=self) - return 1 + def setup_fee(self): + return reduce(lambda acc, record: acc + record.setup_fee, self.records, 0) + + @property + def recurring_price(self): + return reduce(lambda acc, record: acc + record.recurring_price, self.records, 0) + + def add_record(self, setup_fee, recurring_price, description): + OrderRecord.objects.create(order=self, + setup_fee=setup_fee, + recurring_price=recurring_price, + description=description) class OrderRecord(models.Model): order = models.ForeignKey(Order, on_delete=models.CASCADE) diff --git a/uncloud/uncloud_pay/serializers.py b/uncloud/uncloud_pay/serializers.py index eeab444..83eebb6 100644 --- a/uncloud/uncloud_pay/serializers.py +++ b/uncloud/uncloud_pay/serializers.py @@ -26,10 +26,17 @@ class PaymentMethodSerializer(serializers.ModelSerializer): class ProductSerializer(serializers.Serializer): vms = VMProductSerializer(many=True, read_only=True) +class OrderRecordSerializer(serializers.ModelSerializer): + class Meta: + model = OrderRecord + fields = ['setup_fee', 'recurring_price', 'description'] + class OrderSerializer(serializers.ModelSerializer): + records = OrderRecordSerializer(many=True, read_only=True) class Meta: model = Order - fields = '__all__' + fields = ['uuid', 'creation_date', 'starting_date', 'ending_date', + 'bill', 'recurring_period', 'records', 'recurring_price', 'setup_fee'] class UserSerializer(serializers.ModelSerializer): class Meta: diff --git a/uncloud/uncloud_vm/models.py b/uncloud/uncloud_vm/models.py index be1178e..c32f3a5 100644 --- a/uncloud/uncloud_vm/models.py +++ b/uncloud/uncloud_vm/models.py @@ -40,8 +40,6 @@ class VMProduct(Product): blank=True, null=True) - description = "Virtual Machine" - # VM-specific. The name is only intended for customers: it's a pain te # remember IDs (speaking from experience as ungleich customer)! name = models.CharField(max_length=32) @@ -55,6 +53,10 @@ class VMProduct(Product): else: raise Exception('Invalid recurring period for VM Product pricing.') + @property + def description(self): + return "Virtual machine '{}': {} core(s), {}GB memory".format( + self.name, self.cores, self.ram_in_gb) class VMWithOSProduct(VMProduct): pass diff --git a/uncloud/uncloud_vm/views.py b/uncloud/uncloud_vm/views.py index 2dec2ae..5eeec7b 100644 --- a/uncloud/uncloud_vm/views.py +++ b/uncloud/uncloud_vm/views.py @@ -28,6 +28,9 @@ class VMProductViewSet(ProductViewSet): return VMProduct.objects.filter(owner=self.request.user) def create(self, request): + # TODO: what if something blows-up midway? + # => need a transaction + # Create base order. order = Order.objects.create( recurring_period=RecurringPeriod.PER_MONTH, @@ -40,6 +43,10 @@ class VMProductViewSet(ProductViewSet): serializer.is_valid(raise_exception=True) vm = serializer.save(owner=request.user, order=order) + # Add Product record to order (VM is mutable, allows to keep history in order). + order.add_record(vm.setup_fee, + vm.recurring_price(order.recurring_period), vm.description) + return Response(serializer.data)