forked from bton/matekasse
398 lines
15 KiB
Python
398 lines
15 KiB
Python
import queue, time, uuid, json, logging, datetime, os
|
|
from flask import Flask, render_template, render_template_string, request, make_response, session, send_file, g
|
|
from flask_socketio import SocketIO, join_room, leave_room
|
|
from flask_session import Session
|
|
from markupsafe import escape
|
|
from Website.db import get_db
|
|
import Website.db as db
|
|
from datetime import datetime
|
|
finished = None
|
|
preis = 150 #Ein Getraenk
|
|
#flask_config
|
|
DATABASE = './Website/mate.db'
|
|
|
|
def create_app(test_config=None):
|
|
app = Flask(__name__)
|
|
key = str(uuid.uuid4().hex)
|
|
if test_config is None:
|
|
app.config['SESSION_TYPE'] = 'filesystem'
|
|
app.config['SECRET_KEY'] = key
|
|
app.config['DATABASE'] = DATABASE
|
|
else:
|
|
app.config.from_mapping(test_config)
|
|
|
|
try:
|
|
os.makedirs(app.instance_path)
|
|
|
|
except OSError:
|
|
pass
|
|
|
|
Session(app)
|
|
socketio = SocketIO(app)
|
|
|
|
#@app.teardown_appcontext
|
|
#def close_connection(exception):
|
|
# db = getattr(g, '_database', None)
|
|
# if db is not None:
|
|
# db.close()
|
|
# app.logger.info("Website exited")
|
|
|
|
#var
|
|
user_queue = queue.Queue()
|
|
|
|
#website
|
|
@app.route('/favicon.ico')
|
|
def favicon():
|
|
return send_file("../static/Logo_CCC.svg.png")
|
|
|
|
@app.route("/")
|
|
def index():
|
|
return render_template("index.html")
|
|
|
|
@app.route("/list")
|
|
def list():
|
|
db = get_db()
|
|
c = db.cursor()
|
|
c.execute("SELECT * FROM users")
|
|
users = c.fetchall()
|
|
return render_template("list.html", users=escape(users), preis=escape(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 1000")
|
|
transactionlist = c.fetchall()
|
|
for i in transactionlist:
|
|
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'])
|
|
def user_info():
|
|
db = get_db()
|
|
c = db.cursor()
|
|
id = request.args.get("id")
|
|
c.execute(f"SELECT * FROM users WHERE id=?", [id])
|
|
user = c.fetchone()
|
|
if user != None :
|
|
c.execute(f"SELECT * FROM tags WHERE userid={user[0]}")
|
|
tags = c.fetchall()
|
|
return render_template("user.html", user=escape(user), tags=escape(tags))
|
|
|
|
else:
|
|
return render_template("error.html", error_code="043")
|
|
|
|
@app.route("/adduser", methods=['POST'])
|
|
def new_user():
|
|
return render_template("adduser.html")
|
|
|
|
@app.route("/removeuser", methods=['POST'])
|
|
def remove_user():
|
|
db = get_db()
|
|
c = db.cursor()
|
|
user_id = request.form["id"]
|
|
c.execute(f"SELECT * FROM users WHERE id=?", [user_id])
|
|
user = c.fetchone()
|
|
if user != None:
|
|
user_name = user[1]
|
|
db.remove_user(user_id)
|
|
socketio.emit("update", "update")
|
|
return render_template("removeuser.html", user_name=escape(user_name))
|
|
else:
|
|
return render_template("error.html", error_code="043")
|
|
|
|
@app.route("/adduser/user", methods=['POST'])
|
|
def adduser():
|
|
db = get_db()
|
|
c = db.cursor()
|
|
username = request.form["username"]
|
|
if username == None:
|
|
return render_template("error.html", error_code="418")
|
|
c.execute("SELECT * FROM users WHERE username=?", [username])
|
|
if c.fetchall() == []:
|
|
db.add_user(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")
|
|
|
|
|
|
@app.route("/change", methods=['POST'])
|
|
def change():
|
|
db = get_db()
|
|
c = db.cursor()
|
|
try:
|
|
user_id = request.form["id"]
|
|
change = int(float(request.form["change"]) * float(100))
|
|
except:
|
|
return render_template("error.html", error_code="095")
|
|
c.execute(f"SELECT * FROM users WHERE id=?", [user_id])
|
|
users = c.fetchall()
|
|
if users != []:
|
|
balance_old = users[0][2]
|
|
db.change_balance(user_id, change)
|
|
socketio.emit("update", "update")
|
|
return render_template("redirect.html")
|
|
else:
|
|
return render_template("error.html", error_code="043")
|
|
|
|
@app.route("/addtag", methods=['POST'])
|
|
def get_addtag_request():
|
|
try:
|
|
user_id = int(request.form["id"])
|
|
except: #except im Normalen Code!
|
|
return render_template("error.html", error_code="095")
|
|
session_id = uuid.uuid4()
|
|
session[id] = session_id
|
|
user_queue.put([user_id, "add", session_id])
|
|
return render_template("addtag.html", user=escape(user_id))
|
|
|
|
@socketio.on('addtag')
|
|
def request_addtag(data):
|
|
global finished
|
|
global message
|
|
join_room(session[id])
|
|
if len(user_queue.queue) > 0:
|
|
user = user_queue.queue[0]
|
|
if user == [data["data"], "add", session[id]]:
|
|
socketio.emit("wait", "wait", to=session[id])
|
|
i = 0
|
|
while finished != [data["data"], "add", session[id]]:
|
|
time.sleep(1)
|
|
i += 1
|
|
if i > 20:
|
|
socketio.emit("error", "352", to=session[id])
|
|
notimportant = user_queue.get()
|
|
break
|
|
else:
|
|
finished = None
|
|
socketio.emit("finished", f"{message}", to=session[id])
|
|
|
|
else:
|
|
socketio.emit("busy", "busy", to=session[id])
|
|
else:
|
|
socketio.emit("error", "418", to=session[id])
|
|
leave_room(session[id])
|
|
|
|
@app.route("/removetag", methods=['POST'])
|
|
def get_removetag_request():
|
|
try:
|
|
user_id = int(request.form["id"])
|
|
except: #except im Normalen Code!
|
|
return render_template("error.html", error_code="043")
|
|
try:
|
|
tag_id = int(request.form["tagid"])
|
|
except:
|
|
session_id = uuid.uuid4()
|
|
session[id] = session_id
|
|
user_queue.put([user_id, "remove", session_id])
|
|
return render_template("removetag.html", user=escape(user_id))
|
|
else:
|
|
db = get_db()
|
|
c = db.cursor()
|
|
c.execute(f"SELECT * FROM tags WHERE (tagid = ? AND userid = ?)", [tag_id, user_id])
|
|
if c.fetchall != []:
|
|
db.remove_tag(tag_id)
|
|
message = f"Removed {tag_id} from user {user_id}"
|
|
return render_template("redirect.html")
|
|
else:
|
|
return render_template("error.html", error_code="054")
|
|
|
|
@socketio.on('removetag')
|
|
def request_removetag(data):
|
|
global finished
|
|
global message
|
|
join_room(session[id])
|
|
if len(user_queue.queue) > 0:
|
|
queue_item = user_queue.queue[0]
|
|
user = queue_item[0]
|
|
if queue_item == [data["data"], "remove", session[id]]:
|
|
socketio.emit("wait", "wait", to=session[id])
|
|
i = 0
|
|
while finished != [data["data"], "remove", session[id]]:
|
|
time.sleep(1)
|
|
i += 1
|
|
if i > 20:
|
|
socketio.emit("error", "352", to=session[id])
|
|
notimportant = user_queue.get()
|
|
break
|
|
else:
|
|
finished = None
|
|
socketio.emit("finished", f"{message}", to=session[id])
|
|
else:
|
|
socketio.emit("busy", "busy", to=session[id])
|
|
else:
|
|
socketio.emit("error", "418", to=session[id])
|
|
leave_room(session[id])
|
|
|
|
@app.route("/api/balance", methods=['POST', 'GET'])
|
|
def api_change():
|
|
if request.method == 'POST':
|
|
db = get_db()
|
|
c = db.cursor()
|
|
userid = request.form["id"]
|
|
c.execute("SELECT * FROM users WHERE id=?", [userid])
|
|
user_list = c.fetchall()
|
|
if user_list != []:
|
|
user = user_list[0]
|
|
try:
|
|
change = int(request.args.get("change"))
|
|
except:
|
|
change = preis
|
|
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:
|
|
return make_response(json.dumps({"mode":"error","error":"043"}))
|
|
|
|
elif request.method == 'GET':
|
|
db = get_db()
|
|
c = db.cursor()
|
|
userid = request.args.get("id")
|
|
c.execute("SELECT * FROM users WHERE id=?", [userid])
|
|
user = c.fetchone()
|
|
if user != None:
|
|
return make_response(json.dumps({"mode":"balance", "username":user[1], "balance":user[2]}))
|
|
else:
|
|
return make_response(json.dumps({"mode":"error", "error":"043"}))
|
|
|
|
|
|
|
|
@app.route("/api/tag_id", methods=['POST'])
|
|
def get_id():
|
|
db = get_db()
|
|
c = db.cursor()
|
|
global finished
|
|
global message
|
|
try:
|
|
tag_id = request.form["id"]
|
|
except:
|
|
return make_response(json.dumps({"mode":"error", "error":"638"}))
|
|
c.execute(f"SELECT * FROM tags WHERE tagid=?", [tag_id])
|
|
|
|
tag_list = c.fetchall()
|
|
if user_queue.qsize() > 0:
|
|
queue_item = user_queue.get()
|
|
user = queue_item[0]
|
|
|
|
state = queue_item[1]
|
|
c.execute(f"SELECT * FROM users WHERE id=?", [user])
|
|
user = c.fetchone()
|
|
user_id = user[0]
|
|
username = user[1]
|
|
if state == "add":
|
|
c.execute(f"SELECT * FROM tags WHERE tagid={tag_id}")
|
|
if c.fetchall() != []:
|
|
message = "Error: 170"
|
|
finished = queue_item
|
|
return make_response(json.dumps({"mode":"error","error":"170"}))
|
|
else:
|
|
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"}))
|
|
elif state == "remove":
|
|
c.execute(f"SELECT * FROM tags WHERE (tagid = {tag_id} AND userid = ?)", [user_id])
|
|
tags = c.fetchall()
|
|
if tags != []:
|
|
db.commit()
|
|
message = f"Removed {tag_id} from {username}"
|
|
finished = queue_item
|
|
return make_response(json.dumps({"mode":"message","username":"{}".format(username),"message":"A tag was removed"}))
|
|
else:
|
|
message = "054"
|
|
finished = queue_item
|
|
return make_response(json.dumps({"mode":"error","error":"054"}))
|
|
finished = queue_item
|
|
socketio.emit("update", "update")
|
|
return make_response(json.dumps({"mode":"error","error":"418"}))
|
|
|
|
elif tag_list != []:
|
|
tag = tag_list[0]
|
|
c.execute(f"SELECT * FROM users WHERE id={tag[1]}")
|
|
user_list = c.fetchall()
|
|
if user_list != []:
|
|
balance_old = user_list[0][2]
|
|
if user_queue.qsize() == 0:
|
|
db.change_balance(tag[1], preis)
|
|
c.execute(f"SELECT * FROM users WHERE id={tag[1]}")
|
|
user = c.fetchone()
|
|
socketio.emit("update", "update")
|
|
return make_response(json.dumps({"mode":"balance", "username":user[1], "balance":user[2]/100}))
|
|
else:
|
|
return make_response(json.dumps({"mode":"error", "error":"043"}))
|
|
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"]
|
|
|
|
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")
|
|
|
|
#Documentation
|
|
@app.route("/documentation")
|
|
def documentation():
|
|
return render_template("documentation.html")
|
|
|
|
|
|
return {"app":app,"socketio":socketio}
|