1
0
Fork 0

Setup service and add page to set/update service

This commit is contained in:
Ethanell 2020-02-02 23:42:52 +01:00
parent 4efcb7eb06
commit 0c9475013d
5 changed files with 107 additions and 23 deletions

View file

@ -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()

View file

@ -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");

View file

@ -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", `<option value="${u}">`);
}
document.querySelectorAll("input[type='text']").forEach(e => {
if (data["list"].indexOf(e.value) === -1)
e.style.color = "red";
else {
e.style.color = "";
}
});
});

View file

@ -3,7 +3,7 @@
{% block content %}
<nav>
<ul>
<a id="gobackpc" href="pc.html"><li>Retour au PC</li></a>
<a id="gobackpc" href="{{ url_for('pc') }}"><li>Retour au PC</li></a>
</ul>
</nav>
<div class="bg-contact2" id="main-container" style="background-image: url({{ url_for('static',filename='images/bg-01.jpg') }});">
@ -11,30 +11,31 @@
<div id="equipes">
<div id="equipePC">
<label for="pc">PC :</label>
<input type="text" id="pc">
<input list="user_list" type="text" id="pc">
</div>
<div id="sandwich">
<label for="sandwich1">Sandiwch 1 :</label>
<input type="text" id="sandwich1">
<input list="user_list" type="text" id="sandwich1">
<label for="sandwich2">Sandwich 2 :</label>
<input type="text" id="sandwich2">
<input list="user_list" type="text" id="sandwich2">
<label for="sandwich3">Sandwich 3 :</label>
<input type="text" id="sandwich3">
<input list="user_list" type="text" id="sandwich3">
</div>
<div id="commis">
<label for="commis1">Commis 1 :</label>
<input type="text" id="commis1">
<label for="commi1">Commis 1 :</label>
<input list="user_list" type="text" id="commi1">
<label for="commis2">Commis 2 :</label>
<input type="text" id="commis2">
<label for="commi2">Commis 2 :</label>
<input list="user_list" type="text" id="commi2">
</div>
</div>
<div id="okequipe">
<button class="contact2-form-btn">Valider</button>
</div>
<datalist id="user_list">
</datalist>
</form>
</div>
<script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js" integrity="sha256-yr4fRk/GU1ehYJPAs8P4JlTgu0Hdsp4ZKrx8bDEDC3I=" crossorigin="anonymous"></script>

View file

@ -5,7 +5,7 @@
<ul>
<a href="{{ url_for('stocks') }}"><li>Stocks</li></a>
<a href="{{ url_for('menu') }}"><li>Menu</li></a>
<a href="{{ url_for('service') }}"><li>Equipes</li></a>
<a href="{{ url_for('service') }}" target="popup"><li>Equipes</li></a>
<a href="#popup"><li id="deco">Se déconnecter</li></a>
</ul>
</nav>