Compare commits
2 Commits
1b83098fd0
...
319d237e5d
| Author | SHA1 | Date | |
|---|---|---|---|
| 319d237e5d | |||
| af6515e55f |
@@ -1,8 +1,11 @@
|
||||
from flask import Flask, render_template, request, redirect, url_for, jsonify
|
||||
import sqlite3
|
||||
import re
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
pattern = re.compile(r"^(?:[01]\d|2[0-3]):[0-5]\d$") # Regex "XX:XX"
|
||||
|
||||
DB_PATH = "data.db"
|
||||
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
@@ -16,6 +19,11 @@ CREATE TABLE IF NOT EXISTS items (
|
||||
noshutdown INTEGER DEFAULT 0
|
||||
)
|
||||
""")
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS val (
|
||||
default_time TEXT NOT NULL DEFAULT '21:00'
|
||||
)
|
||||
""")
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
@@ -30,9 +38,10 @@ def mainpage():
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("SELECT name, time, noshutdown FROM items ORDER BY name")
|
||||
items = cursor.fetchall()
|
||||
cursor.execute("SELECT default_time FROM val")
|
||||
default_time = cursor.fetchone()
|
||||
conn.close()
|
||||
print(items)
|
||||
return render_template('index.html', title='BS Shutdown', items=items)
|
||||
return render_template('index.html', title='BS Shutdown', items=items, default_time=default_time)
|
||||
|
||||
@app.route('/submit', methods =['POST'])
|
||||
def submit():
|
||||
@@ -45,6 +54,9 @@ def submit():
|
||||
return redirect(url_for("mainpage"))
|
||||
if noshutdown == 1:
|
||||
time_value = 'None'
|
||||
if time_value != 'None':
|
||||
if bool(pattern.match(time_value)) is not True:
|
||||
return redirect(url_for("mainpage"))
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('SELECT 1 FROM items WHERE name = ?', (value,))
|
||||
@@ -57,6 +69,18 @@ def submit():
|
||||
conn.close()
|
||||
return redirect(url_for("mainpage"))
|
||||
|
||||
@app.route("/submit-default", methods=["POST"])
|
||||
def submit_default():
|
||||
time_value = request.form.get("time-select")
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
if bool(pattern.match(time_value)) is not True:
|
||||
return redirect(url_for("mainpage"))
|
||||
cursor.execute("UPDATE val SET default_time = ?", (time_value,))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
return redirect(url_for("mainpage"))
|
||||
|
||||
@app.route("/delete", methods=["POST"])
|
||||
def delete():
|
||||
value = request.form.get("item_name")
|
||||
|
||||
@@ -41,11 +41,20 @@
|
||||
|
||||
<div class="col-auto gap-1 ms-auto">
|
||||
<form method="post" action="/submit-default" class="d-flex gap-1">
|
||||
<span class="text-white">Heure par défaut :</span>
|
||||
<select name="default-time" class="form-select bg-dark text-white flex-grow-1" id="default-time-select">
|
||||
{% for hour in range(0, 24) %}
|
||||
<option value="{{ '%02d:00'|format(hour) }}">{{ '%02d:00'|format(hour) }}</option>
|
||||
<option value="{{ '%02d:30'|format(hour) }}">{{ '%02d:30'|format(hour) }}</option>
|
||||
{% set h1 = '%02d:00'|format(hour) %}
|
||||
{% set h2 = '%02d:30'|format(hour) %}
|
||||
|
||||
<option value="{{ h1 }}"
|
||||
{% if h1 == default_time %}selected{% endif %}>
|
||||
{{ h1 }}
|
||||
</option>
|
||||
|
||||
<option value="{{ h2 }}"
|
||||
{% if h2 == default_time %}selected{% endif %}>
|
||||
{{ h2 }}
|
||||
</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
<button type="submit" class="btn btn-sm btn-primary">
|
||||
|
||||
Reference in New Issue
Block a user