home · contact · privacy
Simplify Todo steps tree calculation/display.
[plomtask] / plomtask / http.py
index adac957f5a1090267fb7807875eabcec2c8c37d3..a1f85fdd6519dabefa9acc795d43d8720c5024af 100644 (file)
@@ -1,5 +1,5 @@
 """Web server stuff."""
-from typing import Any, NamedTuple
+from typing import Any
 from http.server import BaseHTTPRequestHandler
 from http.server import HTTPServer
 from urllib.parse import urlparse, parse_qs
@@ -118,29 +118,25 @@ class TaskHandler(BaseHTTPRequestHandler):
 
     def do_GET_day(self) -> dict[str, object]:
         """Show single Day of ?date=."""
-
-        class ConditionListing(NamedTuple):
-            """Listing of Condition augmented with its enablers, disablers."""
-            condition: Condition
-            enablers: list[Todo]
-            disablers: list[Todo]
-
         date = self.params.get_str('date', todays_date())
-        top_todos = [t for t in Todo.by_date(self.conn, date) if not t.parents]
-        todo_trees = [t.get_undone_steps_tree() for t in top_todos]
-        done_trees = []
-        for t in top_todos:
-            done_trees += t.get_done_steps_tree()
-        condition_listings: list[ConditionListing] = []
-        for cond in Condition.all(self.conn):
-            enablers = Todo.enablers_for_at(self.conn, cond, date)
-            disablers = Todo.disablers_for_at(self.conn, cond, date)
-            condition_listings += [ConditionListing(cond, enablers, disablers)]
+        todays_todos = Todo.by_date(self.conn, date)
+        conditions_present = []
+        enablers_for = {}
+        for todo in todays_todos:
+            for condition in todo.conditions:
+                if condition not in conditions_present:
+                    conditions_present += [condition]
+                    enablers_for[condition.id_] = [p for p in
+                                                   Process.all(self.conn)
+                                                   if condition in p.enables]
+        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),
-                'todo_trees': todo_trees,
-                'done_trees': done_trees,
-                'processes': Process.all(self.conn),
-                'condition_listings': condition_listings}
+                'top_nodes': top_nodes,
+                'enablers_for': enablers_for,
+                'conditions_present': conditions_present,
+                'processes': Process.all(self.conn)}
 
     def do_GET_todo(self) -> dict[str, object]:
         """Show single Todo of ?id=."""