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-28 07:59:32 +00:00
|
|
|
from .helpers import get_balance_for
|
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-03-02 21:26:40 +00:00
|
|
|
import uncloud_pay.stripe as stripe
|
|
|
|
|
2020-03-03 08:13:04 +00:00
|
|
|
###
|
|
|
|
# Users.
|
2020-03-02 09:46:04 +00:00
|
|
|
|
2020-03-03 08:13:04 +00:00
|
|
|
class UserSerializer(serializers.ModelSerializer):
|
2020-02-27 10:59:28 +00:00
|
|
|
class Meta:
|
2020-03-03 08:13:04 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
###
|
|
|
|
# Payments and Payment Methods.
|
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-03-03 08:13:04 +00:00
|
|
|
class PaymentMethodSerializer(serializers.ModelSerializer):
|
2020-03-03 15:55:56 +00:00
|
|
|
stripe_card_last4 = serializers.IntegerField()
|
2020-03-03 08:13:04 +00:00
|
|
|
|
2020-02-27 14:15:12 +00:00
|
|
|
class Meta:
|
|
|
|
model = PaymentMethod
|
2020-03-03 15:55:56 +00:00
|
|
|
fields = ['source', 'description', 'primary', 'stripe_card_last4']
|
2020-03-03 08:13:04 +00:00
|
|
|
|
|
|
|
class CreditCardSerializer(serializers.Serializer):
|
|
|
|
number = serializers.IntegerField()
|
|
|
|
exp_month = serializers.IntegerField()
|
|
|
|
exp_year = serializers.IntegerField()
|
|
|
|
cvc = serializers.IntegerField()
|
2020-03-02 21:26:40 +00:00
|
|
|
|
|
|
|
class CreatePaymentMethodSerializer(serializers.ModelSerializer):
|
|
|
|
credit_card = CreditCardSerializer()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = PaymentMethod
|
|
|
|
fields = ['source', 'description', 'primary', 'credit_card']
|
|
|
|
|
2020-02-27 14:15:12 +00:00
|
|
|
|
2020-03-03 08:13:04 +00:00
|
|
|
###
|
|
|
|
# Bills
|
|
|
|
|
|
|
|
# TODO: remove magic numbers for decimal fields
|
|
|
|
class BillRecordSerializer(serializers.Serializer):
|
|
|
|
order = serializers.CharField()
|
|
|
|
description = serializers.CharField()
|
|
|
|
recurring_period = serializers.CharField()
|
|
|
|
recurring_price = serializers.DecimalField(max_digits=10, decimal_places=2)
|
2020-03-03 10:15:48 +00:00
|
|
|
recurring_count = serializers.DecimalField(max_digits=10, decimal_places=2)
|
2020-03-03 10:27:35 +00:00
|
|
|
one_time_price = serializers.DecimalField(max_digits=10, decimal_places=2)
|
2020-03-03 08:13:04 +00:00
|
|
|
amount = serializers.DecimalField(max_digits=10, decimal_places=2)
|
|
|
|
|
|
|
|
class BillSerializer(serializers.ModelSerializer):
|
|
|
|
records = BillRecordSerializer(many=True, read_only=True)
|
|
|
|
class Meta:
|
|
|
|
model = Bill
|
|
|
|
fields = ['owner', 'total', 'due_date', 'creation_date',
|
|
|
|
'starting_date', 'ending_date', 'records', 'final']
|
|
|
|
|
|
|
|
###
|
|
|
|
# Orders & Products.
|
2020-02-27 16:13:56 +00:00
|
|
|
|
2020-03-02 08:25:03 +00:00
|
|
|
class OrderRecordSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = OrderRecord
|
2020-03-03 10:27:35 +00:00
|
|
|
fields = ['one_time_price', 'recurring_price', 'description']
|
2020-03-02 08:25:03 +00:00
|
|
|
|
2020-03-03 08:13:04 +00:00
|
|
|
|
2020-02-27 11:42:24 +00:00
|
|
|
class OrderSerializer(serializers.ModelSerializer):
|
2020-03-02 08:25:03 +00:00
|
|
|
records = OrderRecordSerializer(many=True, read_only=True)
|
2020-02-27 11:42:24 +00:00
|
|
|
class Meta:
|
|
|
|
model = Order
|
2020-03-02 08:25:03 +00:00
|
|
|
fields = ['uuid', 'creation_date', 'starting_date', 'ending_date',
|
2020-03-03 10:27:35 +00:00
|
|
|
'bill', 'recurring_period', 'records', 'recurring_price', 'one_time_price']
|
2020-02-27 11:42:24 +00:00
|
|
|
|
2020-03-03 08:13:04 +00:00
|
|
|
class ProductSerializer(serializers.Serializer):
|
|
|
|
vms = VMProductSerializer(many=True, read_only=True)
|