pannenkoekenwachtrij/main.py
2022-12-08 21:10:43 +01:00

104 lines
2.6 KiB
Python

import pickle
from datetime import datetime
import humanize
from flask import Flask, render_template, request, redirect, url_for
from os.path import exists
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()
@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"])
people[index].status += 1
if people[index].status == 3:
people.remove(people[index])
if "name" in result:
index = people.index(Person(request.form["name"]))
print(index)
people[index].status += 1
if people[index].status == 3:
people.remove(people[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()