pannenkoekenwachtrij/main.py

105 lines
2.6 KiB
Python
Raw Normal View History

2022-12-08 20:41:30 +01:00
import pickle
from datetime import datetime
import humanize
from flask import Flask, render_template, request, redirect, url_for
2022-12-08 20:41:30 +01:00
from os.path import exists
app = Flask(__name__)
class Person:
2022-12-08 20:41:30 +01:00
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
2022-12-08 20:41:30 +01:00
self.order_time = order_time
2022-12-08 20:41:30 +01:00
def get_remark(self):
return "({})".format(self.remark) if self.remark else self.remark
2022-12-08 20:41:30 +01:00
def get_name(self):
return self.name
def __eq__(self, other):
return self.name.lower() == other.name.lower()
2022-12-08 20:41:30 +01:00
def get_status(self):
return self.statusses[self.status]
2022-12-08 20:41:30 +01:00
def next_status(self):
return self.statusses[self.status + 1]
2022-12-08 20:41:30 +01:00
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)
2022-12-08 20:41:30 +01:00
people_filename = "people.pickle"
people = []
2022-12-08 20:41:30 +01:00
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)
2022-12-08 20:41:30 +01:00
@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])
2022-12-08 20:41:30 +01:00
save_people()
return redirect("/")
2022-12-08 20:41:30 +01:00
@app.route("/add_person", methods=["POST", "GET"])
def add_person():
if request.method == "POST":
result = request.form
2022-12-08 20:41:30 +01:00
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()