1
0
Fork 0
forked from bton/matekasse

added removetag and sessionids

This commit is contained in:
2000-Trek 2023-06-09 21:22:25 +02:00
parent aa9eb5068a
commit 7ba11cbbf5

63
main.py
View file

@ -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'<p>{user[1]}: {user[2]} <form action="/addtag" method="get"><input name="id" type="hidden" value="{user[0]}"><button type="submit">Add Tag</button></form><form action="/change" method="get"><input name="id" type="hidden" value="{user[0]}"> Change balance: <input name="change"><input type="submit"></form></p>'
return f'<p>{user[1]}: {user[2]} <p><form action="/addtag" method="get"><input name="id" type="hidden" value="{user[0]}"><button type="submit">Add Tag</button></form><form action="/removetags" method="get"><input name="id" type="hidden" value="{user[0]}"><button type="submit">Remove Tag</button></form></p><form action="/change" method="get"><input name="id" type="hidden" value="{user[0]}"> Change balance: <input name="change"><input type="submit"></form></p>'
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")