uncloud-mravi/uncloud/uncloud_pay/serializers.py

46 lines
1.4 KiB
Python
Raw Normal View History

2020-02-27 11:38:04 +00:00
from django.contrib.auth import get_user_model
from rest_framework import serializers
from .models import *
from .helpers import get_balance_for
2020-02-27 14:50:46 +00:00
from functools import reduce
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
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']
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
class PaymentMethodSerializer(serializers.ModelSerializer):
class Meta:
model = PaymentMethod
fields = '__all__'
class ProductSerializer(serializers.Serializer):
vms = VMProductSerializer(many=True, read_only=True)
2020-02-27 11:42:24 +00:00
class OrderSerializer(serializers.ModelSerializer):
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 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):
return get_balance_for(user)