Skip to content
Snippets Groups Projects
Verified Commit b3213e9e authored by Robert Metcalf's avatar Robert Metcalf
Browse files

add interfaces to set latitude and longitude information

parent e9eca452
No related branches found
No related tags found
1 merge request!21Resolve "Add form for adding latitude and longitude positions"
...@@ -69,7 +69,12 @@ def lookup_address(address: str): ...@@ -69,7 +69,12 @@ def lookup_address(address: str):
def set_address_latlong(address: str, latlong: Tuple[float, float]): def set_address_latlong(address: str, latlong: Tuple[float, float]):
with Connection() as conn: with Connection() as conn:
latlong_str = f"{ repr(latlong[0]) },{ repr(latlong[1]) }" latlong_str = f"{ repr(latlong[0]) },{ repr(latlong[1]) }"
conn.execute("INSERT INTO AddressToLatLong (address, latlong) VALUES (?, ?)", (address, latlong_str)) if latlong == None:
conn.execute("DELETE FROM AddressToLatLong WHERE address = ?", (address))
else:
conn.execute("UPDATE AddressToLatLong SET latlong = ? WHERE address = ?", (latlong_str, address))
if conn.cursor.rowcount == 0:
conn.execute("INSERT INTO AddressToLatLong (address, latlong) VALUES (?, ?)", (address, latlong_str))
conn.commit() conn.commit()
class Workspace: class Workspace:
......
...@@ -83,6 +83,44 @@ def add_workspace(): ...@@ -83,6 +83,44 @@ def add_workspace():
return render_template("add-workspace-result.html", messages = messages) return render_template("add-workspace-result.html", messages = messages)
@app.route("/admin/set-latlong", methods = ["GET", "POST"])
def set_latlong():
if request.method == "GET":
address = request.args.get("address")
addresses = []
if address == None:
workspaces = database.get_workspaces()
for workspace in workspaces:
if workspace.address not in addresses:
addresses.append(workspace.address)
addresses = [address for address in addresses if database.lookup_address(address) == None]
return render_template("set-latlong.html", address = address, addresses = addresses)
if request.method == "POST":
address = request.form["address"]
errors = []
if len(address.strip()) <= 0:
errors.append("Address must not be empty")
try:
latitude = float(request.form["latitude"])
except ValueError:
errors.append("Latitude must be a number")
try:
longitude = float(request.form["longitude"])
except ValueError:
errors.append("Longitude must be a number")
if len(errors) > 0:
messages = ["Errors were found:"]
messages.extend(errors)
else:
database.set_address_latlong(address, (latitude, longitude))
messages = ["Address set successfully"]
return render_template("set-latlong-result.html", messages = messages)
@app.route("/admin/import-workspaces", methods = ["GET", "POST"]) @app.route("/admin/import-workspaces", methods = ["GET", "POST"])
def import_workspaces(): def import_workspaces():
if request.method == "GET": if request.method == "GET":
......
{% extends "main.html" %}
{% block title %}Set Latitude and Longitude of Address{% endblock %}
{% block mainBlock %}
{% for message in messages %}
<div>{{ message }}</div>
{% endfor %}
{% endblock %}
\ No newline at end of file
{% extends "main.html" %}
{% block title %}Set Latitude and Longitude of Address{% endblock %}
{% block mainBlock %}
{% if address == None %}
<h1>Addresses without latitude and longitude</h1>
{% for address in addresses %}
<div><a href="/admin/set-latlong?address={{ address|urlencode }}">{{ address }}</a></div>
{% endfor %}
{% else %}
<form action="/admin/set-latlong" method="POST">
<table>
<tr>
<td><label for="address">Address:</label></td>
<td><input id="address" type="text" name="address" readonly value="{{ address }}"></td>
</tr>
<tr>
<td><label for="latitude">Latitude:</label></td>
<td><input id="latitude" type="text" name="latitude"></td>
</tr>
<tr>
<td><label for="longitude">Longitude:</label></td>
<td><input id="longitude" type="text" name="longitude"></td>
</tr>
</table>
<input type="submit">
</form>
{% endif %}
{% endblock %}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment