def add_csrf(schema: colander.Schema): """Add a hidden csrf_token field on the existing Colander schema.""" csrf_token = colander.SchemaNode(colander.String(), name="csrf_token", widget=deform.widget.HiddenWidget(), default=deferred_csrf_value) schema.add(csrf_token)
def _add_reference_node(schema: colander.Schema, target_isheet=None): from adhocracy_core.interfaces import ISheet from adhocracy_core.interfaces import SheetToSheet from adhocracy_core.schema import Reference reference_node = Reference(name='reference') isheet = target_isheet or ISheet class PostPoolReference(SheetToSheet): target_isheet = isheet schema.add(reference_node) schema['reference'].reftype = PostPoolReference
def add_csrf(schema: colander.Schema): """Add a hidden CSRF field on the schema.""" csrf_token = colander.SchemaNode( colander.String(), name="csrf_token", widget=deform.widget.HiddenWidget(), default=deferred_csrf_value, validator=deferred_csrf_validator, ) schema.add(csrf_token)
def add_put_data_subschemas(node: colander.Schema, kw: dict): """Add the resource sheet colander schemas that are 'editable'.""" context = kw.get('context', None) request = kw.get('request', None) sheets = request.registry.content.get_sheets_edit(context, request) if request.content_type == 'multipart/form-data': body = unflatten_multipart_request(request) else: body = request.json_body data = body.get('data', {}) for sheet in sheets: name = sheet.meta.isheet.__identifier__ if name not in data: continue subschema = sheet.meta.schema_class(name=name) node.add(subschema.bind(**kw))
def validator_from_model(model): validator = Schema(Mapping()) for key, column in model.__table__.columns.items(): if key in _excludes: # skip things like ID that won't be in the body continue missing = colander.required if column.nullable: missing = colander.drop validator.add( SchemaNode(sqla_to_colander_type(column.type), name=key, missing=missing, **sqla_kwargs(column.type))) return CorniceSchema.from_colander(validator)
def validator_from_model(model): validator = Schema(Mapping()) for key, column in model.__table__.columns.items(): if key in _excludes: # skip things like ID that won't be in the body continue missing = colander.required if column.nullable: missing = colander.drop validator.add( SchemaNode( sqla_to_colander_type(column.type), name=key, missing=missing, **sqla_kwargs(column.type) ) ) return CorniceSchema.from_colander(validator)
def _add_post_pool_node(inst: colander.Schema, iresource_or_service_name=IPool): from adhocracy_core.schema import PostPool post_pool_node = PostPool( name='post_pool', iresource_or_service_name=iresource_or_service_name) inst.add(post_pool_node)
def add_csrf(schema: colander.Schema): """Add a hidden CSRF field on the schema.""" csrf_token = colander.SchemaNode(colander.String(), name="csrf_token", widget=deform.widget.HiddenWidget(), default=deferred_csrf_value, validator=deferred_csrf_validator,) schema.add(csrf_token)
def add_csrf(schema: colander.Schema): """Add a hidden csrf_token field on the existing Colander schema.""" csrf_token = colander.SchemaNode(colander.String(), name="csrf_token", widget=deform.widget.HiddenWidget(), default=deferred_csrf_value) csrf_token.dictify_by_default = False schema.add(csrf_token)
def _add_other_node(inst: colander.Schema): other_node = colander.MappingSchema(name='other', missing={}) inst.add(other_node)
def _add_references_node(inst: colander.Schema): from adhocracy_core.schema import UniqueReferences reference_node = UniqueReferences(name='references') inst.add(reference_node)
class MappingFields(object): def __init__(self, title, name, prefix, l_fields, round=1, desc=None, widget=None): self.title = title self.desc = desc self.prefix = prefix self.name = name self.l_fields = [] for f in l_fields: try: assert (not isinstance(f, MappingFields)) except AssertionError: raise ValueError("Only 1 depth of mapping is allowed") f_ = copy.deepcopy(f) f_.sn.name = '{}-{}'.format(prefix, f.name) self.l_fields += [f_] self.round = round self.mapinit = {f.name: f.mapinit for f in l_fields} self.processing_form = { 'form': lambda x: { f.name: f.processing_form['form'](x[f.name]) for f in self.l_fields }, 'db': lambda x: { f.sn.name: f.processing_form['db'](x[f.name]) for f in self.l_fields } } self.sn = Schema(title=self.title, name=name, description=desc, widget=widget) for f in self.l_fields: self.sn.add(f.sn) def set_mode(self): return MappingFields(self.title, self.name, self.prefix, self.l_fields, self.round, desc=self.desc) def hidden_mode(self): return MappingFields(self.title, self.name, self.prefix, self.l_fields, self.round, widget=HiddenWidget())
def _add_post_pool_node(inst: colander.Schema, iresource_or_service_name=IPool): from adhocracy_core.schema import PostPool post_pool_node = PostPool(name='post_pool', iresource_or_service_name=iresource_or_service_name) inst.add(post_pool_node)
def admin_view(self): schema = Schema() schema.add(self.context.question_schema_node('dummy', lang = self.get_lang(), context = self.context)) form = Form(schema) self.response['form_resources'] = form.get_widget_resources() self.response['dummy_form'] = form.render() return self.response