From 23bf47fbbc14ccaf36fc5183720b413eea892ddf Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Thu, 6 Jun 2024 04:39:12 +0200
Subject: [PATCH] Allow (de-)selection of owners in Process edit view.

---
 plomtask/http.py       |  3 ++-
 plomtask/processes.py  | 21 +++++++++++++++++++++
 templates/process.html | 12 ++++--------
 tests/todos.py         | 10 ++++++----
 4 files changed, 33 insertions(+), 13 deletions(-)

diff --git a/plomtask/http.py b/plomtask/http.py
index 2d5db82..9bb96f9 100644
--- a/plomtask/http.py
+++ b/plomtask/http.py
@@ -244,7 +244,7 @@ class TaskHandler(BaseHTTPRequestHandler):
                 '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),
+                'process_candidates': Process.all(self.conn),
                 'condition_candidates': Condition.all(self.conn)}
 
     def do_GET_process_titles(self) -> dict[str, object]:
@@ -415,6 +415,7 @@ class TaskHandler(BaseHTTPRequestHandler):
         process.set_step_suppressions(self.conn,
                                       self.form_data.get_all_int('suppresses'))
         process.save(self.conn)
+        process.set_owners(self.conn, self.form_data.get_all_int('step_of'))
         if new_process_title:
             title_b64_encoded = b64encode(new_process_title.encode()).decode()
             return f'/process?title_b64={title_b64_encoded}'
diff --git a/plomtask/processes.py b/plomtask/processes.py
index 6222046..23da7c1 100644
--- a/plomtask/processes.py
+++ b/plomtask/processes.py
@@ -163,6 +163,27 @@ class Process(BaseModel[int], ConditionsRelations):
             walk_steps(step)
             self.explicit_steps += [step]
 
+    def set_owners(self, db_conn: DatabaseConnection,
+                   owner_ids: list[int]) -> None:
+        """Re-set owners to those identified in owner_ids."""
+        owners_old = self.used_as_step_by(db_conn)
+        losers = [o for o in owners_old if o.id_ not in owner_ids]
+        owners_old_ids = [o.id_ for o in owners_old]
+        winners = [Process.by_id(db_conn, id_) for id_ in owner_ids
+                   if id_ not in owners_old_ids]
+        steps_to_remove = []
+        for loser in losers:
+            steps_to_remove += [s for s in loser.explicit_steps
+                                if s.step_process_id == self.id_]
+        for step in steps_to_remove:
+            step.remove(db_conn)
+        for winner in winners:
+            assert isinstance(winner.id_, int)
+            assert isinstance(self.id_, int)
+            new_step = ProcessStep(None, winner.id_, self.id_, None)
+            new_explicit_steps = winner.explicit_steps + [new_step]
+            winner.set_steps(db_conn, new_explicit_steps)
+
     def save(self, db_conn: DatabaseConnection) -> None:
         """Add (or re-write) self and connected items to DB."""
         super().save(db_conn)
diff --git a/templates/process.html b/templates/process.html
index f073a57..8239dc2 100644
--- a/templates/process.html
+++ b/templates/process.html
@@ -24,7 +24,7 @@
 </td>
 <td>
 {% if step_node.is_explicit %}
-add sub-step: <input name="new_step_to_{{step_id}}" list="step_candidates" autocomplete="off" />
+add sub-step: <input name="new_step_to_{{step_id}}" list="process_candidates" autocomplete="off" />
 {% elif not step_node.seen %}
 <input type="checkbox" name="suppresses" value="{{step_id}}" {% if step_node.is_suppressed %}checked{% endif %}> suppress
 {% endif %}
@@ -92,17 +92,13 @@ add sub-step: <input name="new_step_to_{{step_id}}" list="step_candidates" autoc
 {{ step_with_steps(step_id, step_node, 0) }}
 {% endfor %}
 </table>
-add: <input name="new_top_step" list="step_candidates" autocomplete="off" />
+add: <input name="new_top_step" list="process_candidates" autocomplete="off" />
 </td>
 </tr>
 
 <tr>
 <th>step of</th>
-<td>
-{% for owner in owners %}
-<a href="process?id={{owner.id_}}">{{owner.title.newest|e}}</a><br />
-{% endfor %}
-</td>
+<td>{{ macros.simple_checkbox_table("step_of", owners, "process", "process_candidates") }}</td>
 </tr>
 
 <tr>
@@ -117,5 +113,5 @@ add: <input name="new_top_step" list="step_candidates" autocomplete="off" />
 </form>
 
 {{ macros.datalist_of_titles("condition_candidates", condition_candidates) }}
-{{ macros.datalist_of_titles("step_candidates", step_candidates) }}
+{{ macros.datalist_of_titles("process_candidates", process_candidates) }}
 {% endblock %}
diff --git a/tests/todos.py b/tests/todos.py
index c45171a..b28ebd8 100644
--- a/tests/todos.py
+++ b/tests/todos.py
@@ -362,10 +362,12 @@ class TestsWithServer(TestCaseWithServer):
 
         def check_nesting_adoption(process_id: int, date: str,
                                    new_top_steps: list[int]) -> None:
-            form_data = self.post_process()
-            form_data = self.post_process(process_id,
-                                          form_data |
-                                          {'new_top_step': new_top_steps})
+            form_data = {'title': '', 'description': '', 'effort': 1,
+                         'step_of': [2]}
+            form_data = self.post_process(1, form_data)
+            form_data['new_top_step'] = new_top_steps
+            form_data['step_of'] = []
+            form_data = self.post_process(process_id, form_data)
             form_data = {'day_comment': '', 'new_todo': [process_id]}
             self.check_post(form_data, f'/day?date={date}', 302)
             day_todos = Todo.by_date(self.db_conn, date)
-- 
2.30.2