hbbs.decentral1.se/hbbs/server.py

73 lines
1.8 KiB
Python
Raw Normal View History

2020-04-04 18:55:53 +02:00
"""Flask server."""
2020-04-04 20:45:54 +02:00
from csv import DictReader
from os import listdir
2020-04-04 21:34:32 +02:00
from os.path import basename, dirname
2020-04-04 20:45:54 +02:00
from pathlib import Path
2020-04-04 21:34:32 +02:00
from flask import Flask, abort, render_template
2020-04-04 20:45:54 +02:00
from ruamel.yaml import YAML
2020-04-04 18:55:53 +02:00
app = Flask(__name__)
2020-04-04 20:45:54 +02:00
cwd = dirname(Path(__file__).absolute())
yaml = YAML()
2020-04-04 18:55:53 +02:00
2020-04-04 21:34:32 +02:00
def get_programmes():
2020-04-04 20:45:54 +02:00
programmes = []
path = cwd / Path("static/programmes/")
for file in listdir(path):
with open(path / file, "r") as handle:
contents = handle.read()
loaded = yaml.load(contents)
programmes.append(loaded)
2020-04-04 21:34:32 +02:00
return programmes
2020-04-04 20:45:54 +02:00
2020-04-04 21:34:32 +02:00
def get_collection():
2020-04-04 20:45:54 +02:00
collection = []
path = cwd / Path("static/csv/collection.csv")
with open(path, "r") as handle:
reader = DictReader(handle)
for row in reader:
collection.append(row)
2020-04-04 21:34:32 +02:00
return collection
@app.route("/")
def about():
return render_template("about.html")
@app.route("/programmes")
def programmes():
programmes = get_programmes()
return render_template("programmes.html", programmes=programmes)
2020-04-05 11:18:51 +02:00
@app.route("/programme/<name>/")
2020-04-04 21:34:32 +02:00
def programme(name):
cleaned_name = name.replace("-", " ").lower()
programmes = get_programmes()
for programme in programmes:
cleaned_programme = programme.get("title", "").lower()
if cleaned_name == cleaned_programme:
return render_template("programme.html", programme=programme)
abort(404, description="Programme not found")
@app.route("/collection")
def collection():
collection = get_collection()
2020-04-04 20:45:54 +02:00
return render_template("collection.html", collection=collection)
2020-04-05 12:13:40 +02:00
@app.route("/glossary")
def glossary():
return render_template("glossary.html")
2020-04-04 20:45:54 +02:00
@app.route("/participate")
def participate():
return render_template("participate.html")