forked from bton/matekasse
log und so
This commit is contained in:
parent
5cb4dcc533
commit
ac907638e6
4 changed files with 99 additions and 55 deletions
|
@ -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_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
|
from .db import get_db, change_db
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
finished = None
|
finished = None
|
||||||
preis = 1.5 #Ein Getraenk
|
preis = 150 #Ein Getraenk
|
||||||
#flask_config
|
#flask_config
|
||||||
DATABASE = './Website/mate.db'
|
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):
|
def create_app(test_config=None):
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
key = str(uuid.uuid4().hex)
|
key = str(uuid.uuid4().hex)
|
||||||
|
@ -28,10 +22,9 @@ def create_app(test_config=None):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
os.makedirs(app.instance_path)
|
os.makedirs(app.instance_path)
|
||||||
|
|
||||||
except OSError:
|
except OSError:
|
||||||
pass
|
pass
|
||||||
#with app.app_context():
|
|
||||||
# create_logs(app)
|
|
||||||
|
|
||||||
Session(app)
|
Session(app)
|
||||||
socketio = SocketIO(app)
|
socketio = SocketIO(app)
|
||||||
|
@ -51,10 +44,6 @@ def create_app(test_config=None):
|
||||||
def favicon():
|
def favicon():
|
||||||
return send_file("../static/Logo_CCC.svg.png")
|
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("/")
|
@app.route("/")
|
||||||
def index():
|
def index():
|
||||||
return render_template("index.html")
|
return render_template("index.html")
|
||||||
|
@ -65,17 +54,54 @@ def create_app(test_config=None):
|
||||||
c = db.cursor()
|
c = db.cursor()
|
||||||
c.execute("SELECT * FROM users")
|
c.execute("SELECT * FROM users")
|
||||||
users = c.fetchall()
|
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")
|
@app.route("/transactionlist")
|
||||||
def transactionlist():
|
def transactionlist():
|
||||||
db = get_db()
|
db = get_db()
|
||||||
c = db.cursor()
|
c = db.cursor()
|
||||||
text = ""
|
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()
|
transactionlist = c.fetchall()
|
||||||
for i in transactionlist:
|
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
|
return text
|
||||||
|
|
||||||
@app.route("/list/user", methods=['GET'])
|
@app.route("/list/user", methods=['GET'])
|
||||||
|
@ -103,13 +129,10 @@ def create_app(test_config=None):
|
||||||
c = db.cursor()
|
c = db.cursor()
|
||||||
user_id = request.form["id"]
|
user_id = request.form["id"]
|
||||||
c.execute(f"SELECT * FROM users WHERE id=?", [user_id])
|
c.execute(f"SELECT * FROM users WHERE id=?", [user_id])
|
||||||
users = c.fetchall()
|
user = c.fetchone()
|
||||||
if users != []:
|
if user != None:
|
||||||
user_name = users[0][1]
|
user_name = user[1]
|
||||||
c.execute(f"DELETE FROM tags WHERE userid=?", [user_id])
|
change_db("removeuser", user_id=user_id, before=user_name)
|
||||||
app.logger.info(f"Deleted all tags from user ?", [user_id])
|
|
||||||
c.execute(f"DELETE FROM users WHERE id=?", [user_id])
|
|
||||||
db.commit()
|
|
||||||
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:
|
||||||
|
@ -124,13 +147,11 @@ 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() == []:
|
||||||
c.execute("INSERT or IGNORE INTO users (username, balance) VALUES (?, 0)", [username])
|
change_db("adduser", after=username)
|
||||||
db.commit()
|
|
||||||
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()
|
||||||
return render_template("redirect.html")
|
return render_template("redirect.html")
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return render_template("error.html", error_code="757")
|
return render_template("error.html", error_code="757")
|
||||||
|
|
||||||
|
@ -148,11 +169,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]
|
||||||
c.execute(f"UPDATE users SET balance = balance + {change} WHERE id={user_id}")
|
change_db("balance", change=change, user_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])
|
|
||||||
socketio.emit("update", "update")
|
socketio.emit("update", "update")
|
||||||
return render_template("redirect.html")
|
return render_template("redirect.html")
|
||||||
else:
|
else:
|
||||||
|
@ -262,12 +279,8 @@ def create_app(test_config=None):
|
||||||
try:
|
try:
|
||||||
change = int(request.args.get("change"))
|
change = int(request.args.get("change"))
|
||||||
except:
|
except:
|
||||||
change = -1.5
|
change = preis
|
||||||
c.execute(f"UPDATE users SET balance = balance + {change*100} WHERE id={user[0]}")
|
change_db("balance", user_id=userid, change=change)
|
||||||
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])
|
|
||||||
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:
|
||||||
|
@ -290,8 +303,6 @@ def create_app(test_config=None):
|
||||||
def get_id():
|
def get_id():
|
||||||
global finished
|
global finished
|
||||||
global message
|
global message
|
||||||
db = get_db()
|
|
||||||
c = db.cursor()
|
|
||||||
try:
|
try:
|
||||||
tag_id = request.form["id"]
|
tag_id = request.form["id"]
|
||||||
except:
|
except:
|
||||||
|
@ -315,20 +326,16 @@ 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:
|
||||||
c.execute(f"INSERT OR IGNORE INTO tags (tagid, userid) VALUES ({tag_id}, ?)", [user_id])
|
change_db("addtag", after=tag_id)
|
||||||
db.commit()
|
|
||||||
message = f"Added {tag_id} to {username}"
|
message = f"Added {tag_id} to {username}"
|
||||||
log(type="addtag", userid=user_id ,after=tag_id)
|
|
||||||
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"}))
|
||||||
elif state == "remove":
|
elif state == "remove":
|
||||||
c.execute(f"SELECT * FROM tags WHERE (tagid = {tag_id} AND userid = ?)", [user_id])
|
c.execute(f"SELECT * FROM tags WHERE (tagid = {tag_id} AND userid = ?)", [user_id])
|
||||||
tags = c.fetchall()
|
tags = c.fetchall()
|
||||||
if tags != []:
|
if tags != []:
|
||||||
c.execute(f"DELETE FROM tags WHERE (tagid = {tag_id} AND userid = ?)", [user_id])
|
|
||||||
db.commit()
|
db.commit()
|
||||||
message = f"Removed {tag_id} from {username}"
|
message = f"Removed {tag_id} from {username}"
|
||||||
log(type="removetag", userid=user_id, before=tag_id)
|
|
||||||
finished = queue_item
|
finished = queue_item
|
||||||
return make_response(json.dumps({"mode":"message","username":"{}".format(username),"message":"A tag was removed"}))
|
return make_response(json.dumps({"mode":"message","username":"{}".format(username),"message":"A tag was removed"}))
|
||||||
else:
|
else:
|
||||||
|
@ -346,11 +353,9 @@ 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:
|
||||||
c.execute(f"UPDATE users SET balance = balance - {preis*100} WHERE id={tag[1]}")
|
change_db("balance", user_id=tag[1], change=preis)
|
||||||
db.commit()
|
|
||||||
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()
|
||||||
log(type="balance", userid=user[0], before=balance_old, after=user[2])
|
|
||||||
socketio.emit("update", "update")
|
socketio.emit("update", "update")
|
||||||
return make_response(json.dumps({"mode":"balance", "username":user[1], "balance":user[2]/100}))
|
return make_response(json.dumps({"mode":"balance", "username":user[1], "balance":user[2]/100}))
|
||||||
else:
|
else:
|
||||||
|
@ -358,6 +363,18 @@ def create_app(test_config=None):
|
||||||
socketio.emit("update", "update")
|
socketio.emit("update", "update")
|
||||||
return make_response(json.dumps({"mode":"error","error":"054"}))
|
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
|
#Documentation
|
||||||
@app.route("/documentation")
|
@app.route("/documentation")
|
||||||
def documentation():
|
def documentation():
|
||||||
|
|
|
@ -1,9 +1,34 @@
|
||||||
from re import M
|
from re import M
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
from datetime import datetime
|
||||||
import click
|
import click
|
||||||
from flask import current_app, g
|
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():
|
def get_db():
|
||||||
if 'db' not in g:
|
if 'db' not in g:
|
||||||
|
|
|
@ -13,9 +13,10 @@ CREATE TABLE IF NOT EXISTS "tags" (
|
||||||
);
|
);
|
||||||
CREATE TABLE IF NOT EXISTS "transaction_log" (
|
CREATE TABLE IF NOT EXISTS "transaction_log" (
|
||||||
"timestamp" INTEGER NOT NULL,
|
"timestamp" INTEGER NOT NULL,
|
||||||
"userid" INTEGER NOT NULL,
|
"type" TEXT NOT NULL,
|
||||||
"type" TEXT NOT NULL,
|
"user_id" INTEGER,
|
||||||
"before" TEXT,
|
"before" TEXT,
|
||||||
"after" TEXT
|
"after" TEXT,
|
||||||
|
"change" INTEGER
|
||||||
);
|
);
|
||||||
COMMIT;
|
COMMIT;
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
<p><a href="/">index page</a>
|
<p><a href="/">index page</a>
|
||||||
| <a href="/list">user and tag list</a>
|
| <a href="/list">user and tag list</a>
|
||||||
| <a href="/documentation">Documentation</a>
|
| <a href="/documentation">Documentation</a>
|
||||||
|
| <a href="/transactionlist">transactionlist</a>
|
||||||
</p>
|
</p>
|
||||||
</nav>
|
</nav>
|
||||||
<hr>
|
<hr>
|
||||||
|
@ -21,4 +22,4 @@
|
||||||
{% block content %} {% endblock %}
|
{% block content %} {% endblock %}
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
Loading…
Reference in a new issue