From 0c9475013dc59a9315827350110d5c3baea096d6 Mon Sep 17 00:00:00 2001 From: flifloo Date: Sun, 2 Feb 2020 23:42:52 +0100 Subject: [PATCH] Setup service and add page to set/update service --- app/sockets.py | 51 +++++++++++++++++++++++++++++++------- app/static/js/kitchen.js | 4 +-- app/static/js/service.js | 52 +++++++++++++++++++++++++++++++++++++++ app/templates/equipe.html | 21 ++++++++-------- app/templates/pc.html | 2 +- 5 files changed, 107 insertions(+), 23 deletions(-) diff --git a/app/sockets.py b/app/sockets.py index abc5e34..38a45ee 100644 --- a/app/sockets.py +++ b/app/sockets.py @@ -237,7 +237,9 @@ def lsdessert(): @socketio.on("list users") @authenticated_only -def lsusers(json): +def lsusers(json=None): + if json is None: + json = {} users = User.query.all() users_list = [] for u in users: @@ -248,14 +250,45 @@ def lsusers(json): @socketio.on("list service") @authenticated_only -def lsservice(): +def lsservice(json=None, broadcast=False): service = Service.query.filter_by(date=datetime.datetime.now().date()).first() - s = [] + s = {} if service: - for u in [service.sandwich1_id, service.sandwich2_id, service.sandwich3_id]: - try: - s.append([u, User.query.get(u).username]) - except AttributeError: - s.append([]) + for i in [["pc", service.pc_id], ["sandwich1", service.sandwich1_id], ["sandwich2", service.sandwich2_id], + ["sandwich3", service.sandwich3_id], ["commi1", service.commi1_id], ["commi2", service.commi2_id]]: + s[i[0]] = User.query.get(i[1]).username + emit("list service", s, broadcast=broadcast) - emit("list service", {"list": s}) + +@socketio.on("set service") +@authenticated_only +def setservice(json): + service = Service.query.filter_by(date=datetime.datetime.now().date()).first() + if not service: + service = Service() + if all(i in json and json[i] for i in ["pc", "sandwich1", "sandwich2", "sandwich3", "commi1", "commi2"]): + for i in [["pc", "pc_id"], ["sandwich1", "sandwich1_id"], ["sandwich2", "sandwich2_id"], + ["sandwich3", "sandwich3_id"], ["commi1", "commi1_id"], ["commi2", "commi2_id"]]: + setattr(service, i[1], User.query.filter_by(username=json[i[0]]).first().id) + else: + dummy = User.query.filter_by(username="dummy").first().id + for i in ["pc_id", "sandwich1_id","sandwich2_id", "sandwich3_id", "commi1_id", "commi2_id"]: + setattr(service, i[1], dummy) + service.sandwich1 = False + service.sandwich2 = False + service.sandwich3 = False + if not service.date: + db.session.add(service) + db.session.commit() + lsservice(broadcast=True) + + +@socketio.on("add user") +@authenticated_only +def adduser(json): + if all(i in json and json[i] for i in ["username", "firstname", "lastname"]): + u = User(username=json["username"], firstname=json["firstname"], lastname=json["lastname"]) + if "password" in json: + u.set_password(json["password"]) + db.session.add(u) + db.session.commit() diff --git a/app/static/js/kitchen.js b/app/static/js/kitchen.js index 9158027..1fa9056 100644 --- a/app/static/js/kitchen.js +++ b/app/static/js/kitchen.js @@ -1,5 +1,4 @@ let socket = io(); -let service = []; let WIP = document.getElementById("encours"); let done = document.getElementById("realisee"); let waiting = document.getElementById("attente"); @@ -73,8 +72,7 @@ socket.on("list command", data => { }); socket.on("list service", data => { - service = data["list"]; - if (service.length === 0) + if (Object.keys(data).length === 0) alert("No service set !"); else socket.emit("list command"); diff --git a/app/static/js/service.js b/app/static/js/service.js index e69de29..940282e 100644 --- a/app/static/js/service.js +++ b/app/static/js/service.js @@ -0,0 +1,52 @@ +let socket = io(); + +document.querySelectorAll("input[type='text']").forEach(e => { + e.addEventListener("keyup", ev => {hinter(ev)}) +}); + +document.querySelector("button").addEventListener("click", ev => { + let users= {}; + document.querySelectorAll("input[type='text']").forEach(e=> { + if (e.style.color) + socket.emit("add user", {"username": e.value, "firstname": prompt(`Prénom pour ${e.value}`), "lastname": prompt(`Nom pour ${e.value}`), "password": prompt(`MDP pour ${e.value}`)}); + users[e.id] = e.value; + }); + socket.emit("set service", users); + window.close(); +}); + +function hinter(ev) { + let input = ev.target; + let min_characters = 0; + if (input.value.length < min_characters) + return; + socket.emit("list users"); +} + +socket.on("connect", data => { + if (data === "ok") { + socket.emit("list service"); + } +}); + +socket.on("list service", data => { + for (let s in data) { + document.getElementById(s).value = data[s] + } +}); + +socket.on("list users", data => { + let user_list = document.getElementById("user_list"); + user_list.innerHTML = ""; + for (let u of data["list"]) { + user_list.insertAdjacentHTML("beforeend", `