''' Schemas (sub) package, for form rendering and validation ''' import colander import deform from deform.renderer import configure_zpt_renderer from .validators import ( deferred_csrf_default, deferred_csrf_validator ) # Base Schema class CSRFSchema(colander.Schema): ''' base class for schemas with csrf validation ''' csrf_token = colander.SchemaNode( colander.String(), default=deferred_csrf_default, validator=deferred_csrf_validator, widget=deform.widget.HiddenWidget(), ) @classmethod def as_form(cls, request, action=None, **kwargs): ''' returns the schema as a form :param pyramid.request.Request request: the current request :param str url: form action url, url is not set, the current context and view name will be used to constuct a url for the form :param kwargs: additional parameters for the form rendering. ''' schema = cls().bind(request=request) if action is None: action = request.resource_url(request.context, request.view_name) settings = {'col_label': 3, 'col_input': 9, 'action': action} settings.update(kwargs) return deform.Form(schema, **settings) def includeme(config): # pragma: no cover ''' Initialize the form schemas Activate this setup using ``config.include('ordr.schemas')``. ''' # Make Deform widgets aware of our widget template paths configure_zpt_renderer(['ordr:templates/deform'])