persistence and layout

This commit is contained in:
mcbloch 2022-12-08 20:41:30 +01:00
parent 716ad05626
commit 5d71f20fba
4 changed files with 106 additions and 44 deletions

2
.gitignore vendored
View file

@ -1,3 +1,5 @@
.idea/ .idea/
*.pyc *.pyc
venv/ venv/
*.pickle
*.db

1
.tool-versions Normal file
View file

@ -0,0 +1 @@
python 3.10.4

58
main.py
View file

@ -1,31 +1,45 @@
import pickle
from datetime import datetime
import humanize
from flask import Flask, render_template, request, redirect, url_for from flask import Flask, render_template, request, redirect, url_for
from os.path import exists
app = Flask(__name__) app = Flask(__name__)
class Person: class Person:
statusses = ["Pannenkoek besteld", "Pannenkoek aan het bakken", "Pannenkoek klaar", "Pannenkoek afgegeven"] statusses = [
"Pannenkoek besteld",
"Pannenkoek aan het bakken",
"Pannenkoek klaar",
"Pannenkoek afgegeven",
]
def __init__(self, name, remark=""): def __init__(self, name, remark="", order_time=None):
self.name = name self.name = name
self.remark = remark.strip() self.remark = remark.strip()
self.status = 0 self.status = 0
self.order_time = order_time
def getRemark(self): def get_remark(self):
return "({})".format(self.remark) if self.remark else self.remark return "({})".format(self.remark) if self.remark else self.remark
def getName(self): def get_name(self):
return self.name return self.name
def __eq__(self, other): def __eq__(self, other):
return self.name.lower() == other.name.lower() return self.name.lower() == other.name.lower()
def getStatus(self): def get_status(self):
return self.statusses[self.status] return self.statusses[self.status]
def nextStatus(self): def next_status(self):
return self.statusses[self.status + 1] return self.statusses[self.status + 1]
def order_time_humanized(self):
return humanize.naturaltime(self.order_time)
def __str__(self): def __str__(self):
return "Persoon: {} met status: {}".format(self.name, self.status) return "Persoon: {} met status: {}".format(self.name, self.status)
@ -33,16 +47,28 @@ class Person:
return str(self) return str(self)
people_filename = "people.pickle"
people = [] 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("/") @app.route("/")
def home(): def home():
return render_template("home.html", people=people) return render_template("home.html", people=people)
@app.route("/statusUpdate", methods=["POST", "GET"]) @app.route("/status_update", methods=["POST", "GET"])
def statusUpdate(): def status_update():
if people and request.method == "POST": if people and request.method == "POST":
result = request.form result = request.form
if "index" in result: if "index" in result:
@ -56,17 +82,21 @@ def statusUpdate():
people[index].status += 1 people[index].status += 1
if people[index].status == 3: if people[index].status == 3:
people.remove(people[index]) people.remove(people[index])
save_people()
return redirect("/") return redirect("/")
@app.route("/addPerson", methods=["POST", "GET"]) @app.route("/add_person", methods=["POST", "GET"])
def addPerson(): def add_person():
if request.method == "POST": if request.method == "POST":
result = request.form result = request.form
if "name" in result: if result["name"]:
newPerson = Person(result["name"], result["remark"]) new_person = Person(
if newPerson not in people: result["name"], result["remark"], order_time=datetime.now()
people.append(newPerson) )
if new_person not in people:
people.append(new_person)
save_people()
return redirect("/") return redirect("/")

View file

@ -1,53 +1,82 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en" data-theme="light">
<head> <head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://unpkg.com/sakura.css/css/sakura.css" type="text/css">
<title>Pannenkoekenwachtrij</title>
<style> <style>
body { body {
background-color: #303030; max-width: 54em;
color: white;
} }
</style> </style>
<meta charset="UTF-8">
<title>Pannenkoekenwachtrij</title>
</head> </head>
<body> <body>
<header> <main class="container">
<h1>Welkom bij de pannenkoekenwachtrij</h1> <h1>Welkom bij de pannenkoekenwachtrij</h1>
<div class="grid">
<form action="/add_person" method="POST">
<label for="name">
Naam van de volgende
<input type="text" id="name" name="name"></label>
<label for="remark">
Specifieke opmerkingen
<input type="text" id="remark" name="remark">
</label>
<p><input type="submit" value="Persoon toevoegen"></p>
</form>
<form action="/addPerson" method="POST"> <form action="/status_update" method="POST">
<p>Naam van de volgende <input type="text" name="name"></p> <input type="hidden" name="index" value="0">
<p>Specifieke opmerkingen <input type="text" name="remark"></p> <p><input type="submit" value="Update First Person"></p>
<p><input type="submit" value="Persoon toevoegen"></p> </form>
</form>
<form action="/statusUpdate" method="POST">
<input type="hidden" name="index" value="0">
<p><input type="submit" value="Update First Person"></p>
</form>
</div>
<h2>Zie hieronder de lijst van personen die een pannenkoek willen</h2> <h2>Zie hieronder de lijst van personen die een pannenkoek willen</h2>
<h3>Volgende Persoon:</h3>
{% if people %}
<tr>
<th>{{ people[0].getName() + people[0].getRemark() + ": " }}</th>
<td>{{ "Huidige status: " + people[0].getStatus() }}</td>
</tr>
{% endif %}
<h3>Andere Personen:</h3>
<table> <table>
{% for person in people[1:] %} <thead>
<tr>
<th>Name</th>
<th>Huidige status</th>
<th>Bestel tijd</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="4" style='text-align:center; vertical-align:middle'>Volgende persoon</td>
</tr>
{% for person in people[:1] %}
<tr> <tr>
<th>{{ person.get_name() + person.get_remark() }}</th>
<td>{{ person.get_status() }}</td>
<td>{{ person.order_time_humanized() if person.order_time else "Unknown" }}</td>
<td> <td>
<form action="/statusUpdate" method="POST"> <form action="/status_update" method="POST">
<input type="hidden" name="name" value="{{ person.getName() }}"> <input type="hidden" name="name" value="{{ person.get_name() }}">
<input type="submit" value="Update"> <input type="submit" value="Update">
</form> </form>
</td> </td>
<th>{{ person.getName() + person.getRemark() + ": " }}</th>
<td>{{ "Huidige status: " + person.getStatus() }}</td>
</tr> </tr>
{% endfor %} {% endfor %}
<tr>
<td colspan="4" style='text-align:center; vertical-align:middle'>Andere personen</td>
</tr>
{% for person in people[1:] %}
<tr>
<th>{{ person.get_name() + person.get_remark() }}</th>
<td>{{ person.get_status() }}</td>
<td>{{ person.order_time_humanized() if person.order_time else "Unknown" }}</td>
<td>
<form action="/status_update" method="POST">
<input type="hidden" name="name" value="{{ person.get_name() }}">
<input type="submit" value="Update">
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table> </table>
</header> </main>
</body> </body>
</html> </html>