New function to return JSON with CORS header

This commit is contained in:
Edward Betts 2021-06-25 11:07:38 +02:00
parent 11afda8d83
commit eb37e0bf7c

View file

@ -80,6 +80,11 @@ def shutdown_session(exception=None):
def global_user(): def global_user():
g.user = flask_login.current_user._get_current_object() g.user = flask_login.current_user._get_current_object()
def cors_jsonify(*args, **kwargs):
response = jsonify(*args, **kwargs)
response.headers["Access-Control-Allow-Origin"] = "*"
return response
def check_for_tagged_qids(qids): def check_for_tagged_qids(qids):
tagged = set() tagged = set()
for qid in qids: for qid in qids:
@ -427,9 +432,7 @@ def api_wikidata_items_count():
) )
t1 = time() - t0 t1 = time() - t0
response = jsonify(success=True, count=q.count(), duration=t1) return cors_jsonify(success=True, count=q.count(), duration=t1)
response.headers["Access-Control-Allow-Origin"] = "*"
return response
@app.route("/api/1/isa") @app.route("/api/1/isa")
@ -467,9 +470,7 @@ def api_wikidata_isa_counts():
isa_count.append(isa) isa_count.append(isa)
t1 = time() - t0 t1 = time() - t0
response = jsonify(success=True, isa_count=isa_count, bounds=bounds, duration=t1) return cors_jsonify(success=True, isa_count=isa_count, bounds=bounds, duration=t1)
response.headers["Access-Control-Allow-Origin"] = "*"
return response
@app.route("/api/1/items") @app.route("/api/1/items")
@ -504,9 +505,7 @@ def api_wikidata_items():
t1 = time() - t0 t1 = time() - t0
print(f"wikidata: {t1} seconds") print(f"wikidata: {t1} seconds")
response = jsonify(success=True, items=items, isa_count=isa_count, duration=t1) return cors_jsonify(success=True, items=items, isa_count=isa_count, duration=t1)
response.headers["Access-Control-Allow-Origin"] = "*"
return response
@app.route("/api/1/osm") @app.route("/api/1/osm")
@ -516,9 +515,7 @@ def api_osm_objects():
objects = get_osm_with_wikidata_tag(bounds) objects = get_osm_with_wikidata_tag(bounds)
t1 = time() - t0 t1 = time() - t0
print(f"OSM: {t1} seconds") print(f"OSM: {t1} seconds")
response = jsonify(success=True, objects=objects, duration=t1) return cors_jsonify(success=True, objects=objects, duration=t1)
response.headers["Access-Control-Allow-Origin"] = "*"
return response
edu = ['Tag:amenity=college', 'Tag:amenity=university', 'Tag:amenity=school', edu = ['Tag:amenity=college', 'Tag:amenity=university', 'Tag:amenity=school',
@ -879,9 +876,10 @@ def api_get_item_tags(item_id):
osm_list = get_item_tags(item) osm_list = get_item_tags(item)
t1 = time() - t0 t1 = time() - t0
response = jsonify(success=True, qid=item.qid, tag_or_key_list=osm_list, duration=t1) return cors_jsonify(success=True,
response.headers["Access-Control-Allow-Origin"] = "*" qid=item.qid,
return response tag_or_key_list=osm_list,
duration=t1)
def get_tag_filter(cls, tag_list): def get_tag_filter(cls, tag_list):
tag_filter = [] tag_filter = []
@ -1135,16 +1133,14 @@ def api_find_osm_candidates(item_id):
nearby.append(cur) nearby.append(cur)
t1 = time() - t0 t1 = time() - t0
response = jsonify(success=True, qid=item.qid, nearby=nearby, duration=t1) return cors_jsonify(success=True, qid=item.qid, nearby=nearby, duration=t1)
response.headers["Access-Control-Allow-Origin"] = "*"
return response
@app.route("/api/1/missing") @app.route("/api/1/missing")
def api_missing_wikidata_items(): def api_missing_wikidata_items():
qids_arg = request.args.get("qids") qids_arg = request.args.get("qids")
if not qids_arg: if not qids_arg:
return jsonify(success=False, return cors_jsonify(success=False,
error="required parameter 'qids' is missing", error="required parameter 'qids' is missing",
items=[], items=[],
isa_count=[]) isa_count=[])
@ -1189,9 +1185,7 @@ def api_missing_wikidata_items():
} }
isa_count.append(isa) isa_count.append(isa)
response = jsonify(success=True, items=items, isa_count=isa_count) return cors_jsonify(success=True, items=items, isa_count=isa_count)
response.headers["Access-Control-Allow-Origin"] = "*"
return response
@app.route("/api/1/search") @app.route("/api/1/search")
@ -1204,9 +1198,7 @@ def api_search():
hit["name"] = nominatim.get_hit_name(hit) hit["name"] = nominatim.get_hit_name(hit)
hit["identifier"] = f"{hit['osm_type']}/{hit['osm_id']}" hit["identifier"] = f"{hit['osm_type']}/{hit['osm_id']}"
response = jsonify(hits=hits) return cors_jsonify(success=True, hits=hits)
response.headers["Access-Control-Allow-Origin"] = "*"
return response
@app.route("/refresh/Q<int:item_id>") @app.route("/refresh/Q<int:item_id>")
def refresh_item(item_id): def refresh_item(item_id):
@ -1351,9 +1343,7 @@ def api_new_edit_session():
session_id = es.id session_id = es.id
response = jsonify(success=True, session_id=session_id) return cors_jsonify(success=True, session_id=session_id)
response.headers["Access-Control-Allow-Origin"] = "*"
return response
@app.route("/api/1/edit/<int:session_id>", methods=["POST"]) @app.route("/api/1/edit/<int:session_id>", methods=["POST"])
def api_edit_session(session_id): def api_edit_session(session_id):
@ -1367,9 +1357,7 @@ def api_edit_session(session_id):
setattr(es, f, incoming[f]) setattr(es, f, incoming[f])
database.session.commit() database.session.commit()
response = jsonify(success=True, session_id=session_id) return cors_jsonify(success=True, session_id=session_id)
response.headers["Access-Control-Allow-Origin"] = "*"
return response
@app.route("/api/1/real_save/<int:session_id>") @app.route("/api/1/real_save/<int:session_id>")
def api_save_changeset(session_id): def api_save_changeset(session_id):