def create(silent=False): """ Create the geometry model from the geometry__country_level0 table """ from openspending.core import db from openspending.model.country import Country result = db.engine.execute("SELECT \ country_level0.gid as gid \ FROM public.geometry__country_level0 as country_level0;" ) for row in result: gid = row['gid'] #check if it already exists and don't overwrite if Country.by_gid(gid): if not silent: log.info("Found existing gid " + str(gid) + ". Will not replace.") continue tempcountry = Country(gid) db.session.add(tempcountry) db.session.commit()
def reindex(): """ Grant admin privileges to given user """ from openspending.core import db from openspending.model import Dataset from openspending.model.country import Country index = whoo.whoosh_index(current_app, Dataset) with index.writer() as writer: for dataset in Dataset.all(): primary_field = dataset.pure_whoosh.primary_key_name searchable = dataset.__searchable__ attrs = {} for key in searchable: try: attrs[key] = unicode(getattr(dataset, key)) except AttributeError: raise AttributeError('{0} does not have {1} field {2}' .format("Dataset", __searchable__, key)) attrs[primary_field] = unicode(getattr(dataset, primary_field)) writer.update_document(**attrs) #make sure we ahve all of the geometry tables in there createCountries(silent=True) index = whoo.whoosh_index(current_app, Country) with index.writer() as writer: for country in Country.all(): primary_field = country.pure_whoosh.primary_key_name searchable = country.__searchable__ attrs = {} for key in searchable: try: attrs[key] = unicode(getattr(country, key)) except AttributeError: raise AttributeError('{0} does not have {1} field {2}' .format("Country", __searchable__, key)) attrs[primary_field] = unicode(getattr(country, primary_field)) writer.update_document(**attrs)
def reindex(): """ Grant admin privileges to given user """ from openspending.core import db from openspending.model import Dataset from openspending.model.country import Country index = whoo.whoosh_index(current_app, Dataset) with index.writer() as writer: for dataset in Dataset.all(): primary_field = dataset.pure_whoosh.primary_key_name searchable = dataset.__searchable__ attrs = {} for key in searchable: try: attrs[key] = unicode(getattr(dataset, key)) except AttributeError: raise AttributeError( '{0} does not have {1} field {2}'.format( "Dataset", __searchable__, key)) attrs[primary_field] = unicode(getattr(dataset, primary_field)) writer.update_document(**attrs) #make sure we ahve all of the geometry tables in there createCountries(silent=True) index = whoo.whoosh_index(current_app, Country) with index.writer() as writer: for country in Country.all(): primary_field = country.pure_whoosh.primary_key_name searchable = country.__searchable__ attrs = {} for key in searchable: try: attrs[key] = unicode(getattr(country, key)) except AttributeError: raise AttributeError( '{0} does not have {1} field {2}'.format( "Country", __searchable__, key)) attrs[primary_field] = unicode(getattr(country, primary_field)) writer.update_document(**attrs)
def create(silent = False): """ Create the geometry model from the geometry__country_level0 table """ from openspending.core import db from openspending.model.country import Country result = db.engine.execute("SELECT \ country_level0.gid as gid \ FROM public.geometry__country_level0 as country_level0;") for row in result: gid = row['gid'] #check if it already exists and don't overwrite if Country.by_gid(gid): if not silent: log.info("Found existing gid " + str(gid) + ". Will not replace.") continue tempcountry = Country(gid) db.session.add(tempcountry) db.session.commit()
def countries_list(): output = Country.get_all_json() return json.dumps(output)
def test_properties_regions(self): tempobj = Country.by_gid(1) assert tempobj.sovereignty == "Netherlands"
def test_properties_regions(self): tempobj = Country.by_gid(1) assert len(tempobj.regions.keys()) == 10 assert tempobj.label == "Aruba"
def test_all_countries(self): result = Country.get_all_json() assert len(result['data']) == 198 assert len(result['data'][0]['regions']) == 8
def countries_list(): country_data = {"data":{ "countries":{}, "regions":[] }} country_data['data']['countries'] = Country.get_all_json() countrygroupingset = [ { "id": "all", "label": "All Countries", "geounit": "all:all", "selected": False, "filtered": False, "regions": {} }, { "id": "continent", "label": "Continent", "geounit": "continent:all", "selected": False, "filtered": False, "regions": {} }, { "id": "dod_cmd", "label": "Department of Defense", "geounit": "dod_cmd:all", "selected": False, "filtered": False, "regions": {} }, { "id": "dos_region", "label": "Department of State", "geounit": "dos_region:all", "selected": False, "filtered": False, "regions": {} }, { "id": "usaid_reg", "label": "USAID", "geounit": "usaid_reg:all", "selected": False, "filtered": False, "regions": {} }, { "id": "wb_inc_lvl", "label": "Income Groups", "geounit": "wb_inc_lvl:all", "selected": False, "filtered": False, "regions": {} }] for countrygroupobj in countrygroupingset: if countrygroupobj['id'] == "all": countrygroupobj['regions']= {"countries": { "id": 'countries', "label": 'countries', "geounit": countrygroupobj['id'] + ":" + 'countries', "selected": False, "filtered": False, 'countries': [conobj for conobj in country_data['data']['countries']] }} continue for countryobj in country_data['data']['countries']: regionid = countryobj['regions'].get(countrygroupobj['id'], None) if regionid: if countrygroupobj['regions'].get(regionid): countrygroupobj['regions'][regionid]['countries'].append(countryobj) else: countrygroupobj['regions'][regionid] = \ { "id": regionid, "label": regionid, "geounit": countrygroupobj['id'] + ":" + regionid, "countries": [countryobj], "selected": False, "filtered": False } country_data['data']['regions'] = countrygroupingset # resp = Response(response=json.dumps({'data':json.dumps(country_data)}), # status=200, \ # mimetype="application/json") return json.dumps(country_data)