Guest User

Untitled

a guest
Jan 31st, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. path('rest-auth/registration/', RegisterUserView.as_view(), name="rest_register"),
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8. class RegisterUserView(CreateAPIView):
  9. """
  10. Create user view.
  11.  
  12. This view provide UserCreateSerializer.
  13.  
  14. """
  15.  
  16. serializer_class = UserCreateSerializer
  17.  
  18.  
  19.  
  20.  
  21. class UserCreateSerializer(serializers.ModelSerializer):
  22. """
  23. Create of user.
  24.  
  25. This class provide creation of user with two fields: email and password.
  26. """
  27.  
  28. password = serializers.CharField(write_only=True)
  29. email = serializers.EmailField(
  30. validators=[UniqueValidator(queryset=User.objects.all())]
  31. )
  32.  
  33. class Meta:
  34. """
  35. Meta class.
  36.  
  37. This class describe model and fields.
  38. """
  39.  
  40. model = User
  41. fields = ["email", "password"]
  42.  
  43. def validate(self, data):
  44. """
  45. Validate password.
  46.  
  47. Validate length of password.
  48. """
  49. user = User(**data)
  50. password = data.get('password')
  51. errors = dict()
  52. try:
  53. password_validation.validate_password(password=password, user=user)
  54. except exceptions.ValidationError as e:
  55. errors['password'] = list(e.messages)
  56. if errors:
  57. raise serializers.ValidationError(errors)
  58. return super(UserCreateSerializer, self).validate(data)
Add Comment
Please, Sign In to add comment