2020-03-18 13:36:40 +00:00
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
from rest_framework import serializers
|
2020-03-18 13:53:26 +00:00
|
|
|
|
2020-05-08 08:56:03 +00:00
|
|
|
from uncloud_pay import AMOUNT_DECIMALS, AMOUNT_MAX_DIGITS
|
2020-05-10 19:47:44 +00:00
|
|
|
from uncloud_pay.models import BillingAddress
|
2020-03-18 13:36:40 +00:00
|
|
|
|
|
|
|
class UserSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = get_user_model()
|
2020-05-10 19:47:44 +00:00
|
|
|
read_only_fields = [ 'username', 'balance', 'maximum_credit' ]
|
|
|
|
fields = read_only_fields + [ 'email', 'primary_billing_address' ]
|
|
|
|
|
|
|
|
def validate(self, data):
|
|
|
|
"""
|
|
|
|
Ensure that the primary billing address belongs to the user
|
|
|
|
"""
|
|
|
|
|
|
|
|
if 'primary_billing_address' in data:
|
|
|
|
if not data['primary_billing_address'].owner == self.instance:
|
|
|
|
raise serializers.ValidationError("Invalid data")
|
|
|
|
|
|
|
|
return data
|
2020-03-18 13:36:40 +00:00
|
|
|
|
2020-04-18 13:11:02 +00:00
|
|
|
class ImportUserSerializer(serializers.Serializer):
|
|
|
|
username = serializers.CharField()
|