Skip to content

User settings

PERSEUS frontend fetches available user settings from the PERSEUS service Settings. The user settings are represented by the UserSetting and UserSettingValue classes.

The function perseus.utils.add_user_setting() helps to add new settings available for the users.

The function expects an identifier, label and a FrontendFormElement. Click here to see a list of available frontend forms.

The example below creates a new input field EmailInput in the user settings.

from perseus.utils import add_user_setting
from perseus.frontend_form import EmailInput
add_user_setting(
f"EMAIL_NOTIFICATIONS",
"Email for notifications",
EmailInput(
identifier=f"EMAIL_NOTIFICATIONS",
label="Email for notifications",
value=None,
),
)

You can access the value of a specific setting by using the DatabaseManager.

from perseus.datamanger import DatabaseManager, UserSettingValue
db_manager = DatabaseManager()
# fetch the value of the setting EMAIL_NOTIFICATIONS for all users
settings = db_manager.get_items(
UserSettingValue,
search_filter={"setting_id": "EMAIL_NOTIFICATIONS"}
)
# fetch all settings for user 'user123'
settings = db_manager.get_items(
UserSettingValue,
search_filter={"user": "user123"}
)
# fetch the value of the setting EMAIL_NOTIFICATIONS for user 'user123'
settings = db_manager.get_item(
UserSettingValue,
search_filter={"setting_id": "EMAIL_NOTIFICATIONS", "user": "user123"}
)

The UserSettingValue class is equipped with the following properties:

@dataclass(kw_only=True)
class UserSettingValue(DatabaseItem):
"""
This class represents the value of a certain setting for a certain user.
:param setting_id: The id of the setting
:type setting_id: str
:param user: The user
:type user: str
:param value: The value
:type value: str
"""
setting_id: str
user: str
value: str | float | int | bool | None = None