matekasse/Website/__init__.py

407 lines
15 KiB
Python
Raw Normal View History

2023-07-28 22:09:42 +00:00
import queue, time, uuid, json, logging, datetime, os
2024-02-17 01:12:27 +00:00
from flask import Flask, render_template, render_template_string, request, make_response, session, send_file, g
2023-07-28 21:30:45 +00:00
from flask_socketio import SocketIO, join_room, leave_room
from flask_session import Session
2024-03-06 19:41:45 +00:00
from Website.db import get_db
2024-03-13 18:26:21 +00:00
import Website.db as db_handler
2023-11-24 23:05:33 +00:00
from datetime import datetime
2023-11-25 15:59:51 +00:00
finished = None
2024-02-25 16:24:44 +00:00
preis = 150 #Ein Getraenk
2023-07-28 21:30:45 +00:00
#flask_config
DATABASE = './Website/mate.db'
2023-07-28 21:30:45 +00:00
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)
2024-02-25 16:24:44 +00:00
2023-07-28 21:30:45 +00:00
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")
2023-07-28 22:09:42 +00:00
2023-07-28 21:30:45 +00:00
#var
2023-08-16 19:36:23 +00:00
user_queue = queue.Queue()
2023-07-28 21:30:45 +00:00
#website
@app.route('/favicon.ico')
2024-02-22 17:31:19 +00:00
@app.route('/ccc_logo.png')
2023-07-28 21:30:45 +00:00
def favicon():
2023-08-16 21:02:36 +00:00
return send_file("../static/Logo_CCC.svg.png")
2023-07-28 21:30:45 +00:00
2024-02-22 17:31:19 +00:00
@app.route('/socket.io.js')
def socketiojs():
return send_file('../static/socket.io.js')
@app.route('/new.css')
def newcss():
return send_file('../static/new.min.css')
2023-07-28 21:30:45 +00:00
@app.route("/")
def index():
2024-02-17 01:12:27 +00:00
return render_template("index.html")
2023-07-28 21:30:45 +00:00
@app.route("/list")
def list():
db = get_db()
c = db.cursor()
c.execute("SELECT * FROM users")
users = c.fetchall()
2024-03-06 20:54:57 +00:00
return render_template("list.html", users=users, preis=(preis/100))
2023-07-28 21:30:45 +00:00
2023-11-24 23:05:33 +00:00
@app.route("/transactionlist")
def transactionlist():
db = get_db()
c = db.cursor()
text = ""
2024-02-25 16:24:44 +00:00
c.execute("SELECT * FROM transaction_log ORDER BY ROWID DESC LIMIT 1000")
2023-11-24 23:05:33 +00:00
transactionlist = c.fetchall()
for i in transactionlist:
2024-02-25 16:24:44 +00:00
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)
2024-03-13 19:50:34 +00:00
elif statement == "add_user":
reverse_statement = "remove_user"
2024-02-25 16:24:44 +00:00
reverse_user_id = user_id
reverse_before = after
reverse_after = None
reverse_change = None
2024-03-13 19:50:34 +00:00
elif statement == "remove_user":
reverse_statement = "add_user"
2024-02-25 16:24:44 +00:00
reverse_user_id = user_id
reverse_before = None
reverse_after = before
reverse_change = None
2024-03-13 19:50:34 +00:00
elif statement == "add_tag":
reverse_statement = "remove_tag"
2024-02-25 16:24:44 +00:00
reverse_user_id = user_id
reverse_before = after
reverse_after = None
reverse_change = None
2024-03-13 19:50:34 +00:00
elif statement == "remove_tag":
reverse_statement = "add_tag"
2024-02-25 16:24:44 +00:00
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>'
2023-11-24 23:05:33 +00:00
return text
2023-07-28 21:30:45 +00:00
@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])
2024-02-17 01:12:27 +00:00
user = c.fetchone()
if user != None :
2023-07-28 21:30:45 +00:00
c.execute(f"SELECT * FROM tags WHERE userid={user[0]}")
tags = c.fetchall()
2024-03-06 20:27:12 +00:00
return render_template("user.html", user=user, tags=tags)
2024-02-17 01:12:27 +00:00
2023-07-28 21:30:45 +00:00
else:
2023-09-19 20:23:55 +00:00
return render_template("error.html", error_code="043")
2024-02-17 01:12:27 +00:00
2023-11-24 17:26:54 +00:00
@app.route("/adduser", methods=['POST'])
2023-07-28 21:30:45 +00:00
def new_user():
2024-02-17 01:12:27 +00:00
return render_template("adduser.html")
2023-07-28 21:30:45 +00:00
2023-11-24 17:26:54 +00:00
@app.route("/removeuser", methods=['POST'])
2023-07-28 21:30:45 +00:00
def remove_user():
db = get_db()
c = db.cursor()
2023-11-24 17:26:54 +00:00
user_id = request.form["id"]
2023-07-28 21:30:45 +00:00
c.execute(f"SELECT * FROM users WHERE id=?", [user_id])
2024-02-25 16:24:44 +00:00
user = c.fetchone()
if user != None:
user_name = user[1]
2024-03-13 18:26:21 +00:00
db_handler.remove_user(user_id)
2023-07-28 21:30:45 +00:00
socketio.emit("update", "update")
2024-03-06 20:27:12 +00:00
return render_template("removeuser.html", user_name=user_name)
2023-07-28 21:30:45 +00:00
else:
2023-09-19 20:23:55 +00:00
return render_template("error.html", error_code="043")
2023-07-28 21:30:45 +00:00
2023-11-24 17:26:54 +00:00
@app.route("/adduser/user", methods=['POST'])
2023-07-28 21:30:45 +00:00
def adduser():
db = get_db()
c = db.cursor()
2023-11-24 17:26:54 +00:00
username = request.form["username"]
2023-07-28 21:30:45 +00:00
if username == None:
2023-09-19 20:23:55 +00:00
return render_template("error.html", error_code="418")
2023-07-28 21:30:45 +00:00
c.execute("SELECT * FROM users WHERE username=?", [username])
if c.fetchall() == []:
2024-03-13 18:26:21 +00:00
db_handler.add_user(username)
2023-07-28 21:30:45 +00:00
socketio.emit("update", "update")
c.execute(f"SELECT * FROM users WHERE username=?", [username])
user = c.fetchone()
2024-02-17 01:12:27 +00:00
return render_template("redirect.html")
2023-07-28 21:30:45 +00:00
else:
2023-09-19 20:23:55 +00:00
return render_template("error.html", error_code="757")
2023-07-28 21:30:45 +00:00
2023-11-24 17:26:54 +00:00
@app.route("/change", methods=['POST'])
2023-07-28 21:30:45 +00:00
def change():
db = get_db()
c = db.cursor()
try:
2023-11-24 17:26:54 +00:00
user_id = request.form["id"]
2024-02-23 23:14:43 +00:00
change = int(float(request.form["change"]) * float(100))
2023-07-28 21:30:45 +00:00
except:
2023-09-19 20:23:55 +00:00
return render_template("error.html", error_code="095")
2023-07-28 21:30:45 +00:00
c.execute(f"SELECT * FROM users WHERE id=?", [user_id])
users = c.fetchall()
if users != []:
balance_old = users[0][2]
2024-03-13 18:26:21 +00:00
db_handler.change_balance(user_id, change)
2023-07-28 21:30:45 +00:00
socketio.emit("update", "update")
2024-02-17 01:12:27 +00:00
return render_template("redirect.html")
2023-07-28 21:30:45 +00:00
else:
2023-09-19 20:23:55 +00:00
return render_template("error.html", error_code="043")
2023-07-28 21:30:45 +00:00
2023-11-24 17:26:54 +00:00
@app.route("/addtag", methods=['POST'])
2023-08-16 19:36:23 +00:00
def get_addtag_request():
2023-07-28 21:30:45 +00:00
try:
2023-11-24 17:26:54 +00:00
user_id = int(request.form["id"])
2023-07-28 21:30:45 +00:00
except: #except im Normalen Code!
2023-09-19 20:23:55 +00:00
return render_template("error.html", error_code="095")
2023-07-28 21:30:45 +00:00
session_id = uuid.uuid4()
session[id] = session_id
2023-08-16 19:36:23 +00:00
user_queue.put([user_id, "add", session_id])
2024-03-13 20:10:34 +00:00
return render_template("addtag.html", user=user_id)
2023-07-28 21:30:45 +00:00
@socketio.on('addtag')
def request_addtag(data):
global finished
global message
join_room(session[id])
2023-08-16 19:36:23 +00:00
if len(user_queue.queue) > 0:
2023-11-24 17:37:10 +00:00
user = user_queue.queue[0]
2023-07-28 21:30:45 +00:00
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])
2023-08-16 19:36:23 +00:00
notimportant = user_queue.get()
2023-07-28 21:30:45 +00:00
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])
2023-11-24 17:26:54 +00:00
@app.route("/removetag", methods=['POST'])
2023-08-16 19:36:23 +00:00
def get_removetag_request():
2023-07-28 21:30:45 +00:00
try:
2023-11-24 17:26:54 +00:00
user_id = int(request.form["id"])
2023-07-28 21:30:45 +00:00
except: #except im Normalen Code!
2023-09-19 20:23:55 +00:00
return render_template("error.html", error_code="043")
2023-11-25 18:28:05 +00:00
try:
tag_id = int(request.form["tagid"])
except:
session_id = uuid.uuid4()
session[id] = session_id
user_queue.put([user_id, "remove", session_id])
2024-03-06 20:27:12 +00:00
return render_template("removetag.html", user=user_id)
2023-11-25 18:28:05 +00:00
else:
db = get_db()
c = db.cursor()
c.execute(f"SELECT * FROM tags WHERE (tagid = ? AND userid = ?)", [tag_id, user_id])
if c.fetchall != []:
2024-03-13 18:26:21 +00:00
db_handler.remove_tag(tag_id)
2023-11-25 18:28:05 +00:00
message = f"Removed {tag_id} from user {user_id}"
2024-02-17 01:12:27 +00:00
return render_template("redirect.html")
2023-11-25 18:28:05 +00:00
else:
return render_template("error.html", error_code="054")
2023-07-28 21:30:45 +00:00
@socketio.on('removetag')
def request_removetag(data):
global finished
global message
join_room(session[id])
2023-08-16 19:36:23 +00:00
if len(user_queue.queue) > 0:
2023-11-25 19:11:15 +00:00
queue_item = user_queue.queue[0]
2023-07-28 21:30:45 +00:00
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])
2023-08-16 19:36:23 +00:00
notimportant = user_queue.get()
2023-07-28 21:30:45 +00:00
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])
2024-02-20 16:33:56 +00:00
@app.route("/api/balance", methods=['POST', 'GET'])
2023-07-28 21:30:45 +00:00
def api_change():
2024-02-20 16:33:56 +00:00
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:
2024-03-01 20:59:30 +00:00
change = preis
2024-03-13 18:26:21 +00:00
db_handler.change_balance(user_id, change)
2024-02-20 16:33:56 +00:00
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()
2024-02-20 17:05:43 +00:00
userid = request.args.get("id")
2024-02-20 16:33:56 +00:00
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"}))
2023-07-28 21:30:45 +00:00
2024-02-16 21:51:32 +00:00
@app.route("/api/tag_id", methods=['POST'])
2023-07-28 21:30:45 +00:00
def get_id():
2024-02-25 16:53:25 +00:00
db = get_db()
2024-02-25 16:54:51 +00:00
c = db.cursor()
2023-07-28 21:30:45 +00:00
global finished
global message
2023-11-25 17:42:53 +00:00
try:
tag_id = request.form["id"]
except:
2024-02-16 20:19:11 +00:00
return make_response(json.dumps({"mode":"error", "error":"638"}))
2023-07-28 21:30:45 +00:00
c.execute(f"SELECT * FROM tags WHERE tagid=?", [tag_id])
tag_list = c.fetchall()
2023-08-16 19:36:23 +00:00
if user_queue.qsize() > 0:
queue_item = user_queue.get()
2023-07-28 21:30:45 +00:00
user = queue_item[0]
state = queue_item[1]
c.execute(f"SELECT * FROM users WHERE id=?", [user])
2023-11-25 15:59:51 +00:00
user = c.fetchone()
2023-11-24 23:14:51 +00:00
user_id = user[0]
username = user[1]
2023-07-28 21:30:45 +00:00
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:
2024-03-13 18:26:21 +00:00
db_handler.add_tag(user_id, tag_id)
2023-11-25 15:59:51 +00:00
message = f"Added {tag_id} to {username}"
2023-07-28 21:30:45 +00:00
finished = queue_item
2023-11-08 20:40:41 +00:00
return make_response(json.dumps({"mode":"message","username":"{}".format(username),"message":"A tag was added"}))
2023-07-28 21:30:45 +00:00
elif state == "remove":
2023-11-25 15:59:51 +00:00
c.execute(f"SELECT * FROM tags WHERE (tagid = {tag_id} AND userid = ?)", [user_id])
2023-07-28 21:30:45 +00:00
tags = c.fetchall()
if tags != []:
2023-11-24 23:13:17 +00:00
db.commit()
2023-11-25 15:59:51 +00:00
message = f"Removed {tag_id} from {username}"
2023-07-28 21:30:45 +00:00
finished = queue_item
2023-11-08 20:40:41 +00:00
return make_response(json.dumps({"mode":"message","username":"{}".format(username),"message":"A tag was removed"}))
2023-07-28 21:30:45 +00:00
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]
2023-08-16 19:36:23 +00:00
if user_queue.qsize() == 0:
2024-03-13 18:26:21 +00:00
db_handler.change_balance(tag[1], preis)
2023-07-28 21:30:45 +00:00
c.execute(f"SELECT * FROM users WHERE id={tag[1]}")
user = c.fetchone()
socketio.emit("update", "update")
2023-11-25 17:26:47 +00:00
return make_response(json.dumps({"mode":"balance", "username":user[1], "balance":user[2]/100}))
2023-07-28 21:30:45 +00:00
else:
return make_response(json.dumps({"mode":"error", "error":"043"}))
socketio.emit("update", "update")
return make_response(json.dumps({"mode":"error","error":"054"}))
2024-02-25 16:24:44 +00:00
@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"]
2024-03-01 20:59:30 +00:00
2024-03-13 19:50:34 +00:00
if statement == "add_user":
2024-03-13 18:26:21 +00:00
db_handler.add_user(after)
2024-03-13 19:50:34 +00:00
elif statement == "remove_user":
2024-03-13 18:26:21 +00:00
db_handler.remove_user(user_id)
2024-03-13 19:50:34 +00:00
elif statement == "add_tag":
2024-03-13 18:26:21 +00:00
db_handler.add_tag(user_id, after)
2024-03-13 19:50:34 +00:00
elif statement == "remove_tag":
2024-03-13 18:26:21 +00:00
db_handler.remove_tag(befor)
2024-03-01 20:59:30 +00:00
elif statement == "balance":
2024-03-13 18:26:21 +00:00
db_handler.change_balance(user_id, change)
2024-03-01 20:59:30 +00:00
else:
return make_response(json.dumps({"mode":"error", "error":"418"})) #Error code
2024-02-25 16:24:44 +00:00
socketio.emit("update", "update")
return render_template("index.html")
2023-07-28 21:30:45 +00:00
#Documentation
@app.route("/documentation")
def documentation():
return render_template("documentation.html")
2024-03-01 20:59:30 +00:00
2024-02-14 19:52:50 +00:00
return {"app":app,"socketio":socketio}