agenda/web_view.py

116 lines
3.1 KiB
Python
Raw Normal View History

2023-10-02 20:35:30 +01:00
#!/usr/bin/python3
"""Web page to show upcoming events."""
2023-10-06 23:29:22 +01:00
import inspect
2023-12-04 21:53:33 +00:00
import operator
2023-11-20 19:48:42 +00:00
import os.path
2023-10-06 23:29:22 +01:00
import sys
import traceback
2023-11-20 19:48:42 +00:00
from datetime import date, datetime
2023-10-02 20:35:30 +01:00
2023-10-06 23:29:22 +01:00
import flask
import werkzeug
import werkzeug.debug.tbtools
2023-11-20 19:48:42 +00:00
import yaml
2023-10-02 20:35:30 +01:00
2023-11-07 15:55:05 +00:00
import agenda.data
2023-12-07 17:15:58 +00:00
import agenda.error_mail
import agenda.travel
2023-10-02 20:35:30 +01:00
2023-10-06 23:29:22 +01:00
app = flask.Flask(__name__)
2023-11-07 15:55:05 +00:00
app.debug = False
app.config.from_object("config.default")
2023-10-02 20:35:30 +01:00
2023-12-07 17:15:58 +00:00
agenda.error_mail.setup_error_mail(app)
2023-10-02 20:35:30 +01:00
2023-10-06 23:29:22 +01:00
@app.errorhandler(werkzeug.exceptions.InternalServerError)
def exception_handler(e: werkzeug.exceptions.InternalServerError) -> tuple[str, int]:
"""Handle exception."""
exec_type, exc_value, current_traceback = sys.exc_info()
assert exc_value
tb = werkzeug.debug.tbtools.DebugTraceback(exc_value)
summary = tb.render_traceback_html(include_title=False)
exc_lines = "".join(tb._te.format_exception_only())
last_frame = list(traceback.walk_tb(current_traceback))[-1][0]
last_frame_args = inspect.getargs(last_frame.f_code)
return (
flask.render_template(
"show_error.html",
plaintext=tb.render_traceback_text(),
exception=exc_lines,
exception_type=tb._te.exc_type.__name__,
summary=summary,
last_frame=last_frame,
last_frame_args=last_frame_args,
),
500,
)
2023-10-02 20:35:30 +01:00
@app.route("/")
async def index() -> str:
2023-10-02 20:35:30 +01:00
"""Index page."""
now = datetime.now()
data = await agenda.data.get_data(now, app.config)
2023-10-02 20:35:30 +01:00
2023-10-21 22:58:44 +01:00
return flask.render_template("index.html", today=now.date(), **data)
2023-10-02 20:35:30 +01:00
2023-12-27 08:22:53 +00:00
@app.route("/gaps")
async def gaps_page() -> str:
"""List of available gaps."""
now = datetime.now()
data = await agenda.data.get_data(now, app.config)
return flask.render_template("gaps.html", today=now.date(), gaps=data["gaps"])
@app.route("/travel")
2023-11-20 19:48:42 +00:00
def travel_list() -> str:
"""Page showing a list of upcoming travel."""
data_dir = app.config["PERSONAL_DATA"]
flights = agenda.travel.parse_yaml("flights", data_dir)
trains = agenda.travel.parse_yaml("trains", data_dir)
2023-11-20 19:48:42 +00:00
return flask.render_template("travel.html", flights=flights, trains=trains)
def as_date(d: date | datetime) -> date:
"""Date of event."""
return d.date() if isinstance(d, datetime) else d
@app.route("/conference")
def conference_list() -> str:
"""Page showing a list of conferences."""
data_dir = app.config["PERSONAL_DATA"]
2023-11-20 19:48:42 +00:00
filepath = os.path.join(data_dir, "conferences.yaml")
item_list = yaml.safe_load(open(filepath))["conferences"]
2023-12-04 21:53:33 +00:00
today = date.today()
for conf in item_list:
conf["start_date"] = as_date(conf["start"])
conf["end_date"] = as_date(conf["end"])
2023-11-20 19:48:42 +00:00
item_list.sort(key=operator.itemgetter("start_date"))
2023-11-20 19:48:42 +00:00
current = [
conf
for conf in item_list
if conf["start_date"] <= today and conf["end_date"] >= today
]
past = [conf for conf in item_list if conf["end_date"] < today]
future = [conf for conf in item_list if conf["start_date"] > today]
2023-12-04 21:53:33 +00:00
return flask.render_template(
"conference_list.html", current=current, past=past, future=future, today=today
2023-12-04 21:53:33 +00:00
)
2023-10-02 20:35:30 +01:00
if __name__ == "__main__":
app.run(host="0.0.0.0")