Guest User

Untitled

a guest
Oct 31st, 2016
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. class AuthenticateSerializer(serializers.ModelSerializer):
  2. username = serializers.CharField(source='user.username')
  3. password = serializers.CharField(source='user.password', style={'input_type': 'password'})
  4. author = AuthorSerializer(allow_null=True, read_only=True)
  5. class Meta:
  6. model = User
  7. depth = 1
  8. fields = [
  9. 'username',
  10. 'password',
  11. 'author',
  12. ]
  13. extra_kwargs = {"password": {"write_only": True}}
  14.  
  15. def validate(self, attrs):
  16. validation_data = dict(attrs)['user']
  17. username = validation_data.get('username', None);
  18. password = validation_data.get('password', None);
  19. try:
  20. user = User.objects.get(username=username)
  21. except:
  22. raise ValidationError("Incorrect login/password.")
  23. if user.check_password(password):
  24. attrs['author'] = user.author
  25. return attrs
  26. raise ValidationError("Incorrect login/password.")
Add Comment
Please, Sign In to add comment