def wrapper(*args, **kwargs): # TODO: use a wrapper so we can namespace the instance property # values away from anything else that is passed to the route # if all the primary keys are available then load the request.view_args # then load the instance or 404 if its not found pk = [pk.key for pk in sa.inspect(model_cls).primary_key] instance = None # TODO: eager load any schema fields that are relationships if set(request.view_args.keys()) >= set(pk): ident = tuple(request.view_args[pk] for pk in pk) instance = model_cls.query.get_or_404(ident) # parse() uses ModelSchema.load will populate an instance from the request # when an instance is provided if instance is None: instance = model_cls() schema = schema_factory(model_cls, instance=instance) try: schema.load(request.params, session=db.session) except ValidationError as err: instance._errors = err.messages return next(instance, *args, **kwargs)
def test_deserialize(session, plant): from bauble.schema import schema_factory session.add(plant) session.commit() plant_json = plant.jsonify() plant, errors = schema_factory(Plant).load(plant_json) assert errors is not None and len(errors) == 0 assert plant_json['accession_id'] == plant.accession_id assert plant_json['code'] == plant.code
def test_deserialize(session, vernacular_name): from bauble.schema import schema_factory session.add(vernacular_name) session.commit() vernacular_name_json = vernacular_name.jsonify() vernacular_name, errors = schema_factory(VernacularName).load(vernacular_name_json) assert errors is not None and len(errors) == 0 assert vernacular_name_json['taxon_id'] == vernacular_name.taxon_id assert vernacular_name_json['name'] == vernacular_name.name
def test_deserialize(session, taxon): from bauble.schema import schema_factory session.add(taxon) session.commit() taxon_json = taxon.jsonify() taxon, errors = schema_factory(Taxon).load(taxon_json) assert errors is not None and len(errors) == 0 assert taxon_json['genus_id'] == taxon.genus_id assert taxon_json['sp'] == taxon.sp
def jsonify(param, *args, schema_cls=None, **kwargs): cls = type(param) if isinstance(param, _Model) else param instance = param if isinstance(param, _Model) else args[0] schema = schema_factory(cls) data, err = schema.dump(instance, **kwargs) if len(err) > 0: raise SerializationError(err) return data
def test_deserialize(session, accession): from bauble.schema import schema_factory session.add(accession) session.commit() accession_json = accession.jsonify() accession, errors = schema_factory(Accession).load(accession_json) assert errors is not None and len(errors) == 0 assert accession_json['taxon_id'] == accession.taxon_id assert accession_json['code'] == accession.code
def test_deserialize(session, vernacular_name): from bauble.schema import schema_factory session.add(vernacular_name) session.commit() vernacular_name_json = vernacular_name.jsonify() vernacular_name, errors = schema_factory(VernacularName).load( vernacular_name_json) assert errors is not None and len(errors) == 0 assert vernacular_name_json['taxon_id'] == vernacular_name.taxon_id assert vernacular_name_json['name'] == vernacular_name.name
def create(): plant, errors = schema_factory(Plant).load(request.params) if errors: if request.prefers_json: return resource.render_json(errors, status=422) return resource.render_html(template_name='new.html.jinja', plant=plant, form=form_factory(plant)) db.session.add(plant) db.session.commit() if request.prefers_json: return resource.render_json(plant, status=201) return resource.render_html(template_name='edit.html.jinja', plant=plant, form=form_factory(plant), status=201)
def create(): accession, errors = schema_factory(Accession).load(request.params) if errors: if request.prefers_json: return resource.render_json(errors, status=422) return resource.render_html(template_name='new.html.jinja', accession=accession, form=form_factory(accession)) db.session.add(accession) db.session.commit() if request.prefers_json: return resource.render_json(accession, status=201) return resource.render_html(template_name='edit.html.jinja', accession=accession, form=form_factory(accession), status=201)