diff --git a/uncloud/uncloud_pay/models.py b/uncloud/uncloud_pay/models.py index d7c4ff1..c4506a2 100644 --- a/uncloud/uncloud_pay/models.py +++ b/uncloud/uncloud_pay/models.py @@ -60,6 +60,13 @@ class Order(models.Model): choices = RecurringPeriod.choices, default = RecurringPeriod.PER_MONTH) + @property + def products(self): + # Blows up due to circular dependency... + # vms = VMProduct.objects.filter(order=self) + vms = [] + return vms + # def amount(self): # amount = recurring_price # if recurring and first_month: diff --git a/uncloud/uncloud_pay/serializers.py b/uncloud/uncloud_pay/serializers.py index 4065fbd..d08f9cf 100644 --- a/uncloud/uncloud_pay/serializers.py +++ b/uncloud/uncloud_pay/serializers.py @@ -4,6 +4,7 @@ from .models import * from functools import reduce from uncloud_vm.serializers import VMProductSerializer +from uncloud_vm.models import VMProduct class BillSerializer(serializers.ModelSerializer): class Meta: @@ -23,25 +24,38 @@ class PaymentMethodSerializer(serializers.ModelSerializer): class ProductSerializer(serializers.Serializer): vms = VMProductSerializer(many=True, required=False) - class meta: - fields = ['vms'] def create(self, validated_data): - pass + owner = validated_data.pop('owner') + order = validated_data.pop('order') + + vms = validated_data.pop('vms') + for vm in vms: + VMProduct.objects.create(owner=owner, order=order, **vm) + + return True # FIXME: shoudl return created objects + class OrderSerializer(serializers.ModelSerializer): - products = ProductSerializer(many=True) + products = ProductSerializer() class Meta: model = Order fields = '__all__' def create(self, validated_data): products_data = validated_data.pop('products') + validated_data['owner'] = self.context["request"].user + + # FIXME: find something to do with this: + validated_data['recurring_price'] = 0 + validated_data['one_time_price'] = 0 + order = Order.objects.create(**validated_data) - for product_data in products_data: - print("spouik") - print(product_data) - pass # TODO + + # Forward product creation to ProductSerializer. + products = ProductSerializer(data=products_data) + products.is_valid(raise_exception=True) + products.save(order=order,owner=order.owner) return order diff --git a/uncloud/uncloud_vm/models.py b/uncloud/uncloud_vm/models.py index 2510837..2db99f3 100644 --- a/uncloud/uncloud_vm/models.py +++ b/uncloud/uncloud_vm/models.py @@ -3,6 +3,7 @@ from django.contrib.auth import get_user_model import uuid from uncloud_pay.models import Product, RecurringPeriod +import uncloud_pay.models as pay_models class VMHost(models.Model):