From 2f1aee818113d41506e4817af4c8fd29048fca47 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Tue, 17 Mar 2020 19:53:14 +0100 Subject: [PATCH] Can create a VMSnapshot w/ order (bugs to be removed) --- uncloud/uncloud_pay/helpers.py | 6 +++--- uncloud/uncloud_vm/serializers.py | 2 +- uncloud/uncloud_vm/views.py | 22 ++++++++++++++-------- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/uncloud/uncloud_pay/helpers.py b/uncloud/uncloud_pay/helpers.py index d02b916..f791564 100644 --- a/uncloud/uncloud_pay/helpers.py +++ b/uncloud/uncloud_pay/helpers.py @@ -16,9 +16,9 @@ def end_of_month(year, month): hour=23, minute=59, second=59, tzinfo=tz) class ProductViewSet(mixins.CreateModelMixin, - mixins.RetrieveModelMixin, - mixins.ListModelMixin, - GenericViewSet): + mixins.RetrieveModelMixin, + mixins.ListModelMixin, + GenericViewSet): """ A customer-facing viewset that provides default `create()`, `retrieve()` and `list()`. diff --git a/uncloud/uncloud_vm/serializers.py b/uncloud/uncloud_vm/serializers.py index c92f108..75bcabe 100644 --- a/uncloud/uncloud_vm/serializers.py +++ b/uncloud/uncloud_vm/serializers.py @@ -31,7 +31,7 @@ class VMDiskImageProductSerializer(serializers.ModelSerializer): model = VMDiskImageProduct fields = '__all__' -class VMProductSerializer(serializers.HyperlinkedModelSerializer): +class VMProductSerializer(serializers.ModelSerializer): # Custom field used at creation (= ordering) only. recurring_period = serializers.ChoiceField( choices=VMProduct.allowed_recurring_periods()) diff --git a/uncloud/uncloud_vm/views.py b/uncloud/uncloud_vm/views.py index e1bbd22..7b5fa4f 100644 --- a/uncloud/uncloud_vm/views.py +++ b/uncloud/uncloud_vm/views.py @@ -29,7 +29,13 @@ class VMDiskImageProductMineViewSet(viewsets.ModelViewSet): serializer_class = VMDiskImageProductSerializer def get_queryset(self): - return VMDiskImageProduct.objects.filter(owner=self.request.user) + if self.request.user.is_superuser: + obj = VMDiskImageProduct.objects.all() + else: + obj = VMDiskImageProduct.objects.filter(owner=self.request.user) + + return obj + def create(self, request): serializer = VMDiskImageProductSerializer(data=request.data, context={'request': request}) @@ -132,9 +138,10 @@ class VMSnapshotProductViewSet(viewsets.ModelViewSet): # This verifies that the VM belongs to the request user serializer.is_valid(raise_exception=True) - disks = VMDiskProduct.objects.filter(vm=serializer.validated_data['vm']) - ssds_size = sum([d.size_in_gb for d in disks if d.storage_class == 'ssd']) - hdds_size = sum([d.size_in_gb for d in disks if d.storage_class == 'hdd']) + vm = vm=serializer.validated_data['vm'] + disks = VMDiskProduct.objects.filter(vm=vm) + ssds_size = sum([d.size_in_gb for d in disks if d.image.storage_class == 'ssd']) + hdds_size = sum([d.size_in_gb for d in disks if d.image.storage_class == 'hdd']) recurring_price = serializer.pricing['per_gb_ssd'] * ssds_size + serializer.pricing['per_gb_hdd'] * hdds_size recurring_period = serializer.pricing['recurring_period'] @@ -142,12 +149,11 @@ class VMSnapshotProductViewSet(viewsets.ModelViewSet): # Create order now = datetime.datetime.now() order = Order(owner=request.user, - creation_date=now, - starting_date=now, - recurring_price=recurring_price, - one_time_price=0, recurring_period=recurring_period) order.save() + order.add_record(one_time_price=0, + recurring_price=recurring_price, + description="Snapshot of VM {} from {}".format(vm, now)) serializer.save(owner=request.user, order=order,