forked from uncloud/uncloud
45 lines
1.4 KiB
Python
45 lines
1.4 KiB
Python
from django.contrib.auth import get_user_model
|
|
from rest_framework import serializers
|
|
from .models import *
|
|
from .helpers import get_balance_for
|
|
|
|
from functools import reduce
|
|
from uncloud_vm.serializers import VMProductSerializer
|
|
from uncloud_vm.models import VMProduct
|
|
|
|
class BillSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = Bill
|
|
fields = ['owner', 'amount', 'due_date', 'creation_date',
|
|
'starting_date', 'ending_date']
|
|
|
|
class PaymentSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = Payment
|
|
fields = ['owner', 'amount', 'source', 'timestamp']
|
|
|
|
class PaymentMethodSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = PaymentMethod
|
|
fields = '__all__'
|
|
|
|
class ProductSerializer(serializers.Serializer):
|
|
vms = VMProductSerializer(many=True, read_only=True)
|
|
|
|
class OrderSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = Order
|
|
fields = '__all__'
|
|
|
|
class UserSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = get_user_model()
|
|
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):
|
|
return get_balance_for(user)
|