class VTSpeciesView(BaseResource): def __init__(self, request): super(VTSpeciesView, self).__init__(request) self.mgr = VTSpeciesManager(request.trfdb) self.limit = 25 self.genus_mgr = GenusManager(request.trfdb) self.wikicollector = WikiCollector() def serialize_object_for_collection_query(self, dbobj): data = dict() for field in ['id', 'genus_id', 'spec_id', 'cname', 'symbol']: data[field] = getattr(dbobj, field) data['genus'] = dbobj.genus.name data['species'] = dbobj.species.name return data def serialize_object(self, dbobj): data = dict() for field in ['id', 'genus_id', 'spec_id', 'cname', 'symbol', 'flower', 'leaf', 'form', 'fruit', 'bark', 'twig']: data[field] = getattr(dbobj, field) data['genus'] = dbobj.genus.name data['species'] = dbobj.species.name data['pictures'] = dict() for key, pobj in dbobj.pictures.items(): value = pobj.serialize() ipath = value['url'].split(url_prefix)[1].replace('%20', '_') ipath = ipath.split('images/')[1] ipath = os.path.join('vtimages', 'vtdendro', ipath) base = self.request.route_url('home') value['localurl'] = os.path.join(base, ipath) data['pictures'][key] = value data['looklikes'] = [dict(id=l.id, cname=l.cname) for l in dbobj.looklikes] data['wikipage'] = dbobj.wikipage return data def collection_query(self): query = self.mgr.query() GET = self.request.GET if 'genus' in GET: genus = self.genus_mgr.get_by_name(GET['genus']) if genus is not None: query = query.filter_by(genus_id=genus.id) else: raise HTTPNotFound, "No genus named %s" % GET['genus'] for field in ['cname', 'form', 'leaf', 'bark', 'fruit', 'flower', 'twig']: if field in GET: value = GET[field] if value: like = '%' + value + '%' dbfield = getattr(VTSpecies, field) query = query.filter(dbfield.like(like)) return query def get(self): id = int(self.request.matchdict['id']) c = self.mgr.get(id) return self.serialize_object(c)
class GenusView(BaseResource): def __init__(self, request): super(GenusView, self).__init__(request) self.mgr = GenusManager(request.trfdb) self.limit = 25 def collection_query(self): return self.mgr.query() def serialize_object(self, dbobj): data = dict() for field in ['id', 'name']: data[field] = getattr(dbobj, field) data['wikipage'] = dbobj.wiki.content return data def get(self): name = self.request.matchdict['name'] g = self.mgr.get_by_name(name) return self.serialize_object(g)
def __init__(self, request): super(VTSpeciesView, self).__init__(request) self.mgr = VTSpeciesManager(request.trfdb) self.limit = 25 self.genus_mgr = GenusManager(request.trfdb) self.wikicollector = WikiCollector()
def __init__(self, request): super(GenusView, self).__init__(request) self.mgr = GenusManager(request.trfdb) self.limit = 25