uncloud/uncloud_auth/serializers.py

26 lines
864 B
Python
Raw Permalink Normal View History

2020-03-18 13:36:40 +00:00
from django.contrib.auth import get_user_model
from rest_framework import serializers
from uncloud_pay import AMOUNT_DECIMALS, AMOUNT_MAX_DIGITS
from uncloud_pay.models import BillingAddress
2020-03-18 13:36:40 +00:00
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = get_user_model()
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
class ImportUserSerializer(serializers.Serializer):
username = serializers.CharField()