diff --git a/uncloud_django_based/uncloud/uncloud_service/models.py b/uncloud_django_based/uncloud/uncloud_service/models.py index 26bedfd..35a479e 100644 --- a/uncloud_django_based/uncloud/uncloud_service/models.py +++ b/uncloud_django_based/uncloud/uncloud_service/models.py @@ -46,7 +46,8 @@ class GenericServiceProduct(Product): decimal_places=AMOUNT_DECIMALS, validators=[MinValueValidator(0)]) - def recurring_price(self, recurring_period=RecurringPeriod.PER_MONTH): + @property + def recurring_price(self): # FIXME: handle recurring_period somehow. return self.custom_recurring_price diff --git a/uncloud_django_based/uncloud/uncloud_service/serializers.py b/uncloud_django_based/uncloud/uncloud_service/serializers.py index 2be2cee..eda1377 100644 --- a/uncloud_django_based/uncloud/uncloud_service/serializers.py +++ b/uncloud_django_based/uncloud/uncloud_service/serializers.py @@ -23,12 +23,26 @@ class MatrixServiceProductSerializer(serializers.ModelSerializer): read_only_fields = ['uuid', 'order', 'owner', 'status'] class GenericServiceProductSerializer(serializers.ModelSerializer): - # Custom field used at creation (= ordering) only. - recurring_period = serializers.ChoiceField( - choices=GenericServiceProduct.allowed_recurring_periods()) - class Meta: model = GenericServiceProduct fields = ['uuid', 'order', 'owner', 'status', 'custom_recurring_price', - 'custom_description', 'custom_one_time_price', 'recurring_period'] + 'custom_description', 'custom_one_time_price'] read_only_fields = ['uuid', 'order', 'owner', 'status'] + +class OrderGenericServiceProductSerializer(GenericServiceProductSerializer): + recurring_period = serializers.ChoiceField( + choices=GenericServiceProduct.allowed_recurring_periods()) + + def __init__(self, *args, **kwargs): + super(OrderGenericServiceProductSerializer, self).__init__(*args, **kwargs) + self.fields['billing_address'] = serializers.ChoiceField( + choices=BillingAddress.get_addresses_for( + self.context['request'].user) + ) + + class Meta: + model = GenericServiceProductSerializer.Meta.model + fields = GenericServiceProductSerializer.Meta.fields + [ + 'recurring_period', 'billing_address' + ] + read_only_fields = GenericServiceProductSerializer.Meta.read_only_fields diff --git a/uncloud_django_based/uncloud/uncloud_service/views.py b/uncloud_django_based/uncloud/uncloud_service/views.py index d4be3a6..2f0f9c2 100644 --- a/uncloud_django_based/uncloud/uncloud_service/views.py +++ b/uncloud_django_based/uncloud/uncloud_service/views.py @@ -44,11 +44,13 @@ class MatrixServiceProductViewSet(ProductViewSet): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) order_recurring_period = serializer.validated_data.pop("recurring_period") + order_billing_address = serializer.validated_data.pop("billing_address") # Create base order.) order = Order.objects.create( recurring_period=order_recurring_period, owner=request.user, + billing_address=order_billing_address, starting_date=timezone.now() ) order.save() @@ -72,22 +74,29 @@ class MatrixServiceProductViewSet(ProductViewSet): class GenericServiceProductViewSet(ProductViewSet): permission_classes = [permissions.IsAuthenticated] - serializer_class = GenericServiceProductSerializer def get_queryset(self): return GenericServiceProduct.objects.filter(owner=self.request.user) + def get_serializer_class(self): + if self.action == 'create': + return OrderGenericServiceProductSerializer + else: + return GenericServiceProductSerializer + @transaction.atomic def create(self, request): # Extract serializer data. serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) order_recurring_period = serializer.validated_data.pop("recurring_period") + order_billing_address = serializer.validated_data.pop("billing_address") # Create base order. order = Order.objects.create( recurring_period=order_recurring_period, owner=request.user, + billing_address=order_billing_address, starting_date=timezone.now() ) order.save() diff --git a/uncloud_django_based/uncloud/ungleich_service/migrations/0005_auto_20200417_0551.py b/uncloud_django_based/uncloud/ungleich_service/migrations/0005_auto_20200417_0551.py deleted file mode 100644 index aed07b6..0000000 --- a/uncloud_django_based/uncloud/ungleich_service/migrations/0005_auto_20200417_0551.py +++ /dev/null @@ -1,18 +0,0 @@ -# Generated by Django 3.0.5 on 2020-04-17 05:51 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('ungleich_service', '0004_auto_20200403_1727'), - ] - - operations = [ - migrations.AlterField( - model_name='matrixserviceproduct', - name='status', - field=models.CharField(choices=[('PENDING', 'Pending'), ('AWAITING_PAYMENT', 'Awaiting payment'), ('BEING_CREATED', 'Being created'), ('SCHEDULED', 'Scheduled'), ('ACTIVE', 'Active'), ('MODIFYING', 'Modifying'), ('DELETED', 'Deleted'), ('DISABLED', 'Disabled'), ('UNUSABLE', 'Unusable')], default='AWAITING_PAYMENT', max_length=32), - ), - ] diff --git a/uncloud_django_based/uncloud/ungleich_service/migrations/0006_genericserviceproduct.py b/uncloud_django_based/uncloud/ungleich_service/migrations/0006_genericserviceproduct.py deleted file mode 100644 index f4bda32..0000000 --- a/uncloud_django_based/uncloud/ungleich_service/migrations/0006_genericserviceproduct.py +++ /dev/null @@ -1,36 +0,0 @@ -# Generated by Django 3.0.5 on 2020-04-17 08:02 - -from django.conf import settings -import django.contrib.postgres.fields.jsonb -import django.core.validators -from django.db import migrations, models -import django.db.models.deletion -import uuid - - -class Migration(migrations.Migration): - - dependencies = [ - migrations.swappable_dependency(settings.AUTH_USER_MODEL), - ('uncloud_pay', '0005_auto_20200417_0551'), - ('ungleich_service', '0005_auto_20200417_0551'), - ] - - operations = [ - migrations.CreateModel( - name='GenericServiceProduct', - fields=[ - ('extra_data', django.contrib.postgres.fields.jsonb.JSONField(blank=True, editable=False, null=True)), - ('uuid', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), - ('status', models.CharField(choices=[('PENDING', 'Pending'), ('AWAITING_PAYMENT', 'Awaiting payment'), ('BEING_CREATED', 'Being created'), ('SCHEDULED', 'Scheduled'), ('ACTIVE', 'Active'), ('MODIFYING', 'Modifying'), ('DELETED', 'Deleted'), ('DISABLED', 'Disabled'), ('UNUSABLE', 'Unusable')], default='AWAITING_PAYMENT', max_length=32)), - ('custom_description', models.TextField()), - ('custom_recurring_price', models.DecimalField(decimal_places=2, default=0.0, max_digits=10, validators=[django.core.validators.MinValueValidator(0)])), - ('custom_one_time_price', models.DecimalField(decimal_places=2, default=0.0, max_digits=10, validators=[django.core.validators.MinValueValidator(0)])), - ('order', models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, to='uncloud_pay.Order')), - ('owner', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), - ], - options={ - 'abstract': False, - }, - ), - ]