def make_schema(cls, **kwargs): """ Requires a class Returns a colander schema """ schema = SchemaNode(Mapping()) columns = make_columns(cls) map(lambda elem: columns.pop(elem), kwargs.get('excludes', [])) # remove the id from the form columns.pop('id') if 'type' in columns.keys(): columns.pop('type') if 'uuid' in columns.keys(): columns.pop('uuid') for k, v in columns.iteritems(): if isinstance(v, list): values = [] for elem in v[1]: values.append((elem[1], elem[2])) schema.add(SchemaNode( String(), widget=widget.SelectWidget(values=values), name=k )) else: schema.add(SchemaNode(v(), name=k)) return schema
def definition(cls, **kwargs): schema = SchemaNode(Mapping(unknown="preserve")) schema.add(SchemaNode(String(), name='label', missing=u'')) schema.add( SchemaNode(String(), name='type', validator=OneOf(["annotation"]))) return schema
def ballot_voting_form(ballot_voting, request): yes_no_choices = [(True, _('Yes')), (False, _('No')), (None, _('Abstention'))] max_points = 9 if len(ballot_voting.options) > 3 else 3 point_choices = [(i, str(i)) for i in range(max_points+1)] schema = Schema() for option in ballot_voting.options: option_schema = SchemaNode(Mapping(), name=str(option.uuid), title=option.title) option_schema.add(SchemaNode( Boolean(), validator=OneOf([x[0] for x in yes_no_choices]), widget=RadioChoiceWidget(values=yes_no_choices, inline=True), name='yes_no', title=f'Stimmst du dem Antrag zu?')) option_schema.add(SchemaNode( Int(), validator=OneOf([x[0] for x in point_choices]), widget=RadioChoiceWidget(values=point_choices, inline=True, null_value='0'), name='points', title='Welche Punktzahl gibst du dem Antrag?', missing=0)) schema.add(option_schema) form = Form(schema, request, buttons=[Button(title=_('check'))]) return form
def definition(cls): schema = SchemaNode(Mapping()) schema.add(SchemaNode(String(), name='name', validator=Regex(r'^[a-zA-Z][a-zA-Z0-9_\-]*$'))) schema.add(SchemaNode(String(), name='label', missing=u'')) schema.add(SchemaNode(String(), name='hint', missing=cls.hint)) schema.add(SchemaNode(Boolean(), name='required', missing=cls.required)) schema.add(SchemaNode(String(), name='type', validator=OneOf(registry.names))) return schema
def deserialize(self, cstruct=null): if cstruct: self.children = [] self.add(SchemaNode(String(), name='title', missing=drop)) self.add(SchemaNode(String(), name='description', missing=drop)) roles = [key for key in six.iterkeys(cstruct) if key not in ('title', 'description')] for key in roles: permSchema = SchemaNode(Mapping(unknown='raise'), name=key) for domain in ('definition', 'records', 'users', 'policy'): permSchema.add(self._crudSchema(domain)) self.add(permSchema) return super(PolicyValidator, self).deserialize(cstruct)
def definition(cls, **kwargs): schema = SchemaNode(Mapping(unknown="preserve")) if kwargs.get('named', True): schema.add(SchemaNode(String(), name='name', validator=Regex(r'^[a-zA-Z][a-zA-Z0-9_\-]*$'))) schema.add(SchemaNode(String(), name='label', missing=u'')) schema.add(SchemaNode(String(), name='hint', missing=cls.hint)) schema.add(SchemaNode(Boolean(), name='required', missing=cls.required)) schema.add(SchemaNode(String(), name='type', validator=OneOf(registry.names))) return schema
def definition(cls, **kwargs): schema = SchemaNode(Mapping(unknown="preserve")) if kwargs.get('named', True): schema.add( SchemaNode(String(), name='name', validator=Regex(r'^[a-zA-Z][a-zA-Z0-9_\-]*$'))) schema.add(SchemaNode(String(), name='label', missing=u'')) schema.add(SchemaNode(String(), name='hint', missing=cls.hint)) schema.add(SchemaNode(Boolean(), name='required', missing=cls.required)) schema.add( SchemaNode(String(), name='type', validator=OneOf(registry.names))) return schema
def add_post_data_subschemas(node: SchemaNode, kw: dict): """Add the resource sheet colander schemas that are 'creatable'.""" context = kw['context'] request = kw['request'] content_type = _get_resource_type_based_on_request_type(request) try: iresource = ContentType().deserialize(content_type) except colander.Invalid: return # the content type is validated later, so we just ignore errors registry = request.registry.content creates = registry.get_sheets_create(context, request, iresource) for sheet in creates: name = sheet.meta.isheet.__identifier__ is_mandatory = sheet.meta.create_mandatory missing = colander.required if is_mandatory else colander.drop schema = sheet.meta.schema_class(name=name, missing=missing) node.add(schema.bind(**kw))
def definition(cls): schema = SchemaNode(Mapping()) schema.add(SchemaNode(String(), name='name')) schema.add(SchemaNode(String(), name='description', missing='')) schema.add(SchemaNode(String(), name='type', validator=OneOf(registry.names))) return schema
def definition(cls): schema = SchemaNode(Mapping()) schema.add(SchemaNode(String(), name='name')) schema.add(SchemaNode(String(), name='description')) schema.add( SchemaNode(String(), name='type', validator=OneOf(types.names))) return schema
def _crudSchema(self, domain): crudSchema = SchemaNode(Mapping(unknown='raise'), name=domain, missing=drop) crudSchema.add(SchemaNode(Boolean(), name='create', missing=drop)) crudSchema.add(SchemaNode(Boolean(), name='read', missing=drop)) crudSchema.add(SchemaNode(Boolean(), name='update', missing=drop)) crudSchema.add(SchemaNode(Boolean(), name='delete', missing=drop)) return crudSchema
class InheritedSchema(TestingSchema): foo = SchemaNode(Int(), missing=1) class ToBoundSchema(TestingSchema): foo = SchemaNode(Int(), missing=1) bazinga = SchemaNode(String(), type='str', location="body", validator=deferred_validator) class DropSchema(MappingSchema): foo = SchemaNode(String(), type='str', missing=drop) bar = SchemaNode(String(), type='str') imperative_schema = SchemaNode(Mapping()) imperative_schema.add(SchemaNode(String(), name='foo', type='str')) imperative_schema.add( SchemaNode(String(), name='bar', type='str', location="body")) imperative_schema.add( SchemaNode(String(), name='baz', type='str', location="querystring")) class TestingSchemaWithHeader(MappingSchema): foo = SchemaNode(String(), type='str') bar = SchemaNode(String(), type='str', location="body") baz = SchemaNode(String(), type='str', location="querystring") qux = SchemaNode(String(), type='str', location="header") class TestSchemas(TestCase): def test_colander_integration(self): # not specifying body should act the same way as specifying it schema = CorniceSchema.from_colander(TestingSchema)
class StrictMappingSchema(MappingSchema): @staticmethod def schema_type(): return MappingSchema.schema_type(unknown='raise') class StrictSchema(StrictMappingSchema): foo = SchemaNode(String(), type='str', location="body", missing=drop) bar = SchemaNode(String(), type='str', location="body") class NestedSchema(MappingSchema): egg = StrictSchema(location='querystring') ham = StrictSchema(location='body') imperative_schema = SchemaNode(Mapping()) imperative_schema.add(SchemaNode(String(), name='foo', type='str')) imperative_schema.add(SchemaNode(String(), name='bar', type='str', location="body")) imperative_schema.add(SchemaNode(String(), name='baz', type='str', location="querystring")) class TestingSchemaWithHeader(MappingSchema): foo = SchemaNode(String(), type='str') bar = SchemaNode(String(), type='str', location="body") baz = SchemaNode(String(), type='str', location="querystring") qux = SchemaNode(String(), type='str', location="header") class MockRequest(object): def __init__(self, body, get=None): self.content_type = 'application/json' self.headers = {}
def definition(cls, **kwargs): schema = SchemaNode(Mapping(unknown="preserve")) schema.add(SchemaNode(String(), name="label", missing=u"")) schema.add(SchemaNode(String(), name="type", validator=OneOf(["annotation"]))) return schema
def _add_node(schema: SchemaNode, node: SchemaNode, name: str): node = node.bind(**schema.bindings) node.name = name schema.add(node)
class DefaultSchema(MappingSchema): foo = SchemaNode(String(), type="str", location="querystring", missing="foo") bar = SchemaNode(String(), type="str", location="querystring", default="bar") class DefaultValueSchema(MappingSchema): foo = SchemaNode(Int(), type="int") bar = SchemaNode(Int(), type="int", missing=10) class QsSchema(MappingSchema): foo = SchemaNode(String(), type="str", location="querystring", missing=drop) class StrictQsSchema(StrictMappingSchema): foo = SchemaNode(String(), type="str", location="querystring", missing=drop) imperative_schema = SchemaNode(Mapping()) imperative_schema.add(SchemaNode(String(), name="foo", type="str")) imperative_schema.add(SchemaNode(String(), name="bar", type="str", location="body")) imperative_schema.add(SchemaNode(String(), name="baz", type="str", location="querystring")) class TestingSchemaWithHeader(MappingSchema): foo = SchemaNode(String(), type="str") bar = SchemaNode(String(), type="str", location="body") baz = SchemaNode(String(), type="str", location="querystring") qux = SchemaNode(String(), type="str", location="header") class PreserveUnkownSchema(MappingSchema): bar = SchemaNode(String(), type="str") @staticmethod def schema_type(): return Mapping(unknown="preserve")