1 """Collecting Day and date-related items."""
2 from __future__ import annotations
4 from sqlite3 import Row
5 from datetime import datetime, timedelta
6 from plomtask.exceptions import HandledException
7 from plomtask.db import DatabaseConnection, BaseModel
8 from plomtask.todos import Todo
9 from plomtask.dating import (DATE_FORMAT, valid_date)
12 class Day(BaseModel[str]):
13 """Individual days defined by their dates."""
20 init_empty_todo_list: bool = False
22 id_ = valid_date(date)
24 self.datetime = datetime.strptime(self.date, DATE_FORMAT)
25 self.comment = comment
26 self.todos: list[Todo] | None = [] if init_empty_todo_list else None
28 def __lt__(self, other: Day) -> bool:
29 return self.date < other.date
32 def from_table_row(cls, db_conn: DatabaseConnection, row: Row | list[Any]
34 """Make from DB row, with linked Todos."""
35 day = super().from_table_row(db_conn, row)
36 assert isinstance(day.id_, str)
37 day.todos = Todo.by_date(db_conn, day.id_)
41 def by_date_range_filled(cls, db_conn: DatabaseConnection,
42 start: str, end: str) -> list[Day]:
43 """Return days existing and non-existing between dates start/end."""
44 ret = cls.by_date_range_with_limits(db_conn, (start, end), 'id')
45 days, start_date, end_date = ret
46 return cls.with_filled_gaps(days, start_date, end_date)
49 def with_filled_gaps(cls, days: list[Day], start_date: str, end_date: str
51 """In days, fill with (un-saved) Days gaps between start/end_date."""
52 if start_date > end_date:
55 if start_date not in [d.date for d in days]:
56 days[:] = [Day(start_date, init_empty_todo_list=True)] + days
57 if end_date not in [d.date for d in days]:
58 days += [Day(end_date, init_empty_todo_list=True)]
61 for i, day in enumerate(days):
64 while day.next_date != days[i+1].date:
65 day = Day(day.next_date, init_empty_todo_list=True)
67 days[:] = gapless_days
71 def date(self) -> str:
72 """Return self.id_ under the assumption it's a date string."""
73 assert isinstance(self.id_, str)
77 def first_of_month(self) -> bool:
78 """Return what month self.date is part of."""
79 assert isinstance(self.id_, str)
80 return self.id_[-2:] == '01'
83 def month_name(self) -> str:
84 """Return what month self.date is part of."""
85 return self.datetime.strftime('%B')
88 def weekday(self) -> str:
89 """Return what weekday matches self.date."""
90 return self.datetime.strftime('%A')
93 def prev_date(self) -> str:
94 """Return date preceding date of this Day."""
95 prev_datetime = self.datetime - timedelta(days=1)
96 return prev_datetime.strftime(DATE_FORMAT)
99 def next_date(self) -> str:
100 """Return date succeeding date of this Day."""
101 next_datetime = self.datetime + timedelta(days=1)
102 return next_datetime.strftime(DATE_FORMAT)
105 def calendarized_todos(self) -> list[Todo]:
106 """Return only those of self.todos that have .calendarize set."""
107 if self.todos is None:
108 msg = 'Trying to return from un-initialized Day.todos.'
109 raise HandledException(msg)
110 # pylint: disable=not-an-iterable
111 # (after the above is-None test, self.todos _should_ be iterable!)
112 return [t for t in self.todos if t.calendarize]