components
django_spire.core.components
__all__ = ['BaseConfirmationComponent', 'BaseFormComponent', 'BaseModelActionConfirmationComponent', 'BaseModelDeleteConfirmationComponent', 'BaseScrollComponent', 'ConfirmationRefusedError', 'FormComponent', 'ModelCrudScrollComponent', 'ModelDeleteConfirmationComponent', 'ModelFormComponent', 'ModelSetDeletedConfirmationComponent', 'QuerySetScrollComponent', 'SearchAndSortScrollMixin', 'Tab', 'TabsComponent']
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
confirm
Source code in django_spire/core/components/confirmation/base_component.py
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
get_permission_model
Return the row's model, whose permissions Glue.ModelAccessMixin reads.
perform_action
abstractmethod
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
ConfirmationRefusedError
Bases: DjangoSpireError
The confirmed action was not performed; its message tells the user why.
ModelDeleteConfirmationComponent
Bases: BaseModelDeleteConfirmationComponent
Deletes the row from the database, with the model's delete().
ModelSetDeletedConfirmationComponent
Bases: BaseModelDeleteConfirmationComponent
Soft-deletes the row, with the set_deleted() of Spire's history mixin.
BaseFormComponent
Bases: Component, ABC
Shows one form. The form class and the template are set on a subclass,
as form_class and template, or passed when the component is built::
ModelFormComponent(form_class=TaskForm, template='task/form.html')
Either way the form class must be importable from its module, because the component is rebuilt on each request from its signed path.
glue_form_path() is the dot path to the form, read by the template
and its partials as component.glue_form_path. glue_form_object_name
is the name the form's Alpine scope gives it, so a template can call it
account_form instead of glue_form. Whether the component is shown
in a modal, inline or on a page is the choice of whoever shows it.
Source code in django_spire/core/components/form/base_component.py
form_base = BaseForm
class-attribute
form_class = None
class-attribute
glue_form_object_name = 'glue_form'
class-attribute
form_path = Glue.ComponentParameter(None)
class-attribute
instance-attribute
template_name = Glue.ComponentParameter(None)
class-attribute
instance-attribute
template = kwargs['template_name']
instance-attribute
__init_subclass__
validate_form_class
classmethod
Source code in django_spire/core/components/form/base_component.py
glue_form_path
abstractmethod
FormComponent
Bases: BaseFormComponent
A :class:BaseFormComponent over a plain form, exposed as the form
child. Override get_form() when the form needs constructor arguments
from the component's parameters.
Source code in django_spire/core/components/form/base_component.py
form
get_form
ModelFormComponent
Bases: BaseFormComponent
A :class:BaseFormComponent over a ModelForm, whose model is the
form's own. model_pk=None opens the create flow and a value opens the
edit flow. The row is exposed as the model child, a Glue.model, so
the form lives at component.model.form.
The data for the child comes from two overridable hooks: get_model()
picks the instance (blank or fetched by model_pk by default), and
get_fields() names the exposed fields (the form's own fields unless
fields is set), so scoping and create defaults stay subclass logic.
A relation field's choices are the form's own: its __init__ narrows a
field's queryset, which the form child reads, with the row as
self.instance.
pk is the deprecated name of model_pk, both as a parameter and as
an attribute.
Source code in django_spire/core/components/form/model_component.py
form_base = ModelForm
class-attribute
instance-attribute
form_class = None
class-attribute
fields = None
class-attribute
model_pk = Glue.ComponentParameter(None)
class-attribute
instance-attribute
pk
property
model
Source code in django_spire/core/components/form/model_component.py
get_initial
get_fields
Source code in django_spire/core/components/form/model_component.py
get_model
Source code in django_spire/core/components/form/model_component.py
get_permission_model
Return the form's model, whose permissions Glue.ModelAccessMixin reads.
get_required_access
ADD to create a row and CHANGE to edit one, for Glue.ModelAccessRequiredMixin.
BaseScrollComponent
Bases: Component, ABC
An infinite list of any kind of item.
Subclasses supply the items through get_items(), get_item() and
get_item_key(); this base owns paging and every client behaviour.
The server keeps no position: the client sends the number of items it
holds, and a render always shows the first batch.
item_template is the markup for one item. Each item is rendered on
the server, so the template must extend the scroll's item/item.html
or item/table_item.html.
A callable that changes an item fires item_added, item_changed or
item_removed with key=, and the list updates that one row.
Served by as_view(), the list fills spire's full page.
template = 'django_spire/component/scroll/scroll.html'
class-attribute
instance-attribute
view_template = 'django_spire/component/page/full_page.html'
class-attribute
instance-attribute
item_template = None
class-attribute
batch_size = 25
class-attribute
item_added = Glue.event()
class-attribute
instance-attribute
item_changed = Glue.event()
class-attribute
instance-attribute
item_removed = Glue.event()
class-attribute
instance-attribute
renders_items_on_server
property
first_batch
cached
property
get_items
abstractmethod
Return up to limit items starting at offset, in an order that
is the same on every call.
get_item
abstractmethod
Return the item with this key, or None when it no longer belongs in the list.
get_item_key
abstractmethod
introduce
Check, once per class, that its templates extend the scroll's own.
Source code in django_spire/core/components/scroll/base_component.py
load_items
Source code in django_spire/core/components/scroll/base_component.py
load_item
Source code in django_spire/core/components/scroll/base_component.py
ModelCrudScrollComponent
Bases: QuerySetScrollComponent, ABC
A :class:QuerySetScrollComponent with ready-made actions on its rows.
The template's createItem(), editItem(item) and deleteItem(item)
perform them, and each takes the item or its key.
item_form_options says how a row is created and edited: a
ModelCrudScrollComponent.FormOptions shows a form component in a
modal, and a ModelCrudScrollComponent.PageFormOptions sends the user
to a page. Left as None, the list has no create or edit.
item_delete_options says how a row is deleted: a
ModelCrudScrollComponent.DeleteOptions shows a confirmation component
in a modal, and a ModelCrudScrollComponent.PageDeleteOptions sends the
user to a page. The default is a confirmation that soft-deletes the row
with set_deleted(). Left as None, the list has no delete.
Only rows in get_queryset() can be edited or deleted. The browser
fetches the form and the confirmation with load_item_form() and
load_item_delete_confirmation(), which check the user's access and
that the row is in the list. Customise an action through the two settings
and get_queryset(), which keep those checks, and not by overriding the
callables.
DeleteOptions = delete_options.DeleteOptions
class-attribute
instance-attribute
PageDeleteOptions = delete_options.PageDeleteOptions
class-attribute
instance-attribute
FormOptions = form_options.FormOptions
class-attribute
instance-attribute
PageFormOptions = form_options.PageFormOptions
class-attribute
instance-attribute
template = 'django_spire/component/scroll/crud_scroll.html'
class-attribute
instance-attribute
item_delete_options = DeleteOptions()
class-attribute
item_form_options = None
class-attribute
can_create
property
Whether createItem() can open the form: a form page guards itself,
and a form component needs ADD access.
can_edit
property
Whether editItem() can open the form on a row: a form page guards
itself, and a form component needs CHANGE access. A list whose
rows are only ever created overrides it to False, and
load_item_form() then refuses a row.
can_delete
property
Whether deleteItem() can delete a row: a delete page guards itself,
and a confirmation needs DELETE access.
__init_subclass__
Source code in django_spire/core/components/scroll/model_crud_component.py
get_item_form_options
get_item_delete_options
load_item_delete_confirmation
Source code in django_spire/core/components/scroll/model_crud_component.py
load_item_form
The form that creates a row, or with pk edits one. ADD access
is the least that opens any form. The form then guards what it does:
it takes this list's access, and Glue's required_save_access lets
its fields and save change a saved row only at CHANGE. A form
component that mixes in Glue.ModelAccessRequiredMixin also refuses
to open for a user without that access. can_edit refuses a row
before any form is built: below CHANGE, and in a list whose rows
are never edited, which no access expresses.
Source code in django_spire/core/components/scroll/model_crud_component.py
QuerySetScrollComponent
Bases: BaseScrollComponent, ABC
A :class:BaseScrollComponent whose items are the rows of a queryset.
Subclasses return the rows from get_queryset(). The primary key
identifies each row and is appended to the queryset's ordering, so that
the order is total; a queryset with no ordering is listed by primary key.
Each item is a model instance.
glue_items sends each row to the browser as the Glue model
get_glue_item() builds, in place of rendering it on the server, so
the row markup can call the model's Glue methods. The rows are drawn in
the browser: item_template is Alpine markup that reads item, drawn
by the scroll_item block of the component template, which a list may
fill in itself instead. The browser then loads rows with
load_glue_items() and load_glue_item(), and the other loads are
refused.
Glue rows never arrive with the render: a Glue child is not derived again
when its component refreshes, so every batch comes from
load_glue_items(). A batch holds up to batch_size rows, and a full
one means there may be more.
glue_items = False
class-attribute
model_name
cached
property
verbose_name
cached
property
verbose_name_plural
cached
property
renders_items_on_server
property
__init_subclass__
Source code in django_spire/core/components/scroll/queryset_component.py
get_queryset
abstractmethod
get_permission_model
Return the listed model, whose permissions Glue.ModelAccessMixin reads.
get_items
Source code in django_spire/core/components/scroll/queryset_component.py
get_item
get_item_key
get_instance
Return the row of get_queryset() with this primary key, for a
callable that acts on one row. A row that is not in the list is
reported as Glue's model_instance_not_found, a 404.
Source code in django_spire/core/components/scroll/queryset_component.py
get_glue_item
Return the Glue model the browser receives for item when
glue_items is set, built with name as its unique name and with
exactly the fields, access and form the row needs. A list that sets
glue_items must override it.
To edit a row in place, pass the row a form, bind the row's inputs
to item.form, and save with the form's own method, such as
item.form.save_model_obj(). That runs the form's validation and
whatever the application's save does. item.save() writes the model
directly instead.
Source code in django_spire/core/components/scroll/queryset_component.py
load_glue_item
Source code in django_spire/core/components/scroll/queryset_component.py
load_glue_items
Source code in django_spire/core/components/scroll/queryset_component.py
SearchAndSortScrollMixin
Adds search and optional sort :class:Glue.ComponentField
attributes to a scroll component without writing a boilerplate
FilterForm.
Declare default_sort as a Django Choices member (e.g.
SolutionSortChoices.NAME) and sort_choices is inferred from
type(default_sort) automatically. Or declare sort_choices alone to
default to its first choice. Omit both for a search-only list.
sort_choices = None
class-attribute
default_sort = None
class-attribute
search
instance-attribute
__init_subclass__
Source code in django_spire/core/components/scroll/search_and_sort_scroll_mixin.py
Tab
dataclass
Declares one tab inside a :class:TabsComponent.
Provide exactly one target:
- component: a Glue.Component class or zero-argument factory callable
(e.g. partial(EmployeeListComponent, is_active=True))
- view_url: a Django route URL loaded lazily via Glue.view(url)
- template: a Django template rendered into the pane (with optional context)
- href: a full-page link rendered as an <a> tag instead of swapping panes
id
instance-attribute
label
instance-attribute
component = None
class-attribute
instance-attribute
view_url = None
class-attribute
instance-attribute
template = None
class-attribute
instance-attribute
context = field(default_factory=dict)
class-attribute
instance-attribute
href = None
class-attribute
instance-attribute
icon = None
class-attribute
instance-attribute
cache = None
class-attribute
instance-attribute
view_method = 'GET'
class-attribute
instance-attribute
kind
property
is_component
property
is_view
property
is_template
property
is_link
property
__post_init__
Source code in django_spire/core/components/tab/options.py
build_component
Source code in django_spire/core/components/tab/options.py
TabsComponent
Bases: Component, ABC
A tabbed container whose panes are loaded lazily and may host Glue
components, Django fragment views (via Glue.view), plain templates, or
full-page links.
Subclasses implement get_tabs() returning a sequence of :class:Tab
items (aliased as self.Tab). Only the initially active tab is rendered
on first paint; other tabs stay empty until selected.
url_param: query parameter synced withhistory.replaceStateand read fromrequest.GETon first render (default'tab'; set toNoneto disable).cache_tabs: whether visited panes stay mounted across switches (defaultTrue; overridable per tab viaTab(..., cache=False)).tab_invalidated: Glue event wired toinvalidateTab(tab_id)so listeners can mark a cached tab stale or force-refresh the active tab.