def get_fields(self): fields = super().get_fields() if self.request and self.request.method.upper() == 'POST': fields['password1'] = PasswordField(max_length=20, min_length=5) fields['password2'] = PasswordField(max_length=20, min_length=5) fields['referral_code'] = serializers.CharField( max_length=10, allow_null=True, required=False, allow_blank=True ) return fields
class PasswordChangeSerializer(DummySerializer): password1 = PasswordField(max_length=20, min_length=5) password2 = PasswordField(max_length=20, min_length=5) def validate(self, attrs): password1 = attrs.get('password1') password2 = attrs.get('password2') if password1 != password2: raise serializers.ValidationError(_( 'Both Password must be same' )) return super().validate(attrs)
class LoginSerializer(serializers.Serializer): """ Stores the username and password for logging a user in """ user = None username = serializers.CharField() password = PasswordField() def validate(self, data): """ Authenticate the username and password to see if they are valid :param data: the data to check for validity :return: the data if it is valid :raise ValidationError: if the username and password do not match """ request = self.context.get('request') user = authenticate(request, username=data.get('username'), password=data.get('password')) if user is not None: self.user = user else: raise serializers.ValidationError('Invalid credentials provided.') return data
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields[self.username_field] = serializers.CharField() self.fields['password'] = PasswordField() # override start (\( ⁰⊖⁰)/) self.fields['app_id'] = serializers.CharField()
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields[self.username_field] = serializers.CharField() self.fields['password'] = PasswordField() self.fields[self.identifier_field] = serializers.ChoiceField( choices=self.identifier_choices )
class CustomTokenObtainSerializer(TokenObtainPairSerializer): """Кастомизация полей для генератора апи.""" password = PasswordField(write_only=True) refresh = serializers.CharField(read_only=True) access = serializers.CharField(read_only=True) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields[self.username_field] = serializers.CharField( required=False, write_only=True)
class UserSerializer(serializers.ModelSerializer): password = PasswordField() class Meta: model = User fields = ['username', 'password'] extra_kwargs = {'password': {'write_only': True}} def validate_password(self, password): if validate_password(password) is None: return make_password(password)
class EmailRegistrationSerializer(serializers.ModelSerializer): """ YamdbUser specific serializer for registration page """ password = PasswordField(required=False) username_field = 'email' class Meta: model = YamdbUser fields = [ 'email', 'password', ]
class RegisterSerializer(ModelSerializer): password = PasswordField(write_only=True) class Meta: model = User fields = ('id', 'username', 'password') def validate(self, attr): validate_password(attr["password"]) return attr def create(self, validated_data): user = User.objects.create(username=validated_data['username']) user.set_password(validated_data['password']) user.save() return user
class LoginSerializer(Serializer): username = fields.EmailField() password = PasswordField() @classmethod def get_token(cls, user): return RefreshToken.for_user(user) def validate(self, attrs: dict): params = {"email": attrs["username"], "password": attrs["password"]} params["request"] = self.context["request"] user = authenticate(**params) if not user: raise AuthenticationFailed("Invalid email or password") refresh = self.get_token(user) data = {} # data['refresh'] = str(refresh) data["access_token"] = str(refresh.access_token) return data
class UserCreateSerializer(ModelSerializer): first_name = CharField() last_name = CharField() email = EmailField() password = PasswordField(min_length=8, required=True, ) class Meta: model = User fields = ( "first_name", "last_name", "username", "email", "password", ) @staticmethod def validate_email(value): user = User.objects.filter(email=value) if user.exists(): raise ValidationError("This email is already taken.") return value def create(self, validated_data) -> User: first_name = validated_data["first_name"] last_name = validated_data["last_name"] username = validated_data["username"] email = validated_data["email"] password = validated_data["password"] user = User(first_name=first_name, last_name=last_name, username=username, email=email) user.set_password(password) user.save() return user
def __init__(self, *args, **kwargs): # 3 super().__init__(*args, **kwargs) self.fields[self.username_field] = CharField() self.fields['password'] = PasswordField()
def __init__(self, *args, **kwargs): super(CustomTokenObtainSerializer, self).__init__(*args, **kwargs) self.fields[self.email_or_username_field] = serializers.CharField() self.fields['password'] = PasswordField()
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields[self.username_field] = serializers.CharField() self.fields["password"] = PasswordField()
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields['email'] = serializers.CharField() self.fields['password'] = PasswordField()
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields['email'] = serializers.EmailField(write_only=True) self.fields['password'] = PasswordField(write_only=True) self.fields['access'] = serializers.CharField(required=False, read_only=True) self.fields['refresh'] = serializers.CharField(required=False, read_only=True)
class RegisterSerializer(Serializer): first_name = fields.CharField(max_length=150) last_name = fields.CharField(max_length=150) email = fields.EmailField() password = PasswordField(min_length=8, max_length=150)
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields[self.username_field] = serializers.CharField(required=False) self.fields['email'] = serializers.CharField(required=False) self.fields['password'] = PasswordField()
class ResetPasswordSerializer(Serializer): password = PasswordField(min_length=8, max_length=150)
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.initial_data.update(self.context['request'].META) self.fields[self.username_field] = serializers.CharField( required=False) self.fields['password'] = PasswordField(required=False)
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields.clear() self.fields[self.profile_id_field] = CharField() self.fields[self.password_field] = PasswordField()