pannenkoekenwachtrij/main.py
2023-12-06 13:39:39 +01:00

114 lines
2.9 KiB
Python

import pickle
from datetime import datetime
import humanize
from flask import Flask, render_template, request, redirect
from os.path import exists
from requests import post, exceptions
app = Flask(__name__)
class Person:
statusses = [
"Pannenkoek besteld",
"Pannenkoek aan het bakken",
"Pannenkoek klaar",
"Pannenkoek afgegeven",
]
def __init__(self, name, remark="", order_time=None):
self.name = name
self.remark = remark.strip()
self.status = 0
self.order_time = order_time
def get_remark(self):
return "({})".format(self.remark) if self.remark else self.remark
def get_name(self):
return self.name
def __eq__(self, other):
return self.name.lower() == other.name.lower()
def get_status(self):
return self.statusses[self.status]
def next_status(self):
return self.statusses[self.status + 1]
def order_time_humanized(self):
return humanize.naturaltime(self.order_time)
def __str__(self):
return "Persoon: {} met status: {}".format(self.name, self.status)
def __repr__(self):
return str(self)
people_filename = "people.pickle"
people = []
if exists(people_filename):
people_file = open(people_filename, "rb")
people = pickle.load(people_file)
people_file.close()
def save_people():
people_file = open(people_filename, "wb")
pickle.dump(people, people_file)
people_file.close()
def update_index(index):
people[index].status += 1
if people[index].status == 2:
try:
post('http://10.1.0.224:8080/blink')
post('http://10.1.2.3', data=f"ScrollingText >>> {people[index].name} <<< Enjoy! ")
post('http://10.1.2.3', data="Option text_trailingWhitespace 1")
except exceptions.ConnectionError:
print('Failed to blink the light')
elif people[index].status == 3:
people.remove(people[index])
@app.route("/")
def home():
return render_template("home.html", people=people)
@app.route("/status_update", methods=["POST", "GET"])
def status_update():
if people and request.method == "POST":
result = request.form
if "index" in result:
index = int(request.form["index"])
update_index(index)
if "name" in result:
index = people.index(Person(request.form["name"]))
update_index(index)
save_people()
return redirect("/")
@app.route("/add_person", methods=["POST", "GET"])
def add_person():
if request.method == "POST":
result = request.form
if result["name"]:
new_person = Person(
result["name"], result["remark"], order_time=datetime.now()
)
if new_person not in people:
people.append(new_person)
save_people()
return redirect("/")
if __name__ == "__main__":
app.run()