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.db import DatabaseConnection, BaseModel
7 from plomtask.todos import Todo
8 from plomtask.dating import (DATE_FORMAT, valid_date)
11 class Day(BaseModel[str]):
12 """Individual days defined by their dates."""
15 can_create_by_id = True
17 def __init__(self, date: str, comment: str = '') -> None:
18 id_ = valid_date(date)
20 self.datetime = datetime.strptime(self.date, DATE_FORMAT)
21 self.comment = comment
22 self.todos: list[Todo] = []
24 def __lt__(self, other: Day) -> bool:
25 return self.date < other.date
28 def as_dict(self) -> dict[str, object]:
29 """Return self as (json.dumps-coompatible) dict."""
31 d['todos'] = [t.as_dict for t in self.todos]
35 def from_table_row(cls, db_conn: DatabaseConnection, row: Row | list[Any]
37 """Make from DB row, with linked Todos."""
38 day = super().from_table_row(db_conn, row)
39 assert isinstance(day.id_, str)
40 day.todos = Todo.by_date(db_conn, day.id_)
44 def by_id(cls, db_conn: DatabaseConnection, id_: str | None) -> Day:
45 """Extend BaseModel.by_id checking for new/lost .todos."""
46 day = super().by_id(db_conn, id_)
47 assert day.id_ is not None
48 if day.id_ in Todo.days_to_update:
49 Todo.days_to_update.remove(day.id_)
50 day.todos = Todo.by_date(db_conn, day.id_)
54 def by_date_range_filled(cls, db_conn: DatabaseConnection,
55 start: str, end: str) -> list[Day]:
56 """Return days existing and non-existing between dates start/end."""
57 ret = cls.by_date_range_with_limits(db_conn, (start, end), 'id')
58 days, start_date, end_date = ret
59 return cls.with_filled_gaps(days, start_date, end_date)
62 def with_filled_gaps(cls, days: list[Day], start_date: str, end_date: str
64 """In days, fill with (un-saved) Days gaps between start/end_date."""
65 if start_date > end_date:
68 if start_date not in [d.date for d in days]:
69 days[:] = [Day(start_date)] + days
70 if end_date not in [d.date for d in days]:
71 days += [Day(end_date)]
74 for i, day in enumerate(days):
77 while day.next_date != days[i+1].date:
78 day = Day(day.next_date)
80 days[:] = gapless_days
84 def date(self) -> str:
85 """Return self.id_ under the assumption it's a date string."""
86 assert isinstance(self.id_, str)
90 def first_of_month(self) -> bool:
91 """Return what month self.date is part of."""
92 assert isinstance(self.id_, str)
93 return self.id_[-2:] == '01'
96 def month_name(self) -> str:
97 """Return what month self.date is part of."""
98 return self.datetime.strftime('%B')
101 def weekday(self) -> str:
102 """Return what weekday matches self.date."""
103 return self.datetime.strftime('%A')
106 def prev_date(self) -> str:
107 """Return date preceding date of this Day."""
108 prev_datetime = self.datetime - timedelta(days=1)
109 return prev_datetime.strftime(DATE_FORMAT)
112 def next_date(self) -> str:
113 """Return date succeeding date of this Day."""
114 next_datetime = self.datetime + timedelta(days=1)
115 return next_datetime.strftime(DATE_FORMAT)
118 def calendarized_todos(self) -> list[Todo]:
119 """Return only those of self.todos that have .calendarize set."""
120 return [t for t in self.todos if t.calendarize]
123 def total_effort(self) -> float:
124 """"Sum all .performed_effort of self.todos."""
126 for todo in self.todos:
127 total_effort += todo.performed_effort