Guest User

Untitled

a guest
Feb 10th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. class UserSerializer(serializers.ModelSerializer):
  2.  
  3. confirm_password = serializers.CharField(max_length=50, required=True, validators=[password_check], write_only=True)
  4. password = serializers.CharField(max_length=50, required=True, validators=[password_check])
  5. Id = serializers.CharField(max_length=100, required=False, allow_blank=True)
  6. user_type = serializers.CharField(max_length=30, required=True, write_only=True)
  7. email = serializers.EmailField(required=True)
  8.  
  9. def validate_username(self, username):
  10. if User.objects.filter(username=username):
  11. raise serializers.ValidationError('Username already chosen!')
  12.  
  13. if len(username) < 8:
  14. raise serializers.ValidationError('Username min length is 8')
  15. return username
  16.  
  17. def validate_email(self, email):
  18. try:
  19. validate_email(email)
  20. except ValidationError:
  21. raise serializers.ValidationError('Enter a valid email')
  22.  
  23. if User.objects.filter(email=email):
  24. raise serializers.ValidationError('Email already chosen!')
  25. return email
  26.  
  27. def validate(self, attrs):
  28. key = 0
  29. try:
  30. if models.Profile.objects.filter(userid=attrs['Id']).exists() and attrs['Id'] != '':
  31. raise serializers.ValidationError('UserId already chosen')
  32. if attrs['Id'] == '':
  33. key=1
  34. except KeyError:
  35. key=1
  36.  
  37. if key == 1:
  38. if attrs['user_type'] != 'Admin' and attrs['user_type'] != 'AdminCoordinate':
  39. raise serializers.ValidationError("UserId can't be empty!")
  40. else:
  41. attrs['user_id'] = ''
  42.  
  43. if attrs['password'] != attrs['confirm_password']:
  44. raise serializers.ValidationError('Enter same passwords both the times!')
  45. return attrs
  46.  
  47. class Meta:
  48. model = User
  49. fields = ('pk', 'username', 'password', 'email', 'confirm_password', 'Id', 'user_type')
  50. read_only_fields = ('pk',)
Add Comment
Please, Sign In to add comment