forked from bton/matekasse
initial
This commit is contained in:
commit
77b504fe76
5 changed files with 205 additions and 0 deletions
146
main.py
Normal file
146
main.py
Normal file
|
@ -0,0 +1,146 @@
|
|||
import queue
|
||||
from flask import Flask, render_template, request, make_response
|
||||
from flask_socketio import SocketIO #https://pythonprogramminglanguage.com/python-flask-websocket/
|
||||
import sqlite3
|
||||
import time
|
||||
import atexit
|
||||
import sys
|
||||
|
||||
db_path = 'mate.db'
|
||||
conn = sqlite3.connect(db_path)
|
||||
c = conn.cursor()
|
||||
|
||||
app = Flask(__name__)
|
||||
socketio = SocketIO(app)
|
||||
app.debug = True
|
||||
|
||||
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:
|
||||
text = text + f'<p>{i[1]}: {i[2]} <form action="/addtag" method="get"><input name="id" type="hidden" value="{i[0]}"><button type="submit">Add Tag</button></form><form action="/change" method="get"><input name="id" type="hidden" value="{i[0]}"> Change balance: <input name="change"><input type="submit"></form></p><br></br>'
|
||||
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
|
||||
|
||||
@app.route("/list/user", methods=['GET'])
|
||||
def user():
|
||||
username = request.args.get("user")
|
||||
c.execute("SELECT * FROM users WHERE username = '%s'" % username)
|
||||
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>'
|
||||
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")
|
||||
c.execute(f"SELECT * FROM users WHERE username='{str(user)}'")
|
||||
if c.fetchall() == []:
|
||||
c.execute("INSERT or IGNORE INTO users (username, balance) VALUES ('%s', 0)" % user)
|
||||
conn.commit()
|
||||
return "alert('Added userd')"
|
||||
else:
|
||||
return "alert('User already exists')"
|
||||
|
||||
|
||||
@app.route("/change", methods=['GET'])
|
||||
def change():
|
||||
user_id = int(request.args.get("id"))
|
||||
try:
|
||||
change = int(request.args.get("change"))
|
||||
except ValueError:
|
||||
return '<p>Pleas enter a number!</p><a href="/list">tags and user list</a>'
|
||||
if change < 0:
|
||||
text = "removed from"
|
||||
elif change > 0:
|
||||
text = "added to"
|
||||
elif change == 0:
|
||||
return "<p>Nothing was done!</p>"
|
||||
c.execute(f"UPDATE users SET balance = balance + {change} WHERE id={user_id}")
|
||||
c.execute(f"SELECT * FROM users WHERE id={user_id}")
|
||||
user = c.fetchall()[0][1]
|
||||
return f'<p>{change} was {text} {user}'
|
||||
|
||||
@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!"
|
||||
users.put(user_id)
|
||||
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]
|
||||
if user == data["data"]:
|
||||
socketio.emit("wait", "wait")
|
||||
i = 0
|
||||
while finished != data["data"]:
|
||||
time.sleep(1)
|
||||
i += 1
|
||||
if i > 20:
|
||||
socketio.emit("error", "timeout")
|
||||
nothing = users.get()
|
||||
pass
|
||||
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}")
|
||||
else:
|
||||
socketio.emit("busy", "busy")
|
||||
else:
|
||||
socketio.emit("error", "error")
|
||||
|
||||
#api
|
||||
@app.route("/api/tag_id", methods=['GET'])
|
||||
def get_id():
|
||||
global users
|
||||
global finished
|
||||
tag_id = int(request.args.get("id"))
|
||||
c.execute(f"SELECT * FROM tags WHERE tagid ={tag_id}")
|
||||
tag_list = c.fetchall()
|
||||
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:
|
||||
user = users.get()
|
||||
c.execute(f"INSERT OR IGNORE INTO tags (tagid, userid) VALUES ({tag_id}, {user})")
|
||||
conn.commit()
|
||||
finished = user
|
||||
return make_response("True")
|
||||
|
||||
return make_response("False")
|
||||
|
||||
def main():
|
||||
atexit.register(exit_handler)
|
||||
socketio.run(app)
|
||||
main()
|
BIN
mate.db
Normal file
BIN
mate.db
Normal file
Binary file not shown.
26
templates/addtag.html
Normal file
26
templates/addtag.html
Normal file
|
@ -0,0 +1,26 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.js" integrity="sha512-q/dWJ3kcmjBLU4Qc47E4A9kTB4m3wuTY7vkFJDTZKjTs8jhyGQnaUrxa0Ytd0ssMZhbNua9hE+E7Qv1j+DyZwA==" crossorigin="anonymous"></script>
|
||||
<script type="text/javascript" charset="utf-8">
|
||||
var socket = io();
|
||||
var user = {{ user }}
|
||||
socket.on('connect', function() {
|
||||
socket.emit('addtag', {data: user});
|
||||
});
|
||||
socket.on("busy", function(){
|
||||
document.write('<p>the nfc reader is busy at the moment. Pleas Wait ...</p>')
|
||||
socket.emit('addtag', {data: user})
|
||||
});
|
||||
socket.on("wait", function(){
|
||||
document.write('<p>wait</p>')
|
||||
});
|
||||
socket.on("error", function(data) {
|
||||
alert(data)
|
||||
window.location="http://localhost:5000/"
|
||||
});
|
||||
socket.on("finished", function(data){
|
||||
alert(data)
|
||||
window.location="http://localhost:5000/"
|
||||
});
|
||||
</script>
|
||||
</html>
|
7
templates/adduser.html
Normal file
7
templates/adduser.html
Normal file
|
@ -0,0 +1,7 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<p>
|
||||
Username:
|
||||
<form action="/adduser/user" method="get"><input name="username"><input type="submit"></form>
|
||||
</p>
|
||||
</html>
|
26
templates/change.html
Normal file
26
templates/change.html
Normal file
|
@ -0,0 +1,26 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.js" integrity="sha512-q/dWJ3kcmjBLU4Qc47E4A9kTB4m3wuTY7vkFJDTZKjTs8jhyGQnaUrxa0Ytd0ssMZhbNua9hE+E7Qv1j+DyZwA==" crossorigin="anonymous"></script>
|
||||
<script type="text/javascript" charset="utf-8">
|
||||
var socket = io();
|
||||
var change = {{change}}
|
||||
socket.on('connect', function() {
|
||||
socket.emit('addtag', {data: user});
|
||||
});
|
||||
socket.on("besetzt", function(){
|
||||
document.write('<p>besetzt</p>')
|
||||
socket.emit('addtag', {data: user})
|
||||
});
|
||||
socket.on("wait", function(){
|
||||
document.write('<p>wait</p>')
|
||||
});
|
||||
socket.on("error", function(data) {
|
||||
alert(data)
|
||||
window.location="http://localhost:5000/"
|
||||
});
|
||||
socket.on("finished", function(data){
|
||||
alert(data)
|
||||
window.location="http://localhost:5000/"
|
||||
});
|
||||
</script>
|
||||
</html>
|
Loading…
Reference in a new issue