Browse Source

Make ampel aware of the order in bearbeiter.txt

Technically this commit makes ampel way more fragile.
It now uses an array to store each employee and identifies
employees now by the index of this array. All this requires
everything to be in order (e.g. the dropdown in the login -
if this is out of order, then login results in another user
than the displayed one).
This was just a simple solution which works way to good.
An alternative whould be of cause another entry in the
previously used dict which shows an index.
master
sqozz 2 years ago
parent
commit
0b9b781fef
2 changed files with 27 additions and 15 deletions
  1. 25
    13
      ampel.py
  2. 2
    2
      assets/templates/login.tpl

+ 25
- 13
ampel.py View File

@@ -3,7 +3,6 @@ import bottle
3 3
 import json
4 4
 from bottle import route, post, run, template, static_file, request, response, redirect
5 5
 import sqlite3
6
-import pdb
7 6
 
8 7
 @route("/")
9 8
 def index():
@@ -13,7 +12,8 @@ def index():
13 12
 def login():
14 13
 	global employees
15 14
 	if request.get_cookie("employeeId") is None:
16
-		return template("login", employees=sorted(employees.items(), key=lambda entry: entry[0]))
15
+		return template("login", employees=employees)
16
+		#return template("login", employees=sorted(employees.items(), key=lambda entry: entry[0]))
17 17
 	else:
18 18
 		redirect("/manage")
19 19
 
@@ -21,31 +21,43 @@ def login():
21 21
 def loginUser():
22 22
 	global employees
23 23
 	employeeId = request.forms.get("employeeId")
24
-	employee = employees[employeeId]
25
-	response.set_cookie("employeeId", employeeId, max_age=60*60*24*365*10)
24
+	response.set_cookie("employeeId", employeeId , max_age=60*60*24*365*10)
26 25
 	redirect("/manage")
27 26
 
27
+def loggedIn():
28
+	try:
29
+		employeeId = int(request.get_cookie("employeeId"))
30
+	except ValueError:
31
+		employeeId = None
32
+	if employeeId is None or employeeId > len(employees) or employeeId < 0:
33
+		response.set_cookie("employeeId", "") #clear cookie
34
+		redirect("/login")
35
+	else:
36
+		return employeeId
37
+
28 38
 @post("/logout")
29 39
 def logout():
40
+	employeeId = loggedIn()
41
+	if employeeId >= 0:
42
+		global employees
43
+		loggedOut = list(employees[employeeId])
44
+		loggedOut[2] = False #sets status to not available
45
+		employees[employeeId] = loggedOut
30 46
 	response.set_cookie("employeeId", "") #clear cookie
31 47
 	redirect("/login")
32 48
 
33 49
 @route("/manage")
34 50
 def manage():
35 51
 	global employees
36
-	if request.get_cookie("employeeId") is None or request.get_cookie("employeeId") not in employees:
37
-		response.set_cookie("employeeId", "") #clear cookie
38
-		redirect("/login")
39
-	else:
40
-		return template("manage", employeeData=employees[request.get_cookie("employeeId")])
52
+	employeeId = loggedIn()
53
+	if employeeId >= 0:
54
+		return template("manage", employeeData=employees[employeeId])
41 55
 
42 56
 @post("/setStatus")
43 57
 def setStatus():
44
-	if request.get_cookie("employeeId") is None:
45
-		redirect("/login")
46
-	else:
58
+	employeeId = loggedIn()
59
+	if employeeId >= 0:
47 60
 		global employees
48
-		employeeId = request.get_cookie("employeeId")
49 61
 		newStatus = list(employees[employeeId])
50 62
 		newStatus[2] = not newStatus[2] #toggle availability
51 63
 		employees[employeeId] = newStatus

+ 2
- 2
assets/templates/login.tpl View File

@@ -33,8 +33,8 @@
33 33
 		<div>Login für Mitarbeiter:</div>
34 34
 		<form method="post">
35 35
 			<select name="employeeId">
36
-				% for employee in employees:
37
-				<option value="{{employee[0]}}">{{employee[1][0]}}</option>
36
+				% for i in range(0, len(employees)):
37
+				<option value="{{i}}">{{employees[i][0]}}</option>
38 38
 				% end
39 39
 			</select>
40 40
 			<button type="submit">Einloggen</button>

Loading…
Cancel
Save