2020-02-27 11:38:04 +00:00
|
|
|
from django.contrib.auth import get_user_model
|
2020-02-27 10:59:28 +00:00
|
|
|
from rest_framework import serializers
|
2020-02-27 14:15:12 +00:00
|
|
|
from .models import *
|
2020-02-27 10:59:28 +00:00
|
|
|
|
2020-02-27 14:50:46 +00:00
|
|
|
from functools import reduce
|
2020-02-27 16:13:56 +00:00
|
|
|
from uncloud_vm.serializers import VMProductSerializer
|
2020-02-27 17:54:13 +00:00
|
|
|
from uncloud_vm.models import VMProduct
|
2020-02-27 14:50:46 +00:00
|
|
|
|
2020-02-27 10:59:28 +00:00
|
|
|
class BillSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Bill
|
2020-02-27 11:42:24 +00:00
|
|
|
fields = ['owner', 'amount', 'due_date', 'creation_date',
|
|
|
|
'starting_date', 'ending_date', 'paid']
|
2020-02-27 10:59:28 +00:00
|
|
|
|
|
|
|
class PaymentSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Payment
|
2020-02-27 11:10:26 +00:00
|
|
|
fields = ['owner', 'amount', 'source', 'timestamp']
|
2020-02-27 11:38:04 +00:00
|
|
|
|
2020-02-27 14:15:12 +00:00
|
|
|
class PaymentMethodSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = PaymentMethod
|
|
|
|
fields = ['owner', 'primary', 'source', 'description']
|
|
|
|
|
2020-02-27 16:13:56 +00:00
|
|
|
class ProductSerializer(serializers.Serializer):
|
|
|
|
vms = VMProductSerializer(many=True, required=False)
|
|
|
|
|
|
|
|
def create(self, validated_data):
|
2020-02-27 17:54:13 +00:00
|
|
|
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
|
|
|
|
|
2020-02-27 16:13:56 +00:00
|
|
|
|
2020-02-27 11:42:24 +00:00
|
|
|
class OrderSerializer(serializers.ModelSerializer):
|
2020-02-27 17:54:13 +00:00
|
|
|
products = ProductSerializer()
|
2020-02-27 11:42:24 +00:00
|
|
|
class Meta:
|
|
|
|
model = Order
|
2020-02-27 11:45:54 +00:00
|
|
|
fields = '__all__'
|
2020-02-27 11:42:24 +00:00
|
|
|
|
2020-02-27 16:13:56 +00:00
|
|
|
def create(self, validated_data):
|
|
|
|
products_data = validated_data.pop('products')
|
2020-02-27 17:54:13 +00:00
|
|
|
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
|
|
|
|
|
2020-02-27 16:13:56 +00:00
|
|
|
order = Order.objects.create(**validated_data)
|
2020-02-27 17:54:13 +00:00
|
|
|
|
|
|
|
# Forward product creation to ProductSerializer.
|
|
|
|
products = ProductSerializer(data=products_data)
|
|
|
|
products.is_valid(raise_exception=True)
|
|
|
|
products.save(order=order,owner=order.owner)
|
2020-02-27 16:13:56 +00:00
|
|
|
|
|
|
|
return order
|
|
|
|
|
2020-02-27 11:38:04 +00:00
|
|
|
class UserSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = get_user_model()
|
2020-02-27 14:50:46 +00:00
|
|
|
fields = ['username', 'email', 'balance']
|
|
|
|
|
|
|
|
# Display current 'balance'
|
|
|
|
balance = serializers.SerializerMethodField('get_balance')
|
|
|
|
def __sum_balance(self, entries):
|
|
|
|
return reduce(lambda acc, entry: acc + entry.amount, entries, 0)
|
|
|
|
|
|
|
|
def get_balance(self, user):
|
|
|
|
bills = self.__sum_balance(Bill.objects.filter(owner=user))
|
|
|
|
payments = self.__sum_balance(Payment.objects.filter(owner=user))
|
|
|
|
balance = payments - bills
|
2020-02-27 11:38:04 +00:00
|
|
|
|
2020-02-27 14:50:46 +00:00
|
|
|
return balance
|