core.directives

Attributes

_T

_FormT

_RequestT

Classes

HtmlHandleFormAction

Register Form view.

CronjobAction

Register a cronjob.

StaticDirectoryAction

Registers a static files directory.

TemplateVariablesRegistry

TemplateVariablesAction

Registers a set of global template variables for chameleon templates.

Functions

fetch_form_class(→ type[_FormT])

Given the form_class defined with the form action, together with

query_form_class(→ type[Form] | None)

Queries the app configuration for the form class associated with

wrap_with_generic_form_handler(→ Callable[[_T, ...)

Wraps a view handler with generic form handling.

Module Contents

core.directives._T[source]
core.directives._FormT[source]
core.directives._RequestT[source]
class core.directives.HtmlHandleFormAction(model: type | str, form: type[Form] | Callable[[Any, _RequestT], type[Form]], render: Callable[[Any, _RequestT], Response] | str | None = None, template: StrOrBytesPath | None = None, load: Callable[[_RequestT], Any] | str | None = None, permission: object | str | None = None, internal: bool = False, **predicates: Any)[source]

Bases: morepath.directive.HtmlAction

Register Form view.

Basically wraps the Morepath’s html directive, registering both POST and GET (if no specific request method is given) and wrapping the view handler with wrap_with_generic_form_handler().

The form is either a class or a function. If it’s a function, it is expected to return a form class when given an instance of the model.

The form may also be None, which is useful under special circumstances. Generally you don’t want that though.

Example:

@App.form(model=Root, template='form.pt',
          permission=Public, form=LoginForm)
def handle_form(self, request, form):
    if form.submitted():
        # do something if the form was submitted with valid data
    else:
        # do something if the form was not submitted or not
        # submitted correctly

    return {}  # template variables
form[source]
perform(obj: Callable[[Any, _RequestT, Any], Any], *args: Any, **kwargs: Any) None[source]

Do whatever configuration is needed for obj.

Needs to be implemented by the Action subclass.

Raise a DirectiveError to indicate that the action cannot be performed due to incorrect configuration.

Parameters:
  • obj – the object that the action should be performed for. Typically a function or a class object.

  • **kw – a dictionary of configuration objects as specified by the config class attribute.

core.directives.fetch_form_class(form_class: type[_FormT] | Callable[[Any, _RequestT], type[_FormT]], model: object, request: _RequestT) type[_FormT][source]

Given the form_class defined with the form action, together with model and request, this function returns the actual class to be used.

core.directives.query_form_class(request: _RequestT, model: object, name: str | None = None) type[Form] | None[source]

Queries the app configuration for the form class associated with the given model and name. Take this configuration for example:

@App.form(model=Model, form_class=Form, name='foobar')
...

The form class defined here can be retrieved as follows:

query_form_class(request, model=Model, name=’foobar’)

core.directives.wrap_with_generic_form_handler(obj: Callable[[_T, _RequestT, _FormT], Any], form_class: type[_FormT] | Callable[[_T, _RequestT], type[_FormT]]) Callable[[_T, _RequestT], Any][source]

Wraps a view handler with generic form handling.

This includes instantiating the form with translations/csrf protection and setting the correct action.

class core.directives.CronjobAction(hour: int | str, minute: int | str, timezone: str, once: bool = False)[source]

Bases: dectate.Action

Register a cronjob.

config[source]

Describe configuration.

A dict mapping configuration names to factory functions. The resulting configuration objects are passed into Action.identifier(), Action.discriminators(), Action.perform(), and Action.before() and Action.after().

After commit completes, the configured objects are found as attributes on App.config.

counter: ClassVar[source]
hour[source]
minute[source]
timezone[source]
name[source]
once = False[source]
identifier(**kw: Any) int[source]

Returns an immutable that uniquely identifies this config.

Needs to be implemented by the Action subclass.

Used for overrides and conflict detection.

If two actions in the same group have the same identifier in the same configurable, those two actions are in conflict and a ConflictError is raised during commit().

If an action in an extending configurable has the same identifier as the configurable being extended, that action overrides the original one in the extending configurable.

Parameters:

**kw – a dictionary of configuration objects as specified by the config class attribute.

Returns:

an immutable value uniquely identifying this action.

perform(func: Callable[[CoreRequest], Any], cronjob_registry: onegov.core.utils.Bunch) None[source]

Do whatever configuration is needed for obj.

Needs to be implemented by the Action subclass.

Raise a DirectiveError to indicate that the action cannot be performed due to incorrect configuration.

Parameters:
  • obj – the object that the action should be performed for. Typically a function or a class object.

  • **kw – a dictionary of configuration objects as specified by the config class attribute.

class core.directives.StaticDirectoryAction[source]

Bases: dectate.Action

Registers a static files directory.

config[source]

Describe configuration.

A dict mapping configuration names to factory functions. The resulting configuration objects are passed into Action.identifier(), Action.discriminators(), Action.perform(), and Action.before() and Action.after().

After commit completes, the configured objects are found as attributes on App.config.

counter: ClassVar[source]
name[source]
identifier(staticdirectory_registry: onegov.core.utils.Bunch) int[source]

Returns an immutable that uniquely identifies this config.

Needs to be implemented by the Action subclass.

Used for overrides and conflict detection.

If two actions in the same group have the same identifier in the same configurable, those two actions are in conflict and a ConflictError is raised during commit().

If an action in an extending configurable has the same identifier as the configurable being extended, that action overrides the original one in the extending configurable.

Parameters:

**kw – a dictionary of configuration objects as specified by the config class attribute.

Returns:

an immutable value uniquely identifying this action.

perform(func: Callable[..., Any], staticdirectory_registry: onegov.core.utils.Bunch) None[source]

Do whatever configuration is needed for obj.

Needs to be implemented by the Action subclass.

Raise a DirectiveError to indicate that the action cannot be performed due to incorrect configuration.

Parameters:
  • obj – the object that the action should be performed for. Typically a function or a class object.

  • **kw – a dictionary of configuration objects as specified by the config class attribute.

class core.directives.TemplateVariablesRegistry[source]
__slots__ = ('callbacks',)[source]
callbacks: list[collections.abc.Callable[[core.request.CoreRequest], dict[str, Any]]] = [][source]
get_variables(request: core.request.CoreRequest, base: dict[str, Any] | None = None) dict[str, Any][source]
class core.directives.TemplateVariablesAction[source]

Bases: dectate.Action

Registers a set of global template variables for chameleon templates.

Only exists once per application. Template variables with conflicting keys defined in child applications override the keys with the same name in the parent application. Non-conflicting keys are kept individually.

Example:

@App.template_variables()
def get_template_variables(request):
    return {
        'foo': 'bar'
    }
config[source]

Describe configuration.

A dict mapping configuration names to factory functions. The resulting configuration objects are passed into Action.identifier(), Action.discriminators(), Action.perform(), and Action.before() and Action.after().

After commit completes, the configured objects are found as attributes on App.config.

counter: ClassVar[source]
name[source]
identifier(templatevariables_registry: TemplateVariablesRegistry) int[source]

Returns an immutable that uniquely identifies this config.

Needs to be implemented by the Action subclass.

Used for overrides and conflict detection.

If two actions in the same group have the same identifier in the same configurable, those two actions are in conflict and a ConflictError is raised during commit().

If an action in an extending configurable has the same identifier as the configurable being extended, that action overrides the original one in the extending configurable.

Parameters:

**kw – a dictionary of configuration objects as specified by the config class attribute.

Returns:

an immutable value uniquely identifying this action.

perform(func: Callable[[CoreRequest], dict[str, Any]], templatevariables_registry: TemplateVariablesRegistry) None[source]

Do whatever configuration is needed for obj.

Needs to be implemented by the Action subclass.

Raise a DirectiveError to indicate that the action cannot be performed due to incorrect configuration.

Parameters:
  • obj – the object that the action should be performed for. Typically a function or a class object.

  • **kw – a dictionary of configuration objects as specified by the config class attribute.