Skip to content

serializers

A module that contains serializers of the users package.

Classes
  • UserSerializer: A class that serializer the AuthUser model.

UserSerializer

Bases: serializers.ModelSerializer

Serializer for the AuthUser model.

Attributes:

Name Type Description
password serializers.CharField

The password of the user and it is written only

Methods
  • validate_password(password): A method to validate the user password when registering
Source code in backend/api/users/serializers.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class UserSerializer(serializers.ModelSerializer):
    """Serializer for the AuthUser model.

    Attributes:
        password: The password of the user and it is written only

    Methods:
        - `validate_password(password)`: A method to validate the user password when registering
    """

    password: serializers.CharField = serializers.CharField(write_only=True)

    class Meta:
        """Metaclass for UserSerializer.

        Attributes:
            model: The AuthUser model
            fields: A list of AuthUser fields that need to be serialized
        """

        model: AuthUser = AuthUser
        fields: list[str] = [
            "user_id",
            "email",
            "username",
            "password",
            "created_at",
            "updated_at",
        ]

    def validate_password(self, password: str) -> str:
        """Validate the entered password.

        Args:
            password: The password entered by the user.

        Raises:
            serializers.ValidationError: If the password is less than 8 characters.

        Returns:
            The validated password.

        """
        if len(password) < 8:
            raise serializers.ValidationError("Password must be more than 8 characters!")
        return password

Meta

Metaclass for UserSerializer.

Attributes:

Name Type Description
model AuthUser

The AuthUser model

fields list[str]

A list of AuthUser fields that need to be serialized

Source code in backend/api/users/serializers.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Meta:
    """Metaclass for UserSerializer.

    Attributes:
        model: The AuthUser model
        fields: A list of AuthUser fields that need to be serialized
    """

    model: AuthUser = AuthUser
    fields: list[str] = [
        "user_id",
        "email",
        "username",
        "password",
        "created_at",
        "updated_at",
    ]

validate_password(password)

Validate the entered password.

Parameters:

Name Type Description Default
password str

The password entered by the user.

required

Raises:

Type Description
serializers.ValidationError

If the password is less than 8 characters.

Returns:

Type Description
str

The validated password.

Source code in backend/api/users/serializers.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def validate_password(self, password: str) -> str:
    """Validate the entered password.

    Args:
        password: The password entered by the user.

    Raises:
        serializers.ValidationError: If the password is less than 8 characters.

    Returns:
        The validated password.

    """
    if len(password) < 8:
        raise serializers.ValidationError("Password must be more than 8 characters!")
    return password