2023-05-14 10:17:30 +01:00
|
|
|
#!/usr/bin/python3
|
2021-05-07 16:46:47 +01:00
|
|
|
|
2021-05-14 10:07:32 +01:00
|
|
|
import json
|
2021-05-15 13:05:44 +01:00
|
|
|
import re
|
2023-05-14 11:30:15 +01:00
|
|
|
from time import sleep, time
|
|
|
|
|
|
|
|
import flask_login
|
|
|
|
import GeoIP
|
2021-07-06 16:24:14 +01:00
|
|
|
import maxminddb
|
2023-05-14 11:30:15 +01:00
|
|
|
import requests
|
2023-05-13 14:01:28 +01:00
|
|
|
import sqlalchemy
|
2023-05-14 11:30:15 +01:00
|
|
|
from flask import (
|
|
|
|
Flask,
|
|
|
|
Response,
|
|
|
|
abort,
|
|
|
|
flash,
|
|
|
|
g,
|
|
|
|
jsonify,
|
|
|
|
redirect,
|
|
|
|
render_template,
|
|
|
|
request,
|
|
|
|
session,
|
|
|
|
stream_with_context,
|
|
|
|
url_for,
|
|
|
|
)
|
|
|
|
from lxml import etree
|
|
|
|
from requests_oauthlib import OAuth1Session
|
|
|
|
from sqlalchemy import func
|
|
|
|
from sqlalchemy.sql.expression import update
|
|
|
|
|
|
|
|
from matcher import (
|
|
|
|
api,
|
|
|
|
commons,
|
|
|
|
database,
|
|
|
|
edit,
|
|
|
|
error_mail,
|
|
|
|
mail,
|
|
|
|
model,
|
|
|
|
nominatim,
|
|
|
|
osm_oauth,
|
|
|
|
wikidata,
|
|
|
|
wikidata_api,
|
|
|
|
)
|
|
|
|
from matcher.data import property_map
|
|
|
|
|
|
|
|
# from werkzeug.debug.tbtools import get_current_traceback
|
2021-05-07 16:46:47 +01:00
|
|
|
|
|
|
|
srid = 4326
|
2023-05-14 11:30:15 +01:00
|
|
|
re_point = re.compile(r"^POINT\((.+) (.+)\)$")
|
2021-05-07 16:46:47 +01:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
app.debug = True
|
2023-05-14 11:30:15 +01:00
|
|
|
app.config.from_object("config.default")
|
2021-10-22 08:02:46 +01:00
|
|
|
error_mail.setup_error_mail(app)
|
2021-05-07 16:46:47 +01:00
|
|
|
|
2021-06-16 14:42:04 +01:00
|
|
|
login_manager = flask_login.LoginManager(app)
|
2023-05-14 11:30:15 +01:00
|
|
|
login_manager.login_view = "login_route"
|
|
|
|
osm_api_base = "https://api.openstreetmap.org/api/0.6"
|
2021-06-16 14:42:04 +01:00
|
|
|
|
2021-07-06 16:24:14 +01:00
|
|
|
maxminddb_reader = maxminddb.open_database(app.config["GEOLITE2"])
|
2021-06-16 14:42:04 +01:00
|
|
|
|
2021-05-07 16:46:47 +01:00
|
|
|
DB_URL = "postgresql:///matcher"
|
|
|
|
database.init_db(DB_URL)
|
2021-05-08 10:02:59 +01:00
|
|
|
entity_keys = {"labels", "sitelinks", "aliases", "claims", "descriptions", "lastrevid"}
|
2021-05-07 16:46:47 +01:00
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
re_qid = re.compile(r"^Q\d+$")
|
2021-06-16 13:31:58 +01:00
|
|
|
|
2021-05-07 16:46:47 +01:00
|
|
|
|
|
|
|
@app.teardown_appcontext
|
|
|
|
def shutdown_session(exception=None):
|
|
|
|
database.session.remove()
|
|
|
|
|
|
|
|
|
2021-06-17 18:17:28 +01:00
|
|
|
@app.before_request
|
|
|
|
def global_user():
|
|
|
|
g.user = flask_login.current_user._get_current_object()
|
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
|
2021-10-22 08:02:46 +01:00
|
|
|
def dict_repr_values(d):
|
|
|
|
return {key: repr(value) for key, value in d.items()}
|
|
|
|
|
|
|
|
|
2023-05-13 14:01:28 +01:00
|
|
|
# @app.errorhandler(werkzeug.exceptions.InternalServerError)
|
|
|
|
# def exception_handler(e):
|
|
|
|
# tb = get_current_traceback()
|
|
|
|
# last_frame = next(frame for frame in reversed(tb.frames) if not frame.is_library)
|
|
|
|
# last_frame_args = inspect.getargs(last_frame.code)
|
|
|
|
# if request.path.startswith("/api/"):
|
|
|
|
# return cors_jsonify({
|
|
|
|
# "success": False,
|
|
|
|
# "error": tb.exception,
|
|
|
|
# "traceback": tb.plaintext,
|
|
|
|
# "locals": dict_repr_values(last_frame.locals),
|
|
|
|
# "last_function": {
|
|
|
|
# "name": tb.frames[-1].function_name,
|
|
|
|
# "args": repr(last_frame_args),
|
|
|
|
# },
|
|
|
|
# }), 500
|
2023-05-14 11:30:15 +01:00
|
|
|
#
|
2023-05-13 14:01:28 +01:00
|
|
|
# return render_template('show_error.html',
|
|
|
|
# tb=tb,
|
|
|
|
# last_frame=last_frame,
|
|
|
|
# last_frame_args=last_frame_args), 500
|
2021-10-22 08:02:46 +01:00
|
|
|
|
|
|
|
|
2021-06-25 10:07:38 +01:00
|
|
|
def cors_jsonify(*args, **kwargs):
|
|
|
|
response = jsonify(*args, **kwargs)
|
|
|
|
response.headers["Access-Control-Allow-Origin"] = "*"
|
|
|
|
return response
|
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
|
2021-05-07 16:46:47 +01:00
|
|
|
def check_for_tagged_qids(qids):
|
|
|
|
tagged = set()
|
|
|
|
for qid in qids:
|
|
|
|
for cls in model.Point, model.Polygon, model.Line:
|
|
|
|
q = cls.query.filter(cls.tags["wikidata"] == qid)
|
|
|
|
if q.count():
|
|
|
|
tagged.add(qid)
|
|
|
|
break
|
|
|
|
|
|
|
|
return tagged
|
|
|
|
|
|
|
|
|
|
|
|
def check_for_tagged_qid(qid):
|
|
|
|
return any(
|
|
|
|
database.session.query(
|
|
|
|
cls.query.filter(
|
|
|
|
cls.tags.has_key("wikidata"), cls.tags["wikidata"] == qid
|
|
|
|
).exists()
|
|
|
|
).scalar()
|
|
|
|
for cls in (model.Point, model.Polygon, model.Line)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-06-25 10:12:50 +01:00
|
|
|
def geoip_user_record():
|
2021-06-25 10:10:25 +01:00
|
|
|
gi = GeoIP.open(app.config["GEOIP_DATA"], GeoIP.GEOIP_STANDARD)
|
2021-05-07 16:46:47 +01:00
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
remote_ip = request.get("ip", request.remote_addr)
|
2021-06-25 10:12:50 +01:00
|
|
|
return gi.record_by_addr(remote_ip)
|
|
|
|
|
|
|
|
|
|
|
|
def get_user_location():
|
2023-05-14 11:30:15 +01:00
|
|
|
remote_ip = request.args.get("ip", request.remote_addr)
|
2022-04-08 10:37:43 +01:00
|
|
|
maxmind = maxminddb_reader.get(remote_ip)
|
|
|
|
return maxmind.get("location") if maxmind else None
|
2021-05-07 16:46:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/")
|
|
|
|
def redirect_from_root():
|
|
|
|
return redirect(url_for("map_start_page"))
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/index")
|
|
|
|
def index_page():
|
|
|
|
return render_template("index.html")
|
|
|
|
|
2021-10-22 10:02:52 +01:00
|
|
|
|
|
|
|
def get_username():
|
|
|
|
user = flask_login.current_user
|
|
|
|
if user.is_authenticated:
|
|
|
|
return user.username
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/isa/Q<int:item_id>", methods=["GET", "POST"])
|
|
|
|
def isa_page(item_id):
|
|
|
|
item = api.get_item(item_id)
|
|
|
|
|
|
|
|
if request.method == "POST":
|
2022-04-08 10:37:43 +01:00
|
|
|
tag_or_key = request.form["tag_or_key"]
|
|
|
|
extra = model.ItemExtraKeys(item=item, tag_or_key=tag_or_key)
|
|
|
|
database.session.add(extra)
|
|
|
|
database.session.commit()
|
|
|
|
flash("extra OSM tag/key added")
|
|
|
|
|
2021-10-22 10:02:52 +01:00
|
|
|
return redirect(url_for(request.endpoint, item_id=item_id))
|
|
|
|
|
|
|
|
q = model.ItemExtraKeys.query.filter_by(item=item)
|
|
|
|
extra = [e.tag_or_key for e in q]
|
|
|
|
subclass_property = "P279"
|
|
|
|
|
|
|
|
subclass_list = []
|
|
|
|
for s in item.get_claim(subclass_property):
|
|
|
|
subclass = api.get_item(s["numeric-id"])
|
2023-05-14 11:30:15 +01:00
|
|
|
subclass_list.append(
|
|
|
|
{
|
|
|
|
"qid": s["id"],
|
|
|
|
"item_id": s["numeric-id"],
|
|
|
|
"label": subclass.label(),
|
|
|
|
"description": subclass.description(),
|
|
|
|
"isa_page_url": url_for("isa_page", item_id=s["numeric-id"]),
|
|
|
|
}
|
|
|
|
)
|
2021-10-22 10:02:52 +01:00
|
|
|
|
2021-11-13 16:42:46 +00:00
|
|
|
tags = api.get_tags_for_isa_item(item)
|
|
|
|
|
2021-10-22 10:02:52 +01:00
|
|
|
return render_template(
|
|
|
|
"isa.html",
|
|
|
|
item=item,
|
|
|
|
extra=extra,
|
|
|
|
subclass_list=subclass_list,
|
2021-11-13 16:42:46 +00:00
|
|
|
username=get_username(),
|
|
|
|
tags=tags,
|
2021-10-22 10:02:52 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-06-25 13:52:06 +01:00
|
|
|
@app.route("/admin/skip_isa")
|
|
|
|
def admin_skip_isa_list():
|
|
|
|
q = model.Item.query.join(model.SkipIsA).order_by(model.Item.item_id)
|
|
|
|
return render_template("admin/skip_isa.html", q=q)
|
|
|
|
|
2021-05-07 16:46:47 +01:00
|
|
|
|
|
|
|
@app.route("/identifier")
|
|
|
|
def identifier_index():
|
|
|
|
return render_template("identifier_index.html", property_map=property_map)
|
|
|
|
|
|
|
|
|
2021-05-08 09:39:06 +01:00
|
|
|
@app.route("/commons/<filename>")
|
|
|
|
def get_commons_image(filename):
|
2021-06-17 18:12:50 +01:00
|
|
|
detail = commons.image_detail([filename], thumbheight=1200, thumbwidth=1200)
|
2021-05-08 09:39:06 +01:00
|
|
|
image = detail[filename]
|
|
|
|
return redirect(image["thumburl"])
|
|
|
|
|
|
|
|
|
2021-05-07 16:46:47 +01:00
|
|
|
@app.route("/identifier/<pid>")
|
|
|
|
def identifier_page(pid):
|
|
|
|
per_page = 10
|
|
|
|
page = int(request.args.get("page", 1))
|
|
|
|
property_dict = {pid: (osm_keys, label) for pid, osm_keys, label in property_map}
|
|
|
|
osm_keys, label = property_dict[pid]
|
|
|
|
|
|
|
|
wd = model.Item.query.filter(model.Item.claims.has_key(pid))
|
|
|
|
total = wd.count()
|
|
|
|
|
|
|
|
start = per_page * (page - 1)
|
|
|
|
items = wd.all()[start : per_page * page]
|
|
|
|
|
|
|
|
qids = [item.qid for item in items]
|
|
|
|
print(qids)
|
|
|
|
|
|
|
|
# pred = None
|
|
|
|
# values = set()
|
|
|
|
# for item in items:
|
|
|
|
# values |= set(item.get_claim(pid))
|
|
|
|
#
|
|
|
|
# for key in osm_keys:
|
|
|
|
# if key == 'ref':
|
|
|
|
# continue
|
|
|
|
# if pred is None:
|
|
|
|
# pred = model.Point.tags[key].in_(values)
|
|
|
|
# else:
|
|
|
|
# pred |= model.Point.tags[key].in_(values)
|
|
|
|
#
|
|
|
|
|
|
|
|
osm_points = {}
|
|
|
|
|
|
|
|
for qid in qids:
|
|
|
|
osm_points[qid] = model.Point.query.filter(
|
|
|
|
model.Point.tags["wikidata"] == qid
|
|
|
|
).all()
|
|
|
|
|
|
|
|
osm_total = len(osm_points)
|
|
|
|
|
|
|
|
return render_template(
|
|
|
|
"identifier_page.html",
|
|
|
|
pid=pid,
|
|
|
|
osm_keys=osm_keys,
|
|
|
|
label=label,
|
|
|
|
items=items,
|
|
|
|
total=total,
|
|
|
|
osm_total=osm_total,
|
|
|
|
osm_points=osm_points,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/map")
|
|
|
|
def map_start_page():
|
2021-07-06 16:24:14 +01:00
|
|
|
loc = get_user_location()
|
2021-06-17 18:12:07 +01:00
|
|
|
|
2022-04-08 10:37:43 +01:00
|
|
|
if loc:
|
|
|
|
lat, lon = loc["latitude"], loc["longitude"]
|
|
|
|
radius = loc["accuracy_radius"]
|
|
|
|
else:
|
|
|
|
lat, lon = 42.2917, -85.5872
|
|
|
|
radius = 5
|
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
return redirect(
|
|
|
|
url_for(
|
|
|
|
"map_location",
|
|
|
|
lat=f"{lat:.5f}",
|
|
|
|
lon=f"{lon:.5f}",
|
|
|
|
zoom=16,
|
|
|
|
radius=radius,
|
|
|
|
ip=request.args.get("ip"),
|
|
|
|
)
|
|
|
|
)
|
2021-06-14 23:08:07 +01:00
|
|
|
|
2021-07-11 16:18:45 +01:00
|
|
|
|
2021-07-14 14:57:21 +01:00
|
|
|
@app.route("/documentation")
|
|
|
|
def documentation_page():
|
|
|
|
user = flask_login.current_user
|
|
|
|
username = user.username if user.is_authenticated else None
|
|
|
|
|
|
|
|
return render_template(
|
2023-05-14 11:30:15 +01:00
|
|
|
"documentation.html", active_tab="documentation", username=username
|
2021-07-14 14:57:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-07-11 16:18:45 +01:00
|
|
|
@app.route("/search")
|
|
|
|
def search_page():
|
|
|
|
loc = get_user_location()
|
2023-05-14 11:30:15 +01:00
|
|
|
q = request.args.get("q")
|
2021-07-11 16:18:45 +01:00
|
|
|
|
|
|
|
user = flask_login.current_user
|
|
|
|
username = user.username if user.is_authenticated else None
|
|
|
|
|
|
|
|
return render_template(
|
|
|
|
"map.html",
|
2021-07-14 14:57:21 +01:00
|
|
|
active_tab="map",
|
2021-07-11 16:18:45 +01:00
|
|
|
lat=f'{loc["latitude"]:.5f}',
|
|
|
|
lon=f'{loc["longitude"]:.5f}',
|
|
|
|
zoom=16,
|
|
|
|
radius=loc["accuracy_radius"],
|
|
|
|
username=username,
|
|
|
|
mode="search",
|
|
|
|
q=q,
|
|
|
|
)
|
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
|
2021-06-14 23:08:07 +01:00
|
|
|
@app.route("/map/<int:zoom>/<float(signed=True):lat>/<float(signed=True):lon>")
|
|
|
|
def map_location(zoom, lat, lon):
|
2021-10-22 11:27:50 +01:00
|
|
|
qid = request.args.get("item")
|
2022-04-08 10:37:43 +01:00
|
|
|
isa_param = request.args.get("isa")
|
2021-10-22 11:27:50 +01:00
|
|
|
if qid:
|
|
|
|
api.get_item(qid[1:])
|
2021-06-17 18:12:07 +01:00
|
|
|
|
2022-04-08 10:37:43 +01:00
|
|
|
isa_list = []
|
|
|
|
if isa_param:
|
|
|
|
for isa_qid in isa_param.split(";"):
|
|
|
|
isa = api.get_item(isa_qid[1:])
|
|
|
|
if not isa:
|
|
|
|
continue
|
|
|
|
cur = {
|
|
|
|
"qid": isa.qid,
|
|
|
|
"label": isa.label(),
|
|
|
|
}
|
|
|
|
isa_list.append(cur)
|
|
|
|
|
2021-07-06 16:24:14 +01:00
|
|
|
return render_template(
|
|
|
|
"map.html",
|
2021-07-14 14:57:21 +01:00
|
|
|
active_tab="map",
|
2021-07-06 16:24:14 +01:00
|
|
|
zoom=zoom,
|
|
|
|
lat=lat,
|
|
|
|
lon=lon,
|
2021-10-22 10:02:52 +01:00
|
|
|
radius=request.args.get("radius"),
|
|
|
|
username=get_username(),
|
2021-07-11 16:18:45 +01:00
|
|
|
mode="map",
|
|
|
|
q=None,
|
2022-04-08 10:37:43 +01:00
|
|
|
item_type_filter=isa_list,
|
2021-07-06 16:24:14 +01:00
|
|
|
)
|
2021-06-14 23:08:07 +01:00
|
|
|
|
|
|
|
|
2022-04-08 10:37:43 +01:00
|
|
|
@app.route("/item/Q<int:item_id>")
|
|
|
|
def lookup_item(item_id):
|
|
|
|
item = api.get_item(item_id)
|
|
|
|
if not item:
|
|
|
|
# TODO: show nicer page for Wikidata item not found
|
|
|
|
return abort(404)
|
|
|
|
|
|
|
|
try:
|
|
|
|
lat, lon = item.locations[0].get_lat_lon()
|
|
|
|
except IndexError:
|
|
|
|
# TODO: show nicer page for Wikidata item without coordinates
|
|
|
|
return abort(404)
|
|
|
|
|
|
|
|
return render_template(
|
|
|
|
"map.html",
|
|
|
|
active_tab="map",
|
|
|
|
zoom=16,
|
|
|
|
lat=lat,
|
|
|
|
lon=lon,
|
|
|
|
username=get_username(),
|
|
|
|
mode="map",
|
|
|
|
q=None,
|
|
|
|
qid=item.qid,
|
|
|
|
item_type_filter=[],
|
|
|
|
)
|
|
|
|
|
|
|
|
url = url_for("map_location", zoom=16, lat=lat, lon=lon, item=item.qid)
|
|
|
|
return redirect(url)
|
|
|
|
|
2021-06-14 23:08:07 +01:00
|
|
|
|
2021-05-07 16:46:47 +01:00
|
|
|
@app.route("/search/map")
|
|
|
|
def search_map_page():
|
|
|
|
user_lat, user_lon = get_user_location() or (None, None)
|
|
|
|
|
|
|
|
q = request.args.get("q")
|
|
|
|
if not q:
|
|
|
|
return render_template("map.html", user_lat=user_lat, user_lon=user_lon)
|
|
|
|
|
|
|
|
hits = nominatim.lookup(q)
|
|
|
|
for hit in hits:
|
|
|
|
if "geotext" in hit:
|
|
|
|
del hit["geotext"]
|
|
|
|
bbox = [hit["boundingbox"] for hit in hits]
|
|
|
|
|
|
|
|
return render_template(
|
|
|
|
"search_map.html",
|
|
|
|
hits=hits,
|
|
|
|
bbox_list=bbox,
|
|
|
|
user_lat=user_lat,
|
|
|
|
user_lon=user_lon,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-07-11 16:18:45 +01:00
|
|
|
@app.route("/old_search")
|
|
|
|
def old_search_page():
|
2021-05-07 16:46:47 +01:00
|
|
|
q = request.args.get("q")
|
|
|
|
if not q:
|
|
|
|
return render_template("search.html", hits=None, bbox_list=None)
|
|
|
|
hits = nominatim.lookup(q)
|
|
|
|
for hit in hits:
|
|
|
|
if "geotext" in hit:
|
|
|
|
del hit["geotext"]
|
|
|
|
bbox = [hit["boundingbox"] for hit in hits]
|
|
|
|
return render_template("search.html", hits=hits, bbox_list=bbox)
|
|
|
|
|
|
|
|
|
2021-06-25 13:52:42 +01:00
|
|
|
def read_bounds_param():
|
|
|
|
return [float(i) for i in request.args["bounds"].split(",")]
|
2021-05-07 16:46:47 +01:00
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
|
2021-07-30 15:02:41 +01:00
|
|
|
def read_isa_filter_param():
|
2023-05-14 11:30:15 +01:00
|
|
|
isa_param = request.args.get("isa")
|
2021-07-30 15:02:41 +01:00
|
|
|
if isa_param:
|
2023-05-14 11:30:15 +01:00
|
|
|
return set(qid.strip() for qid in isa_param.upper().split(","))
|
|
|
|
|
2021-05-07 16:46:47 +01:00
|
|
|
|
2021-06-25 10:12:50 +01:00
|
|
|
@app.route("/api/1/location")
|
|
|
|
def show_user_location():
|
|
|
|
return cors_jsonify(get_user_location())
|
|
|
|
|
|
|
|
|
2021-06-17 18:14:42 +01:00
|
|
|
@app.route("/api/1/count")
|
|
|
|
def api_wikidata_items_count():
|
|
|
|
t0 = time()
|
2021-07-30 15:02:41 +01:00
|
|
|
isa_filter = read_isa_filter_param()
|
|
|
|
count = api.wikidata_items_count(read_bounds_param(), isa_filter=isa_filter)
|
2021-06-17 18:14:42 +01:00
|
|
|
|
|
|
|
t1 = time() - t0
|
2021-06-25 13:52:42 +01:00
|
|
|
return cors_jsonify(success=True, count=count, duration=t1)
|
2021-06-17 18:14:42 +01:00
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
|
2021-07-30 15:02:41 +01:00
|
|
|
@app.route("/api/1/isa_search")
|
|
|
|
def api_isa_search():
|
|
|
|
t0 = time()
|
|
|
|
search_terms = request.args.get("q")
|
|
|
|
items = api.isa_incremental_search(search_terms)
|
|
|
|
t1 = time() - t0
|
|
|
|
|
|
|
|
return cors_jsonify(success=True, items=items, duration=t1)
|
|
|
|
|
2021-06-17 18:14:42 +01:00
|
|
|
|
2021-06-24 17:42:33 +01:00
|
|
|
@app.route("/api/1/isa")
|
|
|
|
def api_wikidata_isa_counts():
|
|
|
|
t0 = time()
|
|
|
|
|
2021-06-25 13:52:42 +01:00
|
|
|
bounds = read_bounds_param()
|
2021-07-30 15:02:41 +01:00
|
|
|
isa_filter = read_isa_filter_param()
|
|
|
|
|
|
|
|
isa_count = api.wikidata_isa_counts(bounds, isa_filter=isa_filter)
|
2021-06-24 17:42:33 +01:00
|
|
|
|
|
|
|
t1 = time() - t0
|
2021-06-25 10:07:38 +01:00
|
|
|
return cors_jsonify(success=True, isa_count=isa_count, bounds=bounds, duration=t1)
|
2021-06-24 17:42:33 +01:00
|
|
|
|
|
|
|
|
2021-05-07 16:46:47 +01:00
|
|
|
@app.route("/api/1/items")
|
|
|
|
def api_wikidata_items():
|
|
|
|
t0 = time()
|
|
|
|
|
2021-06-25 13:52:42 +01:00
|
|
|
bounds = read_bounds_param()
|
2021-07-30 15:02:41 +01:00
|
|
|
isa_filter = read_isa_filter_param()
|
|
|
|
|
|
|
|
ret = api.wikidata_items(bounds, isa_filter=isa_filter)
|
2021-05-07 16:46:47 +01:00
|
|
|
|
2021-06-25 13:52:42 +01:00
|
|
|
t1 = time() - t0
|
|
|
|
return cors_jsonify(success=True, duration=t1, **ret)
|
2021-05-07 16:46:47 +01:00
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
|
2022-04-18 12:24:16 +01:00
|
|
|
@app.route("/api/1/place/<osm_type>/<int:osm_id>")
|
|
|
|
def api_place_items(osm_type, osm_id):
|
|
|
|
t0 = time()
|
|
|
|
|
|
|
|
ret = api.get_place_items(osm_type, osm_id)
|
|
|
|
|
|
|
|
t1 = time() - t0
|
|
|
|
return cors_jsonify(success=True, duration=t1, **ret)
|
|
|
|
|
2021-05-07 16:46:47 +01:00
|
|
|
|
|
|
|
@app.route("/api/1/osm")
|
|
|
|
def api_osm_objects():
|
|
|
|
t0 = time()
|
2021-07-30 15:02:41 +01:00
|
|
|
isa_filter = read_isa_filter_param()
|
|
|
|
objects = api.get_osm_with_wikidata_tag(read_bounds_param(), isa_filter=isa_filter)
|
2021-05-07 16:46:47 +01:00
|
|
|
t1 = time() - t0
|
2021-06-25 10:07:38 +01:00
|
|
|
return cors_jsonify(success=True, objects=objects, duration=t1)
|
2021-05-07 16:46:47 +01:00
|
|
|
|
|
|
|
|
2021-07-03 12:40:26 +01:00
|
|
|
@app.route("/api/1/item/Q<int:item_id>")
|
|
|
|
def api_get_item(item_id):
|
|
|
|
t0 = time()
|
|
|
|
item = model.Item.query.get(item_id)
|
|
|
|
detail = api.item_detail(item)
|
|
|
|
t1 = time() - t0
|
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
return cors_jsonify(success=True, duration=t1, **detail)
|
2021-07-03 12:40:26 +01:00
|
|
|
|
|
|
|
|
2021-05-12 08:27:34 +01:00
|
|
|
@app.route("/api/1/item/Q<int:item_id>/tags")
|
|
|
|
def api_get_item_tags(item_id):
|
|
|
|
t0 = time()
|
|
|
|
item = model.Item.query.get(item_id)
|
2021-07-03 12:39:37 +01:00
|
|
|
tags = api.get_item_tags(item)
|
|
|
|
osm_list = sorted(tags.keys())
|
2021-05-12 08:27:34 +01:00
|
|
|
t1 = time() - t0
|
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
return cors_jsonify(
|
|
|
|
success=True, qid=item.qid, tag_or_key_list=osm_list, tag_src=tags, duration=t1
|
|
|
|
)
|
2021-05-12 08:27:34 +01:00
|
|
|
|
2021-05-14 16:09:56 +01:00
|
|
|
|
2021-10-22 11:30:27 +01:00
|
|
|
def expand_street_name(from_names):
|
|
|
|
ret = set(from_names)
|
|
|
|
for name in from_names:
|
2023-05-14 11:30:15 +01:00
|
|
|
if any(name.startswith(st) for st in ("St ", "St. ")):
|
|
|
|
first_space = name.find(" ")
|
2021-10-22 11:30:27 +01:00
|
|
|
ret.add("Saint" + name[first_space:])
|
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
if ", " in name:
|
2021-10-22 11:30:27 +01:00
|
|
|
for n in set(ret):
|
|
|
|
comma = n.find(", ")
|
|
|
|
ret.add(name[:comma])
|
2023-05-14 11:30:15 +01:00
|
|
|
elif "/" in name:
|
2021-10-22 11:30:27 +01:00
|
|
|
for n in set(ret):
|
|
|
|
ret.extend(part.strip() for part in n.split("/"))
|
|
|
|
|
|
|
|
ret.update({"The " + name for name in ret if not name.startswith("The ")})
|
2021-07-22 13:47:38 +01:00
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2021-05-12 15:55:40 +01:00
|
|
|
@app.route("/api/1/item/Q<int:item_id>/candidates")
|
|
|
|
def api_find_osm_candidates(item_id):
|
|
|
|
t0 = time()
|
|
|
|
item = model.Item.query.get(item_id)
|
2021-07-22 08:49:24 +01:00
|
|
|
if not item:
|
2023-05-14 11:30:15 +01:00
|
|
|
return cors_jsonify(success=True, qid=f"Q{item_id}", error="item doesn't exist")
|
2021-07-22 08:49:24 +01:00
|
|
|
|
2021-10-22 11:31:34 +01:00
|
|
|
if not item.locations:
|
2023-05-14 11:30:15 +01:00
|
|
|
return cors_jsonify(
|
|
|
|
success=True, qid=f"Q{item_id}", error="item has no coordinates"
|
|
|
|
)
|
2021-10-22 11:31:34 +01:00
|
|
|
|
2021-07-22 13:47:38 +01:00
|
|
|
label = item.label()
|
|
|
|
item_is_street = item.is_street()
|
2021-10-22 11:31:34 +01:00
|
|
|
item_is_watercourse = item.is_watercourse()
|
2021-07-22 13:47:38 +01:00
|
|
|
|
|
|
|
if item_is_street:
|
2021-10-22 11:31:34 +01:00
|
|
|
max_distance = 5_000
|
|
|
|
limit = None
|
|
|
|
names = expand_street_name([label] + item.get_aliases())
|
|
|
|
elif item_is_watercourse:
|
|
|
|
max_distance = 20_000
|
2021-07-22 13:47:38 +01:00
|
|
|
limit = None
|
2021-10-22 11:31:34 +01:00
|
|
|
names = {label}
|
2021-07-22 13:47:38 +01:00
|
|
|
else:
|
2021-10-22 11:31:34 +01:00
|
|
|
max_distance = 1_000
|
|
|
|
limit = 40
|
2021-07-22 13:47:38 +01:00
|
|
|
names = None
|
2023-05-14 11:30:15 +01:00
|
|
|
nearby = api.find_osm_candidates(
|
|
|
|
item, limit=limit, max_distance=max_distance, names=names
|
|
|
|
)
|
2021-07-22 13:47:38 +01:00
|
|
|
|
2021-10-22 11:31:34 +01:00
|
|
|
if (item_is_street or item_is_watercourse) and not nearby:
|
2021-07-22 13:47:38 +01:00
|
|
|
# nearby = [osm for osm in nearby if street_name_match(label, osm)]
|
|
|
|
|
|
|
|
# try again without name filter
|
2023-05-14 11:30:15 +01:00
|
|
|
nearby = api.find_osm_candidates(item, limit=100, max_distance=1_000)
|
2021-05-12 15:55:40 +01:00
|
|
|
|
|
|
|
t1 = time() - t0
|
2021-10-06 17:42:06 +01:00
|
|
|
return cors_jsonify(
|
|
|
|
success=True,
|
|
|
|
qid=item.qid,
|
|
|
|
nearby=nearby,
|
|
|
|
duration=t1,
|
2023-05-14 11:30:15 +01:00
|
|
|
max_distance=max_distance,
|
2021-10-06 17:42:06 +01:00
|
|
|
)
|
2021-05-12 15:55:40 +01:00
|
|
|
|
|
|
|
|
2021-05-10 13:59:08 +01:00
|
|
|
@app.route("/api/1/missing")
|
|
|
|
def api_missing_wikidata_items():
|
2021-10-06 17:42:44 +01:00
|
|
|
t0 = time()
|
2021-05-10 13:59:08 +01:00
|
|
|
qids_arg = request.args.get("qids")
|
2021-06-16 13:31:58 +01:00
|
|
|
if not qids_arg:
|
2023-05-14 11:30:15 +01:00
|
|
|
return cors_jsonify(
|
|
|
|
success=False,
|
|
|
|
error="required parameter 'qids' is missing",
|
|
|
|
items=[],
|
|
|
|
isa_count=[],
|
|
|
|
)
|
2021-06-16 13:31:58 +01:00
|
|
|
|
|
|
|
qids = []
|
|
|
|
for qid in qids_arg.upper().split(","):
|
|
|
|
qid = qid.strip()
|
|
|
|
m = re_qid.match(qid)
|
|
|
|
if not m:
|
|
|
|
continue
|
|
|
|
qids.append(qid)
|
|
|
|
if not qids:
|
2021-05-10 13:59:08 +01:00
|
|
|
return jsonify(success=True, items=[], isa_count=[])
|
|
|
|
|
2021-05-15 13:05:44 +01:00
|
|
|
lat, lon = request.args.get("lat"), request.args.get("lon")
|
|
|
|
|
2021-06-25 13:52:42 +01:00
|
|
|
ret = api.missing_wikidata_items(qids, lat, lon)
|
2021-10-06 17:42:44 +01:00
|
|
|
t1 = time() - t0
|
2023-05-14 11:30:15 +01:00
|
|
|
return cors_jsonify(success=True, duration=t1, **ret)
|
2021-05-10 13:59:08 +01:00
|
|
|
|
|
|
|
|
2021-05-07 16:46:47 +01:00
|
|
|
@app.route("/api/1/search")
|
|
|
|
def api_search():
|
|
|
|
q = request.args["q"]
|
|
|
|
hits = nominatim.lookup(q)
|
|
|
|
for hit in hits:
|
|
|
|
hit["name"] = nominatim.get_hit_name(hit)
|
2021-07-08 13:35:32 +01:00
|
|
|
hit["label"] = nominatim.get_hit_label(hit)
|
|
|
|
hit["address"] = list(hit["address"].items())
|
2023-05-13 14:01:28 +01:00
|
|
|
if "osm_type" in hit and "osm_id" in hit:
|
|
|
|
hit["identifier"] = f"{hit['osm_type']}/{hit['osm_id']}"
|
|
|
|
else:
|
|
|
|
print(hit)
|
|
|
|
print(q)
|
2021-06-14 23:09:05 +01:00
|
|
|
|
2021-06-25 10:07:38 +01:00
|
|
|
return cors_jsonify(success=True, hits=hits)
|
2021-06-14 23:09:05 +01:00
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
|
2021-11-13 16:43:30 +00:00
|
|
|
@app.route("/api/1/polygon/<osm_type>/<int:osm_id>")
|
|
|
|
def api_polygon(osm_type, osm_id):
|
|
|
|
obj = model.Polygon.get_osm(osm_type, osm_id)
|
2023-05-14 11:30:15 +01:00
|
|
|
return cors_jsonify(
|
|
|
|
successful=True, osm_type=osm_type, osm_id=osm_id, geojson=obj.geojson()
|
|
|
|
)
|
2021-11-13 16:43:30 +00:00
|
|
|
|
|
|
|
|
2021-06-14 23:09:05 +01:00
|
|
|
@app.route("/refresh/Q<int:item_id>")
|
|
|
|
def refresh_item(item_id):
|
|
|
|
assert not model.Item.query.get(item_id)
|
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
qid = f"Q{item_id}"
|
2021-06-14 23:09:05 +01:00
|
|
|
entity = wikidata_api.get_entity(qid)
|
|
|
|
entity_qid = entity.pop("id")
|
|
|
|
assert qid == entity_qid
|
|
|
|
|
|
|
|
coords = wikidata.get_entity_coords(entity["claims"])
|
|
|
|
assert coords
|
|
|
|
|
|
|
|
obj = {k: v for k, v in entity.items() if k in entity_keys}
|
|
|
|
item = model.Item(item_id=item_id, **obj)
|
|
|
|
print(item)
|
|
|
|
item.locations = model.location_objects(coords)
|
|
|
|
database.session.add(item)
|
|
|
|
database.session.commit()
|
2021-05-07 16:46:47 +01:00
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
return "done"
|
2021-05-07 16:46:47 +01:00
|
|
|
|
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
@app.route("/login")
|
2021-06-16 14:42:04 +01:00
|
|
|
def login_openstreetmap():
|
2023-05-14 11:30:15 +01:00
|
|
|
return redirect(url_for("start_oauth", next=request.args.get("next")))
|
2021-06-16 14:42:04 +01:00
|
|
|
|
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
@app.route("/logout")
|
2021-06-16 14:42:04 +01:00
|
|
|
def logout():
|
2023-05-14 11:30:15 +01:00
|
|
|
next_url = request.args.get("next") or url_for("map_start_page")
|
2021-06-16 14:42:04 +01:00
|
|
|
flask_login.logout_user()
|
2023-05-14 11:30:15 +01:00
|
|
|
flash("you are logged out")
|
2021-06-16 14:42:04 +01:00
|
|
|
return redirect(next_url)
|
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
|
|
|
|
@app.route("/done/")
|
2021-06-16 14:42:04 +01:00
|
|
|
def done():
|
2023-05-14 11:30:15 +01:00
|
|
|
flash("login successful")
|
|
|
|
return redirect(url_for("map_start_page"))
|
2021-06-16 14:42:04 +01:00
|
|
|
|
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
@app.route("/oauth/start")
|
2021-06-16 14:42:04 +01:00
|
|
|
def start_oauth():
|
2023-05-14 11:30:15 +01:00
|
|
|
next_page = request.args.get("next")
|
2021-06-16 14:42:04 +01:00
|
|
|
if next_page:
|
2023-05-14 11:30:15 +01:00
|
|
|
session["next"] = next_page
|
2021-06-16 14:42:04 +01:00
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
client_key = app.config["CLIENT_KEY"]
|
|
|
|
client_secret = app.config["CLIENT_SECRET"]
|
2021-06-16 14:42:04 +01:00
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
request_token_url = "https://www.openstreetmap.org/oauth/request_token"
|
2021-06-16 14:42:04 +01:00
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
callback = url_for("oauth_callback", _external=True)
|
2021-06-16 14:42:04 +01:00
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
oauth = OAuth1Session(
|
|
|
|
client_key, client_secret=client_secret, callback_uri=callback
|
|
|
|
)
|
2021-06-16 14:42:04 +01:00
|
|
|
fetch_response = oauth.fetch_request_token(request_token_url)
|
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
session["owner_key"] = fetch_response.get("oauth_token")
|
|
|
|
session["owner_secret"] = fetch_response.get("oauth_token_secret")
|
2021-06-16 14:42:04 +01:00
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
base_authorization_url = "https://www.openstreetmap.org/oauth/authorize"
|
|
|
|
authorization_url = oauth.authorization_url(
|
|
|
|
base_authorization_url, oauth_consumer_key=client_key
|
|
|
|
)
|
2021-06-16 14:42:04 +01:00
|
|
|
return redirect(authorization_url)
|
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
|
2021-06-16 14:42:04 +01:00
|
|
|
@login_manager.user_loader
|
|
|
|
def load_user(user_id):
|
|
|
|
return model.User.query.get(user_id)
|
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
|
2021-06-16 14:42:04 +01:00
|
|
|
@app.route("/oauth/callback", methods=["GET"])
|
|
|
|
def oauth_callback():
|
2023-05-14 11:30:15 +01:00
|
|
|
client_key = app.config["CLIENT_KEY"]
|
|
|
|
client_secret = app.config["CLIENT_SECRET"]
|
|
|
|
|
|
|
|
oauth = OAuth1Session(
|
|
|
|
client_key,
|
|
|
|
client_secret=client_secret,
|
|
|
|
resource_owner_key=session["owner_key"],
|
|
|
|
resource_owner_secret=session["owner_secret"],
|
|
|
|
)
|
2021-06-16 14:42:04 +01:00
|
|
|
|
|
|
|
oauth_response = oauth.parse_authorization_response(request.url)
|
2023-05-14 11:30:15 +01:00
|
|
|
verifier = oauth_response.get("oauth_verifier")
|
|
|
|
access_token_url = "https://www.openstreetmap.org/oauth/access_token"
|
|
|
|
oauth = OAuth1Session(
|
|
|
|
client_key,
|
|
|
|
client_secret=client_secret,
|
|
|
|
resource_owner_key=session["owner_key"],
|
|
|
|
resource_owner_secret=session["owner_secret"],
|
|
|
|
verifier=verifier,
|
|
|
|
)
|
2021-06-16 14:42:04 +01:00
|
|
|
|
|
|
|
oauth_tokens = oauth.fetch_access_token(access_token_url)
|
2023-05-14 11:30:15 +01:00
|
|
|
session["owner_key"] = oauth_tokens.get("oauth_token")
|
|
|
|
session["owner_secret"] = oauth_tokens.get("oauth_token_secret")
|
2021-06-16 14:42:04 +01:00
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
r = oauth.get(osm_api_base + "/user/details")
|
2021-06-16 14:42:04 +01:00
|
|
|
info = osm_oauth.parse_userinfo_call(r.content)
|
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
user = model.User.query.filter_by(osm_id=info["id"]).one_or_none()
|
2021-06-16 14:42:04 +01:00
|
|
|
|
|
|
|
if user:
|
2023-05-14 11:30:15 +01:00
|
|
|
user.osm_oauth_token = oauth_tokens.get("oauth_token")
|
|
|
|
user.osm_oauth_token_secret = oauth_tokens.get("oauth_token_secret")
|
2021-06-16 14:42:04 +01:00
|
|
|
else:
|
|
|
|
user = model.User(
|
2023-05-14 11:30:15 +01:00
|
|
|
username=info["username"],
|
|
|
|
description=info["description"],
|
|
|
|
img=info["img"],
|
|
|
|
osm_id=info["id"],
|
|
|
|
osm_account_created=info["account_created"],
|
2021-08-05 07:43:15 +01:00
|
|
|
mock_upload=False,
|
2021-06-16 14:42:04 +01:00
|
|
|
)
|
|
|
|
database.session.add(user)
|
|
|
|
database.session.commit()
|
|
|
|
flask_login.login_user(user)
|
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
next_page = session.get("next") or url_for("map_start_page")
|
2021-06-16 14:42:04 +01:00
|
|
|
return redirect(next_page)
|
|
|
|
|
2021-05-07 16:46:47 +01:00
|
|
|
|
2021-06-24 17:40:59 +01:00
|
|
|
def validate_edit_list(edits):
|
|
|
|
for e in edits:
|
|
|
|
assert model.Item.get_by_qid(e["qid"])
|
2021-10-22 11:32:26 +01:00
|
|
|
assert e["op"] in {"add", "remove", "change"}
|
2023-05-14 11:30:15 +01:00
|
|
|
osm_type, _, osm_id = e["osm"].partition("/")
|
2021-06-24 17:40:59 +01:00
|
|
|
osm_id = int(osm_id)
|
2023-05-14 11:30:15 +01:00
|
|
|
if osm_type == "node":
|
2021-07-16 11:00:20 +01:00
|
|
|
assert model.Point.query.get(osm_id)
|
2021-06-24 17:40:59 +01:00
|
|
|
else:
|
|
|
|
src_id = osm_id if osm_type == "way" else -osm_id
|
2023-05-14 11:30:15 +01:00
|
|
|
assert model.Line.query.get(src_id) or model.Polygon.query.get(src_id)
|
2021-06-24 17:40:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/api/1/edit", methods=["POST"])
|
|
|
|
def api_new_edit_session():
|
|
|
|
user = flask_login.current_user
|
|
|
|
incoming = request.json
|
|
|
|
|
|
|
|
validate_edit_list(incoming["edit_list"])
|
2023-05-14 11:30:15 +01:00
|
|
|
es = model.EditSession(
|
|
|
|
user=user, edit_list=incoming["edit_list"], comment=incoming["comment"]
|
|
|
|
)
|
2021-06-24 17:40:59 +01:00
|
|
|
database.session.add(es)
|
|
|
|
database.session.commit()
|
|
|
|
|
|
|
|
session_id = es.id
|
|
|
|
|
2021-06-25 10:07:38 +01:00
|
|
|
return cors_jsonify(success=True, session_id=session_id)
|
2021-06-24 17:40:59 +01:00
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
|
2021-06-24 17:40:59 +01:00
|
|
|
@app.route("/api/1/edit/<int:session_id>", methods=["POST"])
|
|
|
|
def api_edit_session(session_id):
|
|
|
|
es = model.EditSession.query.get(session_id)
|
|
|
|
assert flask_login.current_user.id == es.user_id
|
|
|
|
incoming = request.json
|
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
for f in "edit_list", "comment":
|
2021-06-24 17:40:59 +01:00
|
|
|
if f not in incoming:
|
|
|
|
continue
|
|
|
|
setattr(es, f, incoming[f])
|
|
|
|
database.session.commit()
|
|
|
|
|
2021-06-25 10:07:38 +01:00
|
|
|
return cors_jsonify(success=True, session_id=session_id)
|
2021-06-24 17:40:59 +01:00
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
|
2021-07-16 11:00:20 +01:00
|
|
|
class VersionMismatch(Exception):
|
|
|
|
pass
|
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
|
2021-07-16 11:00:20 +01:00
|
|
|
def osm_object(osm_type, osm_id):
|
|
|
|
if osm_type == "node":
|
|
|
|
return model.Point.query.get(osm_id)
|
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
src_id = int(osm_id) * {"way": 1, "relation": -1}[osm_type]
|
2021-07-16 11:00:20 +01:00
|
|
|
for cls in model.Line, model.Polygon:
|
|
|
|
obj = cls.query.get(src_id)
|
|
|
|
if obj:
|
|
|
|
return obj
|
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
|
2021-07-19 21:06:36 +01:00
|
|
|
def process_edit(changeset_id, e):
|
2023-05-14 11:30:15 +01:00
|
|
|
osm_type, _, osm_id = e["osm"].partition("/")
|
2021-07-16 11:00:20 +01:00
|
|
|
qid = e["qid"]
|
|
|
|
item_id = qid[1:]
|
|
|
|
|
|
|
|
osm = osm_object(osm_type, osm_id)
|
|
|
|
assert osm
|
|
|
|
|
|
|
|
r = edit.get_existing(osm_type, osm_id)
|
|
|
|
if r.status_code == 410 or r.content == b"":
|
|
|
|
return "deleted"
|
|
|
|
|
|
|
|
root = etree.fromstring(r.content)
|
|
|
|
existing = root.find('.//tag[@k="wikidata"]')
|
|
|
|
if e["op"] == "add" and existing is not None:
|
|
|
|
return "already_added"
|
2021-07-19 21:07:41 +01:00
|
|
|
if e["op"] == "remove":
|
|
|
|
if existing is None:
|
|
|
|
return "already_removed"
|
|
|
|
if existing.get("v") != qid:
|
|
|
|
return "different_qid"
|
2021-07-16 11:00:20 +01:00
|
|
|
|
|
|
|
root[0].set("changeset", str(changeset_id))
|
|
|
|
if e["op"] == "add":
|
|
|
|
tag = etree.Element("tag", k="wikidata", v=qid)
|
|
|
|
root[0].append(tag)
|
|
|
|
if e["op"] == "remove":
|
|
|
|
root[0].remove(existing)
|
2021-10-22 11:32:26 +01:00
|
|
|
if e["op"] == "change":
|
|
|
|
existing.set("v", qid)
|
2021-07-16 11:00:20 +01:00
|
|
|
|
|
|
|
element_data = etree.tostring(root)
|
|
|
|
try:
|
|
|
|
success = edit.save_element(osm_type, osm_id, element_data)
|
|
|
|
except requests.exceptions.HTTPError as e:
|
|
|
|
if e.response.status_code == 409 and "Version mismatch" in r.text:
|
|
|
|
raise VersionMismatch
|
|
|
|
mail.error_mail(
|
|
|
|
"error saving element", element_data.decode("utf-8"), e.response
|
|
|
|
)
|
|
|
|
database.session.commit()
|
|
|
|
return "element-error"
|
|
|
|
|
|
|
|
if not success:
|
|
|
|
return "element-error"
|
|
|
|
|
2021-07-19 21:07:41 +01:00
|
|
|
new_tags = dict(osm.tags)
|
2021-10-22 11:32:26 +01:00
|
|
|
if e["op"] in ("add", "change"):
|
2021-07-19 21:07:41 +01:00
|
|
|
new_tags["wikidata"] = qid
|
|
|
|
if e["op"] == "remove":
|
|
|
|
del new_tags["wikidata"]
|
|
|
|
|
|
|
|
cls = type(osm)
|
|
|
|
database.session.execute(
|
2023-05-14 11:30:15 +01:00
|
|
|
update(cls).where(cls.src_id == osm.src_id).values(tags=new_tags)
|
2021-07-19 21:07:41 +01:00
|
|
|
)
|
2021-07-16 11:00:20 +01:00
|
|
|
|
|
|
|
db_edit = model.ChangesetEdit(
|
|
|
|
changeset_id=changeset_id,
|
|
|
|
item_id=item_id,
|
|
|
|
osm_id=osm_id,
|
|
|
|
osm_type=osm_type,
|
|
|
|
)
|
|
|
|
database.session.add(db_edit)
|
|
|
|
database.session.commit()
|
|
|
|
|
|
|
|
return "saved"
|
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
|
2021-07-16 11:00:20 +01:00
|
|
|
@app.route("/api/1/save/<int:session_id>")
|
2021-06-24 17:40:59 +01:00
|
|
|
def api_save_changeset(session_id):
|
2021-07-16 11:00:20 +01:00
|
|
|
assert g.user.is_authenticated
|
|
|
|
|
|
|
|
mock = g.user.mock_upload
|
|
|
|
api_call = api_mock_save_changeset if mock else api_real_save_changeset
|
|
|
|
return api_call(session_id)
|
|
|
|
|
|
|
|
|
2023-05-13 14:01:28 +01:00
|
|
|
@app.route("/sql", methods=["GET", "POST"])
|
|
|
|
def run_sql():
|
|
|
|
if request.method != "POST":
|
|
|
|
return render_template("run_sql.html")
|
|
|
|
|
|
|
|
sql = request.form["sql"]
|
|
|
|
conn = database.session.connection()
|
|
|
|
result = conn.execute(sqlalchemy.text(sql))
|
|
|
|
|
|
|
|
return render_template("run_sql.html", result=result)
|
|
|
|
|
|
|
|
|
2021-07-16 11:00:20 +01:00
|
|
|
def api_real_save_changeset(session_id):
|
2021-06-24 17:40:59 +01:00
|
|
|
es = model.EditSession.query.get(session_id)
|
|
|
|
|
2021-07-16 11:00:20 +01:00
|
|
|
def send(event, **data):
|
2021-06-24 17:40:59 +01:00
|
|
|
data["type"] = event
|
|
|
|
return f"data: {json.dumps(data)}\n\n"
|
|
|
|
|
2021-07-16 11:00:20 +01:00
|
|
|
def stream(user):
|
2021-06-24 17:40:59 +01:00
|
|
|
changeset = edit.new_changeset(es.comment)
|
|
|
|
r = edit.create_changeset(changeset)
|
|
|
|
reply = r.text.strip()
|
|
|
|
|
|
|
|
if reply == "Couldn't authenticate you":
|
|
|
|
mail.open_changeset_error(session_id, changeset, r)
|
2021-07-16 11:00:20 +01:00
|
|
|
yield send("auth-fail", error=reply)
|
2021-06-24 17:40:59 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
if not reply.isdigit():
|
|
|
|
mail.open_changeset_error(session_id, changeset, r)
|
2021-07-16 11:00:20 +01:00
|
|
|
yield send("changeset-error", error=reply)
|
2021-06-24 17:40:59 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
changeset_id = int(reply)
|
2021-07-16 11:00:20 +01:00
|
|
|
yield send("open", id=changeset_id)
|
2021-06-24 17:40:59 +01:00
|
|
|
|
|
|
|
update_count = 0
|
|
|
|
|
2021-07-16 11:00:20 +01:00
|
|
|
change = edit.record_changeset(
|
|
|
|
id=changeset_id, user=user, comment=es.comment, update_count=update_count
|
2021-06-24 17:40:59 +01:00
|
|
|
)
|
|
|
|
|
2021-07-16 11:00:20 +01:00
|
|
|
# each edit contains these keys:
|
|
|
|
# qid: Wikidata item QID
|
|
|
|
# osm: OpenStreetMap identifier
|
|
|
|
# op: either 'add' or 'remove'
|
2021-06-24 17:40:59 +01:00
|
|
|
|
2021-07-16 11:00:20 +01:00
|
|
|
for num, e in enumerate(es.edit_list):
|
|
|
|
print(num, e)
|
|
|
|
yield send("progress", edit=e, num=num)
|
2021-07-19 21:06:36 +01:00
|
|
|
result = process_edit(changeset_id, e)
|
2021-07-16 11:00:20 +01:00
|
|
|
yield send(result, edit=e, num=num)
|
|
|
|
if result == "saved":
|
|
|
|
update_count += 1
|
|
|
|
change.update_count = update_count
|
|
|
|
database.session.commit()
|
2021-06-24 17:40:59 +01:00
|
|
|
|
2021-07-16 11:00:20 +01:00
|
|
|
yield send("closing")
|
|
|
|
edit.close_changeset(changeset_id)
|
|
|
|
yield send("done")
|
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
return Response(stream_with_context(stream(g.user)), mimetype="text/event-stream")
|
|
|
|
|
2021-07-16 11:00:20 +01:00
|
|
|
|
|
|
|
def api_mock_save_changeset(session_id):
|
2021-06-24 17:40:59 +01:00
|
|
|
es = model.EditSession.query.get(session_id)
|
|
|
|
|
|
|
|
def send(event, **data):
|
|
|
|
data["type"] = event
|
|
|
|
return f"data: {json.dumps(data)}\n\n"
|
|
|
|
|
|
|
|
def stream(user):
|
2023-05-14 11:30:15 +01:00
|
|
|
print("stream")
|
2021-06-24 17:40:59 +01:00
|
|
|
changeset_id = database.session.query(func.max(model.Changeset.id) + 1).scalar()
|
|
|
|
sleep(1)
|
|
|
|
yield send("open", id=changeset_id)
|
|
|
|
sleep(1)
|
|
|
|
|
|
|
|
update_count = 0
|
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
print("record_changeset", changeset_id)
|
2021-06-24 17:40:59 +01:00
|
|
|
edit.record_changeset(
|
|
|
|
id=changeset_id, user=user, comment=es.comment, update_count=update_count
|
|
|
|
)
|
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
print("edits")
|
2021-06-24 17:40:59 +01:00
|
|
|
|
|
|
|
for num, e in enumerate(es.edit_list):
|
|
|
|
print(num, e)
|
|
|
|
yield send("progress", edit=e, num=num)
|
|
|
|
sleep(1)
|
|
|
|
yield send("saved", edit=e, num=num)
|
|
|
|
sleep(1)
|
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
print("closing")
|
2021-06-24 17:40:59 +01:00
|
|
|
yield send("closing")
|
|
|
|
sleep(1)
|
|
|
|
yield send("done")
|
|
|
|
|
2023-05-14 11:30:15 +01:00
|
|
|
return Response(stream(g.user), mimetype="text/event-stream")
|
2021-06-24 17:40:59 +01:00
|
|
|
|
|
|
|
|
2021-05-07 16:46:47 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
app.run(host="0.0.0.0")
|