-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Refactor app.py and index.html for improved watering place fun…
…ctionality
- Loading branch information
1 parent
97bf7aa
commit 71a340d
Showing
2 changed files
with
19 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters