5.13 · Reference
Admin API¶
hinoki.admin.Admin ¶
The admin site: prefix, access policy, theme and registered models.
access
instance-attribute
¶
access: Access = access or Access(
auth=AdminSessionAuth(self),
permissions=[BASE_PERMISSION],
)
_csrf
instance-attribute
¶
_csrf = (
Csrf(secret, self.cookie_name(CSRF_COOKIE))
if secret is not None
else None
)
digests
instance-attribute
¶
digests = {
"admin.css": digest(self.templates.stylesheet()),
"admin.js": digest(self.templates.script()),
}
prefix
property
¶
prefix: str
Where the admin lives under its Mount: nothing when the admin is
mounted and names no prefix, /admin when it is included as it is.
__init__ ¶
__init__(
*,
access: Access | None = None,
users: AdminAccounts | None = None,
manage_users: bool = True,
secret: str | bytes | None = None,
prefix: str | None = None,
theme: str | Theme = "studio",
templates: str | None = None,
site: str = "Admin",
per_page: int = 20,
show_count: bool = False,
tagline: str = "",
workspace: str = "Workspace",
links: tuple[Link, ...] = (),
logout_url: str = "",
role: Callable[[Caller[Any]], str] | None = None,
activity: Callable[
[Caller[Any]], Awaitable[list[Event]]
]
| None = None,
dashboard: tuple[str, ...] = (),
secure_cookies: bool | None = None,
) -> None
cookie_name ¶
cookie_name(name: str) -> str
__Secure- before the name when the admin is told its cookies are
always secure (secure_cookies=True): a browser then refuses to set it
over plain HTTP, so a sibling site cannot plant it.
bind_settings ¶
bind_settings(settings: AdminSettings | None) -> None
Use the app's admin settings: the secret (unless the admin was given
its own) and the session and password rules of its own users.
bind_timezone ¶
bind_timezone(timezone: tzinfo) -> None
Use the including app's timezone; reject conflicting app bindings.
register ¶
register(target: A, /, *, slug: str | None = None) -> A
register(
target: type[M],
admin: type[ModelAdmin[M]] | None = None,
*,
slug: str | None = None,
) -> type[M]
register(
target: Any,
admin: Any = None,
*,
slug: str | None = None,
) -> Any
Add a model to the admin and return target, so it also decorates.
site = Admin(access=editors, secret=secret)
@site.register
class PostAdmin(ModelAdmin, model=Post): ... # a customised admin
site.register(User) # the default admin
register(Post, PostAdmin, slug="posts") is the older two-argument form.
refuse_own_tables ¶
refuse_own_tables(
model: type[Model], admin: object
) -> None
The admin's own account tables are managed by the Users section only: a plain model admin would show the password hash and let a grant holder edit who is a superuser.
admin ¶
admin(
model: type[Model], *, slug: str | None = None
) -> Callable[[A], A]
Decorate a ModelAdmin class that does not name its model:
@site.admin(Post)
class PostAdmin(ModelAdmin):
columns = ("title", "published")
The class may also say model=Post itself; naming two different models
is a RegistrationError.
holds_no_connection ¶
holds_no_connection(name: str) -> bool
Routes that may hash a password take no route-wide transaction, so no connection waits on Argon2; their writes open their own, short one.
add_user_admin ¶
add_user_admin() -> None
Register the Users section last, after the application's own models.
route_options ¶
route_options(name: str, write: bool) -> Options
The options of one admin view: public views name no access policy, and the views that hash a password take the sign-in rate limit.
bind_mount ¶
bind_mount(mount: str) -> None
Called by Hinoki.include with the prefix of the Mount around the
admin, so generated links, assets and the CSRF cookie path match the routes.
hinoki.admin.ModelAdmin ¶
How one model appears in the admin. Subclass and override what you need.
querysetscopes every list, lookup, update and delete (it is part of the SQL statement);cangatesview,add,changeanddeletefor a model or a row. Row visibility belongs inqueryset.saveandremoveare the single places that write, so an application can run a domain command there instead of writing around its rules.assigncolumns are set by the server on creation and are not form fields;readonlycolumns are shown but never accepted;excludecolumns are neither shown nor accepted.- After a default write the row must still be inside
queryset; otherwise the write is rolled back.
fieldsets
class-attribute
¶
fieldsets: (
tuple[Fieldset, ...] | Mapping[str, tuple[str, ...]]
) = ()
extras ¶
extras() -> tuple[Extra, ...]
The form fields that are not columns (extra_fields by default).
extra_initial
async
¶
extra_initial(row: M) -> dict[str, Any]
What the extra fields show for row on the edit form.
display ¶
display(row: M, column: str) -> Any
The value shown in a list column (return a Badge or Avatar for
richer cells).
check
async
¶
check(
values: dict[str, Any],
principal: Caller[Any],
row: M | None,
) -> dict[str, str]
Cross-field validation hook: return {field: message} to refuse.
shown_columns ¶
shown_columns() -> tuple[str, ...]
The list columns: columns, else the first six that are not
excluded (an excluded column is never displayed or sortable).
default_search ¶
default_search() -> tuple[str, ...]
The first three shown text columns: what a search box is expected to look in, without ever reaching a column the list does not show.
normalize ¶
normalize(slug: str) -> None
Fill the defaults the model implies and expand the short forms of
badges, tabs and fieldsets.
hinoki.admin.BulkAction
dataclass
¶
A confirmed action for selected rows in a model's admin list.
hinoki.admin.Theme
dataclass
¶
hinoki.admin.Avatar
dataclass
¶
A person: initials in a circle plus the name.
hinoki.admin.Badge
dataclass
¶
A status pill. icon is a Lucide icon name; tone one of TONES.
hinoki.admin.Tab
dataclass
¶
A saved filter shown as a tab: Tab("Drafts", {"published": False}).
hinoki.admin.Fieldset
dataclass
¶
A titled group of form fields; collapsed renders it closed.
hinoki.admin.Extra
dataclass
¶
A form field that is not a column of the model.
kind is "password" (a masked input that is never filled in again) or
"choices" (a set of checkboxes: choices are (value, label) pairs
and the submitted value is a frozenset of the checked ones). The values
reach ModelAdmin.check and save next to the model's own columns.
hinoki.admin.Refusal ¶
Bases: Exception
A write the admin refuses with a message meant for the person: raised from
save or remove, it is shown on the form and rolls the write back.
hinoki.admin.Event
dataclass
¶
One line of the recent-activity panel.
hinoki.admin.Link
dataclass
¶
An extra sidebar link (settings, activity log, ...) supplied by the app.
hinoki.admin.Stat
dataclass
¶
A dashboard card. series (optional numbers, oldest first) draws a
sparkline; without data there is no chart.
hinoki.admin.THEMES
module-attribute
¶
THEMES: dict[str, Theme] = {
t.name: t for t in (STUDIO, CONSOLE, PAPER, BENTO)
}