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 3 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
import json
from bottle import route, post, run, template, static_file, request, response, redirect
import sqlite3
import pdb

@route("/")
def index():
@@ -13,7 +12,8 @@ def index():
def login():
global employees
if request.get_cookie("employeeId") is None:
return template("login", employees=sorted(employees.items(), key=lambda entry: entry[0]))
return template("login", employees=employees)
#return template("login", employees=sorted(employees.items(), key=lambda entry: entry[0]))
else:
redirect("/manage")

@@ -21,31 +21,43 @@ def login():
def loginUser():
global employees
employeeId = request.forms.get("employeeId")
employee = employees[employeeId]
response.set_cookie("employeeId", employeeId, max_age=60*60*24*365*10)
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
if request.get_cookie("employeeId") is None or request.get_cookie("employeeId") not in employees:
response.set_cookie("employeeId", "") #clear cookie
redirect("/login")
else:
return template("manage", employeeData=employees[request.get_cookie("employeeId")])
employeeId = loggedIn()
if employeeId >= 0:
return template("manage", employeeData=employees[employeeId])

@post("/setStatus")
def setStatus():
if request.get_cookie("employeeId") is None:
redirect("/login")
else:
employeeId = loggedIn()
if employeeId >= 0:
global employees
employeeId = request.get_cookie("employeeId")
newStatus = list(employees[employeeId])
newStatus[2] = not newStatus[2] #toggle availability
employees[employeeId] = newStatus

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

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

Loading…
Cancel
Save