ushka.templating.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.
In this module
class StrEnum | Minimal backport of :class:enum.StrEnum. |
class Category | Represents the category or type of a flash message. |
def flash() | Stores a flash message in the user's session. |
def get_flashed_messages() | Pops and retrieves all flashed messages from the session. |
class StrEnum (str, Enum)
Minimal backport of :class:enum.StrEnum.
class Category (StrEnum)
Represents the category or type of a flash message.
Attributes
| Name | Type | Description |
|---|---|---|
SUCCESS | str | Indicates a successful operation ("success"). |
DANGER | str | Indicates a critical error or danger ("danger"). |
WARNING | str | Indicates a potential issue or warning ("warning"). |
INFO | str | Provides general information ("info"). |
def flash
def flash(
request: Request,
message: str,
category: Category | str = Category.INFO,
) -> NoneStores 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
| Name | Type | Description |
|---|---|---|
request | Request | The incoming HTTP request object, which provides access to the session. |
message | str | The content of the message to be flashed. |
category | Category or str | The category of the message (e.g., 'info', 'danger', 'success', 'warning').
Defaults to |
Returns
| Type | Description |
|---|---|
None |
def get_flashed_messages
def get_flashed_messages(
request: Request,
with_categories: bool = False,
) -> list[str] | list[tuple[str, str]]Pops and retrieves all flashed messages from the session.
Parameters
| Name | Type | Description |
|---|---|---|
request | Request | The request object containing the session data. |
with_categories | bool | If True, returns a list of (category, message) tuples. If False, returns only a list of messages. Defaults to False. |
Returns
| Type | Description |
|---|---|
list of str or list of tuple of (str, str) | The list of flashed messages. The messages are removed from the session after retrieval. |