Skip to content

models

A module that contains models of the users package.

Classes
  • AuthUser: A class that creating user model.

AuthUser

Bases: models.Model

Model for authenticated users.

Attributes:

Name Type Description
user_id models.UUIDField

id of the user

email models.EmailField

the email of the user

username models.CharField

the username of the user

password models.CharField

the password of the user

token models.CharField

the token of the user

created_at models.DateTimeField

the datetime when user is created

updated_at models.DateTimeField

the updated datatime when updating the user

Methods
  • set_password(raw_password): A method to set user password to hash password
  • check_password(new_password): A method to check if the password is correct or not
  • set_token(email): A method to set user token
Source code in backend/api/users/models.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
class AuthUser(models.Model):
    """Model for authenticated users.

    Attributes:
        user_id: id of the user
        email: the email of the user
        username: the username of the user
        password: the password of the user
        token: the token of the user
        created_at: the datetime when user is created
        updated_at: the updated datatime when updating the user

    Methods:
        - `set_password(raw_password)`: A method to set user password to hash password
        - `check_password(new_password)`: A method to check if the password is correct or not
        - `set_token(email)`: A method to set user token
    """

    class Meta:
        """Metaclass for AuthUser model.

        Attributes:
            db_table: the table name
        """

        db_table: str = "api_auth_users"

    user_id: models.UUIDField = models.UUIDField(primary_key=True, default=uuid4, editable=False, db_index=True)

    email: models.EmailField = models.EmailField(max_length=255, db_index=True, null=False, blank=False, unique=True)
    username: models.CharField = models.CharField(max_length=50, db_index=True, null=False, blank=False, unique=True)
    password: models.CharField = models.CharField(max_length=255, null=False, blank=False)
    token: models.CharField = models.CharField(max_length=255, null=True, blank=True)

    created_at: models.DateTimeField = models.DateTimeField(auto_now_add=True)
    updated_at: models.DateTimeField = models.DateTimeField(auto_now=True, null=True)

    def set_password(self, raw_password: str) -> None:
        """Set password for the user.

        Args:
            raw_password: The raw password entered by the user.
        """
        self.password = make_password(raw_password)

    def check_password(self, new_password: str) -> bool:
        """Check if the entered password is correct.

        Args:
            new_password: The new password entered by the user.

        Returns:
            True if the password is correct, else False.
        """
        return check_password(new_password, self.password)

    def set_token(self, email: str) -> None:
        """Generate and set token for the user.

        Args:
            email: Email of the user.
        """
        self.token = generate_token(email, settings.SECRET_KEY)

Meta

Metaclass for AuthUser model.

Attributes:

Name Type Description
db_table str

the table name

Source code in backend/api/users/models.py
35
36
37
38
39
40
41
42
class Meta:
    """Metaclass for AuthUser model.

    Attributes:
        db_table: the table name
    """

    db_table: str = "api_auth_users"

check_password(new_password)

Check if the entered password is correct.

Parameters:

Name Type Description Default
new_password str

The new password entered by the user.

required

Returns:

Type Description
bool

True if the password is correct, else False.

Source code in backend/api/users/models.py
62
63
64
65
66
67
68
69
70
71
def check_password(self, new_password: str) -> bool:
    """Check if the entered password is correct.

    Args:
        new_password: The new password entered by the user.

    Returns:
        True if the password is correct, else False.
    """
    return check_password(new_password, self.password)

set_password(raw_password)

Set password for the user.

Parameters:

Name Type Description Default
raw_password str

The raw password entered by the user.

required
Source code in backend/api/users/models.py
54
55
56
57
58
59
60
def set_password(self, raw_password: str) -> None:
    """Set password for the user.

    Args:
        raw_password: The raw password entered by the user.
    """
    self.password = make_password(raw_password)

set_token(email)

Generate and set token for the user.

Parameters:

Name Type Description Default
email str

Email of the user.

required
Source code in backend/api/users/models.py
73
74
75
76
77
78
79
def set_token(self, email: str) -> None:
    """Generate and set token for the user.

    Args:
        email: Email of the user.
    """
    self.token = generate_token(email, settings.SECRET_KEY)