home · contact · privacy
Replace ProcessChildren with more flexible ProcessStep infrastructure.
[plomtask] / plomtask / http.py
index f368232acd33dcc268ca296e69e0d34748f6fa2a..54450e4f89d61f4c085736e2dce6948d678471e5 100644 (file)
@@ -70,6 +70,13 @@ class PostvarsParser:
             msg = f'cannot int form field value: {val}'
             raise BadFormatException(msg) from e
 
+    def get_int_or_none(self, key: str) -> int | None:
+        """Retrieve int value of key from self.postvars, or None."""
+        if key not in self.postvars or \
+                0 == len(''.join(list(self.postvars[key]))):
+            return None
+        return self.get_int(key)
+
     def get_float(self, key: str) -> float:
         """Retrieve float value of key from self.postvars."""
         val = self.get_str(key)
@@ -138,7 +145,7 @@ class TaskHandler(BaseHTTPRequestHandler):
         id_ = params.get_int_or_none('id')
         process = Process.by_id(conn, id_, create=True)
         return self.server.jinja.get_template('process.html').render(
-                process=process, children=process.get_descendants(conn),
+                process=process, steps=process.get_steps(conn),
                 candidates=Process.all(conn))
 
     def do_GET_processes(self, conn: DatabaseConnection,
@@ -183,8 +190,21 @@ class TaskHandler(BaseHTTPRequestHandler):
         process.title.set(form_data.get_str('title'))
         process.description.set(form_data.get_str('description'))
         process.effort.set(form_data.get_float('effort'))
-        process.child_ids = form_data.get_all_int('children')
-        process.save(conn)
+        process.save_without_steps(conn)
+        assert process.id_ is not None  # for mypy
+        process.explicit_steps = []
+        for step_id in form_data.get_all_int('steps'):
+            for step_process_id in\
+                    form_data.get_all_int(f'new_step_to_{step_id}'):
+                process.add_step(conn, None, step_process_id, step_id)
+            if step_id not in form_data.get_all_int('keep_step'):
+                continue
+            step_process_id = form_data.get_int(f'step_{step_id}_process_id')
+            parent_id = form_data.get_int_or_none(f'step_{step_id}_parent_id')
+            process.add_step(conn, step_id, step_process_id, parent_id)
+        for step_process_id in form_data.get_all_int('new_top_step'):
+            process.add_step(conn, None, step_process_id, None)
+        process.fix_steps(conn)
 
     def _init_handling(self) -> tuple[DatabaseConnection, str, ParamsParser]:
         conn = DatabaseConnection(self.server.db)