"""Deform's resource_registry mechanism to allow widgets to include CSS and JS on the page."""
from deform import Form
from deform.widget import ResourceRegistry as _ResourceRegistry
from websauna.system.form.interfaces import IFormResources
from websauna.system.http import Request
"""A resource registry that maintains dynamically included CSS and JS files for the page.
* During view processing, widgets and such can request that their helper JS and CSS is included on the page rendering
* :py:class:`websauna.system.core.render.OnDemandResourceRenderer` maintains a state whether JS should go the end of ``<body>`` or ``<head>``
See also :py:attr`deform.widget.default_resources` which includes the list of default resources activated for any form on the page.
def __init__(self, request: Request):
# Load default resoucres from configuration
form_resources = request.registry.getUtility(IFormResources)
self.registry = form_resources.get_default_resources().copy()
[docs] def pull_in_resources(self, request: Request, form: Form):
"""Add resources CSS and JS resources from Deform form to a Websauna rendering loop."""
on_demand_resource_renderer = request.on_demand_resource_renderer
#: TODO: Support for absolute URLs and such
for css_url in self.get_widget_css_urls(request, form):
for js_url in self.get_widget_js_urls(request, form):
on_demand_resource_renderer.request_resource("js", js_url, js_requires_head=True)