This commit is contained in:
2026-02-20 10:55:55 +00:00
parent 612ee5c740
commit 305b269093
2 changed files with 73 additions and 11 deletions

View File

@@ -1,29 +1,91 @@
from flask import Flask, render_template, request, redirect, url_for
from flask import Flask, render_template, request, redirect, url_for, jsonify
import sqlite3
app = Flask(__name__)
items = {}
DB_PATH = "data.db"
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS items (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
time TEXT NOT NULL
)
""")
conn.commit()
conn.close()
def get_db_connection():
conn = sqlite3.connect(DB_PATH)
conn.row_factory = sqlite3.Row
return conn
@app.route("/")
def mainpage():
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute("SELECT name, time FROM items ORDER BY name")
items = cursor.fetchall()
conn.close()
return render_template('index.html', title='BS Shutdown', items=items)
@app.route('/submit', methods =['POST'])
def submit():
value = request.form.get("query")
time_value = request.form.get("time")
items[value] = time_value
if not value or not time_value:
return redirect(url_for("mainpage"))
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute('SELECT 1 FROM items WHERE name = ?', (value,))
item = cursor.fetchone()
if item:
conn.close()
return redirect(url_for("mainpage"))
cursor.execute(
"INSERT INTO items (name, time) VALUES (?, ?)",
(value, time_value)
)
conn.commit()
conn.close()
return redirect(url_for("mainpage"))
@app.route("/delete", methods=["POST"])
def delete():
value = request.form.get("item_name")
if value in items:
del items[value]
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute('SELECT 1 FROM items WHERE name = ?', (value,))
item = cursor.fetchone()
if not item:
conn.close()
return redirect(url_for("mainpage"))
cursor.execute(
"DELETE FROM items WHERE name = ?",
(value,)
)
conn.commit()
conn.close()
return redirect(url_for("mainpage"))
@app.route("/lookup", methods=["POST"])
def lookup():
name = request.form.get("name")
if name in items:
return jsonify(items[name]), 200
return jsonify({"error": "machine isn't in the list"}), 404
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute(
"SELECT name, time FROM items WHERE name = ?",
(name,)
)
item = cursor.fetchone()
conn.close()
if item:
return jsonify({"name": item["name"], "time": item["time"]}), 200
else:
return jsonify({"error": "Not found"}), 404

View File

@@ -35,11 +35,11 @@
<div class="card bg-dark bg-opacity-75 p-3">
<h5 class="text-white mb-3">Liste des éléments</h5>
<div class="table-items">
{% for name, time in items.items() %}
{% for item in items %}
<div class="d-flex justify-content-between align-items-center py-2">
<span class="text-white">{{ name }}</span>
<span class="text-white">{{ item.name }}</span>
<span class="text-white">{{ time }}</span>
<span class="text-white">{{ item.time }}</span>
<form method="post" action="/delete" class="m-0 p-0">
<input type="hidden" name="item_name" value="{{ name }}">