From 7ba11cbbf5ddca2d19ad4e64f75085e0f641df10 Mon Sep 17 00:00:00 2001 From: 2000-Trek Date: Fri, 9 Jun 2023 21:22:25 +0200 Subject: [PATCH] added removetag and sessionids --- main.py | 63 +++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 55 insertions(+), 8 deletions(-) diff --git a/main.py b/main.py index 7b85f72..7848c3c 100644 --- a/main.py +++ b/main.py @@ -1,10 +1,11 @@ import queue -from flask import Flask, render_template, request, make_response, flash +from flask import Flask, render_template, request, make_response, session from flask_socketio import SocketIO #https://pythonprogramminglanguage.com/python-flask-websocket/ import sqlite3 import time import atexit import sys +import uuid db_path = 'mate.db' conn = sqlite3.connect(db_path, check_same_thread=False) @@ -43,7 +44,7 @@ def user_info(): user = c.fetchall() if user != []: user = user[0] - return f'

{user[1]}: {user[2]}

Change balance:

' + return f'

{user[1]}: {user[2]}

Change balance:

' else: return "User does not exists" @@ -90,7 +91,9 @@ def get_addtag_request(): user_id = int(request.args.get("id")) except: #except im Normalen Code! return "Wrong user id!" - users.put(user_id) + 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') @@ -98,10 +101,10 @@ def request_addtag(data): global finished if len(users.queue) > 0: user = users.queue[len(users.queue) - 1] - if user == data["data"]: + if user == [data["data"], "add", session[id]]: socketio.emit("wait", "wait") i = 0 - while finished != data["data"]: + while finished != [data["data"], "add", session[id]]: time.sleep(1) i += 1 if i > 20: @@ -120,6 +123,45 @@ def request_addtag(data): else: socketio.emit("error", "error") +@app.route("/removetag", methods=['GET']) +def get_addtag_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"]) + return render_template("removetag.html", user=user_id) + +@socketio.on('removetag') +def request_addtag(data): + global finished + if len(users.queue) > 0: + user = users.queue[len(users.queue) - 1] + if user == [data["data"], "remove", session[id]]: + socketio.emit("wait", "wait") + i = 0 + while finished != [data["data"], "remove", session[id]]: + time.sleep(1) + i += 1 + if i > 20: + socketio.emit("error", "timeout") + notimportant = users.get() + break + else: + finished = None + c.execute(f"SELECT * FROM users WHERE id ={user}") + username = c.fetchall()[0][1] + c.execute(f"SELECT * FROM tags WHERE userid={user}") + tag_id = c.fetchall()[0][0] + socketio.emit("finished", f"removed {tag_id} from {username}") + else: + socketio.emit("busy", "busy") + else: + socketio.emit("error", "error") + #api @app.route("/api/tag_id", methods=['GET']) def get_id(): @@ -136,10 +178,15 @@ def get_id(): return make_response("True") elif users.qsize() > 0: - user = users.get() - c.execute(f"INSERT OR IGNORE INTO tags (tagid, userid) VALUES ({tag_id}, {user})") + queue_item = users.get() + user = queue_item[0] + state = queue_item[1] + if state == "add": + c.execute(f"INSERT OR IGNORE INTO tags (tagid, userid) VALUES ({tag_id}, {user})") + elif state == "remove": + c.execute(f"DELETE FROM tags WHERE (tagid = {tag_id} AND userid = {user}) ") conn.commit() - finished = user + finished = queue_item return make_response("True") return make_response("False")