1 """Collecting Day and date-related items."""
2 from __future__ import annotations
3 from datetime import datetime, timedelta
4 from plomtask.db import DatabaseConnection, BaseModel
5 from plomtask.todos import Todo
6 from plomtask.dating import (DATE_FORMAT, valid_date)
9 class Day(BaseModel[str]):
10 """Individual days defined by their dates."""
14 def __init__(self, date: str, comment: str = '') -> None:
15 id_ = valid_date(date)
17 self.datetime = datetime.strptime(self.date, DATE_FORMAT)
18 self.comment = comment
19 self.calendarized_todos: list[Todo] = []
21 def __lt__(self, other: Day) -> bool:
22 return self.date < other.date
25 def by_date_range_filled(cls, db_conn: DatabaseConnection,
26 start: str, end: str) -> list[Day]:
27 """Return days existing and non-existing between dates start/end."""
28 ret = cls.by_date_range_with_limits(db_conn, (start, end), 'id')
29 days, start_date, end_date = ret
30 return cls.with_filled_gaps(days, start_date, end_date)
33 def with_filled_gaps(cls, days: list[Day], start_date: str, end_date: str
35 """In days, fill with (un-saved) Days gaps between start/end_date."""
36 if start_date > end_date:
39 if start_date not in [d.date for d in days]:
40 days[:] = [Day(start_date)] + days
41 if end_date not in [d.date for d in days]:
42 days += [Day(end_date)]
45 for i, day in enumerate(days):
48 while day.next_date != days[i+1].date:
49 day = Day(day.next_date)
51 days[:] = gapless_days
55 def date(self) -> str:
56 """Return self.id_ under the assumption it's a date string."""
57 assert isinstance(self.id_, str)
61 def first_of_month(self) -> bool:
62 """Return what month self.date is part of."""
63 assert isinstance(self.id_, str)
64 return self.id_[-2:] == '01'
67 def month_name(self) -> str:
68 """Return what month self.date is part of."""
69 return self.datetime.strftime('%B')
72 def weekday(self) -> str:
73 """Return what weekday matches self.date."""
74 return self.datetime.strftime('%A')
77 def prev_date(self) -> str:
78 """Return date preceding date of this Day."""
79 prev_datetime = self.datetime - timedelta(days=1)
80 return prev_datetime.strftime(DATE_FORMAT)
83 def next_date(self) -> str:
84 """Return date succeeding date of this Day."""
85 next_datetime = self.datetime + timedelta(days=1)
86 return next_datetime.strftime(DATE_FORMAT)
88 def collect_calendarized_todos(self, db_conn: DatabaseConnection) -> None:
89 """Fill self.calendarized_todos."""
90 self.calendarized_todos = [t for t in Todo.by_date(db_conn, self.date)