Various improvements

This commit is contained in:
Edward Betts 2026-06-30 12:31:51 +01:00
parent 5f6cb57c2a
commit bc7a2e89d0
4 changed files with 160 additions and 75 deletions

View file

@ -3,13 +3,13 @@
import asyncio
import os
import typing
from datetime import date, datetime, timedelta
from datetime import date, datetime, timedelta, timezone
from time import time
import dateutil.rrule
import dateutil.tz
import flask
import lxml
import lxml # type: ignore[import-untyped]
import pytz
from . import (
@ -68,7 +68,7 @@ async def n_somerset_waste_collection_events(
html = await n_somerset_waste.get_html(data_dir, postcode, uprn, force_cache)
root = lxml.html.fromstring(html)
events = n_somerset_waste.parse(root)
return events
return typing.cast(list[Event], events)
async def bristol_waste_collection_events(
@ -76,7 +76,9 @@ async def bristol_waste_collection_events(
) -> list[Event]:
"""Waste colllection events."""
cache = "force" if force_cache else "recent"
return await bristol_waste.get(start_date, data_dir, uprn, cache)
return typing.cast(
list[Event], await bristol_waste.get(start_date, data_dir, uprn, cache)
)
def find_events_during_stay(
@ -168,6 +170,14 @@ def rocket_launch_events(rockets: list[thespacedevs.Summary]) -> list[Event]:
return events
def event_sort_datetime(event: Event) -> datetime:
"""Return a timezone-normalized datetime suitable for sorting events."""
dt = typing.cast(datetime, event.as_datetime)
if dt.tzinfo is None or dt.utcoffset() is None:
return dt
return dt.astimezone(timezone.utc).replace(tzinfo=None)
async def get_data(now: datetime, config: flask.config.Config) -> AgendaData:
"""Get data to display on agenda dashboard."""
data_dir = config["DATA_DIR"]
@ -290,7 +300,7 @@ async def get_data(now: datetime, config: flask.config.Config) -> AgendaData:
# at the top of the list for today. This is achieved by sorting first by
# the datetime attribute, and then ensuring that events with the name
# "today" are ordered before others on the same date.
events.sort(key=lambda e: (e.as_datetime, e.name != "today"))
events.sort(key=lambda e: (event_sort_datetime(e), e.name != "today"))
reply["gaps"] = gaps