uncloud/uncloud_pay/serializers.py

113 lines
4.2 KiB
Python
Raw Permalink Normal View History

2020-02-27 11:38:04 +00:00
from django.contrib.auth import get_user_model
from rest_framework import serializers
2020-05-07 10:05:26 +00:00
from uncloud_auth.serializers import UserSerializer
from django.utils.translation import gettext_lazy as _
from .models import *
2020-03-02 21:26:40 +00:00
###
# Payments and Payment Methods.
class PaymentSerializer(serializers.ModelSerializer):
class Meta:
model = Payment
fields = '__all__'
2020-02-27 11:38:04 +00:00
class PaymentMethodSerializer(serializers.ModelSerializer):
stripe_card_last4 = serializers.IntegerField()
class Meta:
model = PaymentMethod
fields = ['uuid', 'source', 'description', 'primary', 'stripe_card_last4', 'active']
2020-03-03 17:16:25 +00:00
2020-03-05 10:28:43 +00:00
class UpdatePaymentMethodSerializer(serializers.ModelSerializer):
class Meta:
model = PaymentMethod
fields = ['description', 'primary']
2020-03-03 17:16:25 +00:00
class ChargePaymentMethodSerializer(serializers.Serializer):
amount = serializers.DecimalField(max_digits=10, decimal_places=2)
2020-03-02 21:26:40 +00:00
class CreatePaymentMethodSerializer(serializers.ModelSerializer):
please_visit = serializers.CharField(read_only=True)
2020-03-02 21:26:40 +00:00
class Meta:
model = PaymentMethod
fields = ['source', 'description', 'primary', 'please_visit']
2020-03-02 21:26:40 +00:00
2020-03-06 08:32:25 +00:00
###
# Orders & Products.
class OrderSerializer(serializers.ModelSerializer):
2020-05-07 10:05:26 +00:00
owner = serializers.PrimaryKeyRelatedField(queryset=get_user_model().objects.all())
def __init__(self, *args, **kwargs):
# Don't pass the 'fields' arg up to the superclass
admin = kwargs.pop('admin', None)
# Instantiate the superclass normally
super(OrderSerializer, self).__init__(*args, **kwargs)
# Only allows owner in admin mode.
if not admin:
self.fields.pop('owner')
def create(self, validated_data):
billing_address = BillingAddress.get_preferred_address_for(validated_data["owner"])
instance = Order(billing_address=billing_address, **validated_data)
instance.save()
return instance
def validate_owner(self, value):
if BillingAddress.get_preferred_address_for(value) == None:
raise serializers.ValidationError("Owner does not have a valid billing address.")
return value
2020-03-06 08:32:25 +00:00
class Meta:
model = Order
2020-05-08 14:47:32 +00:00
read_only_fields = ['replaced_by', 'depends_on']
2020-05-07 10:08:18 +00:00
fields = ['uuid', 'owner', 'description', 'creation_date', 'starting_date', 'ending_date',
2020-05-08 14:47:32 +00:00
'bill', 'recurring_period', 'recurring_price', 'one_time_price'] + read_only_fields
2020-03-06 08:32:25 +00:00
###
# Bills
# TODO: remove magic numbers for decimal fields
class BillRecordSerializer(serializers.Serializer):
2020-03-06 08:32:25 +00:00
order = serializers.HyperlinkedRelatedField(
view_name='order-detail',
read_only=True)
description = serializers.CharField()
one_time_price = serializers.DecimalField(AMOUNT_MAX_DIGITS, AMOUNT_DECIMALS)
recurring_price = serializers.DecimalField(AMOUNT_MAX_DIGITS, AMOUNT_DECIMALS)
recurring_period = serializers.ChoiceField(choices=RecurringPeriod.choices)
recurring_count = serializers.DecimalField(AMOUNT_MAX_DIGITS, AMOUNT_DECIMALS)
vat_rate = serializers.DecimalField(AMOUNT_MAX_DIGITS, AMOUNT_DECIMALS)
vat_amount = serializers.DecimalField(AMOUNT_MAX_DIGITS, AMOUNT_DECIMALS)
amount = serializers.DecimalField(AMOUNT_MAX_DIGITS, AMOUNT_DECIMALS)
total = serializers.DecimalField(AMOUNT_MAX_DIGITS, AMOUNT_DECIMALS)
class BillingAddressSerializer(serializers.ModelSerializer):
class Meta:
model = BillingAddress
2020-05-07 13:38:49 +00:00
fields = ['uuid', 'organization', 'name', 'street', 'city', 'postal_code', 'country', 'vat_number']
class BillSerializer(serializers.ModelSerializer):
billing_address = BillingAddressSerializer(read_only=True)
records = BillRecordSerializer(many=True, read_only=True)
class Meta:
model = Bill
2020-05-07 13:38:49 +00:00
fields = ['uuid', 'reference', 'owner', 'amount', 'vat_amount', 'total',
'due_date', 'creation_date', 'starting_date', 'ending_date',
'records', 'final', 'billing_address']
2020-04-15 13:17:38 +00:00
# We do not want users to mutate the country / VAT number of an address, as it
# will change VAT on existing bills.
class UpdateBillingAddressSerializer(serializers.ModelSerializer):
class Meta:
model = BillingAddress
fields = ['uuid', 'street', 'city', 'postal_code']