def family_count(args, family_id): data = {} family = Family.query.get_or_404(family_id) for relation in args['relation']: _, base = relation.rsplit('/', 1) data[base] = utils.count_relation(family, relation) return utils.json_response(data)
def taxon_count(args, id): data = {} taxon = Taxon.query.get_or_404(id) for relation in args['relation']: _, base = relation.rsplit('/', 1) data[base] = utils.count_relation(taxon, relation) return utils.json_response(data)
def genus_count(args, id): data = {} genus = Genus.query.get_or_404(id) for relation in args['relation']: _, base = relation.rsplit('/', 1) data[base] = utils.count_relation(genus, relation) return utils.json_response(data)
def accession_count(args, id): data = {} accession = Accession.query.get_or_404(id) for relation in args['relation']: _, base = relation.rsplit('/', 1) data[base] = utils.count_relation(accession, relation) return utils.json_response(data)
def post_taxon(taxon): genus = Genus.query.filter_by(id=taxon.genus_id).first() if not genus: abort(422, "Invalid genus id") db.session.add(taxon) db.session.commit() return utils.json_response(taxon.jsonify(), 201)
def post_plant(plant): location_id = request.values.get('location_id', None) if location_id is not None: location = Location.query.filter_by(id=location_id).first() if not location: abort(422, "Invalid location id") accession_id = request.values.get('accession_id', None) if accession_id is not None: accession = Accession.query.filter_by(id=accession_id).first() if not accession: abort(422, "Invalid accession id") db.session.add(plant) # change = PlantChange(to_location_id=plant.location_id, # quantity=plant.quantity, # store original quantity # person=request.user.fullname if request.user.fullname is not None else request.user.email, # #reason=request.json['change'].get('reason', None) if 'change' in request.json else None, # reason=None, # date=request.json['change'].get('date', None) if 'change' in request.json else None # ) # change.plant = plant # request.session.add(change) db.session.commit() return utils.json_response(plant.jsonify(), 201)
def plant_count(args, id): data = {} plant = Plant.query.get_or_404(id) for relation in args['relation']: _, base = relation.rsplit('/', 1) data[base] = utils.count_relation(plant, relation) return utils.json_response(data)
def location_count(args, location_id): data = {} location = Location.query.get_or_404(location_id) for relation in args['relation']: _, base = relation.rsplit('/', 1) data[base] = utils.count_relation(location, relation) return utils.json_response(data)
def post_genus(genus): # TODO: do an exists query instead of loading the family family = Family.query.filter_by(id=genus.family_id).first() if not family: abort(422, "Invalid family id") db.session.add(genus) db.session.commit() return utils.json_response(genus.jsonify(), 201)
def render_json(self, model, status=200): if isinstance(model, (list, tuple))and len(model) > 0: data = type(model[0]).jsonify(model, many=True) elif isinstance(model, db.Model): data = model.jsonify() else: data = model return utils.json_response(data, status=status)
def index(args, ext=None): query = args.get("q", None) results = search(query, db.session) if query is not None else {} data = {} if request.prefers_json: value_factory = lambda k, v: [obj.jsonify() for obj in v] response_factory = lambda d: utils.json_response(d) else: value_factory = lambda k, v: [ {"url": result_resource_map.get(key).format(obj.id), "str": obj.str()} for obj in v ] response_factory = lambda d: render_template("search/index.html", results=d) for key, values in results.items(): if len(values) > 0: data[key] = value_factory(key, values) return response_factory(data)
def index(args, ext=None): query = args.get('q', None) results = search(query, db.session) if query is not None else {} data = {} if request.prefers_json: value_factory = lambda k, v: [obj.jsonify() for obj in v] response_factory = lambda d: utils.json_response(d) else: value_factory = lambda k, v: [{ 'url': result_resource_map.get(key).format(obj.id), 'str': obj.str() } for obj in v] response_factory = lambda d: render_template('search/index.html', results=d) for key, values in results.items(): if len(values) > 0: data[key] = value_factory(key, values) return response_factory(data)
def patch_plant(plant, id): location_id = request.values.get('location_id', None) if location_id is not None: location = Location.query.filter_by(id=location_id).first() if not location: abort(422, "Invalid location id") accession_id = request.values.get('accession_id', None) if accession_id is not None: accession = Accession.query.filter_by(id=accession_id).first() if not accession: abort(422, "Invalid accession id") # create the plant change # change = PlantChange(plant_id=request.plant.id, # from_location_id=request.plant.location_id, # quantity=request.plant.quantity, # store original quantity # person=request.user.fullname if request.user.fullname is not None else request.user.email, # # reason=request.json['change'].get('reason', None) if 'change' in request.json else None, # reason=None, # date=request.json['change'].get('date', None) if 'change' in request.json else None # ) # request.session.add(change) # if change.from_location_id != request.plant.location_id: # # the change quantity represent the number of plants tranferred to a new location # change.quantity = request.plant.quantity # change.to_location_id = request.plant.location_id # elif request.plant.quantity < change.quantity: # # the change quantity represents the number of plants removed from a location # change.quantity = request.plant.quantity - change.quantity # else: # # the change quantity represents the number of plants added to a location # change.quantity = request.plant.quantity - change.quantity db.session.commit() return utils.json_response(plant.jsonify())
def patch_accession(accession, id): # if 'source' in request.json: # if request.accession.source is None: # request.accession.source = Source() # source = request.accession.source # shorthand # source_json = request.json['source'] # source_data = {col: source_json[col] for col in source_json.keys() # if col in source_mutable} # source_data['source_detail_id'] = source_data.pop('id', None) # # make a copy of the data for only those fields that are columns # source.set_attributes(source_data) # # make sure the propagation type is not empty b/c we'll get an error # # trying to set the propagation details (even if it's an empty dict) if # # the prop_type hasn't been set # if 'propagation' in source_json and len(source_json['propagation']) > 0: # # TODO: validate prop_type # if source.propagation is None: # source.propagation = Propagation() # prop_data = source_json['propagation'] # source.propagation.prop_type = prop_data.pop('prop_type', source.propagation.prop_type) # prop_mutable = prop_seed_mutable if source.propagation.prop_type == 'Seed' \ # else prop_cutting_mutable # source.propagation.details = {col: prop_data[col] for col in prop_data.keys() # if col in prop_mutable} # if 'collection' in source_json and len(source_json['collection']) > 0: # # TODO: validate collection datand set mutable properties # if source.collection is None: # source.collection = Collection() # coll_data = {col: value for col, value in source_json['collection'].items() # if col in coll_mutable} # source.collection.set_attributes(coll_data) db.session.commit() return utils.json_response(accession.jsonify())
def post_accession(accession): taxon = Taxon.query.filter_by(id=accession.taxon_id).first() if not taxon: abort(422, "Invalid taxon id") # if 'source' in request.json: # source_json = request.json['source'] # source_data = {col: source_json[col] for col in source_json.keys() # if col in source_mutable} # source_data['source_detail_id'] = source_data.pop('id', None) # # make a copy of the data for only those fields that are columns # source = Source(**source_data) # request.session.add(source) # if 'propagation' in source_json: # # TODO: validate prop_type # prop_data = source_json['propagation'] # propagation = Propagation(prop_type=prop_data.pop('prop_type')) # prop_mutable = prop_seed_mutable if propagation.prop_type == 'Seed' \ # else prop_cutting_mutable # propagation.details = {col: prop_data[col] for col in prop_data.keys() # if col in prop_mutable} # source.propagation = propagation # collection = Collection() # if 'collection' in source_json: # # TODO: validate collection datand set mutable properties # coll_data = {col: value for col, value in source_json['collection'].items() # if col in coll_mutable} # collection = Collection(**coll_data) # source.collection = collection db.session.add(accession) db.session.commit() return utils.json_response(accession.jsonify(), 201)
def post_location(location): db.session.add(location) db.session.commit() return utils.json_response(location.jsonify(), 201)
def patch_location(location, id): db.session.commit() return utils.json_response(location.jsonify())
def get(family, id): return utils.json_response(family.jsonify())
def index_taxon(): taxon = Taxon.query.all() data = Taxon.jsonify(taxon, many=True) return utils.json_response(data)
def index_accession(): accession = Accession.query.all() data = Accession.jsonify(accession, many=True) return utils.json_response(data)
def list_geography(): geographies = Geography.query.all() return utils.json_response(Geography.jsonify(geographies, many=True))
def post_family(family): db.session.add(family) db.session.commit() return utils.json_response(family.jsonify(), 201)
def patch_family(family, id): db.session.commit() return utils.json_response(family.jsonify())
def get_family(family, family_id): return utils.json_response(family.jsonify())
def get_source(source_detail, id): return utils.json_response(source_detail.jsonify())
def patch_source(source_detail, id): db.session.commit() return utils.json_response(source_detail.jsonify())
def patch(family, id): return utils.json_response(family.jsonify())
def patch_taxon(taxon, taxon_id): db.session.commit() return utils.json_response(taxon.jsonify())
def get_geography(geography_id): geography = Geography.query.get_or_404(geography_id) return utils.json_response(geography.jsonify())
def get_taxon(taxon, id): return utils.json_response(taxon.jsonify())
def get_accession(accession, id): return utils.json_response(accession.jsonify())
def index_source(): sources = SourceDetail.query.all() data = SourceDetail.jsonify(sources, many=True) return utils.json_response(data)
def index_location(): locations = Location.query.all() data = Location.jsonify(locations, many=True) return utils.json_response(data)
def get_location(location, id): return utils.json_response(location.jsonify())
def index_family(): families = Family.query.all() data = Family.jsonify(families, many=True) return utils.json_response(data)
def post_source(source_detail): db.session.add(source_detail) db.session.commit() return utils.json_response(source_detail.jsonify(), 201)