From ff1c4c071834cbf39bab5cc7bac6a9ac8d33df2b Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Mon, 5 Aug 2024 10:01:13 +0200
Subject: [PATCH] Slightly reduce the do_POST_todo code.

---
 plomtask/http.py | 36 ++++++++++++++++--------------------
 1 file changed, 16 insertions(+), 20 deletions(-)

diff --git a/plomtask/http.py b/plomtask/http.py
index b10ce3a..1aa2e49 100644
--- a/plomtask/http.py
+++ b/plomtask/http.py
@@ -636,19 +636,20 @@ class TaskHandler(BaseHTTPRequestHandler):
         # pylint: disable=too-many-locals
         # pylint: disable=too-many-branches
         adopted_child_ids = self._form_data.get_all_int('adopt')
-        processes_to_make_full = self._form_data.get_all_int('make_full')
-        processes_to_make_empty = self._form_data.get_all_int('make_empty')
+        to_make = {'full': self._form_data.get_all_int('make_full'),
+                   'empty': self._form_data.get_all_int('make_empty')}
         step_fillers = self._form_data.get_all_str('step_filler')
         to_update = {
             'is_done': self._form_data.get_bool('done'),
             'calendarize': self._form_data.get_bool('calendarize'),
             'comment': self._form_data.get_str('comment', ignore_strict=True)}
+        cond_rels = [self._form_data.get_all_int(name) for name in
+                     ['conditions', 'blockers', 'enables', 'disables']]
         try:
             to_update['effort'] = self._form_data.get_float_or_none('effort')
         except NotFoundException:
             pass
-        cond_rel_id_lists = [self._form_data.get_all_int(name) for name in
-                             ['conditions', 'blockers', 'enables', 'disables']]
+        todo.set_condition_relations(self.conn, *cond_rels)
         for filler in [f for f in step_fillers if f != 'ignore']:
             target_id: int
             to_int = filler
@@ -661,15 +662,14 @@ class TaskHandler(BaseHTTPRequestHandler):
                 msg = 'bad fill_for target: {filler}'
                 raise BadFormatException(msg) from e
             if filler.startswith('make_empty_'):
-                processes_to_make_empty += [target_id]
+                to_make['empty'] += [target_id]
             elif filler.startswith('make_full_'):
-                processes_to_make_full += [target_id]
+                to_make['full'] += [target_id]
             else:
                 adopted_child_ids += [target_id]
         to_remove = []
         for child in todo.children:
-            assert isinstance(child.id_, int)
-            if child.id_ not in adopted_child_ids:
+            if child.id_ and (child.id_ not in adopted_child_ids):
                 to_remove += [child.id_]
         for id_ in to_remove:
             child = Todo.by_id(self.conn, id_)
@@ -677,19 +677,15 @@ class TaskHandler(BaseHTTPRequestHandler):
         for child_id in adopted_child_ids:
             if child_id not in [c.id_ for c in todo.children]:
                 todo.add_child(Todo.by_id(self.conn, child_id))
-        for process_id in processes_to_make_empty:
-            process = Process.by_id(self.conn, process_id)
-            made = Todo(None, process, False, todo.date)
-            made.save(self.conn)
-            todo.add_child(made)
-        for process_id in processes_to_make_full:
-            process = Process.by_id(self.conn, process_id)
-            made = Todo(None, process, False, todo.date)
-            made.save(self.conn)
-            made.ensure_children(self.conn)
-            todo.add_child(made)
-        todo.set_condition_relations(self.conn, *cond_rel_id_lists)
         todo.update_attrs(**to_update)
+        for approach, proc_ids in to_make.items():
+            for process_id in proc_ids:
+                process = Process.by_id(self.conn, process_id)
+                made = Todo(None, process, False, todo.date)
+                made.save(self.conn)
+                if 'full' == approach:
+                    made.ensure_children(self.conn)
+                todo.add_child(made)
         # todo.save() may destroy Todo if .effort < 0, so retrieve .id_ early
         url = f'/todo?id={todo.id_}'
         todo.save(self.conn)
-- 
2.30.2