Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from django.utils.translation import ugettext_lazy as _
- from django.contrib.auth import get_user_model
- from rest_framework import serializers
- class AuthTokenByEmailSerializer(serializers.Serializer):
- email = serializers.EmailField()
- password = serializers.CharField()
- def validate(self, attrs):
- email = attrs.get('email')
- password = attrs.get('password')
- if email and password:
- try:
- user = get_user_model().objects.get(email=email)
- is_password_correct = user.check_password(password)
- if not is_password_correct:
- msg = _('Unable to login with provided credentials.')
- raise serializers.ValidationError(msg)
- if not user.is_active:
- msg = _('User account is disabled.')
- raise serializers.ValidationError(msg)
- attrs['user'] = user
- return attrs
- except get_user_model().DoesNotExist:
- msg = _('Unable to login with provided credentials.')
- raise serializers.ValidationError(msg)
- else:
- msg = _('Must include "email" and "password"')
- raise serializers.ValidationError(msg)
Add Comment
Please, Sign In to add comment