Exemplo n.º 1
0
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)
Exemplo n.º 2
0
    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)
Exemplo n.º 3
0
 def get_schema(self):
     return Schema()