app/users/serializers.py
from rest_framework import serializers
from django.contrib.auth import get_user_model, authenticate
from django.utils.translation import ugettext_lazy as _
User = get_user_model()
class UserSerializer(serializers.ModelSerializer):
"""Serializer for the user object"""
class Meta:
model = User
fields = ('email', 'password', 'name')
extra_kwargs = {
'password': {
'write_only': True,
'min_length': 5
}
}
def update(self, instance, validated_data):
"""Update the authenticated user and sets the password correctly"""
# Remove the password to set using the set_password method
password = validated_data.pop('password', None)
user = super().update(instance, validated_data)
if password:
instance.set_password(password)
user.save()
return user
def create(self, validated_data):
"""Create a new user with encrypted password and return it"""
return User.objects.create_user(**validated_data)
class AuthTokenSerializer(serializers.Serializer):
"""Serializer for the user authentication token"""
email = serializers.CharField()
password = serializers.CharField(
style={'input_type': 'password'},
trim_whitespace=False,
)
def validate(self, attrs):
"""Validate and authenticate the user"""
email = attrs.get('email')
password = attrs.get('password')
user = authenticate(
request=self.context.get('request'),
username=email,
password=password,
)
if not user:
msg = _('Unable to authenticate with provided credentials')
raise serializers.ValidationError(msg, code='authentication')
attrs['user'] = user
return attrs