diff --git a/templates/accommodation.html b/templates/accommodation.html index 4a5b7de..dda241d 100644 --- a/templates/accommodation.html +++ b/templates/accommodation.html @@ -37,9 +37,18 @@

Accommodation

+

Statistics

diff --git a/web_view.py b/web_view.py index 91a0e94..6e7da53 100755 --- a/web_view.py +++ b/web_view.py @@ -374,22 +374,29 @@ def past_conference_list() -> str: fx_rate=agenda.fx.get_rates(app.config), ) - @app.route("/accommodation") def accommodation_list() -> str: """Page showing a list of past, present and future accommodation.""" data_dir = app.config["PERSONAL_DATA"] items = travel.parse_yaml("accommodation", data_dir) - stays_in_2024 = [item for item in items if item["from"].year == 2024] - total_nights_2024 = sum( - (stay["to"].date() - stay["from"].date()).days for stay in stays_in_2024 - ) + # Create a dictionary to hold stats for each year + year_stats = defaultdict(lambda: {"total_nights": 0, "nights_abroad": 0}) - nights_abroad_2024 = sum( - (stay["to"].date() - stay["from"].date()).days - for stay in stays_in_2024 - if stay["country"] != "gb" + # Calculate stats for each year + for stay in items: + current_date = stay["from"].date() + end_date = stay["to"].date() + while current_date < end_date: + year = current_date.year + year_stats[year]["total_nights"] += 1 + if stay.get("country") != "gb": + year_stats[year]["nights_abroad"] += 1 + current_date += timedelta(days=1) + + # Sort the stats by year in descending order + sorted_year_stats = sorted( + year_stats.items(), key=lambda item: item[0], reverse=True ) trip_lookup = {} @@ -415,8 +422,7 @@ def accommodation_list() -> str: past=past, current=current, future=future, - total_nights_2024=total_nights_2024, - nights_abroad_2024=nights_abroad_2024, + year_stats=sorted_year_stats, get_country=agenda.get_country, fx_rate=agenda.fx.get_rates(app.config), )