Wire VMProduct creation to order
This commit is contained in:
parent
b2fe5014d8
commit
809a55e1dd
3 changed files with 30 additions and 8 deletions
|
@ -60,6 +60,13 @@ class Order(models.Model):
|
||||||
choices = RecurringPeriod.choices,
|
choices = RecurringPeriod.choices,
|
||||||
default = RecurringPeriod.PER_MONTH)
|
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):
|
# def amount(self):
|
||||||
# amount = recurring_price
|
# amount = recurring_price
|
||||||
# if recurring and first_month:
|
# if recurring and first_month:
|
||||||
|
|
|
@ -4,6 +4,7 @@ from .models import *
|
||||||
|
|
||||||
from functools import reduce
|
from functools import reduce
|
||||||
from uncloud_vm.serializers import VMProductSerializer
|
from uncloud_vm.serializers import VMProductSerializer
|
||||||
|
from uncloud_vm.models import VMProduct
|
||||||
|
|
||||||
class BillSerializer(serializers.ModelSerializer):
|
class BillSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
|
@ -23,25 +24,38 @@ class PaymentMethodSerializer(serializers.ModelSerializer):
|
||||||
|
|
||||||
class ProductSerializer(serializers.Serializer):
|
class ProductSerializer(serializers.Serializer):
|
||||||
vms = VMProductSerializer(many=True, required=False)
|
vms = VMProductSerializer(many=True, required=False)
|
||||||
class meta:
|
|
||||||
fields = ['vms']
|
|
||||||
|
|
||||||
def create(self, validated_data):
|
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):
|
class OrderSerializer(serializers.ModelSerializer):
|
||||||
products = ProductSerializer(many=True)
|
products = ProductSerializer()
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Order
|
model = Order
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
|
|
||||||
def create(self, validated_data):
|
def create(self, validated_data):
|
||||||
products_data = validated_data.pop('products')
|
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)
|
order = Order.objects.create(**validated_data)
|
||||||
for product_data in products_data:
|
|
||||||
print("spouik")
|
# Forward product creation to ProductSerializer.
|
||||||
print(product_data)
|
products = ProductSerializer(data=products_data)
|
||||||
pass # TODO
|
products.is_valid(raise_exception=True)
|
||||||
|
products.save(order=order,owner=order.owner)
|
||||||
|
|
||||||
return order
|
return order
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ from django.contrib.auth import get_user_model
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
from uncloud_pay.models import Product, RecurringPeriod
|
from uncloud_pay.models import Product, RecurringPeriod
|
||||||
|
import uncloud_pay.models as pay_models
|
||||||
|
|
||||||
|
|
||||||
class VMHost(models.Model):
|
class VMHost(models.Model):
|
||||||
|
|
Loading…
Reference in a new issue