Utils

Module contents

This package provides utility functions and helper modules that support common or reusable tasks across the project.

Submodules

Functions module

back_chat.utils.functions.add_user(user_: UserSchema) str[source]

This function adds a user to the database. It first checks if the user’s city is unknown and attempts to fetch it using an external API. If the user does not already exist in the database, it creates a new user entry; otherwise, it returns a message indicating the user already exists.

Parameters:

user – An instance of UserSchema containing user details such as name, postal_code, and city.

Returns:

A string message indicating whether the user was added to the database or already exists.

async back_chat.utils.functions.save_file(file: UploadFile)[source]

Asynchronously saves an uploaded file to disk in chunks and notifies connected clients.

The file is saved in the SAVE_FOLDER directory, and any spaces in the filename are replaced with hyphens. The file is written in append-binary mode in case it is uploaded in chunks. After saving, a broadcast message is sent to all connected WebSocket clients to notify them of the new file.

Parameters:

file – The uploaded file to be saved (FastAPI’s UploadFile).

back_chat.utils.functions.update_user(user_: ApiUser, user_update: UserSchema) ApiUser[source]

This function updates an existing ApiUser instance with new data provided in a UserSchema instance and returns the updated ApiUser

Parameters:
  • user – An instance of ApiUser representing the user to be updated.

  • user_update – An instance of UserSchema containing the new data for the user.

Returns:

An updated ApiUser instance with the new data applied.

Logger_api module

Custom logging module with colored console output and rotating file logging.

This module defines a custom logger (LoggerApi) that logs messages to both the console (with colored output for different log levels) and to daily rotating log files. It is useful for debugging and monitoring asynchronous applications.

Classes: - ColoredFormatter: Applies color formatting to log messages based on log level. - LoggerApi: Custom logger class with console and file handlers.

class back_chat.utils.logger_api.ColoredFormatter(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)[source]

Bases: Formatter

Custom log formatter that adds color to log levels for console output.

format(record: LogRecord) str[source]

Format the log message with color based on the log level.

Parameters:

record – The log record to format.

Returns:

The color-formatted log message as a string.

class back_chat.utils.logger_api.LoggerApi(name: str = None, level: int = 10)[source]

Bases: Logger

Custom logger that logs to both console (with color) and rotating log files.

Parameters:
  • name – Name of the logger (used as log file name as well).

  • level – Logging level (default: DEBUG).