From 30aef71506f7d6215b04cddaba8fddba1788f883 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Thu, 6 Jun 2024 05:39:39 +0200
Subject: [PATCH] Also allow creation of new Processes as owner of current
 Process, with respective back-link.

---
 plomtask/http.py       | 32 +++++++++++++++++++++++---------
 templates/process.html |  2 +-
 2 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/plomtask/http.py b/plomtask/http.py
index 9680778..2b630a2 100644
--- a/plomtask/http.py
+++ b/plomtask/http.py
@@ -243,9 +243,12 @@ class TaskHandler(BaseHTTPRequestHandler):
         owners = process.used_as_step_by(self.conn)
         for step_id in self.params.get_all_int('step_to'):
             owners += [Process.by_id(self.conn, step_id)]
+        preset_top_step = None
+        for process_id in self.params.get_all_int('has_step'):
+            preset_top_step = process_id
         return {'process': process, 'is_new': process.id_ is None,
-                'steps': process.get_steps(self.conn),
-                'owners': owners,
+                'preset_top_step': preset_top_step,
+                'steps': process.get_steps(self.conn), 'owners': owners,
                 'n_todos': len(Todo.by_process_id(self.conn, process.id_)),
                 'process_candidates': Process.all(self.conn),
                 'condition_candidates': Condition.all(self.conn)}
@@ -370,6 +373,7 @@ class TaskHandler(BaseHTTPRequestHandler):
 
     def do_POST_process(self) -> str:
         """Update or insert Process of ?id= and fields defined in postvars."""
+        # pylint: disable=too-many-branches
         id_ = self.params.get_int_or_none('id')
         for _ in self.form_data.get_all_str('delete'):
             process = Process.by_id(self.conn, id_)
@@ -405,25 +409,35 @@ class TaskHandler(BaseHTTPRequestHandler):
                     f'new_step_to_{step_id}'):
                 steps += [ProcessStep(None, process.id_, step_process_id,
                                       step_id)]
-        new_process_title = None
+        new_step_title = None
         for step_identifier in self.form_data.get_all_str('new_top_step'):
             try:
                 step_process_id = int(step_identifier)
                 steps += [ProcessStep(None, process.id_, step_process_id,
                                       None)]
             except ValueError:
-                new_process_title = step_identifier
+                new_step_title = step_identifier
         process.uncache()
         process.set_steps(self.conn, steps)
         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()
+        owners_to_set = []
+        new_owner_title = None
+        for owner_identifier in self.form_data.get_all_str('step_of'):
+            try:
+                owners_to_set += [int(owner_identifier)]
+            except ValueError:
+                new_owner_title = owner_identifier
+        process.set_owners(self.conn, owners_to_set)
+        params = f'id={process.id_}'
+        if new_step_title:
+            title_b64_encoded = b64encode(new_step_title.encode()).decode()
             params = f'step_to={process.id_}&title_b64={title_b64_encoded}'
-            return f'/process?{params}'
-        return f'/process?id={process.id_}'
+        elif new_owner_title:
+            title_b64_encoded = b64encode(new_owner_title.encode()).decode()
+            params = f'has_step={process.id_}&title_b64={title_b64_encoded}'
+        return f'/process?{params}'
 
     def do_POST_condition(self) -> str:
         """Update/insert Condition of ?id= and fields defined in postvars."""
diff --git a/templates/process.html b/templates/process.html
index 8239dc2..1be3daf 100644
--- a/templates/process.html
+++ b/templates/process.html
@@ -92,7 +92,7 @@ add sub-step: <input name="new_step_to_{{step_id}}" list="process_candidates" au
 {{ step_with_steps(step_id, step_node, 0) }}
 {% endfor %}
 </table>
-add: <input name="new_top_step" list="process_candidates" autocomplete="off" />
+add: <input name="new_top_step" list="process_candidates" autocomplete="off" value="{{preset_top_step or ''}}" />
 </td>
 </tr>
 
-- 
2.30.2