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

@ -31,7 +31,7 @@ class VMDiskImageProductSerializer(serializers.ModelSerializer):
model = VMDiskImageProduct model = VMDiskImageProduct
fields = '__all__' fields = '__all__'
class VMProductSerializer(serializers.HyperlinkedModelSerializer): class VMProductSerializer(serializers.ModelSerializer):
# Custom field used at creation (= ordering) only. # Custom field used at creation (= ordering) only.
recurring_period = serializers.ChoiceField( recurring_period = serializers.ChoiceField(
choices=VMProduct.allowed_recurring_periods()) choices=VMProduct.allowed_recurring_periods())

View file

@ -29,7 +29,13 @@ class VMDiskImageProductMineViewSet(viewsets.ModelViewSet):
serializer_class = VMDiskImageProductSerializer serializer_class = VMDiskImageProductSerializer
def get_queryset(self): 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): def create(self, request):
serializer = VMDiskImageProductSerializer(data=request.data, context={'request': 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 # This verifies that the VM belongs to the request user
serializer.is_valid(raise_exception=True) serializer.is_valid(raise_exception=True)
disks = VMDiskProduct.objects.filter(vm=serializer.validated_data['vm']) vm = vm=serializer.validated_data['vm']
ssds_size = sum([d.size_in_gb for d in disks if d.storage_class == 'ssd']) disks = VMDiskProduct.objects.filter(vm=vm)
hdds_size = sum([d.size_in_gb for d in disks if d.storage_class == 'hdd']) 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_price = serializer.pricing['per_gb_ssd'] * ssds_size + serializer.pricing['per_gb_hdd'] * hdds_size
recurring_period = serializer.pricing['recurring_period'] recurring_period = serializer.pricing['recurring_period']
@ -142,12 +149,11 @@ class VMSnapshotProductViewSet(viewsets.ModelViewSet):
# Create order # Create order
now = datetime.datetime.now() now = datetime.datetime.now()
order = Order(owner=request.user, order = Order(owner=request.user,
creation_date=now,
starting_date=now,
recurring_price=recurring_price,
one_time_price=0,
recurring_period=recurring_period) recurring_period=recurring_period)
order.save() 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, serializer.save(owner=request.user,
order=order, order=order,