From ef5e7e80355ae276cbb70b738d8e7b23e376f29b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Floure?= Date: Thu, 27 Feb 2020 20:58:12 +0100 Subject: [PATCH] Quickly wire vm creation to orders --- uncloud/uncloud/urls.py | 5 ++++- uncloud/uncloud_pay/models.py | 2 +- uncloud/uncloud_vm/models.py | 1 - uncloud/uncloud_vm/views.py | 19 ++++++++++++++++--- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/uncloud/uncloud/urls.py b/uncloud/uncloud/urls.py index 40b1be5..d1a1cb8 100644 --- a/uncloud/uncloud/urls.py +++ b/uncloud/uncloud/urls.py @@ -35,13 +35,16 @@ router.register(r'order', payviews.OrderViewSet, basename='order') router.register(r'payment', payviews.PaymentViewSet, basename='payment') router.register(r'payment-method', payviews.PaymentMethodViewSet, basename='payment-methods') +# VMs +router.register(r'vm/vm', vmviews.VMProductViewSet, basename='vm') + # admin/staff urls router.register(r'admin/bill', payviews.AdminBillViewSet, basename='admin/bill') router.register(r'admin/payment', payviews.AdminPaymentViewSet, basename='admin/payment') router.register(r'admin/order', payviews.AdminOrderViewSet, basename='admin/order') router.register(r'admin/vmhost', vmviews.VMHostViewSet) router.register(r'admin/opennebula', oneviews.VMViewSet, basename='opennebula') -router.register(r'admin/opennebula_raw', oneviews.RawVMViewSet) +router.register(r'admin/opennebula_raw', oneviews.RawVMViewSet, basename='opennebula_raw') urlpatterns = [ diff --git a/uncloud/uncloud_pay/models.py b/uncloud/uncloud_pay/models.py index d7c4ff1..6077963 100644 --- a/uncloud/uncloud_pay/models.py +++ b/uncloud/uncloud_pay/models.py @@ -43,7 +43,7 @@ class Order(models.Model): editable=False) creation_date = models.DateTimeField(auto_now_add=True) - starting_date = models.DateTimeField() + starting_date = models.DateTimeField(auto_now_add=True) ending_date = models.DateTimeField(blank=True, null=True) diff --git a/uncloud/uncloud_vm/models.py b/uncloud/uncloud_vm/models.py index 2db99f3..26b369f 100644 --- a/uncloud/uncloud_vm/models.py +++ b/uncloud/uncloud_vm/models.py @@ -44,7 +44,6 @@ class VMProduct(Product): cores = models.IntegerField() ram_in_gb = models.FloatField() - @property def recurring_price(self, recurring_period=RecurringPeriod.PER_MONTH): if recurring_period == RecurringPeriod.PER_MONTH: # TODO: move magic numbers in variables diff --git a/uncloud/uncloud_vm/views.py b/uncloud/uncloud_vm/views.py index 444d134..a7171c9 100644 --- a/uncloud/uncloud_vm/views.py +++ b/uncloud/uncloud_vm/views.py @@ -7,8 +7,7 @@ from rest_framework import viewsets, permissions from rest_framework.response import Response from .models import VMHost, VMProduct, VMSnapshotProduct -from uncloud_pay.models import Order - +from uncloud_pay.models import Order, RecurringPeriod from .serializers import VMHostSerializer, VMProductSerializer, VMSnapshotProductSerializer import datetime @@ -27,9 +26,23 @@ class VMProductViewSet(viewsets.ModelViewSet): return VMProduct.objects.filter(owner=self.request.user) def create(self, request): + # Create base order. + order = Order.objects.create( + recurring_period=RecurringPeriod.PER_MONTH, + recurring_price=0, + one_time_price=0, + owner=request.user + ) + + # Create VM. serializer = VMProductSerializer(data=request.data, context={'request': request}) serializer.is_valid(raise_exception=True) - serializer.save(owner=request.user) + vm = serializer.save(owner=request.user, order=order) + + # FIXME: commit everything (VM + order) at once. + order.recurring_price = vm.recurring_price(order.recurring_period) + order.one_time_price = 0 + order.save() return Response(serializer.data)