BloomERP

Create a custom dataview action

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

Count matching records

Add example-count-records to ModelViewSettings.dataview_actions to count the records in the active search and filters.

python
from django.http import HttpResponse

from bloomerp.actions import DATAVIEW_ACTION_REGISTRY
from bloomerp.actions.definition import DataviewActionContext, DataviewActionDefinition
from bloomerp.utils.requests import render_message


def count_records(context: DataviewActionContext) -> HttpResponse:
    '''Report the number of accessible records matching the current view.'''
    count = context.queryset.count()
    return render_message(context.request, f"{count} matching records", "info")


def count_button_attrs(context: DataviewActionContext) -> dict[str, str]:
    '''Display the count in the action response area.'''
    return {"hx-target": "#object-actions-target"}


show_count = DataviewActionDefinition(
    id="example-count-records",
    label="Count records",
    description="Count records matching the active dataview.",
    execute=count_records,
    get_button_attrs=count_button_attrs,
)
DATAVIEW_ACTION_REGISTRY.register(show_count.id, show_count)

Render a toolbar caption

Use render for a toolbar item that displays custom markup. This example scopes the caption to Todo dataviews.

python
from collections.abc import Mapping
from typing import Any

from django.utils.html import format_html

from bloomerp.actions import DATAVIEW_ACTION_REGISTRY
from bloomerp.actions.definition import DataviewActionContext, DataviewActionDefinition
from bloomerp.models.project_management.todo import Todo


def render_caption(context: DataviewActionContext, page_context: Mapping[str, Any]) -> str:
    '''Display the collection name as an escaped HTML fragment.'''
    return format_html("<span>Showing {}</span>", context.model._meta.verbose_name_plural)


caption = DataviewActionDefinition(
    id="example-toolbar-caption",
    models=Todo,
    label="Collection name",
    description="Show the current model's plural name in the toolbar.",
    render=render_caption,
)
DATAVIEW_ACTION_REGISTRY.register(caption.id, caption)