Split birthday code into own file

This commit is contained in:
Edward Betts 2023-11-05 12:43:59 +00:00
parent dae74f0b7c
commit e8bda4f969

View file

@ -23,7 +23,7 @@ from dateutil.relativedelta import FR, relativedelta
from agenda import thespacedevs
from . import calendar, fx, gwr, markets, sun, waste_schedule
from . import birthday, calendar, fx, gwr, markets, sun, waste_schedule
from .types import Event
warnings.simplefilter(action="ignore", category=FutureWarning)
@ -293,38 +293,6 @@ def get_conferences(filepath: str) -> List[Event]:
return events
def next_birthday(from_date: date, birth_date: date) -> tuple[date, int]:
"""Calculate the date of the next birthday based on a given birth date."""
next_birthday_date = birth_date.replace(year=from_date.year)
if from_date > next_birthday_date:
next_birthday_date = birth_date.replace(year=from_date.year + 1)
age_at_next_birthday = next_birthday_date.year - birth_date.year
return next_birthday_date, age_at_next_birthday
def get_birthdays(from_date: date, filepath: str) -> list[Event]:
"""Get birthdays from config."""
events = []
with open(filepath) as f:
entities = yaml.safe_load(f)
for entity in entities:
birthday = date(**entity["birthday"])
bday, age = next_birthday(from_date, birthday)
events.append(
Event(
date=bday,
name="birthday",
title=f'{entity["label"]} (aged {age})',
)
)
return events
def get_accommodation(from_date: date, filepath: str) -> list[Event]:
"""Get birthdays from config."""
events = []
@ -432,6 +400,8 @@ def get_data(now: datetime) -> typing.Mapping[str, str | object]:
"""Get data to display on agenda dashboard."""
rocket_dir = os.path.join(data_dir, "thespacedevs")
today = now.date()
last_week = today - timedelta(weeks=1)
last_year = today - timedelta(days=365)
reply = {
"now": now,
@ -477,7 +447,7 @@ def get_data(now: datetime) -> typing.Mapping[str, str | object]:
events.append(Event(name=f"xmas_last_{key}", date=value))
my_data = config["data"]["personal-data"]
events += get_birthdays(today, os.path.join(my_data, "entities.yaml"))
events += birthday.get_birthdays(last_year, os.path.join(my_data, "entities.yaml"))
events += get_accommodation(today, os.path.join(my_data, "accommodation.yaml"))
events += get_all_travel_events(today)
events += get_conferences(os.path.join(my_data, "conferences.yaml"))
@ -493,7 +463,7 @@ def get_data(now: datetime) -> typing.Mapping[str, str | object]:
events.sort(key=operator.attrgetter("as_datetime"))
reply["events"] = events
reply["last_week"] = today - timedelta(weeks=1)
reply["last_week"] = last_week
reply["fullcalendar_events"] = calendar.build_events(events)