Guest User

Untitled

a guest
Jul 31st, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. from django.utils.translation import ugettext_lazy as _
  2.  
  3. from rest_framework import serializers
  4. from rest_framework.compat import authenticate
  5.  
  6.  
  7. class AuthTokenSerializer(serializers.Serializer):
  8. email = serializers.CharField(label=_("Email"))
  9. password = serializers.CharField(
  10. label=_("Password"),
  11. style={'input_type': 'password'},
  12. trim_whitespace=False
  13. )
  14.  
  15. def validate(self, attrs):
  16. email = attrs.get('email')
  17. password = attrs.get('password')
  18.  
  19. if email and password:
  20. user = authenticate(request=self.context.get('request'),
  21. email=email, password=password)
  22.  
  23. # The authenticate call simply returns None for is_active=False
  24. # users. (Assuming the default ModelBackend authentication
  25. # backend.)
  26. if not user:
  27. msg = _('Unable to log in with provided credentials.')
  28. raise serializers.ValidationError(msg, code='authorization')
  29. else:
  30. msg = _('Must include "email" and "password".')
  31. raise serializers.ValidationError(msg, code='authorization')
  32.  
  33. attrs['user'] = user
  34. return attrs
Add Comment
Please, Sign In to add comment