From 5ffb389c536102a1034147c2330f2d267683512a Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Wed, 21 Feb 2024 13:06:40 +0000 Subject: [PATCH] Add weekend availability view Closes: #130 --- agenda/data.py | 42 ++++++++++++++++++++++++++++++++++++--- templates/navbar.html | 1 + templates/weekends.html | 44 +++++++++++++++++++++++++++++++++++++++++ web_view.py | 13 +++++++++++- 4 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 templates/weekends.html diff --git a/agenda/data.py b/agenda/data.py index 27e2cf1..d209020 100644 --- a/agenda/data.py +++ b/agenda/data.py @@ -287,9 +287,10 @@ async def time_function( return name, result, end_time - start_time, exception -def gap_list( +def get_busy_events( today: date, config: flask.config.Config, trips: list[Trip] -) -> list[StrDict]: +) -> list[Event]: + """Find busy events from a year ago to two years in the future.""" last_year = today - timedelta(days=365) next_year = today + timedelta(days=2 * 365) @@ -311,6 +312,7 @@ def gap_list( url=flask.url_for("trip_page", start=trip.start.isoformat()), ) ) + # pprint(events) busy_events = [ e @@ -318,7 +320,41 @@ def gap_list( if e.as_date > today and e.as_date < next_year and busy_event(e) ] - return find_gaps(busy_events) + return busy_events + + +def weekends(busy_events: list[Event]) -> typing.Sequence[StrDict]: + """Next ten weekends.""" + today = datetime.today() + weekday = today.weekday() + + # Calculate the difference to the next or previous Saturday + if weekday == 6: # Sunday + start_date = (today - timedelta(days=1)).date() + else: + start_date = (today + timedelta(days=(5 - weekday))).date() + + weekends_info = [] + for i in range(52): + saturday = start_date + timedelta(weeks=i) + sunday = saturday + timedelta(days=1) + + saturday_events = [ + event + for event in busy_events + if event.end_date and event.as_date <= saturday <= event.end_as_date + ] + sunday_events = [ + event + for event in busy_events + if event.end_date and event.as_date <= sunday <= event.end_as_date + ] + + weekends_info.append( + {"date": saturday, "saturday": saturday_events, "sunday": sunday_events} + ) + + return weekends_info async def get_data( diff --git a/templates/navbar.html b/templates/navbar.html index 2ba8830..4f4046c 100644 --- a/templates/navbar.html +++ b/templates/navbar.html @@ -7,6 +7,7 @@ {"endpoint": "travel_list", "label": "Travel" }, {"endpoint": "accommodation_list", "label": "Accommodation" }, {"endpoint": "gaps_page", "label": "Gaps" }, + {"endpoint": "weekends", "label": "Weekends" }, {"endpoint": "launch_list", "label": "Space launches" }, {"endpoint": "holiday_list", "label": "Holidays" }, ] %} diff --git a/templates/weekends.html b/templates/weekends.html new file mode 100644 index 0000000..a5d7659 --- /dev/null +++ b/templates/weekends.html @@ -0,0 +1,44 @@ +{% extends "base.html" %} + +{% block content %} +
+ +

Weekends

+ + + + + + + + + + + + {% for weekend in items %} + + + + {% for day in "saturday", "sunday" %} + + {% endfor %} + + {% endfor %} + +
WeekDateSaturdaySunday
+ {{ weekend.date.isocalendar().week }} + + {{ weekend.date.strftime("%-d %b %Y") }} + + {% if weekend[day] %} + {% for event in weekend[day] %} + {{ event.title }}{% if not loop.last %},{%endif %} + {% endfor %} + {% else %} + 🆓 🍃 📖 + {% endif %} +
+
+ +{% endblock %} + diff --git a/web_view.py b/web_view.py index db52555..50c0c20 100755 --- a/web_view.py +++ b/web_view.py @@ -87,10 +87,21 @@ async def gaps_page() -> str: """List of available gaps.""" now = datetime.now() trip_list = agenda.trip.build_trip_list() - gaps = agenda.data.gap_list(now.date(), app.config, trip_list) + busy_events = agenda.data.get_busy_events(now.date(), app.config, trip_list) + gaps = agenda.data.find_gaps(busy_events) return flask.render_template("gaps.html", today=now.date(), gaps=gaps) +@app.route("/weekends") +async def weekends() -> str: + """List of available gaps.""" + now = datetime.now() + trip_list = agenda.trip.build_trip_list() + busy_events = agenda.data.get_busy_events(now.date(), app.config, trip_list) + weekends = agenda.data.weekends(busy_events) + return flask.render_template("weekends.html", today=now.date(), items=weekends) + + @app.route("/travel") def travel_list() -> str: """Page showing a list of upcoming travel."""