2023-05-31 18:33:45 +00:00
import queue
2023-06-09 19:22:25 +00:00
from flask import Flask , render_template , request , make_response , session
2023-05-31 18:33:45 +00:00
from flask_socketio import SocketIO #https://pythonprogramminglanguage.com/python-flask-websocket/
import sqlite3
import time
import atexit
import sys
2023-06-09 19:22:25 +00:00
import uuid
2023-05-31 18:33:45 +00:00
db_path = ' mate.db '
2023-05-31 19:23:40 +00:00
conn = sqlite3 . connect ( db_path , check_same_thread = False )
2023-05-31 18:33:45 +00:00
c = conn . cursor ( )
app = Flask ( __name__ )
2023-06-09 19:38:20 +00:00
app . secret_key = str ( uuid . uuid4 ( ) )
2023-06-09 19:47:15 +00:00
socketio = SocketIO ( app )
2023-05-31 18:33:45 +00:00
status = True
users = queue . Queue ( )
finished = None
def exit_handler ( ) :
conn . close ( )
sys . exit ( " Program sucsesfully exited " )
#website
@app.route ( " / " )
def index ( ) :
return ' <a href= " /list " >user and tag list</a> <p>The creator of this website accepts no liability for any linguistic or technical errors!</p> '
@app.route ( " /list " )
def list ( ) :
c . execute ( " SELECT * FROM users " )
users = c . fetchall ( )
text = " "
for i in users :
2023-06-09 18:11:51 +00:00
text = text + f ' <p><a href= " list/user?user= { i [ 1 ] } " > { i [ 1 ] } </a>: { i [ 2 ] } <form action= " /change " method= " get " ><input name= " id " type= " hidden " value= " { i [ 0 ] } " > Change balance: <input name= " change " ><input type= " submit " ></form></p> <br style= " line-height: 50%; " ></br> '
2023-06-09 17:59:34 +00:00
return ' <form action= " /list/user " method= " get " > Search for User: <input name= " user " ><input type= " submit " ></form> <form action= " /adduser " method= " get " ><button type= " submit " >Add User</button></form> <br></br> ' + text
2023-05-31 18:33:45 +00:00
@app.route ( " /list/user " , methods = [ ' GET ' ] )
2023-05-31 20:29:47 +00:00
def user_info ( ) :
2023-05-31 18:33:45 +00:00
username = request . args . get ( " user " )
c . execute ( " SELECT * FROM users WHERE username = ' %s ' " % username )
user = c . fetchall ( )
if user != [ ] :
user = user [ 0 ]
2023-06-09 19:35:12 +00:00
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= " /removetag " 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> '
2023-05-31 18:33:45 +00:00
else :
return " User does not exists "
@app.route ( " /adduser " )
def new_user ( ) :
return render_template ( " adduser.html " )
@app.route ( " /adduser/user " , methods = [ ' GET ' ] )
def adduser ( ) :
user = request . args . get ( " username " )
2023-05-31 19:23:40 +00:00
c . execute ( f " SELECT * FROM users WHERE username= ' { str ( user ) } ' " )
2023-05-31 18:33:45 +00:00
if c . fetchall ( ) == [ ] :
c . execute ( " INSERT or IGNORE INTO users (username, balance) VALUES ( ' %s ' , 0) " % user )
conn . commit ( )
2023-05-31 19:23:40 +00:00
return ' Added user <a href= " /list " >user and tag list</a> <p>The creator of this website accepts no liability for any linguistic or technical errors!</p> '
2023-05-31 18:33:45 +00:00
else :
2023-05-31 19:23:40 +00:00
return ' User already exists <a href= " /list " >user and tag list</a> <p>The creator of this website accepts no liability for any linguistic or technical errors!</p> '
2023-05-31 18:33:45 +00:00
@app.route ( " /change " , methods = [ ' GET ' ] )
def change ( ) :
user_id = int ( request . args . get ( " id " ) )
try :
change = int ( request . args . get ( " change " ) )
except ValueError :
2023-06-09 17:53:27 +00:00
return ' <p>Please enter a number!</p><a href= " /list " >tags and user list</a> '
2023-06-09 18:17:30 +00:00
c . execute ( f " UPDATE users SET balance = balance + { change } WHERE id= { user_id } " )
conn . commit ( )
2023-05-31 18:33:45 +00:00
if change < 0 :
text = " removed from "
2023-06-09 18:17:30 +00:00
change_text = change * - 1
2023-05-31 18:33:45 +00:00
elif change > 0 :
text = " added to "
elif change == 0 :
return " <p>Nothing was done!</p> "
c . execute ( f " SELECT * FROM users WHERE id= { user_id } " )
user = c . fetchall ( ) [ 0 ] [ 1 ]
2023-06-09 18:17:30 +00:00
return f ' <p> { change_text } was { text } { user } </p> <a href= " /list " >back to the list</a> '
2023-05-31 18:33:45 +00:00
@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 " Wrong user id! "
2023-06-09 19:22:25 +00:00
session_id = uuid . uuid4 ( )
2023-06-09 20:23:01 +00:00
session [ id ] = session_id
2023-06-09 19:22:25 +00:00
users . put ( [ user_id , " add " , session_id ] )
2023-05-31 18:33:45 +00:00
return render_template ( " addtag.html " , user = user_id )
@socketio.on ( ' addtag ' )
def request_addtag ( data ) :
global finished
if len ( users . queue ) > 0 :
user = users . queue [ len ( users . queue ) - 1 ]
2023-06-09 19:22:25 +00:00
if user == [ data [ " data " ] , " add " , session [ id ] ] :
2023-05-31 18:33:45 +00:00
socketio . emit ( " wait " , " wait " )
i = 0
2023-06-09 19:22:25 +00:00
while finished != [ data [ " data " ] , " add " , session [ id ] ] :
2023-05-31 18:33:45 +00:00
time . sleep ( 1 )
i + = 1
if i > 20 :
socketio . emit ( " error " , " timeout " )
2023-06-09 17:11:45 +00:00
notimportant = users . get ( )
2023-05-31 20:29:47 +00:00
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 " Added { tag_id } to { username } " )
2023-05-31 18:33:45 +00:00
else :
socketio . emit ( " busy " , " busy " )
else :
socketio . emit ( " error " , " error " )
2023-06-09 19:22:25 +00:00
@app.route ( " /removetag " , methods = [ ' GET ' ] )
2023-06-09 19:26:17 +00:00
def get_removetag_request ( ) :
2023-06-09 19:22:25 +00:00
global users
try :
user_id = int ( request . args . get ( " id " ) )
except : #except im Normalen Code!
return " Wrong user id! "
session_id = uuid . uuid4 ( )
2023-06-09 20:23:01 +00:00
session [ id ] = session_id
users . put ( [ user_id , " remove " ] , session_id )
2023-06-09 19:22:25 +00:00
return render_template ( " removetag.html " , user = user_id )
@socketio.on ( ' removetag ' )
2023-06-09 19:26:17 +00:00
def request_removetag ( data ) :
2023-06-09 19:22:25 +00:00
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 " )
2023-05-31 18:33:45 +00:00
#api
@app.route ( " /api/tag_id " , methods = [ ' GET ' ] )
def get_id ( ) :
global finished
tag_id = int ( request . args . get ( " id " ) )
c . execute ( f " SELECT * FROM tags WHERE tagid = { tag_id } " )
tag_list = c . fetchall ( )
2023-06-09 16:52:38 +00:00
print ( tag_list )
2023-05-31 18:33:45 +00:00
if 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 ( )
return make_response ( " True " )
elif users . qsize ( ) > 0 :
2023-06-09 19:22:25 +00:00
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 } ) " )
2023-05-31 18:33:45 +00:00
conn . commit ( )
2023-06-09 19:22:25 +00:00
finished = queue_item
2023-05-31 18:33:45 +00:00
return make_response ( " True " )
return make_response ( " False " )
def main ( ) :
atexit . register ( exit_handler )
2023-05-31 19:23:40 +00:00
socketio . run ( app , host = ' 0.0.0.0 ' , port = 5000 )