Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.
Open
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
39 changes: 29 additions & 10 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,32 @@ def request_search():
else:
abort(404)

def fix_request_id_chi(request_id):
'''
A function to try to fix-up the request-id if we couldn't find it
on the first try.

:param request_id: the request-id to try and fix up
:return: the fixed up id (that may or may not actually be different

'''
# Chicago's SR IDs are always \d\d-\d{8}, if we get just digits, reformat and try again

request_id_digits = re.sub(r'\D', '', request_id)
if len(request_id_digits) == 8:
# Try prepending the year if it's only 8 digits
request_id_digits = datetime.date.today().strftime('%y') + request_id_digits
if len(request_id_digits) == 10:
reformatted = '%s-%s' % (request_id_digits[:2], request_id_digits[2:])
# so we got a newly formatted request, try again
return reformatted

# fell through, return the original
return request_id


# tweak this to be configurable via config files
fix_request_id = fix_request_id_chi

@app.route("/requests/<request_id>", methods=["GET", "POST"])
def show_request(request_id):
Expand All @@ -104,16 +130,9 @@ def show_request(request_id):
params['api_key'] = app.config['OPEN311_API_KEY']
r = requests.get(url, params=params)
if r.status_code == 404:
# TODO: how to generalize this?
# Chicago's SR IDs are always \d\d-\d{8}, if we get just digits, reformat and try again
request_id_digits = re.sub(r'\D', '', request_id)
if len(request_id_digits) == 8:
# Try prepending the year if it's only 8 digits
request_id_digits = datetime.date.today().strftime('%y') + request_id_digits
if len(request_id_digits) == 10:
reformatted = '%s-%s' % (request_id_digits[:2], request_id_digits[2:])
if reformatted != request_id:
return redirect(url_for('show_request', request_id=reformatted))
reformatted = fix_request_id(request_id)
if reformatted != request_id:
return redirect(url_for('show_request', request_id=reformatted))

# It would be nice to log this for analytical purposes (what requests are being checked that we can't show?)
# but that would be better done through GA or KISS Metrics than through server logging
Expand Down