Skip to content

base_component

django_spire.core.components.confirmation.base_component

BaseConfirmationComponent

Bases: Component, ABC

Asks the user to confirm one action.

Subclasses perform the action in on_confirm(). The prompt is worded when the component is built, with title, message and confirm_label; one left unset keeps the template's own wording::

ArchiveConfirmationComponent(title='Archive this report?', confirm_label='Archive')

A subclass that needs markup, such as an icon, extends the template's confirmation_title, confirmation_message and confirmation_button blocks. confirmation_button holds the whole confirm button.

The component does not know what it is shown in, so it closes nothing. Whoever shows it listens for confirmed, which carries what on_confirm() returned, and for cancelled, and puts it away. Cancelling does nothing on the server, so the template raises cancelled in the browser.

What the user sees once the action is done, such as a toast, is the template's: its on_confirmed block runs in the browser after a successful confirm(), with what on_confirm() returned as result, as a form's glue_form_on_saved block does after a save.

An action that cannot be performed, because its row changed while the prompt was open say, raises ConfirmationRefusedError: confirmed is not fired, and the prompt stays open and shows the error's message.

confirm_access is the access the user needs in order to confirm.

template = 'django_spire/component/confirmation/confirmation.html' class-attribute instance-attribute

confirm_access = Glue.Access.VIEW class-attribute

title = Glue.ComponentParameter(None) class-attribute instance-attribute

message = Glue.ComponentParameter(None) class-attribute instance-attribute

confirm_label = Glue.ComponentParameter(None) class-attribute instance-attribute

cancelled = Glue.event() class-attribute instance-attribute

confirmed = Glue.event() class-attribute instance-attribute

on_confirm abstractmethod

Perform the confirmed action and return the detail of confirmed, or raise ConfirmationRefusedError to leave the prompt open with its message.

Source code in django_spire/core/components/confirmation/base_component.py
@abstractmethod
def on_confirm(self) -> dict[str, Any]:
    """
    Perform the confirmed action and return the detail of ``confirmed``, or
    raise ``ConfirmationRefusedError`` to leave the prompt open with its message.
    """

confirm

Source code in django_spire/core/components/confirmation/base_component.py
@Glue.attr(required_access=lambda component: component.confirm_access, skip_rerender=True)
def confirm(self) -> Glue.Response:
    try:
        detail = self.on_confirm()
    except ConfirmationRefusedError as error:
        return Glue.Response(messages=[Glue.Response.Message.error(str(error))])

    self.confirmed(**detail)

    return Glue.Response(result={'confirmed': True, **detail})