def get_str(self, key: str, default: str = '') -> str:
"""Retrieve string value of key from self.params."""
- if key not in self.params:
+ if key not in self.params or 0 == len(self.params[key]):
return default
return self.params[key][0]
def get_int_or_none(self, key: str) -> int | None:
"""Retrieve int value of key from self.params, on empty return None."""
- if key not in self.params or not self.params[key]:
+ if key not in self.params or \
+ 0 == len(''.join(list(self.params[key]))):
return None
- val = self.params[key][0]
+ val_str = self.params[key][0]
try:
- return int(val)
+ return int(val_str)
except ValueError as e:
- raise BadFormatException(f'Bad ?{key}= value: {val}') from e
+ raise BadFormatException(f'Bad ?{key}= value: {val_str}') from e
class PostvarsParser:
def get_str(self, key: str) -> str:
"""Retrieve string value of key from self.postvars."""
- if key not in self.postvars:
- raise BadFormatException(f'missing value for form field: {key}')
- return self.postvars[key][0]
+ all_str = self.get_all_str(key)
+ if 0 == len(all_str):
+ raise BadFormatException(f'missing value for key: {key}')
+ return all_str[0]
+
+ def get_int(self, key: str) -> int:
+ """Retrieve int value of key from self.postvars."""
+ val = self.get_str(key)
+ try:
+ return int(val)
+ except ValueError as e:
+ msg = f'cannot int form field value: {val}'
+ raise BadFormatException(msg) from e
def get_float(self, key: str) -> float:
"""Retrieve float value of key from self.postvars."""
msg = f'cannot float form field value: {val}'
raise BadFormatException(msg) from e
+ def get_all_str(self, key: str) -> list[str]:
+ """Retrieve list of string values at key from self.postvars."""
+ if key not in self.postvars:
+ return []
+ return self.postvars[key]
+
+ def get_all_int(self, key: str) -> list[int]:
+ """Retrieve list of int values at key from self.postvars."""
+ all_str = self.get_all_str(key)
+ try:
+ return [int(s) for s in all_str if len(s) > 0]
+ except ValueError as e:
+ msg = f'cannot int a form field value: {all_str}'
+ raise BadFormatException(msg) from e
+
class TaskHandler(BaseHTTPRequestHandler):
"""Handles single HTTP request."""
params: ParamsParser) -> str:
"""Show process of ?id=."""
id_ = params.get_int_or_none('id')
+ process = Process.by_id(conn, id_, create=True)
return self.server.jinja.get_template('process.html').render(
- process=Process.by_id(conn, id_, create=True))
+ process=process, children=process.children(conn),
+ candidates=Process.all(conn))
def do_GET_processes(self, conn: DatabaseConnection,
_: ParamsParser) -> str:
conn, site, params = self._init_handling()
length = int(self.headers['content-length'])
postvars = parse_qs(self.rfile.read(length).decode(),
- keep_blank_values=True)
+ keep_blank_values=True, strict_parsing=True)
form_data = PostvarsParser(postvars)
if site in ('day', 'process'):
getattr(self, f'do_POST_{site}')(conn, params, form_data)
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.child_ids = form_data.get_all_int('children')
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))
+ params = ParamsParser(parse_qs(parsed_url.query, strict_parsing=True))
return conn, site, params
def _redirect(self, target: str) -> None: