agenda/agenda/calendar.py
2023-12-05 00:04:56 +00:00

80 lines
2.2 KiB
Python

"""Calendar."""
import typing
from datetime import timedelta
from .types import Event
event_type_color_map = {
"bank_holiday": "success-subtle",
"conference": "primary-subtle",
"us_holiday": "secondary-subtle",
"birthday": "info-subtle",
"waste_schedule": "danger-subtle",
}
colors = {
"primary-subtle": "#cfe2ff",
"secondary-subtle": "#e2e3e5",
"success-subtle": "#d1e7dd",
"info-subtle": "#cff4fc",
"warning-subtle": "#fff3cd",
"danger-subtle": "#f8d7da",
}
def build_events(events: list[Event]) -> list[dict[str, typing.Any]]:
"""Build list of events for FullCalendar."""
items: list[dict[str, typing.Any]] = []
one_day = timedelta(days=1)
for e in events:
if e.name == "today":
continue
if e.name == "accommodation":
assert e.title and e.end_date
item = {
"allDay": True,
"title": e.display_title,
"start": e.as_date.isoformat(),
"end": (e.end_as_date + one_day).isoformat(),
"url": e.url,
}
items.append(item)
item = {
"allDay": False,
"title": "checkin: " + e.title,
"start": e.date.isoformat(),
"url": e.url,
}
items.append(item)
item = {
"allDay": False,
"title": "checkout: " + e.title,
"start": e.end_date.isoformat(),
"url": e.url,
}
items.append(item)
continue
if e.has_time:
end = e.end_date or e.date + timedelta(hours=1)
else:
end = (e.end_as_date if e.end_date else e.as_date) + one_day
item = {
"allDay": not e.has_time,
"title": e.display_title,
"start": e.date.isoformat(),
"end": end.isoformat(),
}
if e.name in event_type_color_map:
item["color"] = colors[event_type_color_map[e.name]]
item["textColor"] = "black"
if e.url:
item["url"] = e.url
items.append(item)
return items