5. Helper methods
Every state inherits a number of helper methods from BaseState. This page describes the ones that are useful when
implementing your own state. All of them are class methods, so call them on your state class, e.g.
MyState.get_project_storage(project).
Store data
Section titled “Store data”States often need to remember information between two calls, e.g. the ID of an external ticket or the time a reminder
was sent. BaseState offers two kinds of storage for this purpose. Both store a dictionary that must be serializable to
the database (strings, numbers, booleans, lists and dictionaries).
Project storage
Section titled “Project storage”The project storage holds data that belongs to one project while it is in this state.
@classmethoddef pretreatment(cls, project: Project) -> bool: storage = cls.get_project_storage(project) if "ticket_id" not in storage: storage["ticket_id"] = create_ticket(project) # your own logic cls.set_project_storage(project, storage) return Trueget_project_storage(project: Project) -> dict[str, Any]
Returns the storage of the state for the given project, or an empty dictionary if nothing has been stored yet.
set_project_storage(project: Project, content: dict[str, Any]) -> bool
Replaces the storage of the state for the given project with content. Returns whether saving was successful.
Persistent storage
Section titled “Persistent storage”The persistent storage holds data that belongs to the state as a whole, independent of projects, and is kept until you change it. It is suitable e.g. for counters or the time of the last synchronization with an external system.
storage = MyState.get_persistent_storage()storage["last_sync"] = datetime.now(tz=timezone.utc).isoformat()MyState.set_persistent_storage(storage)get_persistent_storage() -> dict[str, Any]
Returns the persistent storage of the state, or an empty dictionary if nothing has been stored yet.
set_persistent_storage(content: dict[str, Any]) -> bool
Replaces the persistent storage of the state with content. Returns whether saving was successful.
Services have the same two methods get_persistent_storage() and set_persistent_storage() for their own persistent
storage.
Assign tasks
Section titled “Assign tasks”Users assign tasks to themselves in the PERSEUS frontend (see State tasks). You can do the same from your code, e.g. to assign a task to the person responsible for a project automatically.
@classmethoddef pretreatment(cls, project: Project) -> bool: reviewer = find_reviewer(project) # your own logic, returns a username if reviewer is not None and cls.task_assignee(project, "CHECKING") is None: cls.assign_task(project, "CHECKING", reviewer) return Truetask_assignee(project: Project, task_id: str) -> str | None
Returns the username of the person the task is assigned to, or None if it is not assigned.
assign_task(project: Project, task_id: str, username: str) -> bool
Assigns the task to the user with the given username. Returns False if the task is already assigned to someone;
unassign it first to change the assignee.
unassign_task(project: Project, task_id: str) -> bool
Removes the assignment of the task. Returns True if the task is not assigned (anymore).
Both assign_task() and unassign_task() update the badge of the state in the menu (see below).
Menu badge
Section titled “Menu badge”The menu entry of a state shows a badge with a number. By default, this is the number of tasks of all projects in the state that are not assigned to anyone. PERSEUS updates the badges every minute and whenever a task is assigned or unassigned.
To show a different number, override get_notifications():
@classmethoddef get_notifications(cls) -> int: # only count projects that are waiting for longer than 7 days return len([project for project in cls.get_state_projects() if waiting_too_long(project)])get_notifications() -> int
Returns the number shown in the badge. 0 hides the badge.
Internal states
Section titled “Internal states”While a project is in a state, the state additionally keeps an internal state machine for it. By default, it consists
of the internal states PRETREATMENT (the pretreatment
has not been completed yet) and IN_STATE.
get_current_internal_states(project: Project) -> list[str]
Returns the active internal states of the project, e.g. ["IN_STATE"].
if "PRETREATMENT" in MyState.get_current_internal_states(project): # the project is still being prepared ...get_state_projects(exclude_pretreatment: bool = True) uses this information: by default, it only returns projects
whose pretreatment has been completed.