Compare commits
13 Commits
8a15a29dcf
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| e3a4fc4758 | |||
| c2fab079a5 | |||
| 11a3f8a31e | |||
| e6c8964f19 | |||
| f273daefc5 | |||
| fef6f7f015 | |||
| aa3329c4ee | |||
| a249127121 | |||
| 8c870484ac | |||
| b430d944bc | |||
| bb6408e816 | |||
| 29c1e40205 | |||
| 326362d157 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
compiti.json
|
||||||
45
app.py
45
app.py
@@ -1,5 +1,19 @@
|
|||||||
from flask import Flask, jsonify, request, render_template, redirect
|
from flask import Flask, jsonify, request, render_template, redirect
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
|
||||||
|
compiti = []
|
||||||
|
id_counter = 0
|
||||||
|
|
||||||
|
def update_file():
|
||||||
|
with open('compiti.json', 'w') as f:
|
||||||
|
json.dump(compiti, f)
|
||||||
|
|
||||||
|
if os.path.isfile('compiti.json'):
|
||||||
|
with open('compiti.json', 'r') as f:
|
||||||
|
compiti = json.load(f)
|
||||||
|
id_counter = compiti[-1]["id"] + 1
|
||||||
|
else:
|
||||||
compiti = [
|
compiti = [
|
||||||
{"id": 1, "descrizione": "Compito matematica"},
|
{"id": 1, "descrizione": "Compito matematica"},
|
||||||
{"id": 2, "descrizione": "Consegna TPSIT progetto API REST"},
|
{"id": 2, "descrizione": "Consegna TPSIT progetto API REST"},
|
||||||
@@ -7,13 +21,16 @@ compiti = [
|
|||||||
{"id": 4, "descrizione": "Compito di italiano"}
|
{"id": 4, "descrizione": "Compito di italiano"}
|
||||||
]
|
]
|
||||||
id_counter = 5
|
id_counter = 5
|
||||||
|
update_file()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
@app.route("/", methods=["GET"])
|
@app.route("/", methods=["GET"])
|
||||||
def home():
|
def home():
|
||||||
html = "<h1>Home page API REST Compiti</h1><h2>ciao, 3cx merda</h2>"
|
html = "<h1>Home page API REST Compiti</h1>"
|
||||||
html += "<a href=\"/doc\">Documentazione</a>"
|
html += "<a href=\"/ui\">Apri interfaccia web</a><br><a href=\"/doc\">Documentazione</a>"
|
||||||
return html, 200
|
return html, 200
|
||||||
|
|
||||||
@app.route("/doc", methods=["GET"])
|
@app.route("/doc", methods=["GET"])
|
||||||
@@ -38,6 +55,7 @@ def create_compito():
|
|||||||
j["id"] = id_counter
|
j["id"] = id_counter
|
||||||
id_counter += 1
|
id_counter += 1
|
||||||
compiti.append(j)
|
compiti.append(j)
|
||||||
|
update_file()
|
||||||
return jsonify(j), 201
|
return jsonify(j), 201
|
||||||
|
|
||||||
@app.route("/compiti/<int:id>", methods=["DELETE"])
|
@app.route("/compiti/<int:id>", methods=["DELETE"])
|
||||||
@@ -45,23 +63,26 @@ def delete_compito(id):
|
|||||||
for ciscomerda in compiti:
|
for ciscomerda in compiti:
|
||||||
if ciscomerda["id"] == id:
|
if ciscomerda["id"] == id:
|
||||||
compiti.remove(ciscomerda)
|
compiti.remove(ciscomerda)
|
||||||
|
update_file()
|
||||||
return jsonify(ciscomerda), 200
|
return jsonify(ciscomerda), 200
|
||||||
return jsonify([]), 404
|
return jsonify([]), 404
|
||||||
|
|
||||||
@app.route("/ui")
|
@app.route("/ui")
|
||||||
def web_ui():
|
def web_ui():
|
||||||
# Tabella compiti
|
# Tabella compiti
|
||||||
html = "<table border>"
|
html = "<html><head>"
|
||||||
|
html += "<meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><title>Gestione compiti</title>"
|
||||||
|
html += "</head><body><table border>"
|
||||||
for ciscomerda in compiti:
|
for ciscomerda in compiti:
|
||||||
html += "<tr>"
|
html += "<tr>"
|
||||||
html += f"<td>{ciscomerda["descrizione"]}</td>"
|
html += f"<td>{ciscomerda["descrizione"]}</td><td><a href=\"/form_delete?id={ciscomerda["id"]}\" class=\"button\"><button>Elimina</button></a>"
|
||||||
html += "</tr>"
|
html += "</tr>"
|
||||||
html += "</table>"
|
html += "</table>"
|
||||||
|
|
||||||
html += "<form action=\"/form_add\">"
|
html += "<form action=\"/form_add\">"
|
||||||
html += "<input type=\"text\" name=\"descrizione\" placeholder=\"Descrizione compito\">"
|
html += "<input type=\"text\" name=\"descrizione\" placeholder=\"Descrizione compito\">"
|
||||||
html += "<input type=\"submit\" value=\"Aggiungi compito\">"
|
html += "<input type=\"submit\" value=\"Aggiungi compito\">"
|
||||||
html += "</form>"
|
html += "</form></body></html>"
|
||||||
|
|
||||||
return html, 200
|
return html, 200
|
||||||
|
|
||||||
@@ -74,6 +95,20 @@ def form_add():
|
|||||||
d = request.args.get("descrizione")
|
d = request.args.get("descrizione")
|
||||||
compiti.append({"id": id_counter, "descrizione": d})
|
compiti.append({"id": id_counter, "descrizione": d})
|
||||||
id_counter += 1
|
id_counter += 1
|
||||||
|
update_file()
|
||||||
return redirect("/ui")
|
return redirect("/ui")
|
||||||
|
|
||||||
|
@app.route("/form_delete")
|
||||||
|
def form_delete():
|
||||||
|
if not request.args.get("id"):
|
||||||
|
return "Errore: id mancante", 400
|
||||||
|
else:
|
||||||
|
id = int(request.args.get("id"))
|
||||||
|
for ciscomerda in compiti:
|
||||||
|
if ciscomerda["id"] == id:
|
||||||
|
compiti.remove(ciscomerda)
|
||||||
|
update_file()
|
||||||
|
return redirect("/ui")
|
||||||
|
return "Errore: compito non trovato", 404
|
||||||
|
|
||||||
app.run("0.0.0.0")
|
app.run("0.0.0.0")
|
||||||
|
|||||||
Reference in New Issue
Block a user