Define custom fields and serializer for MatrixServiceProduct

This commit is contained in:
fnux 2020-02-28 16:26:45 +01:00
parent eaa483e018
commit af1265003e
4 changed files with 17 additions and 3 deletions

View file

@ -163,5 +163,9 @@ class Product(models.Model):
def recurring_price(self, recurring_period=RecurringPeriod.PER_MONTH):
pass # To be implemented in child.
@property
def setup_fee(self):
return 0
class Meta:
abstract = True

View file

@ -12,9 +12,9 @@ class VMHostSerializer(serializers.HyperlinkedModelSerializer):
class VMProductSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = VMProduct
fields = ['uuid', 'description', 'order', 'owner', 'status', 'name', \
fields = ['uuid', 'order', 'owner', 'status', 'name', \
'cores', 'ram_in_gb']
read_only_fields = ['uuid', 'description', 'order', 'owner', 'status']
read_only_fields = ['uuid', 'order', 'owner', 'status']
class VMSnapshotProductSerializer(serializers.ModelSerializer):
class Meta:

View file

@ -9,12 +9,18 @@ class MatrixServiceProduct(Product):
setup_fee = 30
description = "Managed Matrix HomeServer"
# Specific to Matrix-as-a-Service
vm = models.ForeignKey(
VMProduct, on_delete=models.CASCADE
)
domain = models.CharField(max_length=255, default='domain.tld')
def recurring_price(self, recurring_period=RecurringPeriod.PER_MONTH):
if recurring_period == RecurringPeriod.PER_MONTH:
return monthly_managment_fee + vm.recurring_price(RecurringPeriod.PER_MONTH)
else:
raise Exception('Invalid recurring period for VM Product pricing.')
def setup_fee(self):
return setup_fee

View file

@ -1,7 +1,11 @@
from rest_framework import serializers
from .models import MatrixServiceProduct
from uncloud_vm.serializers import VMProductSerializer
class MatrixServiceProductSerializer(serializers.ModelSerializer):
vm = VMProductSerializer()
class Meta:
model = MatrixServiceProduct
fields = '__all__'
fields = ['uuid', 'order', 'owner', 'status', 'vm', 'domain']
read_only_fields = ['uuid', 'order', 'owner', 'status']