home · contact · privacy
Add Process.children and improve Params/Postvars parsing and testing.
[plomtask] / plomtask / http.py
index cd3e4459dbe96f6ad0a65cba6ddf45a3162f0f3f..3710595d6c51378426c4575656d14c641dc125ef 100644 (file)
@@ -32,19 +32,20 @@ class ParamsParser:
 
     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:
@@ -55,9 +56,19 @@ 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."""
@@ -68,6 +79,21 @@ class PostvarsParser:
             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."""
@@ -109,8 +135,10 @@ class TaskHandler(BaseHTTPRequestHandler):
                        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:
@@ -124,7 +152,7 @@ class TaskHandler(BaseHTTPRequestHandler):
             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)
@@ -153,13 +181,14 @@ class TaskHandler(BaseHTTPRequestHandler):
         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: