From: Christian Heller Date: Fri, 14 Jun 2024 13:53:33 +0000 (+0200) Subject: Re-factor Day.todos code. X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/decks/condition?a=commitdiff_plain;h=344d6c234e50953b36ac9fed2cfce8d4ba64a5b3;p=plomtask Re-factor Day.todos code. --- diff --git a/plomtask/days.py b/plomtask/days.py index 2531a01..155ed03 100644 --- a/plomtask/days.py +++ b/plomtask/days.py @@ -23,7 +23,7 @@ class Day(BaseModel[str]): super().__init__(id_) self.datetime = datetime.strptime(self.date, DATE_FORMAT) self.comment = comment - self.todos: list[Todo] | None = [] if init_empty_todo_list else None + self._todos: list[Todo] | None = [] if init_empty_todo_list else None def __lt__(self, other: Day) -> bool: return self.date < other.date @@ -32,9 +32,25 @@ class Day(BaseModel[str]): def from_table_row(cls, db_conn: DatabaseConnection, row: Row | list[Any] ) -> Day: """Make from DB row, with linked Todos.""" + # pylint: disable=protected-access + # (since on ._todo we're only meddling within cls) day = super().from_table_row(db_conn, row) assert isinstance(day.id_, str) - day.todos = Todo.by_date(db_conn, day.id_) + day._todos = Todo.by_date(db_conn, day.id_) + return day + + @classmethod + def by_id(cls, + db_conn: DatabaseConnection, id_: str | None, + create: bool = False, + init_empty_todo_list: bool = False + ) -> Day: + """Extend BaseModel.by_id with init_empty_todo_list flag.""" + # pylint: disable=protected-access + # (since on ._todo we're only meddling within cls) + day = super().by_id(db_conn, id_, create) + if init_empty_todo_list and day._todos is None: + day._todos = [] return day @classmethod @@ -102,11 +118,22 @@ class Day(BaseModel[str]): return next_datetime.strftime(DATE_FORMAT) @property - def calendarized_todos(self) -> list[Todo]: - """Return only those of self.todos that have .calendarize set.""" - if self.todos is None: + def todos(self) -> list[Todo]: + """Return self.todos if initialized, else raise Exception.""" + if self._todos is None: msg = 'Trying to return from un-initialized Day.todos.' raise HandledException(msg) - # pylint: disable=not-an-iterable - # (after the above is-None test, self.todos _should_ be iterable!) + return list(self._todos) + + @property + def calendarized_todos(self) -> list[Todo]: + """Return only those of self.todos that have .calendarize set.""" return [t for t in self.todos if t.calendarize] + + @property + def total_effort(self) -> float: + """"Sum all .performed_effort of self.todos.""" + total_effort = 0.0 + for todo in self.todos: + total_effort += todo.performed_effort + return total_effort diff --git a/plomtask/http.py b/plomtask/http.py index 91db326..230ed3f 100644 --- a/plomtask/http.py +++ b/plomtask/http.py @@ -205,12 +205,13 @@ class TaskHandler(BaseHTTPRequestHandler): def do_GET_day(self) -> dict[str, object]: """Show single Day of ?date=.""" date = self._params.get_str('date', date_in_n_days(0)) + day = Day.by_id(self.conn, date, create=True, + init_empty_todo_list=True) make_type = self._params.get_str('make_type') - todays_todos = Todo.by_date(self.conn, date) conditions_present = [] enablers_for = {} disablers_for = {} - for todo in todays_todos: + for todo in day.todos: for condition in todo.conditions + todo.blockers: if condition not in conditions_present: conditions_present += [condition] @@ -222,9 +223,8 @@ class TaskHandler(BaseHTTPRequestHandler): if condition in p.disables] seen_todos: set[int] = set() top_nodes = [t.get_step_tree(seen_todos) - for t in todays_todos if not t.parents] - return {'day': Day.by_id(self.conn, date, create=True), - 'total_effort': Todo.total_effort_at_date(self.conn, date), + for t in day.todos if not t.parents] + return {'day': day, 'top_nodes': top_nodes, 'make_type': make_type, 'enablers_for': enablers_for, diff --git a/plomtask/todos.py b/plomtask/todos.py index 008f7a2..fa009b1 100644 --- a/plomtask/todos.py +++ b/plomtask/todos.py @@ -155,16 +155,6 @@ class Todo(BaseModel[int], ConditionsRelations): """Collect all Todos for Day of date.""" return cls.by_date_range(db_conn, (date, date)) - @classmethod - def total_effort_at_date(cls, db_conn: DatabaseConnection, date: str - ) -> float: - """Sum all .performed_effort of Todos at Day of date.""" - total_effort = 0.0 - days_todos = cls.by_date(db_conn, date) - for todo in days_todos: - total_effort += todo.performed_effort - return total_effort - @property def is_doable(self) -> bool: """Decide whether .is_done settable based on children, Conditions.""" diff --git a/templates/calendar.html b/templates/calendar.html index 4e83573..11ac2a8 100644 --- a/templates/calendar.html +++ b/templates/calendar.html @@ -28,6 +28,9 @@ td.day_name { td.today { font-weight: bold; } +span.total_effort { + white-space: pre; +} {% endblock %} @@ -58,7 +61,10 @@ to {% endif %} -{{day.weekday|truncate(2,True,'',0)}} {% if day.date == today %} {% endif %}{{day.date}} {{day.comment|e}} + +{{day.weekday|truncate(2,True,'',0)}} {% if day.date == today %} {% endif %}{{day.date}} +[{{ '{:5.1f}'.format(day.total_effort) }}] +{{day.comment|e}} {% for todo in day.calendarized_todos %} diff --git a/templates/day.html b/templates/day.html index a34f05b..f980cd1 100644 --- a/templates/day.html +++ b/templates/day.html @@ -124,7 +124,7 @@ O  {% block content %} -

{{day.date}} / {{day.weekday}} ({{total_effort|round(1)}})

+

{{day.date}} / {{day.weekday}} ({{day.total_effort|round(1)}})

prev | next