From 24e053f450d35056d533896c5d2653afa10658b8 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Sun, 14 May 2023 11:11:23 +0200 Subject: [PATCH] Add missing code. --- matcher/edit.py | 71 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 matcher/edit.py diff --git a/matcher/edit.py b/matcher/edit.py new file mode 100644 index 0000000..664b408 --- /dev/null +++ b/matcher/edit.py @@ -0,0 +1,71 @@ +from flask import g +from . import user_agent_headers, database, osm_oauth, mail +from .model import Changeset +import requests +import html + +really_save = True +osm_api_base = "https://api.openstreetmap.org/api/0.6" + + +def new_changeset(comment): + return f""" + + + + + +""" + + +def osm_request(path, **kwargs): + return osm_oauth.api_put_request(path, **kwargs) + + +def create_changeset(changeset): + try: + return osm_request("/changeset/create", data=changeset.encode("utf-8")) + except requests.exceptions.HTTPError as r: + print(changeset) + print(r.response.text) + raise + + +def close_changeset(changeset_id): + return osm_request(f"/changeset/{changeset_id}/close") + + +def save_element(osm_type, osm_id, element_data): + osm_path = f"/{osm_type}/{osm_id}" + r = osm_request(osm_path, data=element_data) + reply = r.text.strip() + if reply.isdigit(): + return r + + subject = f"matcher error saving element: {osm_path}" + username = g.user.username + body = f""" +https://www.openstreetmap.org{osm_path} + +user: {username} +message user: https://www.openstreetmap.org/message/new/{username} + +error: +{reply} +""" + + mail.send_mail(subject, body) + + +def record_changeset(**kwargs): + change = Changeset(created=database.now_utc(), **kwargs) + + database.session.add(change) + database.session.commit() + + return change + + +def get_existing(osm_type, osm_id): + url = f"{osm_api_base}/{osm_type}/{osm_id}" + return requests.get(url, headers=user_agent_headers())