log und so

This commit is contained in:
bton 2024-02-25 17:24:44 +01:00
parent 5cb4dcc533
commit ac907638e6
4 changed files with 99 additions and 55 deletions

View file

@ -3,19 +3,13 @@ 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
from .db import get_db, change_db
from datetime import datetime
finished = None
preis = 1.5 #Ein Getraenk
preis = 150 #Ein Getraenk
#flask_config
DATABASE = './Website/mate.db'
def log(type=None, userid=None, before=None, after=None):
db = get_db()
c = db.cursor()
c.execute("INSERT or IGNORE INTO transaction_log (timestamp, userid, type, before, after) VALUES (?, ?, ?, ?, ?)", [datetime.now(), userid, type, before, after])
db.commit()
def create_app(test_config=None):
app = Flask(__name__)
key = str(uuid.uuid4().hex)
@ -28,10 +22,9 @@ def create_app(test_config=None):
try:
os.makedirs(app.instance_path)
except OSError:
pass
#with app.app_context():
# create_logs(app)
Session(app)
socketio = SocketIO(app)
@ -51,10 +44,6 @@ def create_app(test_config=None):
def favicon():
return send_file("../static/Logo_CCC.svg.png")
#@app.route('/socket.io.js')
#def socketiojs():
# return url_for('static', filename='socket.io.js')
@app.route("/")
def index():
return render_template("index.html")
@ -65,17 +54,54 @@ def create_app(test_config=None):
c = db.cursor()
c.execute("SELECT * FROM users")
users = c.fetchall()
return render_template("list.html", users=users, preis=preis)
return render_template("list.html", users=users, preis=preis/100)
@app.route("/transactionlist")
def transactionlist():
db = get_db()
c = db.cursor()
text = ""
c.execute("SELECT * FROM transaction_log ORDER BY ROWID DESC LIMIT 100")
c.execute("SELECT * FROM transaction_log ORDER BY ROWID DESC LIMIT 1000")
transactionlist = c.fetchall()
for i in transactionlist:
text = text + f'<form action /api/<p style="display: inline;">{i[0]} userid: {i[1]} {i[2]} {i[3]} to {i[4]}</p>'
statement = i[1]
user_id = i[2]
before = i[3]
after = i[4]
change = i[5]
if statement == "balance":
reverse_statement = "balance"
reverse_user_id = user_id
reverse_before = None
reverse_after = None
reverse_change = change *(-1)
elif statement == "adduser":
reverse_statement = "removeuser"
reverse_user_id = user_id
reverse_before = after
reverse_after = None
reverse_change = None
elif statement == "removeuser":
reverse_statement = "adduser"
reverse_user_id = user_id
reverse_before = None
reverse_after = before
reverse_change = None
elif statement == "addtag":
reverse_statement = "removetag"
reverse_user_id = user_id
reverse_before = after
reverse_after = None
reverse_change = None
elif statement == "removetag":
reverse_statement = "addtag"
reverse_user_id = user_id
reverse_before = None
reverse_after = beforeaddtag
reverse_change = None
else:
raise Exception(statement)
text = text + f'<form action="/api/change" method="post"><p style="display: inline;">{i[0]} {statement} userid:{user_id} {before} {after} {change}</p><input type="hidden" name="statement" value={reverse_statement}><input type="hidden" name="user_id" value={reverse_user_id}><input type="hidden" name="before" value={reverse_before}><input type="hidden" name="after" value={reverse_after}><input type="hidden" name="change" value={reverse_change}><button type="submit">rollback</button></form><br></br>'
return text
@app.route("/list/user", methods=['GET'])
@ -103,13 +129,10 @@ def create_app(test_config=None):
c = db.cursor()
user_id = request.form["id"]
c.execute(f"SELECT * FROM users WHERE id=?", [user_id])
users = c.fetchall()
if users != []:
user_name = users[0][1]
c.execute(f"DELETE FROM tags WHERE userid=?", [user_id])
app.logger.info(f"Deleted all tags from user ?", [user_id])
c.execute(f"DELETE FROM users WHERE id=?", [user_id])
db.commit()
user = c.fetchone()
if user != None:
user_name = user[1]
change_db("removeuser", user_id=user_id, before=user_name)
socketio.emit("update", "update")
return render_template("removeuser.html", user_name=user_name)
else:
@ -124,13 +147,11 @@ 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() == []:
c.execute("INSERT or IGNORE INTO users (username, balance) VALUES (?, 0)", [username])
db.commit()
change_db("adduser", after=username)
socketio.emit("update", "update")
c.execute(f"SELECT * FROM users WHERE username=?", [username])
user = c.fetchone()
return render_template("redirect.html")
else:
return render_template("error.html", error_code="757")
@ -148,11 +169,7 @@ def create_app(test_config=None):
users = c.fetchall()
if users != []:
balance_old = users[0][2]
c.execute(f"UPDATE users SET balance = balance + {change} WHERE id={user_id}")
db.commit()
c.execute(f"SELECT * FROM users WHERE id={user_id}")
user = c.fetchone()
log(type="balance", userid=user[0], before=balance_old, after=user[2])
change_db("balance", change=change, user_id=user_id)
socketio.emit("update", "update")
return render_template("redirect.html")
else:
@ -262,12 +279,8 @@ def create_app(test_config=None):
try:
change = int(request.args.get("change"))
except:
change = -1.5
c.execute(f"UPDATE users SET balance = balance + {change*100} WHERE id={user[0]}")
db.commit()
c.execute(f"SELECT * FROM users WHERE id = {userid}")
user_new = c.fetchone()
log(type="balance", userid=user[0], before=user[2], after=user_new[2])
change = preis
change_db("balance", user_id=userid, change=change)
socketio.emit("update", "update")
return make_response(json.dumps({"mode":"balance", "username":user[1], "balance":user_new[2]}))
else:
@ -290,8 +303,6 @@ def create_app(test_config=None):
def get_id():
global finished
global message
db = get_db()
c = db.cursor()
try:
tag_id = request.form["id"]
except:
@ -315,20 +326,16 @@ def create_app(test_config=None):
finished = queue_item
return make_response(json.dumps({"mode":"error","error":"170"}))
else:
c.execute(f"INSERT OR IGNORE INTO tags (tagid, userid) VALUES ({tag_id}, ?)", [user_id])
db.commit()
change_db("addtag", after=tag_id)
message = f"Added {tag_id} to {username}"
log(type="addtag", userid=user_id ,after=tag_id)
finished = queue_item
return make_response(json.dumps({"mode":"message","username":"{}".format(username),"message":"A tag was added"}))
elif state == "remove":
c.execute(f"SELECT * FROM tags WHERE (tagid = {tag_id} AND userid = ?)", [user_id])
tags = c.fetchall()
if tags != []:
c.execute(f"DELETE FROM tags WHERE (tagid = {tag_id} AND userid = ?)", [user_id])
db.commit()
message = f"Removed {tag_id} from {username}"
log(type="removetag", userid=user_id, before=tag_id)
finished = queue_item
return make_response(json.dumps({"mode":"message","username":"{}".format(username),"message":"A tag was removed"}))
else:
@ -346,11 +353,9 @@ def create_app(test_config=None):
if user_list != []:
balance_old = user_list[0][2]
if user_queue.qsize() == 0:
c.execute(f"UPDATE users SET balance = balance - {preis*100} WHERE id={tag[1]}")
db.commit()
change_db("balance", user_id=tag[1], change=preis)
c.execute(f"SELECT * FROM users WHERE id={tag[1]}")
user = c.fetchone()
log(type="balance", userid=user[0], before=balance_old, after=user[2])
socketio.emit("update", "update")
return make_response(json.dumps({"mode":"balance", "username":user[1], "balance":user[2]/100}))
else:
@ -358,6 +363,18 @@ def create_app(test_config=None):
socketio.emit("update", "update")
return make_response(json.dumps({"mode":"error","error":"054"}))
@app.route("/api/change", methods=['POST'])
def reroll():
statement = request.form["statement"]
user_id = request.form["user_id"]
before = request.form["before"]
after = request.form["after"]
change = request.form["change"]
change_db(statement, user_id, before, after, change)
socketio.emit("update", "update")
return render_template("index.html")
#Documentation
@app.route("/documentation")
def documentation():

View file

@ -1,9 +1,34 @@
from re import M
import sqlite3
from datetime import datetime
import click
from flask import current_app, g
def log(statement, user_id, before, after, change):
db = get_db()
c = db.cursor()
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):
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])
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:
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)
db.commit()
def get_db():
if 'db' not in g:

View file

@ -13,9 +13,10 @@ CREATE TABLE IF NOT EXISTS "tags" (
);
CREATE TABLE IF NOT EXISTS "transaction_log" (
"timestamp" INTEGER NOT NULL,
"userid" INTEGER NOT NULL,
"type" TEXT NOT NULL,
"before" TEXT,
"after" TEXT
"type" TEXT NOT NULL,
"user_id" INTEGER,
"before" TEXT,
"after" TEXT,
"change" INTEGER
);
COMMIT;
COMMIT;

View file

@ -14,6 +14,7 @@
<p><a href="/">index page</a>
| <a href="/list">user and tag list</a>
| <a href="/documentation">Documentation</a>
| <a href="/transactionlist">transactionlist</a>
</p>
</nav>
<hr>
@ -21,4 +22,4 @@
{% block content %} {% endblock %}
</div>
</body>
</html>
</html>