import queue from flask import Flask, render_template, request, make_response, session from flask_socketio import SocketIO, join_room, leave_room from flask_session import Session import sqlite3 import time import atexit import sys import uuid import json db_path = 'mate.db' conn = sqlite3.connect(db_path, check_same_thread=False) c = conn.cursor() app = Flask(__name__) key = str(uuid.uuid4().hex) app.config['SESSION_TYPE'] = 'filesystem' app.config['SECRET_KEY'] = key Session(app) socketio = SocketIO(app) status = True users = queue.Queue() finished = None message = None def exit_handler(): conn.close() sys.exit("Program sucsesfully exited") #website @app.route("/") def index(): return 'user and tag list
The creator of this website accepts no liability for any linguistic or technical errors!
{i[1]}: {i[2]}
{tag[0]}
" return f"""{user[1]} : {user[2]}
Tags:
{text}Deleted user {user_name}
return to the tags and user list' @app.route("/adduser/user", methods=['GET']) def adduser(): user = request.args.get("username") c.execute("SELECT * FROM users WHERE username=?", [user]) if c.fetchall() == []: c.execute("INSERT or IGNORE INTO users (username, balance) VALUES (?, 0)", [user]) conn.commit() socketio.emit("update", "update") return 'Added user user and tag listThe creator of this website accepts no liability for any linguistic or technical errors!
' else: return 'Error: 170
user and tag list' @app.route("/change", methods=['GET']) def change(): user_id = int(request.args.get("id")) try: change = int(request.args.get("change")) except ValueError: return 'Error: 095
tags and user list' c.execute(f"UPDATE users SET balance = balance + {change} WHERE id={user_id}") socketio.emit("update", "update") return """ """ #socketio.emit("update", "update") #conn.commit() #if change < 0: # text = "removed from" # change_text = change * -1 #elif change > 0: # text = "added to" # change_text = change #elif change == 0: # return "Nothing was done!
" #c.execute(f"SELECT * FROM users WHERE id={user_id}") #user = c.fetchall()[0][1] #return f'{change_text} was {text} {user}
back to the list' @app.route("/addtag", methods=['GET']) def get_addtag_request(): global users try: user_id = int(request.args.get("id")) except: #except im Normalen Code! return "Error: 095" session_id = uuid.uuid4() session[id] = session_id users.put([user_id, "add", session_id]) return render_template("addtag.html", user=user_id) @socketio.on('addtag') def request_addtag(data): global finished global message join_room(session[id]) if len(users.queue) > 0: user = users.queue[len(users.queue) - 1] 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 = users.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=['GET']) def get_removetag_request(): global users try: user_id = int(request.args.get("id")) except: #except im Normalen Code! return "Wrong user id!" session_id = uuid.uuid4() session[id] = session_id users.put([user_id, "remove", session_id]) return render_template("removetag.html", user=user_id) @socketio.on('removetag') def request_removetag(data): global finished global message join_room(session[id]) if len(users.queue) > 0: queue_item = users.queue[len(users.queue) - 1] 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 = users.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]) #api @app.route("/api/tag_id", methods=['GET']) def get_id(): global finished global message tag_id = int(request.args.get("id")) c.execute(f"SELECT * FROM tags WHERE tagid ={tag_id}") tag_list = c.fetchall() if users.qsize() > 0: queue_item = users.get() user = queue_item[0] state = queue_item[1] c.execute(f"SELECT * FROM users WHERE id={user}") username = c.fetchall()[0][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":"0","error":"170"})) else: c.execute(f"INSERT OR IGNORE INTO tags (tagid, userid) VALUES ({tag_id}, {user})") message = f"Added {tag_id} to {username}" finished = queue_item return make_response(json.dumps({"mode":"2","username":username,"code":"1"})) elif state == "remove": c.execute(f"SELECT * FROM tags WHERE (tagid = {tag_id} AND userid = {user})") tags = c.fetchall() if tags != []: c.execute(f"DELETE FROM tags WHERE (tagid = {tag_id} AND userid = {user}) ") message = f"Removed {tag_id} from {username}" finished = queue_item return make_response(json.dumps({"mode":"2","username":username,"code":"2"})) else: message = "Tag does not exist" finished = queue_item return make_response(json.dumps({"mode":"0","error":"054"})) finished = queue_item socketio.emit("update", "update") return make_response(json.dumps({"mode":"0","error":"418"})) elif tag_list != []: tag = tag_list[0] if users.qsize() == 0: c.execute(f"UPDATE users SET balance = balance - 1 WHERE id={tag[1]}") conn.commit() c.execute(f"SELECT * FROM users WHERE id={tag[1]}") user = c.fetchall()[0] socketio.emit("update", "upfinished = queue_itemdate") return make_response(json.dumps({"mode":"1", "username":user[1], "balance":user[2]})) socketio.emit("update", "update") return make_response(json.dumps({"mode":"0","error":"054"})) #Documentation @app.route("/documentation") def documentation(): return render_template("documentation.html") def main(): atexit.register(exit_handler) socketio.run(app, host='0.0.0.0', port=5000)