Skip to content

Commit

Permalink
chore: Refactor app.py and index.html for improved watering place fun…
Browse files Browse the repository at this point in the history
…ctionality
  • Loading branch information
angelof-exe committed Aug 4, 2024
1 parent 97bf7aa commit 71a340d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 52 deletions.
66 changes: 16 additions & 50 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,62 +1,28 @@
from flask import Flask, render_template, jsonify, request, redirect, url_for, flash
from flask_bootstrap import Bootstrap
from flask import Flask, render_template
import requests
import json
import os
from flask_bootstrap import Bootstrap

app = Flask(__name__)
app.secret_key = 'supersecretkey'
Bootstrap(app)

OVERPASS_URL = "http://overpass-api.de/api/interpreter"
OVERPASS_QUERY = """
[out:json][timeout:25];
nwr["amenity"="watering_place"](36.3203615818532,12.008056640625,39.15214258358986,16.644287109375004);
out geom;
"""

STATUS_FILE = 'watering_place_status.json'

# Carica lo stato degli abbeveratoi dal file
if os.path.exists(STATUS_FILE):
with open(STATUS_FILE, 'r') as f:
watering_place_status = json.load(f)
else:
watering_place_status = {}

@app.route('/', methods=['GET', 'POST'])
def index():
return render_template('index.html')

@app.route('/api/watering_places')
def watering_places():
response = requests.get(OVERPASS_URL, params={'data': OVERPASS_QUERY})
# Load OpenStreetMap data with status
def load_watering_places():
overpass_url = "http://overpass-api.de/api/interpreter"
overpass_query = """
[out:json][timeout:25];
nwr["amenity"="watering_place"](36.3203615818532,12.008056640625,39.15214258358986,16.644287109375004);
out geom;
"""
response = requests.get(overpass_url, params={'data': overpass_query})
data = response.json()
return data['elements']

# Aggiungi lo stato degli abbeveratoi
for element in data['elements']:
if element['type'] == 'node':
element_id = str(element['id'])
element['status'] = watering_place_status.get(element_id, 'unknown')
watering_places = load_watering_places()

return jsonify(data)

@app.route('/report_status', methods=['POST'])
def report_status():
node_id = request.form['node_id']
status = request.form.get('status')

if status not in ['active', 'inactive']:
flash('Per favore seleziona lo stato dell\'abbeveratoio.')
return redirect(url_for('index'))

watering_place_status[node_id] = status

# Salva lo stato degli abbeveratoi nel file
with open(STATUS_FILE, 'w') as f:
json.dump(watering_place_status, f)

return redirect(url_for('index'))
@app.route('/')
def index():
return render_template('index.html', watering_places=watering_places)

if __name__ == '__main__':
app.run(debug=True)
5 changes: 3 additions & 2 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ <h2>Lista degli abbeveratoi</h2>
<ul id="watering-place-list" class="list-group">
{% for place in watering_places %}
<li class="list-group-item watering-place-item" data-status="{{ place.tags.status or 'unknown' }}"
onclick="focusOnMap({{ place.lat or place.center.lat }}, {{ place.lon or place.center.lon }})">
onclick="focusOnMap({{ place.lat or place.get('center', {}).get('lat', '0') }}, {{ place.lon or place.get('center', {}).get('lon', '0') }})">
<strong>{{ place.tags.name or 'Abbeveratoio' }}</strong> - {{ place.tags['addr:city'] or
place.tags['addr:town'] or place.tags['addr:village'] or 'Sconosciuta' }}
<br>
Expand All @@ -37,11 +37,12 @@ <h2>Lista degli abbeveratoi</h2>
{{ 'Attivo' if place.tags.status == 'active' else 'Non attivo' if place.tags.status ==
'inactive' else 'Sconosciuto' }}
</span>
<a href="https://www.openstreetmap.org/?mlat={{ place.lat }}&mlon={{ place.lon }}#map=19/{{ place.lat }}/{{ place.lon }}"
<a href="https://www.openstreetmap.org/?mlat={{ place.lat or place.get('center', {}).get('lat', '0') }}&mlon={{ place.lon or place.get('center', {}).get('lon', '0') }}#map=19/{{ place.lat or place.get('center', {}).get('lat', '0') }}/{{ place.lon or place.get('center', {}).get('lon', '0') }}"
target="_blank" class="ml-3">Vedi su OpenStreetMap</a>
</li>
{% endfor %}
</ul>

</div>
</div>
</div>
Expand Down

0 comments on commit 71a340d

Please sign in to comment.