home · contact · privacy
Refactor POST /todo code.
authorChristian Heller <c.heller@plomlompom.de>
Sun, 21 Jul 2024 06:43:34 +0000 (08:43 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Sun, 21 Jul 2024 06:43:34 +0000 (08:43 +0200)
plomtask/http.py
plomtask/todos.py

index d7af591012ef54362e1cad5af335091d66402b5b..43e45508e2535acbcd99c45c8d5168eb2e82b624 100644 (file)
@@ -627,23 +627,20 @@ class TaskHandler(BaseHTTPRequestHandler):
         """Update Todo and its children."""
         # pylint: disable=too-many-locals
         # pylint: disable=too-many-branches
-        # pylint: disable=too-many-statements
         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')
         step_fillers = self._form_data.get_all_str('step_filler')
-        with_effort_post = True
+        d: dict[str, str | float | None] = {}
         try:
-            effort = self._form_data.get_float_or_none('effort')
+            d['effort'] = self._form_data.get_float_or_none('effort')
         except NotFoundException:
-            with_effort_post = False
-        conditions = self._form_data.get_all_int('conditions')
-        disables = self._form_data.get_all_int('disables')
-        blockers = self._form_data.get_all_int('blockers')
-        enables = self._form_data.get_all_int('enables')
-        is_done = len(self._form_data.get_all_str('done')) > 0
-        calendarize = len(self._form_data.get_all_str('calendarize')) > 0
-        comment = self._form_data.get_str('comment', ignore_strict=True)
+            pass
+        cond_rel_id_lists = [self._form_data.get_all_int(name) for name in
+                             ['conditions', 'blockers', 'enables', 'disables']]
+        d['is_done'] = len(self._form_data.get_all_str('done')) > 0
+        d['calendarize'] = len(self._form_data.get_all_str('calendarize')) > 0
+        d['comment'] = self._form_data.get_str('comment', ignore_strict=True)
         for filler in [f for f in step_fillers if f != 'ignore']:
             target_id: int
             to_int = filler
@@ -680,13 +677,8 @@ class TaskHandler(BaseHTTPRequestHandler):
         for process_id in processes_to_make_full:
             made = Todo.create_with_children(self.conn, process_id, todo.date)
             todo.add_child(made)
-        if with_effort_post:
-            todo.effort = effort
-        todo.set_condition_relations(
-                self.conn, conditions, blockers, enables, disables)
-        todo.is_done = is_done
-        todo.calendarize = calendarize
-        todo.comment = comment
+        todo.set_condition_relations(self.conn, *cond_rel_id_lists)
+        todo.update_attrs(**d)
         # todo.save() may destroy Todo if .effort < 0, so retrieve .id_ early
         url = f'/todo?id={todo.id_}'
         todo.save(self.conn)
index cb72640fb2c088ed317302df42cf5d12a4ff9108..03c454cdb8d75aad81f7c276f22124b9a75f7277 100644 (file)
@@ -346,6 +346,11 @@ class Todo(BaseModel[int], ConditionsRelations):
         self.children.remove(child)
         child.parents.remove(self)
 
+    def update_attrs(self, **kwargs: Any) -> None:
+        """Update self's attributes listed in kwargs."""
+        for k, v in kwargs.items():
+            setattr(self, k, v)
+
     def save(self, db_conn: DatabaseConnection) -> None:
         """On save calls, also check if auto-deletion by effort < 0."""
         if self.effort and self.effort < 0 and self.is_deletable: