+ conn: DatabaseConnection
+ _site: str
+ _form_data: InputsParser
+ _params: InputsParser
+
+ def _send_page(self,
+ ctx: dict[str, Any],
+ tmpl_name: str,
+ code: int = 200
+ ) -> None:
+ """Send ctx as proper HTTP response."""
+ body = self.server.render(ctx, tmpl_name)
+ self.send_response(code)
+ for header_tuple in self.server.headers:
+ self.send_header(*header_tuple)
+ self.end_headers()
+ self.wfile.write(bytes(body, 'utf-8'))
+
+ @staticmethod
+ def _request_wrapper(http_method: str, not_found_msg: str
+ ) -> Callable[..., Callable[[TaskHandler], None]]:
+ def decorator(f: Callable[..., str | None]
+ ) -> Callable[[TaskHandler], None]:
+ def wrapper(self: TaskHandler) -> None:
+ # pylint: disable=protected-access
+ # (because pylint here fails to detect the use of wrapper as a
+ # method to self with respective access privileges)
+ try:
+ 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)
+ handler_name = f'do_{http_method}_{self._site}'
+ if hasattr(self, handler_name):
+ handler = getattr(self, handler_name)
+ redir_target = f(self, handler)
+ if redir_target:
+ self.send_response(302)
+ self.send_header('Location', redir_target)
+ self.end_headers()
+ else:
+ msg = f'{not_found_msg}: {self._site}'
+ raise NotFoundException(msg)
+ except HandledException as error:
+ for cls in (Day, Todo, Condition, Process, ProcessStep):
+ assert hasattr(cls, 'empty_cache')
+ cls.empty_cache()
+ ctx = {'msg': error}
+ self._send_page(ctx, 'msg', error.http_code)
+ finally:
+ self.conn.close()
+ return wrapper
+ return decorator
+
+ @_request_wrapper('GET', 'Unknown page')
+ def do_GET(self, handler: Callable[[], str | dict[str, object]]
+ ) -> str | None:
+ """Render page with result of handler, or redirect if result is str."""
+ tmpl_name = f'{self._site}'
+ ctx_or_redir_target = handler()
+ if isinstance(ctx_or_redir_target, str):
+ return ctx_or_redir_target
+ self._send_page(ctx_or_redir_target, tmpl_name)
+ return None
+
+ @_request_wrapper('POST', 'Unknown POST target')
+ def do_POST(self, handler: Callable[[], str]) -> str:
+ """Handle POST with handler, prepare redirection to result."""
+ 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)
+ redir_target = handler()
+ self.conn.commit()
+ return redir_target
+
+ # GET handlers
+
+ @staticmethod
+ def _get_item(target_class: Any
+ ) -> Callable[..., Callable[[TaskHandler],
+ dict[str, object]]]:
+ def decorator(f: Callable[..., dict[str, object]]
+ ) -> Callable[[TaskHandler], dict[str, object]]:
+ def wrapper(self: TaskHandler) -> dict[str, object]:
+ # pylint: disable=protected-access
+ # (because pylint here fails to detect the use of wrapper as a
+ # method to self with respective access privileges)
+ id_ = self._params.get_int_or_none('id')
+ if target_class.can_create_by_id:
+ item = target_class.by_id_or_create(self.conn, id_)
+ else:
+ item = target_class.by_id(self.conn, id_)
+ return f(self, item)
+ return wrapper
+ return decorator
+
+ def do_GET_(self) -> str:
+ """Return redirect target on GET /."""
+ return '/day'
+
+ def _do_GET_calendar(self) -> dict[str, object]:
+ """Show Days from ?start= to ?end=.
+
+ Both .do_GET_calendar and .do_GET_calendar_txt refer to this to do the
+ same, the only difference being the HTML template they are rendered to,
+ which .do_GET selects from their method name.
+ """
+ start = self._params.get_str('start')
+ end = self._params.get_str('end')
+ if not end:
+ end = date_in_n_days(366)