def resourcemap(req): """Resource-specific JSON response listing all resource instances.""" rsc = req.params.get('rsc') if rsc == 'language': q = DBSession.query( common.Language.id, common.Language.name, common.Language.latitude, common.Language.longitude, common.Identifier.type.label('itype'), common.Identifier.name.label('iname')).select_from( common.Language).outerjoin( join( common.LanguageIdentifier, common.Identifier, and_( common.LanguageIdentifier.identifier_pk == common.Identifier.pk, common.Identifier.type != 'name'))).filter( common.Language.active == true()).order_by( common.Language.id) def resources(): for (id, name, lat, lon), rows in itertools.groupby( q, operator.itemgetter(0, 1, 2, 3)): identifiers = [{ 'type': r.itype, 'identifier': r.iname.lower() if r.itype.startswith('WALS') else r.iname } for r in rows if r.iname is not None] yield { 'id': id, 'name': name, 'latitude': lat, 'longitude': lon, 'identifiers': identifiers } elif rsc == 'parameter': q = DBSession.query(common.Parameter.id, common.Parameter.name).order_by( common.Parameter.pk) def resources(): for id, name in q: yield {'id': id, 'name': name} else: return HTTPNotFound() return { 'properties': { 'dataset': req.dataset.id, 'uri_template': get_url_template(req, rsc, relative=False) }, 'resources': list(resources()) }
def resourcemap(req): res = {'properties': {'dataset': req.dataset.id}, 'resources': []} rsc = req.params.get('rsc') if rsc: res['properties']['uri_template'] = get_url_template(req, rsc, relative=False) if rsc == 'language': q = DBSession.query( common.Language.id, common.Language.name, common.Language.latitude, common.Language.longitude, common.Identifier.type, common.Identifier.name)\ .join(common.Language.languageidentifier)\ .join(common.LanguageIdentifier.identifier)\ .filter(common.Language.active == True)\ .filter(common.Identifier.type != 'name')\ .order_by(common.Language.id) for lang, codes in groupby(q, lambda r: (r[0], r[1], r[2], r[3])): res['resources'].append({ 'id': lang[0], 'name': lang[1], 'latitude': lang[2], 'longitude': lang[3], 'identifiers': [{ 'type': c.type, 'identifier': c.name.lower() if c.type.startswith('WALS') else c.name } for c in codes] }) return res if rsc == 'parameter': for id, name in DBSession.query( common.Parameter.id, common.Parameter.name, ).order_by(common.Parameter.pk): res['resources'].append({'id': id, 'name': name}) return res return HTTPNotFound()
def resourcemap(req): """Resource-specific JSON response listing all resource instances.""" rsc = req.params.get('rsc') if rsc == 'language': q = DBSession.query( common.Language.id, common.Language.name, common.Language.latitude, common.Language.longitude, common.Identifier.type.label('itype'), common.Identifier.name.label('iname') ).select_from(common.Language).outerjoin(join( common.LanguageIdentifier, common.Identifier, and_( common.LanguageIdentifier.identifier_pk == common.Identifier.pk, common.Identifier.type != 'name') )).filter(common.Language.active == true()).order_by(common.Language.id) def resources(): for (id, name, lat, lon), rows in groupby(q, itemgetter(0, 1, 2, 3)): identifiers = [ {'type': r.itype, 'identifier': r.iname.lower() if r.itype.startswith('WALS') else r.iname} for r in rows if r.iname is not None] yield {'id': id, 'name': name, 'latitude': lat, 'longitude': lon, 'identifiers': identifiers} elif rsc == 'parameter': q = DBSession.query( common.Parameter.id, common.Parameter.name ).order_by(common.Parameter.pk) def resources(): for id, name in q: yield {'id': id, 'name': name} else: return HTTPNotFound() return { 'properties': { 'dataset': req.dataset.id, 'uri_template': get_url_template(req, rsc, relative=False)}, 'resources': list(resources())}
def resourcemap(req): res = {'properties': {'dataset': req.dataset.id}, 'resources': []} rsc = req.params.get('rsc') if rsc: res['properties']['uri_template'] = get_url_template(req, rsc, relative=False) if rsc == 'language': q = DBSession.query( common.Language.id, common.Language.name, common.Language.latitude, common.Language.longitude, common.Identifier.type, common.Identifier.name)\ .join(common.Language.languageidentifier)\ .join(common.LanguageIdentifier.identifier)\ .filter(common.Language.active == True)\ .filter(common.Identifier.type != 'name')\ .order_by(common.Language.id) for lang, codes in groupby(q, lambda r: (r[0], r[1], r[2], r[3])): res['resources'].append({ 'id': lang[0], 'name': lang[1], 'latitude': lang[2], 'longitude': lang[3], 'identifiers': [ {'type': c.type, 'identifier': c.name.lower() if c.type.startswith('WALS') else c.name} for c in codes]}) return res if rsc == 'parameter': for id, name in DBSession.query( common.Parameter.id, common.Parameter.name, ).order_by(common.Parameter.pk): res['resources'].append({'id': id, 'name': name}) return res return HTTPNotFound()
def url_template(req, route, id_name): return URITemplate(get_url_template( req, route, relative=False, variable_map={'id': id_name}))
def test_get_url_template(env): from clld.web.util.helpers import get_url_template assert get_url_template(env['request'], 'parameter', variable_map={'id': 'ID'}) == '/parameters/{ID}'
def url_template(req, route, id_name): return URITemplate( get_url_template(req, route, relative=False, variable_map={'id': id_name}))
def test_get_url_template(self): from clld.web.util.helpers import get_url_template self.assertEqual( get_url_template(self.env['request'], 'parameter', variable_map={'id': 'ID'}), '/parameters/{ID}')