Can create a VMSnapshot w/ order (bugs to be removed)

This commit is contained in:
Nico Schottelius 2020-03-17 19:53:14 +01:00
parent 6a382fab23
commit 2f1aee8181
3 changed files with 18 additions and 12 deletions

View file

@ -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()`.

View file

@ -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())

View file

@ -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,