Skip to content

Commit

Permalink
[ZA] Fix intermittent problem with Ward Councillor lookups
Browse files Browse the repository at this point in the history
This code was making a bad assumption about the order of things in the
response from the code4sa mapit API. It was assuming that ward would
always come first, and then municipality. But it seems that was not the
case, the order of results from mapit is actually random and shouldn't
be relied on.

So this change adds some extra checks to ensure we're using the correct
bits of the JSON for the ward and municipality.
  • Loading branch information
chrismytton committed Feb 4, 2019
1 parent 9dcb3a3 commit fe2e374
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions pombola/south_africa/views/geolocalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,15 @@ def get_ward_councillors(self, location):
mapit_json = r.json()
if not mapit_json:
return []
ward_id = mapit_json.values()[0]['codes']['MDB']
muni_id = mapit_json.values()[1]['codes']['MDB']
ward = None
muni = None
for item in mapit_json.values():
if item['type_name'] == 'Ward':
ward = item
elif item['type_name'] == 'Municipality':
muni = item
ward_id = ward['codes']['MDB']
muni_id = muni['codes']['MDB']

# Then find the ward councillor from that ward ID. There
# should only be one at the moment, but make it a list in case
Expand Down

0 comments on commit fe2e374

Please sign in to comment.