Beispiel #1
0
        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)
Beispiel #2
0
        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)
Beispiel #3
0
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
Beispiel #4
0
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
Beispiel #5
0
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
Beispiel #6
0
    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
Beispiel #7
0
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
Beispiel #8
0
    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
Beispiel #9
0
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
Beispiel #10
0
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
Beispiel #11
0
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)
Beispiel #12
0
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)