forked from uncloud/uncloud
38 lines
1.2 KiB
Python
38 lines
1.2 KiB
Python
from django.contrib.auth import get_user_model
|
|
|
|
from rest_framework import serializers
|
|
from .models import VMHost, VMProduct, VMSnapshotProduct
|
|
from uncloud_pay.models import RecurringPeriod
|
|
|
|
class VMHostSerializer(serializers.HyperlinkedModelSerializer):
|
|
class Meta:
|
|
model = VMHost
|
|
fields = '__all__'
|
|
|
|
|
|
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:
|
|
model = VMProduct
|
|
fields = ['uuid', 'order', 'owner', 'status', 'name', \
|
|
'cores', 'ram_in_gb', 'recurring_period']
|
|
read_only_fields = ['uuid', 'order', 'owner', 'status']
|
|
|
|
class VMSnapshotProductSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = VMSnapshotProduct
|
|
fields = '__all__'
|
|
|
|
|
|
# verify that vm.owner == user.request
|
|
def validate_vm(self, value):
|
|
print(value)
|
|
return True
|