home · contact · privacy
Minor TaskHandler re-factorings.
[plomtask] / plomtask / days.py
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)
7
8
9 class Day(BaseModel[str]):
10     """Individual days defined by their dates."""
11     table_name = 'days'
12     to_save = ['comment']
13
14     def __init__(self, date: str, comment: str = '') -> None:
15         id_ = valid_date(date)
16         super().__init__(id_)
17         self.datetime = datetime.strptime(self.date, DATE_FORMAT)
18         self.comment = comment
19         self.calendarized_todos: list[Todo] = []
20
21     def __lt__(self, other: Day) -> bool:
22         return self.date < other.date
23
24     @classmethod
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)
31
32     @classmethod
33     def with_filled_gaps(cls, days: list[Day], start_date: str, end_date: str
34                          ) -> list[Day]:
35         """In days, fill with (un-saved) Days gaps between start/end_date."""
36         if start_date > end_date:
37             return days
38         days.sort()
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)]
43         if len(days) > 1:
44             gapless_days = []
45             for i, day in enumerate(days):
46                 gapless_days += [day]
47                 if i < len(days) - 1:
48                     while day.next_date != days[i+1].date:
49                         day = Day(day.next_date)
50                         gapless_days += [day]
51             days[:] = gapless_days
52         return days
53
54     @property
55     def date(self) -> str:
56         """Return self.id_ under the assumption it's a date string."""
57         assert isinstance(self.id_, str)
58         return self.id_
59
60     @property
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'
65
66     @property
67     def month_name(self) -> str:
68         """Return what month self.date is part of."""
69         return self.datetime.strftime('%B')
70
71     @property
72     def weekday(self) -> str:
73         """Return what weekday matches self.date."""
74         return self.datetime.strftime('%A')
75
76     @property
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)
81
82     @property
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)
87
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)
91                                    if t.calendarize]