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 # pylint: disable=protected-access
36 # (since on ._todo we're only meddling within cls)
37 day = super().from_table_row(db_conn, row)
38 assert isinstance(day.id_, str)
39 day._todos = Todo.by_date(db_conn, day.id_)
44 db_conn: DatabaseConnection, id_: str | None,
46 init_empty_todo_list: bool = False
48 """Extend BaseModel.by_id with init_empty_todo_list flag."""
49 # pylint: disable=protected-access
50 # (since on ._todo we're only meddling within cls)
51 day = super().by_id(db_conn, id_, create)
52 if init_empty_todo_list and day._todos is None:
57 def by_date_range_filled(cls, db_conn: DatabaseConnection,
58 start: str, end: str) -> list[Day]:
59 """Return days existing and non-existing between dates start/end."""
60 ret = cls.by_date_range_with_limits(db_conn, (start, end), 'id')
61 days, start_date, end_date = ret
62 return cls.with_filled_gaps(days, start_date, end_date)
65 def with_filled_gaps(cls, days: list[Day], start_date: str, end_date: str
67 """In days, fill with (un-saved) Days gaps between start/end_date."""
68 if start_date > end_date:
71 if start_date not in [d.date for d in days]:
72 days[:] = [Day(start_date, init_empty_todo_list=True)] + days
73 if end_date not in [d.date for d in days]:
74 days += [Day(end_date, init_empty_todo_list=True)]
77 for i, day in enumerate(days):
80 while day.next_date != days[i+1].date:
81 day = Day(day.next_date, init_empty_todo_list=True)
83 days[:] = gapless_days
87 def date(self) -> str:
88 """Return self.id_ under the assumption it's a date string."""
89 assert isinstance(self.id_, str)
93 def first_of_month(self) -> bool:
94 """Return what month self.date is part of."""
95 assert isinstance(self.id_, str)
96 return self.id_[-2:] == '01'
99 def month_name(self) -> str:
100 """Return what month self.date is part of."""
101 return self.datetime.strftime('%B')
104 def weekday(self) -> str:
105 """Return what weekday matches self.date."""
106 return self.datetime.strftime('%A')
109 def prev_date(self) -> str:
110 """Return date preceding date of this Day."""
111 prev_datetime = self.datetime - timedelta(days=1)
112 return prev_datetime.strftime(DATE_FORMAT)
115 def next_date(self) -> str:
116 """Return date succeeding date of this Day."""
117 next_datetime = self.datetime + timedelta(days=1)
118 return next_datetime.strftime(DATE_FORMAT)
121 def todos(self) -> list[Todo]:
122 """Return self.todos if initialized, else raise Exception."""
123 if self._todos is None:
124 msg = 'Trying to return from un-initialized Day.todos.'
125 raise HandledException(msg)
126 return list(self._todos)
129 def calendarized_todos(self) -> list[Todo]:
130 """Return only those of self.todos that have .calendarize set."""
131 return [t for t in self.todos if t.calendarize]
134 def total_effort(self) -> float:
135 """"Sum all .performed_effort of self.todos."""
137 for todo in self.todos:
138 total_effort += todo.performed_effort