Ushka

API reference

ushka.utils.flash

This module provides "flash messaging" functionality for the Ushka framework, allowing messages to be stored in the user's session and retrieved for display on the next request.

It includes a Category enum for message types and functions to store (flash) and retrieve (get_flashed_messages) these messages.

class StrEnum(str, Enum)

Minimal backport of :class:enum.StrEnum.

class Category(StrEnum)

Represents the category or type of a flash message.

Attributes

SUCCESSstr

Indicates a successful operation ("success").

DANGERstr

Indicates a critical error or danger ("danger").

WARNINGstr

Indicates a potential issue or warning ("warning").

INFOstr

Provides general information ("info").

def flash(request: Request, message: str, category: Category | str = Category.INFO) -> None

Stores a flash message in the user's session.

These messages are typically retrieved and displayed on a subsequent request, then automatically removed from the session.

Parameters

requestRequest

The incoming HTTP request object, which provides access to the session.

messagestr

The content of the message to be flashed.

categoryCategory or str

The category of the message (e.g., 'info', 'danger', 'success', 'warning'). Defaults to Category.INFO.

Returns

None

def get_flashed_messages(request: Request, with_categories: bool = False) -> Union[List[str], List[Tuple[str, str]]]

Pops and retrieves all flashed messages from the session.

Parameters

requestRequest

The request object containing the session data.

with_categoriesbool

If True, returns a list of (category, message) tuples. If False, returns only a list of messages. Defaults to False.

Returns

list of str or list of tuple of (str, str)

The list of flashed messages. The messages are removed from the session after retrieval.