def save(self, *args, **kwargs): if self.address: geocoder = Nominatim(user_agent='represent') g = geocoder.geocode(self.address, country_codes='us', addressdetails=True) lat, long = [g.raw.get(i) for i in ['lat', 'lon']] city = g.raw.get('address').get('city') county = g.raw.get('address').get('county') self.city = city self.county = county legislators = locate_legislators(lat=lat, long=long) self.rep = [ i.get('leg_id') for i in legislators if i.get('chamber') == 'lower' ][0] self.senator = [ i.get('leg_id') for i in legislators if i.get('chamber') == 'upper' ][0] super().save(*args, **kwargs)
def testLegislatorGeolocation(self): """Legislator geolocation""" lat = 35.79 long = 78.38 state = "nc" results = pyopenstates.locate_legislators(lat, long) for legislator in results: self.assertEqual(legislator["state"], state.lower())
def get_state_legislators(self, location): if not location.latitude and location.longitude: raise LocationError('USDataProvider.get_state_legislators requires location with lat/lon') legislators = pyopenstates.locate_legislators(location.latitude, location.longitude) # save results individually in local cache for leg in legislators: key = self.KEY_OPENSTATES.format(id=leg['leg_id']) leg['cache_key'] = key self.cache_set(key, leg) return legislators
def get_state_legislators(self, location): if not (location.latitude and location.longitude): location = self.get_location(LOCATION_POSTAL, location.raw, ignore_local_cache=True) if not (location.latitude and location.longitude): raise LocationError('USDataProvider.get_state_legislators requires location with lat/lon') legislators = pyopenstates.locate_legislators(location.latitude, location.longitude) # save results individually in local cache for leg in legislators: key = self.KEY_OPENSTATES.format(id=leg['leg_id']) leg['cache_key'] = key self.cache_set(key, leg) return legislators