scrapple/app.py

59 lines
1.4 KiB
Python
Raw Normal View History

2021-05-22 19:23:17 +00:00
import csv
2021-06-04 21:49:42 +00:00
import math
from flask import Flask, render_template, abort, request
2021-05-22 18:29:40 +00:00
app = Flask(__name__)
2021-05-23 02:42:39 +00:00
_employees = {}
2021-05-22 18:29:40 +00:00
2021-05-23 02:18:20 +00:00
def employees():
2021-05-23 02:42:39 +00:00
# thanks to http://www.figmentfly.com/bb/badguys3.html for names
global _employees
if not _employees:
with open("data/employees.csv") as f:
_employees = {e["id"]: e for e in csv.DictReader(f)}
return _employees
2021-05-23 02:18:20 +00:00
2021-05-22 18:29:40 +00:00
@app.route("/")
def index():
return render_template("index.html")
@app.route("/about")
def about():
return render_template("about.html")
2021-05-22 19:23:17 +00:00
@app.route("/staff")
def staff():
2021-06-04 21:49:42 +00:00
page = int(request.args.get("page", 1))
per_page = 10
total_items = len(employees())
max_page = math.ceil(total_items / per_page)
print(max_page)
if page < 1 or page > max_page:
abort(404)
page_employees = list(employees().values())[(page - 1) * per_page : page * per_page]
return render_template(
"staff.html",
employees=page_employees,
page=page,
prev_page=page - 1,
next_page=page + 1 if page < max_page else None,
)
2021-05-23 02:42:39 +00:00
2021-05-22 19:23:17 +00:00
2021-05-23 02:42:39 +00:00
@app.route("/staff/<id_>")
def staff_detail(id_):
2021-06-05 05:35:56 +00:00
if id_ == "404":
abort(
404,
"This page intentionally left blank. (No really! This is an intentional error for demonstration purposes.)",
)
2021-05-23 02:42:39 +00:00
if employee := employees().get(id_):
return render_template("staff_detail.html", employee=employee)
else:
2021-05-23 02:57:58 +00:00
abort(404)