Configure the widgets

All Deform widgets can be used with c2cgeoform. See the Deform examples and widgets API reference for detailed description about available options.

Additionally, c2cgeoform provides some extra widgets:

class c2cgeoform.ext.deform_ext.FileUploadWidget(tmpstore, get_url=None, **kw)

Extension of deform.widget.FileUploadWidget to be used in a model class that extends the models.FileData mixin class.

Note that, contrary to deform.widget.FileUploadWidget, this extension is not meant to be used with the deform.FileData Colander type. Instead it works with the colander.Mapping type, which is what colanderalchemy uses for an SQLAlchemy model class.

Note also that it is required to set unknown to 'preserve' in the __colanderalchemy_config__ dictionary.

Example usage

from c2cgeoform import models
from c2cgeoform.ext import deform_ext

class Photo(models.FileData, Base):
    __tablename__ = 'photo'
    __colanderalchemy_config__ = {
        'title': _('Photo'),
        'unknown': 'preserve',
        'widget': deform_ext.FileUploadWidget(file_upload_temp_store)
    }
    permission_id = Column(Integer, ForeignKey('excavations.id'))

Attributes/Arguments

get_url (optional)

A callback function function(request, id) -> string which returns the URL to get the file. Example usage:

'widget': deform_ext.FileUploadWidget(
    _file_upload_temp_store,
    get_url=lambda request, id: request.route_url('file', id=id)
)
class c2cgeoform.ext.deform_ext.MapWidget(**kw)

A Deform widget that fits with GeoAlchemy 2 geometry columns and shows an OpenLayers 3 map which allows to draw and modify geometries.

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(
                base_layer='new ol.layer.Tile({ source: new ol.source.OSM() })',
                center=[829170, 5933942],
                zoom=7,
                fit_max_zoom=14
            )
        }})

To customize the map, the template file map.pt has to be overwritten.

Attributes/Arguments

base_layer (str, optional):
Javascript code returning the map base layer.
center ([x, y], optional):
Initial center when no geometry is given.
zoom (int, optional):
Initial zoom when no geometry is given.
fit_max_zoom (int, optional):
Maximum zoom when fitting to given geometry.
class c2cgeoform.ext.deform_ext.RecaptchaWidget(**kw)

A Deform widget for Google reCaptcha.

In c2cgeoform this widget can be used by setting the show_captcha flag when calling register_schema().

Example usage:

register_schema(
    'comment', model.Comment, show_confirmation=False,
    show_captcha=True,
    recaptcha_public_key=settings.get('recaptcha_public_key'),
    recaptcha_private_key=settings.get('recaptcha_private_key'))

Attributes/arguments

public_key (required)
The Google reCaptcha site key.
private_key (required)
The Google reCaptcha secret key.
class c2cgeoform.ext.deform_ext.RelationCheckBoxListWidget(model, id_field='id', label_field='label', order_by=None, **kw)

Extension of the widget ``deform.widget.CheckboxChoiceWidget which loads the values from the database using a SQLAlchemy model.

For n:m relations the widget can be used like so:

situations = relationship(
    "Situation",
    secondary=situation_for_permission,
    cascade="save-update,merge,refresh-expire",
    info={
        'colanderalchemy': {
            'title': _('Situations'),
            'widget': RelationCheckBoxListWidget(
                Situation,
                'id',
                'name',
                order_by='name',
                edit_url=lambda request, value: request.route_url(
                        'c2cgeoform_item',
                        table='situations',
                        id=value
                        )
            ),
            'includes': ['id'],
            'validator': manytomany_validator
        }
    })

Attributes/Arguments

model (required)
The SQLAlchemy model that is used to generate the list of values.
id_field
The property of the model that is used as value. Default: id.
label_field
The property of the model that is used as label. Default: label.
order_by
The property of the model that is used for the order_by clause of the SQL query. Default: None.
edit_url (optionnal)
a function taking request and value as parameter and returning an url to the correponding resource.

For further attributes, please refer to the documentation of deform.widget.Select2Widget in the deform documentation: <http://deform.readthedocs.org/en/latest/api.html>

class c2cgeoform.ext.deform_ext.RelationRadioChoiceWidget(model, id_field='id', label_field='label', order_by=None, **kw)

Extension of the widget ``deform.widget.RadioChoiceWidget which loads the values from the database using a SQLAlchemy model.

Example usage

districtId = Column(Integer, ForeignKey('district.id'), info={
    'colanderalchemy': {
        'title': 'District',
        'widget': deform_ext.RelationRadioChoiceWidget(
            District,
            'id',
            'name',
            order_by='name'
        )
    }})

The values of this field are filled with entries of model District, whereas property id is used as value and name as label.

Attributes/Arguments

model (required)
The SQLAlchemy model that is used to generate the list of values.
id_field
The property of the model that is used as value. Default: id.
label_field
The property of the model that is used as label. Default: label.
order_by
The property of the model that is used for the order_by clause of the SQL query. Default: None.

For further attributes, please refer to the documentation of deform.widget.RadioChoiceWidget in the deform documentation: <http://deform.readthedocs.org/en/latest/api.html>

class c2cgeoform.ext.deform_ext.RelationSearchWidget(url, **kw)

A Deform widget to select an item via a search field. This widget is similar to the RelationSelectWidget, but instead of a select-box a Twitter Typeahead search field is shown.

Example usage:

address_id = Column(Integer, ForeignKey('address.id'), info={
    'colanderalchemy': {
        'title': _('Address'),
        'widget': deform_ext.RelationSearchWidget(
            url=lambda request: request.route_url('addresses'),
            model=Address,
            min_length=1,
            id_field='id',
            label_field='label'
        )
    }})

The user is responsible for providing a web-service at the given URL. The web service should expect requests of the form ?term=<search_terms>. And it should return responses of this form:

[{"id": 0, "label": "foo"}, {"id": 1, "label": "bar"}]

The name of the id and label keys are configurable. See below.

Attributes/arguments

url (required)
The search service URL, or a function that takes a request a return the search service URL.
model (required)
The SQLAlchemy model class associated to the linked table.
min_length
The minimum character length needed before suggestions start getting rendered. Default: 1.
id_field
The name of the “id” property in JSON responses. Default: "id".
label_field
The name of the “label” property in JSON responses. Default: "label".
limit
The maximum number of suggestions. Default: 8.
class c2cgeoform.ext.deform_ext.RelationSelect2Widget(model, id_field='id', label_field='label', default_value=None, order_by=None, **kw)

Extension of the widget ``deform.widget.Select2Widget which loads the values from the database using a SQLAlchemy model.

Example usage

districtId = Column(Integer, ForeignKey('district.id'), info={
    'colanderalchemy': {
        'title': 'District',
        'widget': deform_ext.RelationSelect2Widget(
            District,
            'id',
            'name',
            order_by='name',
            default_value=('', _('- Select -'))
        )
    }})

The values of this <select> field are filled with entries of model District, whereas property id is used as value and name as label.

For n:m relations the widget can be used like so:

situations = relationship(
    "Situation",
    secondary=situation_for_permission,
    cascade="save-update,merge,refresh-expire",
    info={
        'colanderalchemy': {
            'title': _('Situations'),
            'widget': RelationSelect2Widget(
                Situation,
                'id',
                'name',
                order_by='name',
                multiple=True
            ),
            'includes': ['id'],
            'validator': manytomany_validator
        }
    })

Attributes/Arguments

model (required)
The SQLAlchemy model that is used to generate the list of values.
id_field
The property of the model that is used as value. Default: id.
label_field
The property of the model that is used as label. Default: label.
order_by
The property of the model that is used for the order_by clause of the SQL query. Default: None.
default_value
A default value that is added add the beginning of the list of values that were loaded from the database. For example: default_value=('', _('- Select -')) Default: None.
multiple
Allow to select multiple values. Requires a n:m relationship. Default: False.

For further attributes, please refer to the documentation of deform.widget.Select2Widget in the deform documentation: <http://deform.readthedocs.org/en/latest/api.html>

class c2cgeoform.ext.deform_ext.RelationSelectMapWidget(url, label_field='label', **kw)

A Deform widget to select an item on a map. From the idea, this widget is similar to the RelationSelectWidget, but instead of a select-box a map is shown.

Example usage

bus_stop = Column(Integer, ForeignKey('bus_stops.id'), info={
    'colanderalchemy': {
        'title': 'Bus stop',
        'widget': deform_ext.RelationSelectMapWidget(
            label_field='name', url='/bus_stops'
        )
    }})

The user is responsible for providing a web-service under the given URL, which returns a list of features as GeoJSON. The features must contain the two properties specified with id_field and label_field. The geometries are expected to use the CRS EPSG:4326.

To customize the map, the template file map_select.pt has to be overwritten.

Attributes/Arguments

url (required)

The URL to the web-service which returns the GeoJSON features or a callback function function(request) -> string which returns the URL to the web-service. Example usage:

'widget': deform_ext.RelationSelectMapWidget(
    label_field='name',
    url=lambda request: request.route_url('bus_stops')
)
label_field
The property of the GeoJSON features that is used as label. Default: label.
class c2cgeoform.ext.deform_ext.RelationSelectWidget(model, id_field='id', label_field='label', default_value=None, order_by=None, **kw)

Extension of the widget ``deform.widget.SelectWidget which loads the values from the database using a SQLAlchemy model.

Example usage

districtId = Column(Integer, ForeignKey('district.id'), info={
    'colanderalchemy': {
        'title': 'District',
        'widget': deform_ext.RelationSelectWidget(
            District,
            'id',
            'name',
            order_by='name',
            default_value=('', _('- Select -'))
        )
    }})

The values of this <select> field are filled with entries of model District, whereas property id is used as value and name as label.

For n:m relations the widget can be used like so:

situations = relationship(
    "Situation",
    secondary=situation_for_permission,
    cascade="save-update,merge,refresh-expire",
    info={
        'colanderalchemy': {
            'title': _('Situations'),
            'widget': RelationSelectWidget(
                Situation,
                'id',
                'name',
                order_by='name',
                multiple=True
            ),
            'includes': ['id'],
            'validator': manytomany_validator
        }
    })

Attributes/Arguments

model (required)
The SQLAlchemy model that is used to generate the list of values.
id_field
The property of the model that is used as value. Default: id.
label_field
The property of the model that is used as label. Default: label.
order_by
The property of the model that is used for the order_by clause of the SQL query. Default: None.
default_value
A default value that is added add the beginning of the list of values that were loaded from the database. For example: default_value=('', _('- Select -')) Default: None.
multiple
Allow to select multiple values. Requires a n:m relationship. Default: False.

For further attributes, please refer to the documentation of deform.widget.SelectWidget in the deform documentation: <http://deform.readthedocs.org/en/latest/api.html>