From 42066f9ddecbb5e3e790d55dfff96a0014c1fd7f Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Wed, 13 Nov 2024 14:31:35 +0000 Subject: [PATCH 1/6] Refactor --- agenda/utils.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/agenda/utils.py b/agenda/utils.py index 974f554..0c17610 100644 --- a/agenda/utils.py +++ b/agenda/utils.py @@ -42,6 +42,11 @@ def timedelta_display(delta: timedelta) -> str: ) +def plural(value: int, unit: str) -> str: + """Value + unit with unit written as singular or plural as appropriate.""" + return f"{value} {unit}{'s' if value > 1 else ''}" + + def human_readable_delta(future_date: date) -> str | None: """ Calculate the human-readable time delta for a given future date. @@ -64,14 +69,11 @@ def human_readable_delta(future_date: date) -> str | None: weeks, days = divmod(days, 7) # Formatting the output - parts = [] - if months > 0: - parts.append(f"{months} month{'s' if months > 1 else ''}") - if weeks > 0: - parts.append(f"{weeks} week{'s' if weeks > 1 else ''}") - if days > 0: - parts.append(f"{days} day{'s' if days > 1 else ''}") - + parts = [ + plural(value, unit) + for value, unit in ((months, "month"), (weeks, "week"), (days, "days")) + if value > 0 + ] return " ".join(parts) if parts else None From 7a08c4b56db9ba3b4b000720c3a7a92cbd02cf2a Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Wed, 13 Nov 2024 14:32:00 +0000 Subject: [PATCH 2/6] Adjust size of map on trip page to avoid scrollbar. --- templates/trip_page.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/trip_page.html b/templates/trip_page.html index bf961fa..b7733f3 100644 --- a/templates/trip_page.html +++ b/templates/trip_page.html @@ -53,7 +53,7 @@ } .half-map { - height: 100vh; + height: 90vh; } .full-window-map { From b3e7070b84a01aabc8d7828d18e430cfcaa0740a Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Wed, 13 Nov 2024 14:32:29 +0000 Subject: [PATCH 3/6] Update tests, function moved. --- tests/test_agenda.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_agenda.py b/tests/test_agenda.py index 51d6643..35b3102 100644 --- a/tests/test_agenda.py +++ b/tests/test_agenda.py @@ -5,7 +5,6 @@ from decimal import Decimal import pytest from agenda import ( - get_gbpusd, get_next_bank_holiday, get_next_timezone_transition, next_economist, @@ -14,6 +13,7 @@ from agenda import ( timedelta_display, uk_financial_year_end, ) +from agenda.fx import get_gbpusd @pytest.fixture From 40bac837901c72647d192855565e76e9e4c3781f Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Wed, 13 Nov 2024 14:36:19 +0000 Subject: [PATCH 4/6] Remove old unused code. --- web_view.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/web_view.py b/web_view.py index a73a1b1..fb65c7b 100755 --- a/web_view.py +++ b/web_view.py @@ -580,9 +580,6 @@ def trip_stats() -> str: route_distances = agenda.travel.load_route_distances(app.config["DATA_DIR"]) trip_list = get_trip_list(route_distances) - # today = date.today() - # past = [item for item in trip_list if (item.end or item.start) < today] - conferences = sum(len(item.conferences) for item in trip_list) yearly_stats = agenda.stats.calculate_yearly_stats(trip_list) From 53f6d05e527d767fadc980d1bd3a178ccbb17338 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Wed, 13 Nov 2024 14:36:49 +0000 Subject: [PATCH 5/6] Bug fix to return 404 for unknown trip. --- web_view.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/web_view.py b/web_view.py index fb65c7b..7f6f555 100755 --- a/web_view.py +++ b/web_view.py @@ -486,13 +486,15 @@ def get_prev_current_and_next_trip( """Get previous trip, this trip and next trip.""" trip_iter = iter(trip_list) prev_trip = None + current_trip = None for trip in trip_iter: if trip.start.isoformat() == start: + current_trip = trip break prev_trip = trip next_trip = next(trip_iter, None) - return (prev_trip, trip, next_trip) + return (prev_trip, current_trip, next_trip) @app.route("/trip/") From f4fc8399260227b8058fee9481893d9ed5f3a6b1 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Wed, 13 Nov 2024 15:52:02 +0100 Subject: [PATCH 6/6] Move code around --- agenda/holidays.py | 21 ++++++++++++++++++++- web_view.py | 19 ++----------------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/agenda/holidays.py b/agenda/holidays.py index 620b9ab..a93ec5d 100644 --- a/agenda/holidays.py +++ b/agenda/holidays.py @@ -3,11 +3,30 @@ import collections from datetime import date, timedelta +import flask + import agenda.uk_holiday import holidays from .event import Event -from .types import Holiday +from .types import Holiday, Trip + + +def get_trip_holidays(trip: Trip) -> list[Holiday]: + """Get holidays happening during trip.""" + if not trip.end: + return [] + countries = {c.alpha_2 for c in trip.countries} + return sorted( + ( + hol + for hol in get_all( + trip.start, trip.end, flask.current_app.config["DATA_DIR"] + ) + if hol.country.upper() in countries + ), + key=lambda item: (item.date, item.country), + ) def us_holidays(start_date: date, end_date: date) -> list[Holiday]: diff --git a/web_view.py b/web_view.py index 7f6f555..c485657 100755 --- a/web_view.py +++ b/web_view.py @@ -507,8 +507,6 @@ def trip_page(start: str) -> str: if not trip: flask.abort(404) - today = date.today() - coordinates = agenda.trip.collect_trip_coordinates(trip) routes = agenda.trip.get_trip_routes(trip, app.config["PERSONAL_DATA"]) @@ -520,30 +518,17 @@ def trip_page(start: str) -> str: app.config["PERSONAL_DATA"], route.pop("geojson_filename") ) - if trip.end: - countries = {c.alpha_2 for c in trip.countries} - holidays = [ - hol - for hol in agenda.holidays.get_all( - trip.start, trip.end, app.config["DATA_DIR"] - ) - if hol.country.upper() in countries - ] - holidays.sort(key=lambda item: (item.date, item.country)) - else: - holidays = [] - return flask.render_template( "trip_page.html", trip=trip, prev_trip=prev_trip, next_trip=next_trip, - today=today, + today=date.today(), coordinates=coordinates, routes=routes, get_country=agenda.get_country, format_list_with_ampersand=format_list_with_ampersand, - holidays=holidays, + holidays=agenda.holidays.get_trip_holidays(trip), human_readable_delta=agenda.utils.human_readable_delta, )