Understanding the schemas

ColanderAlchemy allows creating Colander schemas directly from SQLAlchemy model classes.

Additionally, c2cgeoform provides its own classes with extended features. A basic use case schema creation will look like:

from model import MyClass
schema = GeoFormSchemaNode(MyClass)

See the following API to understand what is going on behind the scene.

class c2cgeoform.schema.GeoFormSchemaNode(*args, **kw)

An SQLAlchemySchemaNode with deferred request and dbsession properties. This will allow defining schemas that requires the request and dbsession at module-scope.

Example usage:

schema = GeoFormSchemaNode(MyModel)

def create_form(request, dbsession):
    return Form(
        schema = schema.bind(
            request=request,
            dbsession=request.dbsession),
        ...
    )
add_unique_validator(column, column_id)

Adds an unique validator on this schema instance.

column
SQLAlchemy ColumnProperty that should be unique.
column_id
SQLAlchemy MapperProperty that is used to recognize the entity, basically the primary key ColumnProperty.
class c2cgeoform.schema.GeoFormManyToManySchemaNode(class_, includes=None, *args, **kw)

A GeoFormSchemaNode that properly handles many to many relationships.

includes:
Default to primary key name(s) only.
objectify(dict_, context=None)

Method override that returns the existing ORM class instance instead of creating a new one.

c2cgeoform.schema.manytomany_validator(node, cstruct)

Validator function that checks if cstruct values exist in the related table.

Note that entities are retrieved using only one query and placed in SQLAlchemy identity map before looping on cstruct.

class c2cgeoform.ext.colander_ext.BinaryData

A Colander type meant to be used with LargeBinary columns.

Example usage

class Model():
    id = Column(Integer, primary_key=True)
    data = Colum(LargeBinary, info={
        'colanderalchemy': {
            'typ': colander_ext.BinaryData()
        }})

It is usually not used directly in application models, but through the c2cgeoform.models.FileData mixin, which is meant to be used with a deform_ext.FileUploadWidget.

The serialize method just returns colander.null. This is because the FileUploadWidget’s template does not use and need the binary data.

The deserialize method gets a Python file object and returns a bytes string that is appropriate for the database.

deserialize(node, cstruct)

In Colander speak: Converts a serialized value (a cstruct) into a Python data structure (a appstruct). Or: Converts a Python file stream to plain binary data.

serialize(node, appstruct)

In Colander speak: Converts a Python data structure (an appstruct) into a serialization (a cstruct).

class c2cgeoform.ext.colander_ext.Geometry(geometry_type='GEOMETRY', srid=-1, map_srid=-1)

A Colander type meant to be used with GeoAlchemy 2 geometry columns.

Example usage

geom = Column(
    geoalchemy2.Geometry('POLYGON', 4326, management=True), info={
        'colanderalchemy': {
            'typ': colander_ext.Geometry(
                'POLYGON', srid=4326, map_srid=3857),
            'widget': deform_ext.MapWidget()
        }})

Attributes/Arguments

geometry_type
The geometry type should match the column geometry type.
srid
The SRID of the geometry should also match the column definition.
map_srid
The projection used for the OpenLayers map. The geometries will be reprojected to this projection.
deserialize(node, cstruct)

In Colander speak: Converts a serialized value (a cstruct) into a Python data structure (a appstruct). Or: Converts a GeoJSON string into a WKBElement.

serialize(node, appstruct)

In Colander speak: Converts a Python data structure (an appstruct) into a serialization (a cstruct). Or: Converts a WKBElement into a GeoJSON string.