Allow to select billing period when registering VM
This commit is contained in:
parent
5559d600c7
commit
b31aa72f84
4 changed files with 25 additions and 5 deletions
|
@ -299,5 +299,9 @@ class Product(models.Model):
|
||||||
def setup_fee(self):
|
def setup_fee(self):
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
@property
|
||||||
|
def recurring_period(self):
|
||||||
|
return self.order.recurring_period
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
abstract = True
|
abstract = True
|
||||||
|
|
|
@ -47,9 +47,11 @@ class VMProduct(Product):
|
||||||
ram_in_gb = models.FloatField()
|
ram_in_gb = models.FloatField()
|
||||||
|
|
||||||
def recurring_price(self, recurring_period=RecurringPeriod.PER_MONTH):
|
def recurring_price(self, recurring_period=RecurringPeriod.PER_MONTH):
|
||||||
if recurring_period == RecurringPeriod.PER_MONTH:
|
|
||||||
# TODO: move magic numbers in variables
|
# TODO: move magic numbers in variables
|
||||||
|
if recurring_period == RecurringPeriod.PER_MONTH:
|
||||||
return self.cores * 3 + self.ram_in_gb * 2
|
return self.cores * 3 + self.ram_in_gb * 2
|
||||||
|
elif recurring_period == RecurringPeriod.PER_HOUR:
|
||||||
|
return self.cores * 4.0/(30 * 24) + self.ram_in_gb * 3.0/(30* 24)
|
||||||
else:
|
else:
|
||||||
raise Exception('Invalid recurring period for VM Product pricing.')
|
raise Exception('Invalid recurring period for VM Product pricing.')
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ from django.contrib.auth import get_user_model
|
||||||
|
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
from .models import VMHost, VMProduct, VMSnapshotProduct
|
from .models import VMHost, VMProduct, VMSnapshotProduct
|
||||||
|
from uncloud_pay.models import RecurringPeriod
|
||||||
|
|
||||||
class VMHostSerializer(serializers.HyperlinkedModelSerializer):
|
class VMHostSerializer(serializers.HyperlinkedModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
|
@ -10,10 +11,19 @@ class VMHostSerializer(serializers.HyperlinkedModelSerializer):
|
||||||
|
|
||||||
|
|
||||||
class VMProductSerializer(serializers.HyperlinkedModelSerializer):
|
class VMProductSerializer(serializers.HyperlinkedModelSerializer):
|
||||||
|
# TODO: move this to VMProduct.
|
||||||
|
allowed_recurring_periods=list(filter(
|
||||||
|
lambda pair: pair[0] in [RecurringPeriod.PER_MONTH, RecurringPeriod.PER_HOUR],
|
||||||
|
RecurringPeriod.choices))
|
||||||
|
|
||||||
|
# Custom field used at creation (= ordering) only.
|
||||||
|
recurring_period = serializers.ChoiceField(
|
||||||
|
choices=allowed_recurring_periods)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = VMProduct
|
model = VMProduct
|
||||||
fields = ['uuid', 'order', 'owner', 'status', 'name', \
|
fields = ['uuid', 'order', 'owner', 'status', 'name', \
|
||||||
'cores', 'ram_in_gb']
|
'cores', 'ram_in_gb', 'recurring_period']
|
||||||
read_only_fields = ['uuid', 'order', 'owner', 'status']
|
read_only_fields = ['uuid', 'order', 'owner', 'status']
|
||||||
|
|
||||||
class VMSnapshotProductSerializer(serializers.ModelSerializer):
|
class VMSnapshotProductSerializer(serializers.ModelSerializer):
|
||||||
|
|
|
@ -32,19 +32,23 @@ class VMProductViewSet(ProductViewSet):
|
||||||
# if something goes wrong.
|
# if something goes wrong.
|
||||||
@transaction.atomic
|
@transaction.atomic
|
||||||
def create(self, request):
|
def create(self, request):
|
||||||
|
# Extract serializer data.
|
||||||
|
serializer = VMProductSerializer(data=request.data, context={'request': request})
|
||||||
|
serializer.is_valid(raise_exception=True)
|
||||||
|
order_recurring_period = serializer.validated_data.pop("recurring_period")
|
||||||
|
|
||||||
# Create base order.
|
# Create base order.
|
||||||
order = Order.objects.create(
|
order = Order.objects.create(
|
||||||
recurring_period=RecurringPeriod.PER_MONTH,
|
recurring_period=order_recurring_period,
|
||||||
owner=request.user
|
owner=request.user
|
||||||
)
|
)
|
||||||
order.save()
|
order.save()
|
||||||
|
|
||||||
# Create VM.
|
# Create VM.
|
||||||
serializer = VMProductSerializer(data=request.data, context={'request': request})
|
|
||||||
serializer.is_valid(raise_exception=True)
|
|
||||||
vm = serializer.save(owner=request.user, order=order)
|
vm = serializer.save(owner=request.user, order=order)
|
||||||
|
|
||||||
# Add Product record to order (VM is mutable, allows to keep history in order).
|
# Add Product record to order (VM is mutable, allows to keep history in order).
|
||||||
|
# XXX: Move this to some kind of on_create hook in parent Product class?
|
||||||
order.add_record(vm.setup_fee,
|
order.add_record(vm.setup_fee,
|
||||||
vm.recurring_price(order.recurring_period), vm.description)
|
vm.recurring_price(order.recurring_period), vm.description)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue