cardinal_pythonlib.deform_utils


Original code copyright (C) 2009-2022 Rudolf Cardinal (rudolf@pobox.com).

This file is part of cardinal_pythonlib.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.


class cardinal_pythonlib.deform_utils.DeformErrorInterface(msg: str, *children: DeformErrorInterface)[source]

Class to record information about Deform errors.

Parameters:
  • msg – error message

  • children – further, child errors (e.g. from subfields with problems)

class cardinal_pythonlib.deform_utils.DynamicDescriptionsForm(*args, dynamic_descriptions: bool = True, dynamic_titles: bool = False, **kwargs)[source]

For explanation, see cardinal_pythonlib.colander_utils.ValidateDangerousOperationNode.

In essence, this allows a schema to change its description properties during form validation, and then to have them reflected in the form (which won’t happen with a standard Deform Form, since it normally copies its descriptions from its schema at creation time).

The upshot is that we can store temporary values in a form and validate against them.

The use case is to generate a random string which the user has to enter to confirm dangerous operations.

Parameters:
  • args – other positional arguments to InformativeForm

  • dynamic_descriptions – use dynamic descriptions?

  • dynamic_titles – use dynamic titles?

  • kwargs – other keyword arguments to InformativeForm

validate(controls: Iterable[Tuple[str, str]], subcontrol: str | None = None) Any[source]

Validates the form.

Parameters:
  • controls – an iterable of (key, value) tuples

  • subcontrol

Returns:

a Colander appstruct

Raises:

ValidationFailure – on failure

class cardinal_pythonlib.deform_utils.InformativeForm(schema, action='', method='POST', buttons=(), formid='deform', use_ajax=False, ajax_options='{}', autocomplete=None, focus='on', **kw)[source]

A Deform form class that shows its errors.

validate(controls: Iterable[Tuple[str, str]], subcontrol: str | None = None) Any[source]

Validates the form.

Parameters:
  • controls – an iterable of (key, value) tuples

  • subcontrol

Returns:

a Colander appstruct

Raises:

ValidationFailure – on failure

cardinal_pythonlib.deform_utils.debug_validator(validator: Callable[[SchemaNode, Any], None]) Callable[[SchemaNode, Any], None][source]

Use as a wrapper around a validator, e.g.

self.validator = debug_validator(OneOf(["some", "values"]))

If you do this, the log will show the thinking of the validator (what it’s trying to validate, and whether it accepted or rejected the value).

cardinal_pythonlib.deform_utils.gen_fields(field: Field) Generator[Field, None, None][source]

Starting with a Deform Field, yield the field itself and any children.

cardinal_pythonlib.deform_utils.get_head_form_html(req: Request, forms: List[Form]) str[source]

Returns the extra HTML that needs to be injected into the <head> section for a Deform form to work properly.