Skip to content
This repository was archived by the owner on Feb 1, 2020. It is now read-only.

Allow billy to handle newly-defined Imago boundary sets #257

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion billy/core/default_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
MONGO_USER_DATABASE = 'billy_userdata'

BOUNDARY_SERVICE_URL = 'http://localhost:8001/1.0/'
BOUNDARY_SERVICE_SETS = 'sldl-14,sldu-14,nh-12'
BOUNDARY_SERVICE_SETS = r'sld[lu]-[a-z]{2}-[0-9]{2}(?:-.+)?'
ENABLE_ELASTICSEARCH = False
ENABLE_ELASTICSEARCH_PUSH = False
ELASTICSEARCH_HOST = '127.0.0.1:9200'
Expand Down
26 changes: 12 additions & 14 deletions billy/web/api/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from piston.handler import BaseHandler, HandlerMetaClass

AT_LARGE = ['At-Large', 'Chairman']
ACTIVE_BOUNDARY_SETS = settings.BOUNDARY_SERVICE_SETS.split(",")
active_boundary_sets_re = re.compile(settings.BOUNDARY_SERVICE_SETS)


_lower_fields = (settings.LEVEL_FIELD, 'chamber')
Expand Down Expand Up @@ -402,17 +402,12 @@ def read(self, request):

for dist in resp['results']:
ocdid = dist['id']
# ocd-division/country:us/state:oh/cd:11
_, localpart = ocdid.rsplit("/", 1)
set_, series = localpart.split(":", 1)
if set_ not in ['sldl', 'sldu', 'ward']:
# Place, CD, County, ...
continue

districts = db.districts.find({'division_id': ocdid})
count = districts.count()

if count == 1:
if count == 0:
continue
elif count == 1:
district = districts[0]
boundary_id = district['division_id']

Expand All @@ -425,7 +420,7 @@ def read(self, request):
district['chamber'])] = boundary_id

jurisdiction = district['abbr']
elif count != 0:
elif count >= 2:
raise ValueError('multiple districts with boundary_id: %s' %
boundary_id)

Expand Down Expand Up @@ -489,10 +484,13 @@ def _ocd_id_to_shape_url(self, ocd_id):
settings.API_KEY,
)
data = json.load(urllib2.urlopen(url))
geometries = filter(
lambda x: x['boundary_set']['name'] in ACTIVE_BOUNDARY_SETS,
data['geometries']
)
today = datetime.date.today().strftime('%Y-%m-%d')
geometries = [
x for x in data['geometries'] if
active_boundary_sets_re.match(x['boundary_set']['name']) and
(x['boundary_set']['start_date'] <= today or x['boundary_set']['start_date'] is None) and
(x['boundary_set']['end_date'] >= today or x['boundary_set']['end_date'] is None)
]
if len(geometries) == 1:
geom, = geometries
return "{}{}".format(settings.BOUNDARY_SERVICE_URL,
Expand Down