BloomERP

Create a custom object action

See the built-in reference for the definition fields and available built-ins.

Show the record ID

Add example-show-record-id to a model's object action list.

python
from django.http import HttpResponse

from bloomerp.actions import OBJECT_ACTION_REGISTRY
from bloomerp.actions.definition import ObjectActionContext, ObjectActionDefinition
from bloomerp.utils.requests import render_message


def show_record_id(context: ObjectActionContext) -> HttpResponse:
    '''Show the primary key of the current record.'''
    return render_message(context.request, f"Record ID: {context.object.pk}", "info")


show_id = ObjectActionDefinition(
    id="example-show-record-id",
    label="Show record ID",
    description="Display the primary key of the current record.",
    execute=show_record_id,
)
OBJECT_ACTION_REGISTRY.register(show_id.id, show_id)

Open a workspace in a new tab

Set models to the model that supplies the destination URL.

python
from bloomerp.actions import OBJECT_ACTION_REGISTRY
from bloomerp.actions.definition import ObjectActionContext, ObjectActionDefinition
from bloomerp.models.workspaces import Workspace


def workspace_link(context: ObjectActionContext) -> dict[str, str]:
    '''Return the attributes for an ordinary link to the workspace.'''
    return {"href": context.object.get_absolute_url(), "target": "_blank", "rel": "noopener"}


open_workspace = ObjectActionDefinition(
    id="example-open-workspace-tab",
    label="Open in new tab",
    description="Open this workspace in a new browser tab.",
    models=Workspace,
    get_button_attrs=workspace_link,
)
OBJECT_ACTION_REGISTRY.register(open_workspace.id, open_workspace)