Properly wire billing addresses to uncloud_service

This commit is contained in:
fnux 2020-04-18 10:40:11 +02:00
parent dd0c1cba94
commit a49fe6ff51
2 changed files with 27 additions and 9 deletions

View file

@ -4,24 +4,36 @@ from uncloud_vm.serializers import ManagedVMProductSerializer
from uncloud_vm.models import VMProduct from uncloud_vm.models import VMProduct
from uncloud_pay.models import RecurringPeriod, BillingAddress 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): class MatrixServiceProductSerializer(serializers.ModelSerializer):
vm = ManagedVMProductSerializer() 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: class Meta:
model = MatrixServiceProduct model = MatrixServiceProduct
fields = ['uuid', 'order', 'owner', 'status', 'vm', 'domain', fields = ['uuid', 'order', 'owner', 'status', 'vm', 'domain',
'recurring_period'] 'recurring_period']
read_only_fields = ['uuid', 'order', 'owner', 'status'] 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 GenericServiceProductSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = GenericServiceProduct model = GenericServiceProduct

View file

@ -38,6 +38,12 @@ class MatrixServiceProductViewSet(ProductViewSet):
def get_queryset(self): def get_queryset(self):
return MatrixServiceProduct.objects.filter(owner=self.request.user) return MatrixServiceProduct.objects.filter(owner=self.request.user)
def get_serializer_class(self):
if self.action == 'create':
return OrderMatrixServiceProductSerializer
else:
return MatrixServiceProductSerializer
@transaction.atomic @transaction.atomic
def create(self, request): def create(self, request):
# Extract serializer data. # Extract serializer data.