+ def do_GET_conditions(self) -> dict[str, object]:
+ """Show all Conditions."""
+ return {'conditions': Condition.all(self.conn)}
+
+ def do_GET_condition(self) -> dict[str, object]:
+ """Show Condition of ?id=."""
+ id_ = self.params.get_int_or_none('id')
+ return {'condition': Condition.by_id(self.conn, id_, create=True)}
+
+ def do_GET_process(self) -> dict[str, object]:
+ """Show process of ?id=."""
+ id_ = self.params.get_int_or_none('id')
+ process = Process.by_id(self.conn, id_, create=True)
+ return {'process': process,
+ 'steps': process.get_steps(self.conn),
+ 'owners': process.used_as_step_by(self.conn),
+ 'process_candidates': Process.all(self.conn),
+ 'condition_candidates': Condition.all(self.conn)}
+
+ def do_GET_processes(self) -> dict[str, object]:
+ """Show all Processes."""
+ return {'processes': Process.all(self.conn)}
+
+ def do_POST(self) -> None:
+ """Handle any POST request."""
+ # pylint: disable=attribute-defined-outside-init
+ try:
+ self._init_handling()
+ length = int(self.headers['content-length'])
+ postvars = parse_qs(self.rfile.read(length).decode(),
+ keep_blank_values=True, strict_parsing=True)
+ self.form_data = InputsParser(postvars)
+ if self.site in ('day', 'process', 'todo', 'condition'):
+ getattr(self, f'do_POST_{self.site}')()
+ self.conn.commit()
+ else:
+ msg = f'Page not known as POST target: /{self.site}'
+ raise NotFoundException(msg)
+ self._redirect('/')
+ except HandledException as error:
+ self._send_msg(error, code=error.http_code)
+ finally:
+ self.conn.close()
+
+ def do_POST_day(self) -> None:
+ """Update or insert Day of date and Todos mapped to it."""
+ date = self.params.get_str('date')
+ day = Day.by_id(self.conn, date, create=True)
+ day.comment = self.form_data.get_str('comment')
+ day.save(self.conn)
+ process_id = self.form_data.get_int_or_none('new_todo')
+ if process_id is not None:
+ process = Process.by_id(self.conn, process_id)
+ todo = Todo(None, process, False, day.date)
+ todo.save(self.conn)
+
+ def do_POST_todo(self) -> None:
+ """Update Todo and its children."""
+ id_ = self.params.get_int('id')
+ todo = Todo.by_id(self.conn, id_)
+ child_id = self.form_data.get_int_or_none('adopt')
+ if child_id is not None:
+ child = Todo.by_id(self.conn, child_id)
+ todo.add_child(child)
+ todo.set_conditions(self.conn, self.form_data.get_all_int('condition'))
+ todo.set_enables(self.conn, self.form_data.get_all_int('enables'))
+ todo.set_disables(self.conn, self.form_data.get_all_int('disables'))
+ todo.is_done = len(self.form_data.get_all_str('done')) > 0
+ todo.save(self.conn)
+ for condition in todo.enables:
+ condition.save(self.conn)
+ for condition in todo.disables:
+ condition.save(self.conn)
+
+ def do_POST_process(self) -> None:
+ """Update or insert Process of ?id= and fields defined in postvars."""
+ id_ = self.params.get_int_or_none('id')
+ process = Process.by_id(self.conn, id_, create=True)
+ process.title.set(self.form_data.get_str('title'))
+ process.description.set(self.form_data.get_str('description'))
+ process.effort.set(self.form_data.get_float('effort'))
+ process.set_conditions(self.conn,
+ self.form_data.get_all_int('condition'))
+ process.set_enables(self.conn, self.form_data.get_all_int('enables'))
+ process.set_disables(self.conn, self.form_data.get_all_int('disables'))
+ process.save_core(self.conn)
+ assert process.id_ is not None # for mypy
+ process.explicit_steps = []
+ steps: list[tuple[int | None, int, int | None]] = []
+ for step_id in self.form_data.get_all_int('steps'):
+ for step_process_id in self.form_data.get_all_int(
+ f'new_step_to_{step_id}'):
+ steps += [(None, step_process_id, step_id)]
+ if step_id not in self.form_data.get_all_int('keep_step'):
+ continue
+ step_process_id = self.form_data.get_int(
+ f'step_{step_id}_process_id')
+ parent_id = self.form_data.get_int_or_none(
+ f'step_{step_id}_parent_id')
+ steps += [(step_id, step_process_id, parent_id)]
+ for step_process_id in self.form_data.get_all_int('new_top_step'):
+ steps += [(None, step_process_id, None)]
+ process.set_steps(self.conn, steps)
+ process.save(self.conn)
+
+ def do_POST_condition(self) -> None:
+ """Update/insert Condition of ?id= and fields defined in postvars."""
+ id_ = self.params.get_int_or_none('id')
+ condition = Condition.by_id(self.conn, id_, create=True)
+ condition.title.set(self.form_data.get_str('title'))
+ condition.description.set(self.form_data.get_str('description'))
+ condition.save(self.conn)
+
+ def _init_handling(self) -> None:
+ # pylint: disable=attribute-defined-outside-init
+ self.conn = DatabaseConnection(self.server.db)
+ parsed_url = urlparse(self.path)
+ self.site = path_split(parsed_url.path)[1]
+ params = parse_qs(parsed_url.query, strict_parsing=True)
+ self.params = InputsParser(params, False)
+
+ def _redirect(self, target: str) -> None:
+ self.send_response(302)
+ self.send_header('Location', target)
+ self.end_headers()