Skip to content

confirmation

django_spire.core.components.confirmation

__all__ = ['BaseConfirmationComponent', 'BaseModelActionConfirmationComponent', 'BaseModelDeleteConfirmationComponent', 'ConfirmationRefusedError', 'DeleteOptions', 'ModelDeleteConfirmationComponent', 'ModelSetDeletedConfirmationComponent', 'PageDeleteOptions'] module-attribute

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})

ConfirmationRefusedError

Bases: DjangoSpireError

The confirmed action was not performed; its message tells the user why.

BaseModelActionConfirmationComponent

Bases: BaseConfirmationComponent, ABC

A :class:BaseConfirmationComponent for one action on one model row.

Built with the row, as instance=, it works for any model. Confirming performs the action with perform_action(), which a subclass writes, and fires confirmed with the row's pk.

model_name property

instance

Source code in django_spire/core/components/confirmation/model/action_component.py
@Glue.ComponentParameter
def instance(self, model: type[Model], pk: int) -> Model:
    return model._default_manager.get(pk=pk)

get_permission_model

Return the row's model, whose permissions Glue.ModelAccessMixin reads.

Source code in django_spire/core/components/confirmation/model/action_component.py
def get_permission_model(self) -> type[Model]:
    """Return the row's model, whose permissions ``Glue.ModelAccessMixin`` reads."""
    return self.instance._meta.model

perform_action abstractmethod

Perform the confirmed action on self.instance.

Source code in django_spire/core/components/confirmation/model/action_component.py
@abstractmethod
def perform_action(self) -> None:
    """Perform the confirmed action on ``self.instance``."""

on_confirm

Source code in django_spire/core/components/confirmation/model/action_component.py
def on_confirm(self) -> dict[str, Any]:
    pk = self.instance.pk

    self.perform_action()

    return {'pk': pk}

BaseModelDeleteConfirmationComponent

Bases: BaseModelActionConfirmationComponent, ABC

A :class:BaseModelActionConfirmationComponent worded and guarded as a delete: confirming needs DELETE access, and title, message and confirm_label name the row and its model when left unset.

A subclass deletes the row in perform_action().

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

confirm_access = Glue.Access.DELETE class-attribute instance-attribute

ModelDeleteConfirmationComponent

Bases: BaseModelDeleteConfirmationComponent

Deletes the row from the database, with the model's delete().

perform_action

Source code in django_spire/core/components/confirmation/model/delete_component.py
def perform_action(self) -> None:
    self.instance.delete()

ModelSetDeletedConfirmationComponent

Bases: BaseModelDeleteConfirmationComponent

Soft-deletes the row, with the set_deleted() of Spire's history mixin.

perform_action

Source code in django_spire/core/components/confirmation/model/delete_component.py
def perform_action(self) -> None:
    self.instance.set_deleted()

DeleteOptions dataclass

A row is deleted through a confirmation component, shown in a modal. component is the confirmation, which soft-deletes the row with set_deleted() unless another is given. It fires confirmed with the row's pk.

component = ModelSetDeletedConfirmationComponent class-attribute instance-attribute

__post_init__

Source code in django_spire/core/components/confirmation/options.py
def __post_init__(self) -> None:
    if not isinstance(self.component, type) or not issubclass(
        self.component,
        BaseModelDeleteConfirmationComponent,
    ):
        message = (
            'DeleteOptions needs component to be a '
            'BaseModelDeleteConfirmationComponent class.'
        )
        raise ImproperlyConfigured(message)

PageDeleteOptions dataclass

A row is deleted on a page of its own. url_name is the route that takes the row's key as pk.

The link carries a return_url for the page to send the user to afterwards: the route named by return_url_name, or when that is left out, the address the list is shown at. A page that does not read return_url goes wherever it always does.

url_name instance-attribute

return_url_name = None class-attribute instance-attribute