Guest User

Untitled

a guest
Jun 24th, 2016
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. from django.utils.translation import ugettext_lazy as _
  2. from django.contrib.auth import get_user_model
  3. from rest_framework import serializers
  4.  
  5.  
  6. class AuthTokenByEmailSerializer(serializers.Serializer):
  7. email = serializers.EmailField()
  8. password = serializers.CharField()
  9.  
  10. def validate(self, attrs):
  11. email = attrs.get('email')
  12. password = attrs.get('password')
  13.  
  14. if email and password:
  15. try:
  16. user = get_user_model().objects.get(email=email)
  17. is_password_correct = user.check_password(password)
  18. if not is_password_correct:
  19. msg = _('Unable to login with provided credentials.')
  20. raise serializers.ValidationError(msg)
  21.  
  22. if not user.is_active:
  23. msg = _('User account is disabled.')
  24. raise serializers.ValidationError(msg)
  25.  
  26. attrs['user'] = user
  27. return attrs
  28. except get_user_model().DoesNotExist:
  29. msg = _('Unable to login with provided credentials.')
  30. raise serializers.ValidationError(msg)
  31. else:
  32. msg = _('Must include "email" and "password"')
  33. raise serializers.ValidationError(msg)
Add Comment
Please, Sign In to add comment