home · contact · privacy
Add Todos view, filterable and sortable by process, date, comment, etc.
[plomtask] / plomtask / http.py
index dbc78ebaeb0b17aa3ff5173c40c6c96036255261..f6b17a076c6fa92455983b1198351cc486c79906 100644 (file)
@@ -152,6 +152,41 @@ class TaskHandler(BaseHTTPRequestHandler):
                 'todo_candidates': Todo.by_date(self.conn, todo.date),
                 'condition_candidates': Condition.all(self.conn)}
 
+    def do_GET_todos(self) -> dict[str, object]:
+        """Show Todos from ?start= to ?end=, of ?process=, ?comment= pattern"""
+        sort_by = self.params.get_str('sort_by')
+        start = self.params.get_str('start')
+        end = self.params.get_str('end')
+        process_id = self.params.get_int_or_none('process_id')
+        comment_pattern = self.params.get_str('comment_pattern')
+        todos = []
+        for t in Todo.matching(self.conn, comment_pattern):
+            # pylint: disable=too-many-boolean-expressions
+            if (start and t.date < start)\
+                    or (end and t.date > end)\
+                    or (process_id and t.process.id_ != process_id):
+                continue
+            todos += [t]
+        if sort_by == 'doneness':
+            todos.sort(key=lambda t: t.is_done)
+        elif sort_by == '-doneness':
+            todos.sort(key=lambda t: t.is_done, reverse=True)
+        elif sort_by == 'process':
+            todos.sort(key=lambda t: t.process.title.newest)
+        elif sort_by == '-process':
+            todos.sort(key=lambda t: t.process.title.newest, reverse=True)
+        elif sort_by == 'comment':
+            todos.sort(key=lambda t: t.comment)
+        elif sort_by == '-comment':
+            todos.sort(key=lambda t: t.comment, reverse=True)
+        elif sort_by == '-date':
+            todos.sort(key=lambda t: t.date, reverse=True)
+        else:
+            todos.sort(key=lambda c: c.title.newest)
+        return {'start': start, 'end': end, 'process_id': process_id,
+                'comment_pattern': comment_pattern, 'todos': todos,
+                'all_processes': Process.all(self.conn), 'sort_by': sort_by}
+
     def do_GET_conditions(self) -> dict[str, object]:
         """Show all Conditions."""
         pattern = self.params.get_str('pattern')
@@ -193,6 +228,7 @@ class TaskHandler(BaseHTTPRequestHandler):
         return {'process': process,
                 'steps': process.get_steps(self.conn),
                 'owners': process.used_as_step_by(self.conn),
+                'n_todos': len(Todo.by_process_id(self.conn, process.id_)),
                 'step_candidates': Process.all(self.conn),
                 'condition_candidates': Condition.all(self.conn)}