Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. class UserLoginSerializer(ModelSerializer):
  2. token = CharField(allow_blank=True, read_only=True)
  3. username = CharField(label='Código do Usuário',
  4. allow_blank=True, required=False)
  5.  
  6. class Meta:
  7. model = User
  8. fields = ['username', 'password', 'token']
  9. extra_kwargs = {'password':
  10. {'write_only': True
  11. }
  12. }
  13.  
  14. def validate(self, data):
  15. user_obj = None
  16. username = data.get('username', None)
  17. password = data['password']
  18. if not username:
  19. raise ValidationError('Insira o Código de Usuário!')
  20.  
  21. user = User.objects.filter(
  22. Q(username=username)
  23. ).distinct()
  24. if user.exists() and user.count() == 1:
  25. user_obj = user.first()
  26. else:
  27. raise ValidationError('Esse Código de Usuário não é válido!')
  28.  
  29. if user_obj:
  30. if not user_obj.check_password(password):
  31. raise ValidationError('Credenciais Incorretas!')
  32.  
  33. data['token'] = 'Some token Here'
  34.  
  35. return data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement