Utils

Generals

modules.utils.generals.check_password(to_check: str, hashed: str) bool[source]

Check a string password with the hashed password.

Parameters:
  • to_check (str) – The string password.

  • hashed (str) – The hashed password.

Returns:

True if is checked, else False.

Return type:

bool

modules.utils.generals.creation_date() str[source]

Give the actual date.

Returns:

Actual date in format ISO 8601.

Return type:

str

modules.utils.generals.encode_password(password: str) str[source]

Encode a password.

Parameters:

password (str) – The password to encode.

Returns:

The encoded password.

Return type:

str

modules.utils.generals.is_valid_email(email: str) bool[source]

Check if is a valid email.

Conditions: - An standart email direction (example@email.com)

Parameters:

email (str) – Email to validate.

Returns:

True if is valid, else False.

Return type:

bool

modules.utils.generals.is_valid_image(url: str) bool[source]

Check if is a valid image url.

Conditions: - It can be with or without ‘http://’ or ‘https://’ - Valid domain and optional route. - It can be a JPEG, PNG or GIF.

Parameters:

url (str) – Url with image to validate.

Returns:

True if is valid, else False.

Return type:

bool

modules.utils.generals.is_valid_password(password: str) bool[source]

Check if is a valid password.

Conditions: - Almost 8 characters. - Almost one uppercase. - Almost one lowecase. - Almost one number.

Parameters:

password (str) – Password to validate.

Returns:

True if is valid, else False.

Return type:

bool

modules.utils.generals.is_valid_username(username: str) bool[source]

Check if is a valid username.

Conditions: - It can have letters (upper or lowercase), numbers, dots and underscores. - It can’t end with a dot.

Parameters:

username (str) – Username to validate.

Returns:

True if is valid, else False.

Return type:

bool

modules.utils.generals.logify(exclude: list | None = None) str[source]

Make a log with the function´s args excluding someones.

Parameters:

exclude (Optional[list], optional) – Args to exculde. Defaults to None.

Returns:

Formatted log.

Return type:

str

Exceptions

exception modules.utils.exceptions.AlreadyInUse(error: str | None = None)[source]

Bases: CustomException

Custom exception for already in use.

default_data: str = 'Already in use'
default_error: str = 'Username or email'
exception modules.utils.exceptions.CustomException(data: str = 'Error', error: str | None = None, log: str | None = None, code: int = 500)[source]

Bases: Exception

Base class for custom exceptions with auto-logging

exception modules.utils.exceptions.GeneralError(data: str = 'Error', error: Exception | None = None, log: str | None = None, code: int = 500)[source]

Bases: CustomException

Custom exception for an exception.

default_error: str = 'Error desconocido'
exception modules.utils.exceptions.IncorrectCredential(user_id: ObjectId | None = None)[source]

Bases: CustomException

Custom exception for incorrect credential.

exception modules.utils.exceptions.InvalidCredential(error: str | None = None)[source]

Bases: CustomException

Custom exception for invalid credentaial.

default_data: str = 'Invalid credentials'
default_error: str = 'Username, email or password'
exception modules.utils.exceptions.NotFound(error: str | None = None)[source]

Bases: CustomException

Custom exception for not found.

default_data: str = 'Not Found'
default_error: str = 'Object'
exception modules.utils.exceptions.Unauthorized(error: str | None = None)[source]

Bases: CustomException

Custom exception for unauthorized.

default_data: str = 'Unauthorized'
default_error: str = 'Unauthorized'

Log

class modules.utils.log.Log[source]

Bases: object

Control the backend’s log.

Parameters:

__LOG_FILE_PATH (str) – The path of the log file.

classmethod error(message: str, **kwargs: Any) None[source]

Register an error in the log.

classmethod info(message: str, **kwargs: Any) None[source]

Register an info in the log.

logger: Logger = <Logger API_Logger (INFO)>
classmethod warning(message: str, **kwargs: Any) None[source]

Register an warning in the log.

Token

class modules.utils.token.Token[source]

Bases: object

Controls the JWT in the app.

Args: __JWT_SECRET_KEY (str): The key to hash the JWT. __JWT_ALGORITHM (str): The JWT algorithm. Defaults to “HS256”. __JWT_EXPIRATION (int): The valid time of the token.

classmethod encode(user_id: ObjectId, username: str) str[source]

Encode a token.

Parameters:
  • user_id (ObjectId) – The user’s ID.

  • username (str) – The user’s username.

Raises:

RuntimeError – If it can’t be encoded.

Returns:

The JWT token.

Return type:

str

classmethod is_valid(token: str) Dict[str, Any][source]

Verify a JWT.

Parameters:

token (str) – The token to verify.

Raises:

RuntimeError – If the token can’t be validate.

Returns:

{“verify”: True, “user”: (token info)} or {“verify”: False, “error”: (error)}

Return type:

Dict[str, Any]