139 lines
4.3 KiB
Python
139 lines
4.3 KiB
Python
from flask import Flask, render_template, request, redirect, url_for, jsonify
|
|
import sqlite3
|
|
import re
|
|
from datetime import datetime
|
|
|
|
# -- INIT --
|
|
app = Flask(__name__)
|
|
pattern = re.compile(r"^(?:[01]\d|2[0-3]):[0-5]\d$") # Regex "XX:XX"
|
|
|
|
# -- GLOBAL VAR --
|
|
DB_PATH = "data.db"
|
|
DEFAULT_TIME = "21:00"
|
|
|
|
## -- INIT 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,
|
|
noshutdown INTEGER DEFAULT 0
|
|
)
|
|
""")
|
|
cursor.execute("""
|
|
CREATE TABLE IF NOT EXISTS val (
|
|
default_time TEXT NOT NULL DEFAULT '21:00'
|
|
)
|
|
""")
|
|
cursor.execute("SELECT COUNT(*) FROM val")
|
|
count = cursor.fetchone()[0]
|
|
if count == 0:
|
|
cursor.execute("INSERT INTO val (default_time) VALUES (?)", ('21:00',))
|
|
conn.commit()
|
|
conn.close()
|
|
# -- END --
|
|
|
|
def get_db_connection():
|
|
conn = sqlite3.connect(DB_PATH)
|
|
conn.row_factory = sqlite3.Row
|
|
return conn
|
|
|
|
# --- MAIN ---
|
|
@app.route("/")
|
|
def mainpage():
|
|
conn = get_db_connection()
|
|
cursor = conn.cursor()
|
|
cursor.execute("SELECT name, time, noshutdown, last_connection FROM items ORDER BY name")
|
|
items = cursor.fetchall()
|
|
cursor.execute("SELECT default_time FROM val")
|
|
default_time_row = cursor.fetchone()
|
|
default_time = default_time_row[0] if default_time_row else DEFAULT_TIME
|
|
conn.close()
|
|
return render_template('index.html', title='BS Shutdown', items=items, default_time=default_time)
|
|
|
|
@app.route('/submit', methods =['POST'])
|
|
def submit():
|
|
value = request.form.get("query")
|
|
time_value = request.form.get("time")
|
|
noshutdown = 1 if request.form.get("noshutdown") == "on" else 0
|
|
if not value:
|
|
return redirect(url_for("mainpage"))
|
|
if noshutdown == 0 and not time_value:
|
|
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,))
|
|
item = cursor.fetchone()
|
|
if item:
|
|
cursor.execute("UPDATE items SET time = ?, noshutdown = ? WHERE name = ?", (time_value, noshutdown, value))
|
|
else:
|
|
cursor.execute("INSERT INTO items (name, time, noshutdown) VALUES (?, ?, ?)", (value, time_value, noshutdown))
|
|
conn.commit()
|
|
conn.close()
|
|
return redirect(url_for("mainpage"))
|
|
|
|
@app.route("/submit-default", methods=["POST"])
|
|
def submit_default():
|
|
time_value = request.form.get("default-time")
|
|
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")
|
|
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"))
|
|
|
|
# --- END MAIN ---
|
|
# --- API ---
|
|
@app.route("/lookup", methods=["POST"])
|
|
def lookup():
|
|
data = request.get_json()
|
|
if not data or "name" not in data:
|
|
return jsonify({"error": "Not found"}), 404
|
|
name = data["name"]
|
|
conn = get_db_connection()
|
|
cursor = conn.cursor()
|
|
cursor.execute(
|
|
"SELECT name, time, noshutdown FROM items WHERE name = ?",
|
|
(name,)
|
|
)
|
|
item = cursor.fetchone()
|
|
cursor.execute("SELECT default_time FROM val")
|
|
default_time_row = cursor.fetchone()
|
|
default_time = default_time_row[0] if default_time_row else DEFAULT_TIME
|
|
if item:
|
|
cursor.execute("UPDATE items SET last_connection = ? WHERE name = ?", (datetime.now(), item["name"]))
|
|
conn.commit()
|
|
conn.close()
|
|
|
|
if item:
|
|
return jsonify({"name": item["name"], "noshutdown": item["noshutdown"], "time": item["time"]}), 200
|
|
else:
|
|
return jsonify({"noshutdown": 0, "time": default_time}), 200
|
|
# --- END API --- |