|
53 | 53 |
|
54 | 54 |
|
55 | 55 | app = Flask(__name__, template_folder='templates/')
|
56 |
| -app.debug = not getenv('WEBSEARCH_DEBUG') is None |
57 |
| -app.debug = True |
| 56 | +app.debug = not (getenv('WEBSEARCH_DEBUG') is None) |
58 | 57 |
|
59 | 58 |
|
60 | 59 | # ---------------------------------------------------------
|
@@ -473,14 +472,16 @@ def formatResponse(data, code=200):
|
473 | 472 | """Format response output."""
|
474 | 473 | # Format json - return empty
|
475 | 474 | result = data['result'] if 'result' in data else {}
|
476 |
| - format = 'json' |
| 475 | + if app.debug and 'debug' in data: |
| 476 | + result['debug'] = data['debug'] |
| 477 | + output_format = 'json' |
477 | 478 | if request.args.get('format'):
|
478 |
| - format = request.args.get('format') |
| 479 | + output_format = request.args.get('format') |
479 | 480 | if 'format' in data:
|
480 |
| - format = data['format'] |
| 481 | + output_format = data['format'] |
481 | 482 |
|
482 | 483 | tpl = data['template'] if 'template' in data else 'answer.html'
|
483 |
| - if format == 'html' and tpl is not None: |
| 484 | + if output_format == 'html' and tpl is not None: |
484 | 485 | if 'route' not in data:
|
485 | 486 | data['route'] = '/'
|
486 | 487 | return render_template(tpl, rc=(code == 200), **data), code
|
@@ -1017,15 +1018,17 @@ def ppretty(value):
|
1017 | 1018 | # returns - result, distance tuple
|
1018 | 1019 | def reverse_search(lon, lat, debug):
|
1019 | 1020 | result = {
|
| 1021 | + 'total_found': 0, |
1020 | 1022 | 'count': 0,
|
1021 |
| - 'results': [] |
| 1023 | + 'matches': [] |
1022 | 1024 | }
|
1023 | 1025 |
|
1024 | 1026 | if debug:
|
1025 | 1027 | result['debug'] = {
|
1026 | 1028 | 'longitude': lon,
|
1027 | 1029 | 'latitude': lat,
|
1028 |
| - 'queries': [] |
| 1030 | + 'queries': [], |
| 1031 | + 'results': [], |
1029 | 1032 | }
|
1030 | 1033 |
|
1031 | 1034 | try:
|
@@ -1080,37 +1083,39 @@ def reverse_search(lon, lat, debug):
|
1080 | 1083 | # limit the result set to the single closest match
|
1081 | 1084 | limit = " ORDER BY distance ASC LIMIT 1"
|
1082 | 1085 |
|
1083 |
| - result = {} |
| 1086 | + myresult = {} |
1084 | 1087 | # form the final queries and execute
|
1085 | 1088 | for where in wherelon:
|
1086 | 1089 | sql = select + " AND ".join([where, wherelat, whereclass]) + limit
|
1087 |
| - if debug: |
1088 |
| - result['debug']['queries'].append(sql) |
1089 | 1090 | # Boolean, {'matches': [{'weight': 0, 'id', 'attrs': {}}], 'total_found': 0}
|
1090 | 1091 | status, result_new = get_query_result(cursor, sql, ())
|
1091 |
| - if 'matches' in result and len(result['matches']) > 0: |
1092 |
| - result = mergeResultObject(result, result_new) |
| 1092 | + if debug: |
| 1093 | + result['debug']['queries'].append(sql) |
| 1094 | + result['debug']['results'].append(result_new) |
| 1095 | + if 'matches' in myresult and len(myresult['matches']) > 0: |
| 1096 | + myresult = mergeResultObject(myresult, result_new) |
1093 | 1097 | else:
|
1094 |
| - result = result_new.copy() |
| 1098 | + myresult = result_new.copy() |
1095 | 1099 |
|
1096 |
| - count = len(result['matches']) |
| 1100 | + count = len(myresult['matches']) |
1097 | 1101 | db.close()
|
1098 | 1102 |
|
1099 | 1103 | if debug:
|
1100 |
| - result['debug']['matches'] = result['matches'] |
| 1104 | + result['debug']['matches'] = myresult['matches'] |
1101 | 1105 |
|
1102 | 1106 | smallest_row = None
|
1103 | 1107 | smallest_distance = None
|
1104 | 1108 |
|
1105 | 1109 | # For the rows returned, find the smallest calculated distance
|
1106 | 1110 | # (the 180 meridian case may result in 2 rows to check)
|
1107 |
| - for match in result['matches']: |
| 1111 | + for match in myresult['matches']: |
1108 | 1112 | distance = match['attrs']['distance']
|
1109 | 1113 |
|
1110 | 1114 | if smallest_row is None or distance < smallest_distance:
|
1111 | 1115 | smallest_row = match
|
1112 | 1116 | smallest_distance = distance
|
1113 | 1117 |
|
| 1118 | + result = mergeResultObject(result, myresult) |
1114 | 1119 | result['count'] = 1
|
1115 | 1120 | result['matches'] = [smallest_row]
|
1116 | 1121 | result['start_index'] = 1
|
@@ -1152,10 +1157,11 @@ def reverse_search_url(lon, lat):
|
1152 | 1157 | code = 200
|
1153 | 1158 | result, distance = reverse_search(lon, lat, debug)
|
1154 | 1159 | data['result'] = prepareResultJson(result)
|
1155 |
| - |
1156 | 1160 | if debug:
|
1157 | 1161 | times['process'] = time() - times['start']
|
1158 |
| - result['debug']['times'] = times |
| 1162 | + data['debug'] = result['debug'] |
| 1163 | + data['debug']['distance'] = distance |
| 1164 | + data['debug_times'] = times |
1159 | 1165 |
|
1160 | 1166 | return formatResponse(data, code)
|
1161 | 1167 |
|
|
0 commit comments