1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
Kfet-depreciated/app/sockets.py

262 lines
8 KiB
Python
Raw Normal View History

2020-02-01 17:44:42 +01:00
import datetime
import functools
from flask_login import current_user
from flask_socketio import emit, disconnect
from app import socketio, db
2020-02-02 18:51:02 +01:00
from app.models import User, Command, Plate, Ingredient, Sauce, Drink, Dessert, Service
2020-02-01 17:44:42 +01:00
def authenticated_only(f):
@functools.wraps(f)
def wrapped(*args, **kwargs):
if not current_user.is_authenticated:
disconnect()
else:
return f(*args, **kwargs)
2020-02-01 17:44:42 +01:00
return wrapped
def command_json(c):
ingredient = " - ".join([s.id for s in c.content])
2020-02-01 17:44:42 +01:00
sauces = " - ".join([s.id for s in c.sauce])
2020-02-02 22:30:28 +01:00
sandwich = None
2020-02-01 17:44:42 +01:00
if c.error:
state = "error"
elif c.give:
state = "gave"
elif c.done:
state = "done"
2020-02-02 18:51:02 +01:00
elif c.WIP:
state = "WIP"
2020-02-01 17:44:42 +01:00
elif c.take:
state = "waiting"
else:
state = "unknown"
2020-02-02 22:30:28 +01:00
if c.sandwich_id:
2020-02-02 18:51:02 +01:00
try:
2020-02-02 22:30:28 +01:00
sandwich = User.query.get(c.sandwich_id).username
2020-02-02 18:51:02 +01:00
except AttributeError:
pass
return {"id": c.number, "plate": c.plate_id, "ingredient": ingredient, "sauce": sauces, "drink": c.drink_id,
2020-02-02 22:30:28 +01:00
"dessert": c.dessert_id, "state": state, "sandwich": sandwich}
2020-02-01 17:44:42 +01:00
@socketio.on("connect")
@authenticated_only
def connect():
print("New connection")
emit("connect", "ok")
@socketio.on("list command")
@authenticated_only
def lscmd():
commands = []
for c in Command.query.filter_by(date=datetime.datetime.now().date()).all():
commands.append(command_json(c))
emit("list command", {"list": commands})
@socketio.on("add command")
@authenticated_only
def addcmd(json):
c = Command()
try:
c.number = Command.query.filter_by(date=datetime.datetime.now().date()).order_by(
Command.number.desc()).first().number + 1
2020-02-01 17:44:42 +01:00
except AttributeError:
c.number = 1
c.pc_id = current_user.id
if all(i in json and json[i] for i in ["firstname", "lastname", "client"]):
db.session.add(User(username=json["client"], firstname=json["firstname"], lastname=json["lastname"]))
2020-02-01 17:44:42 +01:00
if "client" in json:
try:
c.client_id = User.query.filter_by(username=json["client"]).first().id
2020-02-01 17:44:42 +01:00
except AttributeError:
c.client_id = User.query.filter_by(username="dummy").first().id
2020-02-01 17:44:42 +01:00
if "plate" in json:
try:
c.plate_id = Plate.query.get(json["plate"]).id
except AttributeError:
pass
if "ingredient" in json:
for i in json["ingredient"]:
2020-02-01 17:44:42 +01:00
try:
c.content.append(Ingredient.query.get(i))
except AttributeError:
pass
if "sauce" in json:
for s in json["sauce"]:
try:
2020-02-01 17:46:52 +01:00
c.sauce.append(Sauce.query.get(s))
2020-02-01 17:44:42 +01:00
except AttributeError:
pass
if "drink" in json:
try:
c.drink_id = Drink.query.get(json["drink"]).id
except AttributeError:
pass
if "dessert" in json:
try:
c.dessert_id = Dessert.query.get(json["dessert"]).id
except AttributeError:
pass
db.session.add(c)
db.session.commit()
emit("new command", command_json(c), broadcast=True)
@socketio.on("clear command")
@authenticated_only
def rmcmd(json):
2020-02-02 21:47:38 +01:00
c = Command.query.filter_by(date=datetime.datetime.now().date(), number=json["id"]).first()
2020-02-01 17:44:42 +01:00
if c:
c.done = None
c.give = None
c.error = False
2020-02-02 18:51:02 +01:00
service = Service.query.filter_by(date=datetime.datetime.now().date()).first()
if c.WIP and service:
2020-02-02 22:30:28 +01:00
sandwichs = [service.sandwich1_id, service.sandwich2_id, service.sandwich3_id]
if c.sandwich_id in sandwichs:
setattr(service, f"sandwich{sandwichs.index(c.sandwich_id)+1}", False)
2020-02-02 18:51:02 +01:00
c.WIP = False
2020-02-01 17:44:42 +01:00
db.session.commit()
emit("cleared command", {"id": json["id"]}, broadcast=True)
@socketio.on("done command")
@authenticated_only
def donecmd(json):
2020-02-02 21:47:38 +01:00
c = Command.query.filter_by(date=datetime.datetime.now().date(), number=json["id"]).first()
2020-02-01 17:44:42 +01:00
if c:
c.done = datetime.datetime.now().time()
2020-02-02 18:51:02 +01:00
service = Service.query.filter_by(date=datetime.datetime.now().date()).first()
if service and c.WIP:
2020-02-02 22:30:28 +01:00
sandwichs = [service.sandwich1_id, service.sandwich2_id, service.sandwich3_id]
if c.sandwich_id in sandwichs:
setattr(service, f"sandwich{sandwichs.index(c.sandwich_id)+1}", False)
2020-02-02 18:51:02 +01:00
c.WIP = False
2020-02-01 17:44:42 +01:00
db.session.commit()
emit("finish command", {"id": json["id"]}, broadcast=True)
@socketio.on("give command")
@authenticated_only
def givecmd(json):
2020-02-02 21:47:38 +01:00
c = Command.query.filter_by(date=datetime.datetime.now().date(), number=json["id"]).first()
2020-02-01 17:44:42 +01:00
if c:
c.give = datetime.datetime.now().time()
2020-02-02 21:47:38 +01:00
service = Service.query.filter_by(date=datetime.datetime.now().date()).first()
if service and c.WIP:
2020-02-02 22:30:28 +01:00
sandwichs = [service.sandwich1_id, service.sandwich2_id, service.sandwich3_id]
if c.sandwich_id in sandwichs:
setattr(service, f"sandwich{sandwichs.index(c.sandwich_id)+1}", False)
2020-02-02 21:47:38 +01:00
c.WIP = False
2020-02-01 17:44:42 +01:00
db.session.commit()
emit("gave command", {"id": json["id"]}, broadcast=True)
2020-02-02 18:51:02 +01:00
@socketio.on("WIP command")
@authenticated_only
def wipcmd(json):
2020-02-02 21:47:38 +01:00
c = Command.query.filter_by(date=datetime.datetime.now().date(), number=json["id"]).first()
2020-02-02 18:51:02 +01:00
if c:
c.WIP = True
service = Service.query.filter_by(date=datetime.datetime.now().date()).first()
2020-02-02 22:30:28 +01:00
sandwich = None
2020-02-02 18:51:02 +01:00
if service:
2020-02-02 22:30:28 +01:00
sandwichs = [service.sandwich1, service.sandwich2, service.sandwich3]
for i, s in enumerate(sandwichs):
2020-02-02 18:51:02 +01:00
if not s:
2020-02-02 22:30:28 +01:00
setattr(service, f"sandwich{i+1}", True)
c.sandwich_id = getattr(service, f"sandwich{i+1}_id")
sandwich = User.query.get(c.sandwich_id).username
2020-02-02 18:51:02 +01:00
break
db.session.commit()
2020-02-02 22:30:28 +01:00
emit("WIPed command", {"id": json["id"], "sandwich": sandwich}, broadcast=True)
2020-02-02 18:51:02 +01:00
2020-02-01 17:44:42 +01:00
@socketio.on("error command")
@authenticated_only
def errcmd(json):
2020-02-02 21:47:38 +01:00
c = Command.query.filter_by(date=datetime.datetime.now().date(), number=json["id"]).first()
2020-02-01 17:44:42 +01:00
if c:
c.error = True
db.session.commit()
emit("glitched command", {"id": json["id"]}, broadcast=True)
@socketio.on("list plate")
@authenticated_only
def lsplate():
plates = []
for p in Plate.query.all():
plates.append({"id": p.id, "name": p.name})
emit("list plate", {"list": plates})
@socketio.on("list ingredient")
@authenticated_only
def lsingredient():
ingredients = []
for p in Ingredient.query.all():
ingredients.append({"id": p.id, "name": p.name})
emit("list ingredient", {"list": ingredients})
@socketio.on("list sauce")
@authenticated_only
def lssauce():
sauces = []
for p in Sauce.query.all():
sauces.append({"id": p.id, "name": p.name})
emit("list sauce", {"list": sauces})
@socketio.on("list drink")
@authenticated_only
def lsdrink():
drinks = []
for p in Drink.query.all():
drinks.append({"id": p.id, "name": p.name})
emit("list drink", {"list": drinks})
@socketio.on("list dessert")
@authenticated_only
def lsdessert():
desserts = []
for p in Dessert.query.all():
desserts.append({"id": p.id, "name": p.name})
emit("list dessert", {"list": desserts})
@socketio.on("list users")
@authenticated_only
def lsusers(json):
users = User.query.all()
users_list = []
for u in users:
if not json or "user" not in json or json["user"] in u.username:
users_list.append(u.username)
emit("list users", {"list": users_list})
2020-02-02 18:51:02 +01:00
@socketio.on("list service")
@authenticated_only
def lsservice():
service = Service.query.filter_by(date=datetime.datetime.now().date()).first()
s = []
if service:
2020-02-02 22:30:28 +01:00
for u in [service.sandwich1_id, service.sandwich2_id, service.sandwich3_id]:
2020-02-02 18:51:02 +01:00
try:
s.append([u, User.query.get(u).username])
except AttributeError:
s.append([])
emit("list service", {"list": s})