Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def validate(self, data):
- email = data.get('email', None)
- username = data.get('username', None)
- password = data.get('password', None)
- if email is None and username is None:
- raise serializers.ValidationError(
- error_messages['field required'].format('Email or Username'))
- if password is None:
- raise serializers.ValidationError(
- error_messages['field required'].format('Password'))
- if username is not None:
- try:
- user_object = User.objects.get(
- username=username.strip().lower())
- email = user_object.email
- except User.DoesNotExist:
- raise serializers.ValidationError(
- 'A user with this username and password was not found.')
- user = authenticate(username=email, password=password)
- if user is None:
- raise serializers.ValidationError(
- 'A user with this email and password was not found.')
- if not user.is_active:
- raise serializers.ValidationError(
- 'This user has been deactivated.')
- return {
- 'email': user.email,
- 'username': user.username,
- 'token': user.token}
Add Comment
Please, Sign In to add comment