Add weekend availability view

Closes: #130
This commit is contained in:
Edward Betts 2024-02-21 13:06:40 +00:00
parent 7a9fbcec7b
commit 5ffb389c53
4 changed files with 96 additions and 4 deletions

View file

@ -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(