Plugins
A plugin is a package that declares an entry point in the hyperadmin.plugins group and
provides an object that satisfies the Plugin protocol. Admin discovers installed plugins,
validates their settings and calls on_register(admin) before the admin is mounted.
# the plugin's pyproject.toml
[project.entry-points."hyperadmin.plugins"]
reports = "my_reports.plugin:ReportsPlugin"
from fastapi import APIRouter
from hyperadmin import Admin
class ReportsPlugin:
name = "reports"
def on_register(self, admin: Admin) -> None:
router = APIRouter()
admin.plugins.add_page(self, router, prefix="/reports")
admin.plugins.add_nav_entry(self, label="Reports", url_name="reports-index")
admin.plugins.add_permission(self, "view_report_sales")
From on_register a plugin may call add_page, add_nav_entry, add_template_dir,
set_static_dir, add_permission and set_home_page. These are the public registry methods
(see What is public API); the other methods of
PluginRegistry are used by Admin itself. Pass disabled_plugins= or set
HYPERADMIN_DISABLED_PLUGINS to skip a plugin, and plugin_settings= to configure one.
Lifecycle hooks
LIFECYCLE_HOOKS lists the names of the lifecycle hooks a plugin may implement as methods.
HyperAdmin calls them with keyword arguments only, so write them to accept extra keyword
arguments (**kwargs). A hook that raises is logged on the hyperadmin.core.plugins logger
and skipped; it never breaks the request or the other plugins. When each hook fires is
described in the hook reference.
| Hook | Keyword arguments |
|---|---|
on_model_register |
admin, model, options |
on_before_create / on_after_create |
admin, model, data / admin, model, instance |
on_before_update / on_after_update |
admin, model, pk, data / admin, model, instance |
on_before_delete / on_after_delete |
admin, model, pk |
on_before_adapter_call / on_after_adapter_call |
admin, model, op, kwargs / admin, model, op, result, error |
on_validation_error |
admin, model, field_errors |
on_auth_event |
admin, event, user, username, request (and model for permission_denied) |
API Reference
hyperadmin.core.plugins.Plugin
Bases: Protocol
What every plugin provides. All lifecycle hooks are optional methods.
A plugin may also define settings_model (a pydantic model class); the
registry validates the operator's settings into it before on_register.
Source code in src/hyperadmin/core/plugins.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | |
on_register(admin)
Contribute pages, nav entries, templates, static files and permissions.
Called once per Admin, in plugin order, before the admin is mounted. Use the
add_* and set_static_dir methods of admin.plugins (a PluginRegistry).
Source code in src/hyperadmin/core/plugins.py
77 78 79 80 81 82 | |
hyperadmin.core.plugins.PluginRegistry
Discovered plugins, their settings, collected UI seams and hook dispatch.
Source code in src/hyperadmin/core/plugins.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 | |
home_page
property
The plugin endpoint serving the admin index, or None for the welcome page.
nav_entries
property
Nav entries sorted by (order, label); ties keep plugin order.
skipped
property
Plugins that were found but not registered, with the reason.
add_nav_entry(plugin, *, label, url_name, icon=None, permission=None, section=None, order=0, url_params=None, testid=None)
Add a sidebar link; url_name is resolved in the hyperadmin: namespace.
testid names the link for tests when several entries share a
url_name (one per url_params); by default it is derived from it.
Source code in src/hyperadmin/core/plugins.py
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | |
add_page(plugin, router, *, prefix=None)
Mount router (a FastAPI APIRouter) under prefix in the admin.
prefix defaults to /<plugin name> and must be unique across plugins.
Source code in src/hyperadmin/core/plugins.py
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | |
add_permission(plugin, codename, name=None)
Declare a permission codename so it shows in the permission system.
Source code in src/hyperadmin/core/plugins.py
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 | |
add_template_dir(plugin, path)
Add a template search path (searched after core and host templates).
Source code in src/hyperadmin/core/plugins.py
393 394 395 396 397 398 399 400 401 | |
discover(*, admin, disabled=(), settings=None)
classmethod
Load every hyperadmin.plugins entry point, isolating failures.
Entry points are processed sorted by (name, value) so the order is
deterministic; a later entry point reusing a name is skipped.
Source code in src/hyperadmin/core/plugins.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |
dispatch_hook(hook, /, **kwargs)
Call every plugin's hook in plugin order; exceptions are logged, not raised.
Source code in src/hyperadmin/core/plugins.py
508 509 510 511 512 513 514 515 516 | |
has_handlers(hook)
Whether any plugin implements hook (hot-path short-circuit).
Source code in src/hyperadmin/core/plugins.py
504 505 506 | |
load_entry_points(*, admin, disabled=(), settings=None)
Like :meth:discover, into this registry.
Admin assigns admin.plugins first and then loads into it, so a
plugin's on_register can call admin.plugins.add_*.
Source code in src/hyperadmin/core/plugins.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | |
names()
Registered plugin names, in registration order.
Source code in src/hyperadmin/core/plugins.py
311 312 313 | |
origin(name)
Import path (entry-point value) a plugin was loaded from.
Source code in src/hyperadmin/core/plugins.py
315 316 317 | |
register(plugin, *, admin, settings=None, origin=None)
Validate settings, run on_register and keep the plugin on success.
Any failure is logged and rolls back everything the plugin contributed; returns whether the plugin was registered.
Source code in src/hyperadmin/core/plugins.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | |
set_home_page(plugin, endpoint)
Serve endpoint at the admin index {prefix}/ (one plugin per admin).
endpoint is async (request) -> Response. The index route keeps its name
(admin-dashboard) and falls back to the welcome page if the endpoint raises
anything but an HTTPException.
Raises:
| Type | Description |
|---|---|
PluginError
|
If a home page is already set, or outside |
Source code in src/hyperadmin/core/plugins.py
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 | |
set_static_dir(plugin, path)
Serve path under {prefix}/static/plugins/<plugin name>/ (one per plugin).
Source code in src/hyperadmin/core/plugins.py
403 404 405 406 407 408 409 410 411 412 | |
settings(name)
The validated settings_model instance of a plugin, or None.
Source code in src/hyperadmin/core/plugins.py
324 325 326 | |
hyperadmin.core.plugins.PluginError
Bases: Exception
A plugin misused the registration API (bad argument, wrong time, conflict).
Source code in src/hyperadmin/core/plugins.py
63 64 | |