Advertisement
blohinn

Untitled

Feb 5th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. class SignupByEmailSerializer(serializers.ModelSerializer):
  2. def create(self, validated_data):
  3. def __generate_username():
  4. username = get_random_string(
  5. length=8
  6. )
  7. try:
  8. SwishUser.objects.get(
  9. username=username
  10. )
  11. return __generate_username()
  12. except SwishUser.DoesNotExist:
  13. return username
  14.  
  15. user = SwishUser(
  16. email=validated_data['email'],
  17. username=__generate_username()
  18. )
  19. user.set_password(validated_data['password'])
  20.  
  21. user.save()
  22. return user
  23.  
  24. def validate(self, attrs):
  25. user = SwishUser(**attrs)
  26.  
  27. password = attrs.get('password')
  28.  
  29. errors = dict()
  30. try:
  31. password_validation.validate_password(
  32. password=password,
  33. user=user,
  34. password_validators=[
  35. CommonPasswordValidator(),
  36. MinimumLengthValidator(min_length=4)
  37. ]
  38. )
  39. except exceptions.ValidationError as e:
  40. errors['password'] = list(e.messages)
  41.  
  42. if errors:
  43. raise serializers.ValidationError(errors)
  44.  
  45. return super(SignupByEmailSerializer, self).validate(attrs)
  46.  
  47. class Meta:
  48. model = SwishUser
  49. fields = ('email', 'password')
  50. fields = ('email',)
  51. extra_kwargs = {
  52. 'password': {
  53. 'write_only': True
  54. 'email': {
  55. 'required': True,
  56. 'allow_null': False,
  57. 'allow_blank': False
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement