diff --git a/uncloud_django_based/uncloud/uncloud_service/serializers.py b/uncloud_django_based/uncloud/uncloud_service/serializers.py index eda1377..6666a15 100644 --- a/uncloud_django_based/uncloud/uncloud_service/serializers.py +++ b/uncloud_django_based/uncloud/uncloud_service/serializers.py @@ -4,24 +4,36 @@ from uncloud_vm.serializers import ManagedVMProductSerializer from uncloud_vm.models import VMProduct from uncloud_pay.models import RecurringPeriod, BillingAddress +# XXX: the OrderSomethingSomthingProductSerializer classes add a lot of +# boilerplate: can we reduce it somehow? + class MatrixServiceProductSerializer(serializers.ModelSerializer): vm = ManagedVMProductSerializer() - # Custom field used at creation (= ordering) only. - recurring_period = serializers.ChoiceField( - choices=MatrixServiceProduct.allowed_recurring_periods()) - - def __init__(self, *args, **kwargs): - super(MatrixServiceProductSerializer, self).__init__(*args, **kwargs) - self.fields['billing_address'] = serializers.ChoiceField( - choices=BillingAddress.get_addresses_for(self.context['request'].user)) - class Meta: model = MatrixServiceProduct fields = ['uuid', 'order', 'owner', 'status', 'vm', 'domain', 'recurring_period'] read_only_fields = ['uuid', 'order', 'owner', 'status'] +class OrderMatrixServiceProductSerializer(MatrixServiceProductSerializer): + recurring_period = serializers.ChoiceField( + choices=MatrixServiceProduct.allowed_recurring_periods()) + + def __init__(self, *args, **kwargs): + super(OrderMatrixServiceProductSerializer, self).__init__(*args, **kwargs) + self.fields['billing_address'] = serializers.ChoiceField( + choices=BillingAddress.get_addresses_for( + self.context['request'].user) + ) + + class Meta: + model = MatrixServiceProductSerializer.Meta.model + fields = MatrixServiceProductSerializer.Meta.fields + [ + 'recurring_period', 'billing_address' + ] + read_only_fields = MatrixServiceProductSerializer.Meta.read_only_fields + class GenericServiceProductSerializer(serializers.ModelSerializer): class Meta: model = GenericServiceProduct diff --git a/uncloud_django_based/uncloud/uncloud_service/views.py b/uncloud_django_based/uncloud/uncloud_service/views.py index 2f0f9c2..abd4a05 100644 --- a/uncloud_django_based/uncloud/uncloud_service/views.py +++ b/uncloud_django_based/uncloud/uncloud_service/views.py @@ -38,6 +38,12 @@ class MatrixServiceProductViewSet(ProductViewSet): def get_queryset(self): return MatrixServiceProduct.objects.filter(owner=self.request.user) + def get_serializer_class(self): + if self.action == 'create': + return OrderMatrixServiceProductSerializer + else: + return MatrixServiceProductSerializer + @transaction.atomic def create(self, request): # Extract serializer data.