Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SignupByEmailSerializer(serializers.ModelSerializer):
- def create(self, validated_data):
- def __generate_username():
- username = get_random_string(
- length=8
- )
- try:
- SwishUser.objects.get(
- username=username
- )
- return __generate_username()
- except SwishUser.DoesNotExist:
- return username
- user = SwishUser(
- email=validated_data['email'],
- username=__generate_username()
- )
- user.set_password(validated_data['password'])
- user.save()
- return user
- def validate(self, attrs):
- user = SwishUser(**attrs)
- password = attrs.get('password')
- errors = dict()
- try:
- password_validation.validate_password(
- password=password,
- user=user,
- password_validators=[
- CommonPasswordValidator(),
- MinimumLengthValidator(min_length=4)
- ]
- )
- except exceptions.ValidationError as e:
- errors['password'] = list(e.messages)
- if errors:
- raise serializers.ValidationError(errors)
- return super(SignupByEmailSerializer, self).validate(attrs)
- class Meta:
- model = SwishUser
- fields = ('email', 'password')
- fields = ('email',)
- extra_kwargs = {
- 'password': {
- 'write_only': True
- 'email': {
- 'required': True,
- 'allow_null': False,
- 'allow_blank': False
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement