new db system

This commit is contained in:
bton 2024-03-01 21:59:30 +01:00
parent 7f1f908961
commit ce009a278b
2 changed files with 64 additions and 28 deletions

View file

@ -3,7 +3,8 @@ from flask import Flask, render_template, render_template_string, request, make_
from flask_socketio import SocketIO, join_room, leave_room from flask_socketio import SocketIO, join_room, leave_room
from flask_session import Session from flask_session import Session
from markupsafe import escape from markupsafe import escape
from .db import get_db, change_db from .db import get_db
import .db as db
from datetime import datetime from datetime import datetime
finished = None finished = None
preis = 150 #Ein Getraenk preis = 150 #Ein Getraenk
@ -132,7 +133,7 @@ def create_app(test_config=None):
user = c.fetchone() user = c.fetchone()
if user != None: if user != None:
user_name = user[1] user_name = user[1]
change_db("removeuser", user_id=user_id, before=user_name) db.remove_user(user_id)
socketio.emit("update", "update") socketio.emit("update", "update")
return render_template("removeuser.html", user_name=user_name) return render_template("removeuser.html", user_name=user_name)
else: else:
@ -147,7 +148,7 @@ def create_app(test_config=None):
return render_template("error.html", error_code="418") return render_template("error.html", error_code="418")
c.execute("SELECT * FROM users WHERE username=?", [username]) c.execute("SELECT * FROM users WHERE username=?", [username])
if c.fetchall() == []: if c.fetchall() == []:
change_db("adduser", after=username) db.add_user(username)
socketio.emit("update", "update") socketio.emit("update", "update")
c.execute(f"SELECT * FROM users WHERE username=?", [username]) c.execute(f"SELECT * FROM users WHERE username=?", [username])
user = c.fetchone() user = c.fetchone()
@ -169,7 +170,7 @@ def create_app(test_config=None):
users = c.fetchall() users = c.fetchall()
if users != []: if users != []:
balance_old = users[0][2] balance_old = users[0][2]
change_db("balance", change=change, user_id=user_id) db.change_balance(user_id, change)
socketio.emit("update", "update") socketio.emit("update", "update")
return render_template("redirect.html") return render_template("redirect.html")
else: else:
@ -231,10 +232,8 @@ def create_app(test_config=None):
c = db.cursor() c = db.cursor()
c.execute(f"SELECT * FROM tags WHERE (tagid = ? AND userid = ?)", [tag_id, user_id]) c.execute(f"SELECT * FROM tags WHERE (tagid = ? AND userid = ?)", [tag_id, user_id])
if c.fetchall != []: if c.fetchall != []:
c.execute(f"DELETE FROM tags WHERE (tagid = ? AND userid = ?)", [tag_id, user_id]) db.remove_tag(tag_id)
db.commit()
message = f"Removed {tag_id} from user {user_id}" message = f"Removed {tag_id} from user {user_id}"
log(type="removetag", userid=user_id, before=tag_id)
return render_template("redirect.html") return render_template("redirect.html")
else: else:
return render_template("error.html", error_code="054") return render_template("error.html", error_code="054")
@ -280,7 +279,7 @@ def create_app(test_config=None):
change = int(request.args.get("change")) change = int(request.args.get("change"))
except: except:
change = preis change = preis
change_db("balance", user_id=userid, change=change) db.change_balance(user_id, change)
socketio.emit("update", "update") socketio.emit("update", "update")
return make_response(json.dumps({"mode":"balance", "username":user[1], "balance":user_new[2]})) return make_response(json.dumps({"mode":"balance", "username":user[1], "balance":user_new[2]}))
else: else:
@ -328,7 +327,7 @@ def create_app(test_config=None):
finished = queue_item finished = queue_item
return make_response(json.dumps({"mode":"error","error":"170"})) return make_response(json.dumps({"mode":"error","error":"170"}))
else: else:
change_db("addtag", after=tag_id, user_id=user_id) db.add_tag(user_id, tag_id)
message = f"Added {tag_id} to {username}" message = f"Added {tag_id} to {username}"
finished = queue_item finished = queue_item
return make_response(json.dumps({"mode":"message","username":"{}".format(username),"message":"A tag was added"})) return make_response(json.dumps({"mode":"message","username":"{}".format(username),"message":"A tag was added"}))
@ -355,7 +354,7 @@ def create_app(test_config=None):
if user_list != []: if user_list != []:
balance_old = user_list[0][2] balance_old = user_list[0][2]
if user_queue.qsize() == 0: if user_queue.qsize() == 0:
change_db("balance", user_id=tag[1], change=preis) db.change_balance(tag[1], preis)
c.execute(f"SELECT * FROM users WHERE id={tag[1]}") c.execute(f"SELECT * FROM users WHERE id={tag[1]}")
user = c.fetchone() user = c.fetchone()
socketio.emit("update", "update") socketio.emit("update", "update")
@ -373,7 +372,20 @@ def create_app(test_config=None):
before = request.form["before"] before = request.form["before"]
after = request.form["after"] after = request.form["after"]
change = request.form["change"] change = request.form["change"]
change_db(statement, user_id, before, after, change)
if statement == "adduser":
db.add_user(after)
elif statement == "removeuser":
db.remove_user(user_id)
elif statement == "addtag":
db.add_tag(user_id, after)
elif statement == "removetag":
db.remove_tag(befor)
elif statement == "balance":
db.change_balance(user_id, change)
else:
return make_response(json.dumps({"mode":"error", "error":"418"})) #Error code
socketio.emit("update", "update") socketio.emit("update", "update")
return render_template("index.html") return render_template("index.html")
@ -382,4 +394,5 @@ def create_app(test_config=None):
def documentation(): def documentation():
return render_template("documentation.html") return render_template("documentation.html")
return {"app":app,"socketio":socketio} return {"app":app,"socketio":socketio}

View file

@ -10,24 +10,47 @@ def log(statement, user_id, before, after, change):
c.execute("INSERT INTO transaction_log (timestamp, type, user_id, before, after, change) VALUES (?, ?, ?, ?, ?, ?)", [datetime.now(), statement, user_id, before, after, change]) c.execute("INSERT INTO transaction_log (timestamp, type, user_id, before, after, change) VALUES (?, ?, ?, ?, ?, ?)", [datetime.now(), statement, user_id, before, after, change])
db.commit() db.commit()
def change_db(statement, user_id=None, before=None, after=None, change=None): def add_user(after):
db = get_db() db = get_db()
c = db.cursor() c = db.cursor()
if statement == "adduser" and after != None:
c.execute("INSERT or IGNORE INTO users (username, balance) VALUES (?, 0)", [after]) c.execute("INSERT or IGNORE INTO users (username, balance) VALUES (?, 0)", [after])
user_id = c.lastrowid user_id = c.lastrowid
elif statement == "removeuser" and user_id != None and before != None: log("add_user", user_id=user_id, after=after)
c.execute("DELETE FROM tags WHERE userid=?", [user_id]) db.commit()
def remove_user(user_id):
db = get_db()
c = db.cursor()
c.execute("SELECT * FROM users WHERE id = ?", [user_id])
user_name = c.fetchone()[1]
c.execute("SELECT * FROM tags WHERE userid = ?", [user_id])
for tag in c.fetchall():
remove_tag(tag[0])
c.execute("DELETE FROM users WHERE id = ?", [user_id]) c.execute("DELETE FROM users WHERE id = ?", [user_id])
elif statement == "addtag" and after != None and user_id != None: log("remove_user", user_id=user_id, before=user_name)
c.execute("INSERT OR IGNORE INTO tags (tagid, userid) VALUES ?, ?)", [after, user_id]) db.commit()
elif statement == "removetag" and before != None and user_id != None:
c.execute("DELETE FROM tags WHERE (tagid = ? AND userid = ?)", [before, user_id]) def add_tag(user_id, tag_id):
elif statement == "balance" and change != None and user_id != None: db = get_db()
c = db.cursor()
c.execute("INSERT OR IGNORE INTO tags (tagid, userid) VALUES ?, ?)", [tag_id, user_id])
db.commit()
log("addtag", after=tag_id, user_id=user_id)
def remove_tag(tag_id):
db = get_db()
c = db.cursor()
c.execute("SELECT * FROM tags WHERE tagid = ?", [tag_id])
user_id = c.fetchone()[1]
c.execute("DELETE FROM tags WHERE tagid = ?", [tag_id])
log("removetag", before=tag_id, user_id=user_id)
db.commit()
def change_balance(user_id, change):
db = get_db()
c = db.cursor()
c.execute("UPDATE users SET balance = balance + ? WHERE id=?", [change, user_id]) c.execute("UPDATE users SET balance = balance + ? WHERE id=?", [change, user_id])
else: log("balance", user_id=user_id, change=change)
raise Exception("wrong or missing argument for change_db")
log(statement, user_id, before, after, change)
db.commit() db.commit()
def get_db(): def get_db():