From: Christian Heller Date: Sun, 21 Jul 2024 06:43:34 +0000 (+0200) Subject: Refactor POST /todo code. X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/calendar?a=commitdiff_plain;h=e7896508dc7dd7708df44f8c6e49e5026c33cb11;p=plomtask Refactor POST /todo code. --- diff --git a/plomtask/http.py b/plomtask/http.py index d7af591..43e4550 100644 --- a/plomtask/http.py +++ b/plomtask/http.py @@ -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) diff --git a/plomtask/todos.py b/plomtask/todos.py index cb72640..03c454c 100644 --- a/plomtask/todos.py +++ b/plomtask/todos.py @@ -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: