Merge branch 'webui'
This commit is contained in:
48
app.py
48
app.py
@@ -1,4 +1,4 @@
|
||||
from flask import Flask, jsonify, request, render_template
|
||||
from flask import Flask, jsonify, request, render_template, redirect
|
||||
import json
|
||||
import os
|
||||
|
||||
@@ -29,8 +29,8 @@ app = Flask(__name__)
|
||||
|
||||
@app.route("/", methods=["GET"])
|
||||
def home():
|
||||
html = "<h1>Home page API REST Compiti</h1><h2>ciao, 3cx merda</h2>"
|
||||
html += "<a href=\"/doc\">Documentazione</a>"
|
||||
html = "<h1>Home page API REST Compiti</h1>"
|
||||
html += "<a href=\"/ui\">Apri interfaccia web</a><br><a href=\"/doc\">Documentazione</a>"
|
||||
return html, 200
|
||||
|
||||
@app.route("/doc", methods=["GET"])
|
||||
@@ -68,4 +68,46 @@ def delete_compito(id):
|
||||
update_file()
|
||||
return jsonify([]), 404
|
||||
|
||||
@app.route("/ui")
|
||||
def web_ui():
|
||||
# Tabella compiti
|
||||
html = "<table border>"
|
||||
for ciscomerda in compiti:
|
||||
html += "<tr>"
|
||||
html += f"<td>{ciscomerda["descrizione"]}</td><td><a href=\"/form_delete?id={ciscomerda["id"]}\" class=\"button\">Elimina</a>"
|
||||
html += "</tr>"
|
||||
html += "</table>"
|
||||
|
||||
html += "<form action=\"/form_add\">"
|
||||
html += "<input type=\"text\" name=\"descrizione\" placeholder=\"Descrizione compito\">"
|
||||
html += "<input type=\"submit\" value=\"Aggiungi compito\">"
|
||||
html += "</form>"
|
||||
|
||||
return html, 200
|
||||
|
||||
@app.route("/form_add")
|
||||
def form_add():
|
||||
if not request.args.get("descrizione"):
|
||||
return "Errore: descrizione mancante", 400
|
||||
else:
|
||||
global id_counter
|
||||
d = request.args.get("descrizione")
|
||||
compiti.append({"id": id_counter, "descrizione": d})
|
||||
id_counter += 1
|
||||
update_file()
|
||||
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")
|
||||
|
||||
Reference in New Issue
Block a user