You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.4 KiB
54 lines
1.4 KiB
7 years ago
|
''' Schemas (sub) package, for form rendering and validation '''
|
||
|
|
||
|
import colander
|
||
|
import deform
|
||
|
|
||
|
from deform.renderer import configure_zpt_renderer
|
||
|
|
||
|
from .helpers 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, url=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.
|
||
|
'''
|
||
|
if url is None:
|
||
|
url = request.resource_url(request.context, request.view_name)
|
||
|
schema = cls().bind(request=request)
|
||
|
form = deform.Form(schema, action=url, **kwargs)
|
||
|
return form
|
||
|
|
||
|
|
||
|
def includeme(config):
|
||
|
'''
|
||
|
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'])
|