forked from sqozz/ampel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ampel.py
executable file
·102 lines (86 loc) · 2.53 KB
/
ampel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python3
import bottle
import json
from bottle import route, post, run, template, static_file, request, response, redirect
import sqlite3
@route("/")
def index():
return template("display")
@route("/login")
def login():
global employees
if request.get_cookie("employeeId") is None:
return template("login", employees=employees)
#return template("login", employees=sorted(employees.items(), key=lambda entry: entry[0]))
else:
redirect("/manage")
@post("/login")
def loginUser():
global employees
employeeId = request.forms.get("employeeId")
response.set_cookie("employeeId", employeeId , max_age=60*60*24*365*10)
redirect("/manage")
def loggedIn():
try:
employeeId = int(request.get_cookie("employeeId"))
except ValueError:
employeeId = None
if employeeId is None or employeeId > len(employees) or employeeId < 0:
response.set_cookie("employeeId", "") #clear cookie
redirect("/login")
else:
return employeeId
@post("/logout")
def logout():
employeeId = loggedIn()
if employeeId >= 0:
global employees
loggedOut = list(employees[employeeId])
loggedOut[2] = False #sets status to not available
employees[employeeId] = loggedOut
response.set_cookie("employeeId", "") #clear cookie
redirect("/login")
@route("/manage")
def manage():
global employees
employeeId = loggedIn()
if employeeId >= 0:
return template("manage", employeeData=employees[employeeId])
@post("/setStatus")
def setStatus():
employeeId = loggedIn()
if employeeId >= 0:
global employees
newStatus = list(employees[employeeId])
newStatus[2] = not newStatus[2] #toggle availability
employees[employeeId] = newStatus
redirect("/manage")
@post("/getStatus")
def getStatus():
response.content_type = 'application/json'
global employees
employeeId = request.get_cookie("employeeId")
resp = json.dumps(employees)
return resp
@route("/assets/static/<filename>")
def staticContent(filename):
return static_file(filename, root="./assets/static")
@route("/assets/js/<filename>")
def staticContent(filename):
return static_file(filename, root="./assets/js")
@route("/assets/css/<filename>")
def staticContent(filename):
return static_file(filename, root="./assets/css")
def initTool():
global employees
employees = []
with open("bearbeiter.txt", "r") as f:
for line in f.readlines():
name, room = line.strip().split("|")
status = False
employeeId = name.strip().replace(" ", "_")
employees.append((name, room, status))
pass
bottle.TEMPLATE_PATH += ["./assets/templates/"]
initTool()
run(host='0.0.0.0', port=8080, debug=False)