+ def do_GET_process(self, conn: DatabaseConnection,
+ params: ParamsParser) -> str:
+ """Show process of ?id=."""
+ id_ = params.get_int_or_none('id')
+ return self.server.jinja.get_template('process.html').render(
+ process=Process.by_id(conn, id_, create=True))
+
+ def do_GET_processes(self, conn: DatabaseConnection,
+ _: ParamsParser) -> str:
+ """Show all Processes."""
+ return self.server.jinja.get_template('processes.html').render(
+ processes=Process.all(conn))
+
+ def do_POST(self) -> None:
+ """Handle any POST request."""
+ try:
+ conn, site, params = self._init_handling()
+ length = int(self.headers['content-length'])
+ postvars = parse_qs(self.rfile.read(length).decode(),
+ keep_blank_values=True)
+ form_data = PostvarsParser(postvars)
+ if site in ('day', 'process'):
+ getattr(self, f'do_POST_{site}')(conn, params, form_data)
+ conn.commit()
+ conn.close()
+ self._redirect('/')
+ except HandledException as error:
+ self._send_msg(error, code=error.http_code)
+
+ def do_POST_day(self, conn: DatabaseConnection, params: ParamsParser,
+ form_data: PostvarsParser) -> None:
+ """Update or insert Day of date and fields defined in postvars."""
+ date = params.get_str('date')
+ day = Day.by_date(conn, date, create=True)
+ day.comment = form_data.get_str('comment')
+ day.save(conn)
+
+ def do_POST_process(self, conn: DatabaseConnection, params: ParamsParser,
+ form_data: PostvarsParser) -> None:
+ """Update or insert Process of id_ and fields defined in postvars."""
+ id_ = params.get_int_or_none('id')
+ process = Process.by_id(conn, id_, create=True)
+ 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.save(conn)
+
+ def _init_handling(self) -> tuple[DatabaseConnection, str, ParamsParser]:
+ conn = DatabaseConnection(self.server.db)
+ parsed_url = urlparse(self.path)
+ site = path_split(parsed_url.path)[1]
+ params = ParamsParser(parse_qs(parsed_url.query))
+ return conn, site, params
+
+ def _redirect(self, target: str) -> None:
+ self.send_response(302)
+ self.send_header('Location', target)
+ self.end_headers()
+
+ def _send_html(self, html: str, code: int = 200) -> None: