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/
*.pyc
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 os.path import exists
app = Flask(__name__)
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.remark = remark.strip()
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
def getName(self):
def get_name(self):
return self.name
def __eq__(self, other):
return self.name.lower() == other.name.lower()
def getStatus(self):
def get_status(self):
return self.statusses[self.status]
def nextStatus(self):
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)
@ -33,16 +47,28 @@ class Person:
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("/statusUpdate", methods=["POST", "GET"])
def statusUpdate():
@app.route("/status_update", methods=["POST", "GET"])
def status_update():
if people and request.method == "POST":
result = request.form
if "index" in result:
@ -56,17 +82,21 @@ def statusUpdate():
people[index].status += 1
if people[index].status == 3:
people.remove(people[index])
save_people()
return redirect("/")
@app.route("/addPerson", methods=["POST", "GET"])
def addPerson():
@app.route("/add_person", methods=["POST", "GET"])
def add_person():
if request.method == "POST":
result = request.form
if "name" in result:
newPerson = Person(result["name"], result["remark"])
if newPerson not in people:
people.append(newPerson)
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("/")

View File

@ -1,53 +1,82 @@
<!DOCTYPE html>
<html lang="en">
<html lang="en" data-theme="light">
<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>
body {
background-color: #303030;
color: white;
max-width: 54em;
}
</style>
<meta charset="UTF-8">
<title>Pannenkoekenwachtrij</title>
</head>
<body>
<header>
<main class="container">
<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">
<p>Naam van de volgende <input type="text" name="name"></p>
<p>Specifieke opmerkingen <input type="text" name="remark"></p>
<p><input type="submit" value="Persoon toevoegen"></p>
</form>
<form action="/statusUpdate" method="POST">
<input type="hidden" name="index" value="0">
<p><input type="submit" value="Update First Person"></p>
</form>
<form action="/status_update" 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>
<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>
{% 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>
<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="/statusUpdate" method="POST">
<input type="hidden" name="name" value="{{ person.getName() }}">
<form action="/status_update" method="POST">
<input type="hidden" name="name" value="{{ person.get_name() }}">
<input type="submit" value="Update">
</form>
</td>
<th>{{ person.getName() + person.getRemark() + ": " }}</th>
<td>{{ "Huidige status: " + person.getStatus() }}</td>
</tr>
{% 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>
</header>
</main>
</body>
</html>