websauna.system.crud.formgenerator module

Abstraction over generating Deform forms from different CRUD models.

This is used by CRUD/admin to create a form for a model to edit. Model doesn’t need to be any particular kind. SQLAlchemy is supported by default.

class websauna.system.crud.formgenerator.FormGenerator(schema_customizer=None, schema_binder=None)[source]

Bases: object

Generate a Deform form.

Parameters
  • schema_customizer (Optional[Callable]) – is a callback customize_schema(schema=schema, request=request, context=context, mode=mode, model=model) and can dynamically adjust generated form schema after its generation. Customizers edits schema in place and does not return anything.

  • schema_binder (Optional[Callable]) – is a callback bind_schema(schema=schema, request=request, context=context, mode=mode, model=model). It should call schema.bind() and pass any extra arguments for Colander schema binding.

By default we usually pass:

schema.bind(request=request, context=context)
create_deform(schema, request, context, mode, buttons, model)[source]

Create a Deform based on a given generated schema.

This method does the following:

  • Adds CSRF token to schema

  • Calls schema customizer

  • Binds schema to request and context or calls custom schema binder

  • Creates the form instance

Parameters
  • schema (Schema) – Schema to be used.

  • request (Request) – Current HTTP request.

  • context (Resource) – Traversal context.

  • mode (EditMode) – Form mode.

  • buttons (List[Button]) – List of buttons to be added.

  • model (type) – SQLAlchemy model class

Return type

Form

Returns

Constructed form object

abstract generate_form(request, context, mode, buttons, model)[source]

Generate form.

Parameters
  • request (Request) – Current HTTP request

  • context (Resource) – Traversal context

  • mode (EditMode) – Of of different form edit modes

  • buttons (List[Button]) – List of form buttons

  • model (type) – SQLAlchemy model class

Return type

Form

Returns

Constructed form object

class websauna.system.crud.formgenerator.SQLAlchemyFormGenerator(includes=None, field_mapper=<websauna.system.form.fieldmapper.DefaultSQLAlchemyFieldMapper object>, customize_schema=None, schema_binder=None)[source]

Bases: websauna.system.crud.formgenerator.FormGenerator

Automatically create a Deform form from underlying SQLAlchemy model.

Parameters

includes – This is a list of [column name, colander.SchemaNode] we extract from the SQLAlchemy model and generate fields for. If you supply a column name as a string the schema type and widget is automatically resolved. If you give colander.SchemaNode(name=name) then this schema type is explicitly used, regardless if such a column exist in the model or not.

If includes is set to None it tries to convert all columns to fields it sees on a model.

For example use case see websauna.system.user.adminviews.UserAdd.

generate_form(request, context, mode, buttons, model)[source]

Generate form.

Parameters
  • request (Request) – Current HTTP request

  • context (Resource) – Traversal context

  • mode (EditMode) – Of of different form edit modes

  • buttons (List[Button]) – List of form buttons

  • model (type) – SQLAlchemy model class

Return type

Form

Returns

Constructed form object

websauna.system.crud.formgenerator.default_schema_binder(schema, request, context, **kwargs)[source]

Initialize Colander field dynamic default values.

By default, don’t do anything and pass request and context to schema.

Parameters
  • schema (Schema) – A colander Schema.

  • request (Request) – Current HTTP Request.

  • context (Resource) – Traversal context

  • kwargs – Optional keyword arguments (not used atm)

Return type

Schema

Returns

Bound colander.Schema