new db system
This commit is contained in:
parent
7f1f908961
commit
ce009a278b
2 changed files with 64 additions and 28 deletions
|
@ -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_session import Session
|
||||
from markupsafe import escape
|
||||
from .db import get_db, change_db
|
||||
from .db import get_db
|
||||
import .db as db
|
||||
from datetime import datetime
|
||||
finished = None
|
||||
preis = 150 #Ein Getraenk
|
||||
|
@ -132,7 +133,7 @@ def create_app(test_config=None):
|
|||
user = c.fetchone()
|
||||
if user != None:
|
||||
user_name = user[1]
|
||||
change_db("removeuser", user_id=user_id, before=user_name)
|
||||
db.remove_user(user_id)
|
||||
socketio.emit("update", "update")
|
||||
return render_template("removeuser.html", user_name=user_name)
|
||||
else:
|
||||
|
@ -147,7 +148,7 @@ def create_app(test_config=None):
|
|||
return render_template("error.html", error_code="418")
|
||||
c.execute("SELECT * FROM users WHERE username=?", [username])
|
||||
if c.fetchall() == []:
|
||||
change_db("adduser", after=username)
|
||||
db.add_user(username)
|
||||
socketio.emit("update", "update")
|
||||
c.execute(f"SELECT * FROM users WHERE username=?", [username])
|
||||
user = c.fetchone()
|
||||
|
@ -169,7 +170,7 @@ def create_app(test_config=None):
|
|||
users = c.fetchall()
|
||||
if users != []:
|
||||
balance_old = users[0][2]
|
||||
change_db("balance", change=change, user_id=user_id)
|
||||
db.change_balance(user_id, change)
|
||||
socketio.emit("update", "update")
|
||||
return render_template("redirect.html")
|
||||
else:
|
||||
|
@ -231,10 +232,8 @@ def create_app(test_config=None):
|
|||
c = db.cursor()
|
||||
c.execute(f"SELECT * FROM tags WHERE (tagid = ? AND userid = ?)", [tag_id, user_id])
|
||||
if c.fetchall != []:
|
||||
c.execute(f"DELETE FROM tags WHERE (tagid = ? AND userid = ?)", [tag_id, user_id])
|
||||
db.commit()
|
||||
db.remove_tag(tag_id)
|
||||
message = f"Removed {tag_id} from user {user_id}"
|
||||
log(type="removetag", userid=user_id, before=tag_id)
|
||||
return render_template("redirect.html")
|
||||
else:
|
||||
return render_template("error.html", error_code="054")
|
||||
|
@ -280,7 +279,7 @@ def create_app(test_config=None):
|
|||
change = int(request.args.get("change"))
|
||||
except:
|
||||
change = preis
|
||||
change_db("balance", user_id=userid, change=change)
|
||||
db.change_balance(user_id, change)
|
||||
socketio.emit("update", "update")
|
||||
return make_response(json.dumps({"mode":"balance", "username":user[1], "balance":user_new[2]}))
|
||||
else:
|
||||
|
@ -328,7 +327,7 @@ def create_app(test_config=None):
|
|||
finished = queue_item
|
||||
return make_response(json.dumps({"mode":"error","error":"170"}))
|
||||
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}"
|
||||
finished = queue_item
|
||||
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 != []:
|
||||
balance_old = user_list[0][2]
|
||||
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]}")
|
||||
user = c.fetchone()
|
||||
socketio.emit("update", "update")
|
||||
|
@ -373,7 +372,20 @@ def create_app(test_config=None):
|
|||
before = request.form["before"]
|
||||
after = request.form["after"]
|
||||
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")
|
||||
return render_template("index.html")
|
||||
|
||||
|
@ -382,4 +394,5 @@ def create_app(test_config=None):
|
|||
def documentation():
|
||||
return render_template("documentation.html")
|
||||
|
||||
|
||||
return {"app":app,"socketio":socketio}
|
||||
|
|
|
@ -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])
|
||||
db.commit()
|
||||
|
||||
def change_db(statement, user_id=None, before=None, after=None, change=None):
|
||||
def add_user(after):
|
||||
db = get_db()
|
||||
c = db.cursor()
|
||||
if statement == "adduser" and after != None:
|
||||
c.execute("INSERT or IGNORE INTO users (username, balance) VALUES (?, 0)", [after])
|
||||
user_id = c.lastrowid
|
||||
elif statement == "removeuser" and user_id != None and before != None:
|
||||
c.execute("DELETE FROM tags WHERE userid=?", [user_id])
|
||||
log("add_user", user_id=user_id, after=after)
|
||||
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])
|
||||
elif statement == "addtag" and after != None and user_id != None:
|
||||
c.execute("INSERT OR IGNORE INTO tags (tagid, userid) VALUES ?, ?)", [after, user_id])
|
||||
elif statement == "removetag" and before != None and user_id != None:
|
||||
c.execute("DELETE FROM tags WHERE (tagid = ? AND userid = ?)", [before, user_id])
|
||||
elif statement == "balance" and change != None and user_id != None:
|
||||
log("remove_user", user_id=user_id, before=user_name)
|
||||
db.commit()
|
||||
|
||||
def add_tag(user_id, tag_id):
|
||||
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])
|
||||
else:
|
||||
raise Exception("wrong or missing argument for change_db")
|
||||
log(statement, user_id, before, after, change)
|
||||
log("balance", user_id=user_id, change=change)
|
||||
db.commit()
|
||||
|
||||
def get_db():
|
||||
|
|
Loading…
Reference in a new issue