Guest User

Untitled

a guest
Feb 1st, 2019
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. def validate(self, data):
  2. email = data.get('email', None)
  3. username = data.get('username', None)
  4. password = data.get('password', None)
  5. if email is None and username is None:
  6. raise serializers.ValidationError(
  7. error_messages['field required'].format('Email or Username'))
  8. if password is None:
  9. raise serializers.ValidationError(
  10. error_messages['field required'].format('Password'))
  11. if username is not None:
  12. try:
  13. user_object = User.objects.get(
  14. username=username.strip().lower())
  15. email = user_object.email
  16. except User.DoesNotExist:
  17. raise serializers.ValidationError(
  18. 'A user with this username and password was not found.')
  19. user = authenticate(username=email, password=password)
  20. if user is None:
  21. raise serializers.ValidationError(
  22. 'A user with this email and password was not found.')
  23. if not user.is_active:
  24. raise serializers.ValidationError(
  25. 'This user has been deactivated.')
  26. return {
  27. 'email': user.email,
  28. 'username': user.username,
  29. 'token': user.token}
Add Comment
Please, Sign In to add comment