websauna.system.form.sqlalchemy module

SQLAlchemy integration for Colander and Deform frameworks.

class websauna.system.form.sqlalchemy.ForeignKeyValue(model=None)[source]

Bases: websauna.system.form.sqlalchemy.ModelSchemaType, colander.String

Hold a reference to one SQLAlchemy object for Colander schema serialization.

See Widgets for more information.

deserialize(node, cstruct)[source]

Convert incoming form value - string id - back to a SQLAlchemy object.

preprocess_appstruct_value(node, appstruct)[source]

Convert items to appstruct ids.

Return type

List[str]

preprocess_cstruct_value(node, cstruct)[source]

Parse incoming form values to Python objects if needed.

Return type

Union[set, List]

query_item(node, dbsession, model, match_column, value)[source]

Query the actual model to get the concrete SQLAlchemy objects.

Return type

List[object]

serialize(node, appstruct)[source]
class websauna.system.form.sqlalchemy.ModelSchemaType(model=None)[source]

Bases: object

Mixin class for using SQLAlchemy with Colander types.

Provide utilitiy functions to extract model and dbsession from the context.

convert_to_id(item)[source]
get_dbsession(node)[source]
Return type

Session

get_match_column(node, model)[source]

Get the column we are filtering out.

Return type

Column

get_model(node)[source]

Which model we are quering.

Return type

type

label_column = None

Name of the column which provides label or such for items in sequence. If not present item __str__ is used.

match_column = None

Name of the column on the model we use to fetch objects using IN query

model = None

Point this to the model this set is supposed to query

class websauna.system.form.sqlalchemy.ModelSet(model=None)[source]

Bases: websauna.system.form.sqlalchemy.ModelSchemaType, colander.Set

Presents set of chosen SQLAlchemy models instances.

This automatically turns SQLAlchemy objects to (id, label) tuples, so that they can be referred in various widgets (select, checkbox).

See Widgets for more information.

deserialize(node, cstruct)[source]
deserialize_set_to_models(node, cstruct)[source]
preprocess_appstruct_values(node, appstruct)[source]

Convert items to appstruct ids.

Return type

List[str]

preprocess_cstruct_values(node, cstruct)[source]

Parse incoming form values to Python objects if needed.

Return type

Union[set, List]

query_items(node, dbsession, model, match_column, values)[source]

Query the actual model to get the concrete SQLAlchemy objects.

Return type

List[object]

serialize(node, appstruct)[source]
class websauna.system.form.sqlalchemy.ModelSetResultList[source]

Bases: list

Mark that the result is through SQLAlchemy query.

class websauna.system.form.sqlalchemy.UUIDForeignKeyValue(model, match_column=None)[source]

Bases: websauna.system.form.sqlalchemy.ForeignKeyValue

Hold a reference to SQLAlchemy object through base64 encoded UUID value.

Useful for select widget, etc.

See Widgets for more information.

preprocess_appstruct_value(node, appstruct)[source]

Convert items to appstruct ids.

Return type

List[str]

preprocess_cstruct_value(node, cstruct)[source]

Parse incoming form values to Python objects if needed.

class websauna.system.form.sqlalchemy.UUIDModelSet(model=None, match_column=None, label_column=None)[source]

Bases: websauna.system.form.sqlalchemy.ModelSet

A set of SQLAlchemy objects queried by base64 encoded UUID value.

See Widgets for more information.

preprocess_appstruct_values(node, appstruct)[source]

Convert items to appstruct ids.

Return type

List[str]

preprocess_cstruct_values(node, cstruct)[source]

Parse incoming form values to Python objects if needed.

websauna.system.form.sqlalchemy.convert_query_to_tuples(query, first_column, second_column, default_choice=None)[source]

Convert SQLAlchemy query results to (id, name) tuples for select and checkbox widgets.

Parameters
  • first_column (Union[str, Callable]) – Column name used to populate value in the first tuple

  • second_column (Union[str, Callable]) – Column name used to populate value in the second tuple

Oaram default_choice

If given use this as “Select here” or when the value is None

Return type

List[Tuple[str, str]]

websauna.system.form.sqlalchemy.extract_uuid_to_slug(item)[source]

Reads uuid attribute on the model name returns it as B64 encoded slug.

websauna.system.form.sqlalchemy.get_uuid_vocabulary_for_model(dbsession, model, first_column=<function extract_uuid_to_slug>, second_column=<class 'str'>, default_choice=None)[source]

Create a select/checkbox vocabulary containing all items of a model.

Return type

List[Tuple]