home · contact · privacy
Improve readability of POST /todo code.
authorChristian Heller <c.heller@plomlompom.de>
Sun, 21 Jul 2024 07:10:53 +0000 (09:10 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Sun, 21 Jul 2024 07:10:53 +0000 (09:10 +0200)
plomtask/http.py
tests/misc.py

index 43e45508e2535acbcd99c45c8d5168eb2e82b624..15c17db62278b1e8d1338d9c4d312bed86ebf385 100644 (file)
@@ -94,6 +94,10 @@ class InputsParser:
             msg = f'cannot float form field value for key {key}: {val}'
             raise BadFormatException(msg) from e
 
+    def get_bool(self, key: str) -> bool:
+        """Return if key occurs and maps to a boolean Truth."""
+        return bool(self.get_all_str(key))
+
     def get_all_str(self, key: str) -> list[str]:
         """Retrieve list of string values at key."""
         if key not in self.inputs.keys():
@@ -631,16 +635,16 @@ class TaskHandler(BaseHTTPRequestHandler):
         processes_to_make_full = self._form_data.get_all_int('make_full')
         processes_to_make_empty = self._form_data.get_all_int('make_empty')
         step_fillers = self._form_data.get_all_str('step_filler')
-        d: dict[str, str | float | None] = {}
+        to_update = {
+            'is_done': self._form_data.get_bool('done'),
+            'calendarize': self._form_data.get_bool('calendarize'),
+            'comment': self._form_data.get_str('comment', ignore_strict=True)}
         try:
-            d['effort'] = self._form_data.get_float_or_none('effort')
+            to_update['effort'] = self._form_data.get_float_or_none('effort')
         except NotFoundException:
             pass
         cond_rel_id_lists = [self._form_data.get_all_int(name) for name in
                              ['conditions', 'blockers', 'enables', 'disables']]
-        d['is_done'] = len(self._form_data.get_all_str('done')) > 0
-        d['calendarize'] = len(self._form_data.get_all_str('calendarize')) > 0
-        d['comment'] = self._form_data.get_str('comment', ignore_strict=True)
         for filler in [f for f in step_fillers if f != 'ignore']:
             target_id: int
             to_int = filler
@@ -678,7 +682,7 @@ class TaskHandler(BaseHTTPRequestHandler):
             made = Todo.create_with_children(self.conn, process_id, todo.date)
             todo.add_child(made)
         todo.set_condition_relations(self.conn, *cond_rel_id_lists)
-        todo.update_attrs(**d)
+        todo.update_attrs(**to_update)
         # todo.save() may destroy Todo if .effort < 0, so retrieve .id_ early
         url = f'/todo?id={todo.id_}'
         todo.save(self.conn)
@@ -711,7 +715,7 @@ class TaskHandler(BaseHTTPRequestHandler):
         blockers = self._form_data.get_all_int('blockers')
         enables = self._form_data.get_all_int('enables')
         disables = self._form_data.get_all_int('disables')
-        calendarize = self._form_data.get_all_str('calendarize') != []
+        calendarize = self._form_data.get_bool('calendarize')
         suppresses = self._form_data.get_all_int('suppresses')
         step_of = self._form_data.get_all_str('step_of')
         keep_steps = self._form_data.get_all_int('keep_step')
index 1713432576e779d72d7f47a35f120ce05e4d47ed..ce5f87a4234f1ded58d26676ffc62ccda8c583f1 100644 (file)
@@ -145,6 +145,34 @@ class TestsSansServer(TestCase):
             parser = InputsParser({'foo': []}, strictness)
             self.assertEqual(None, parser.get_float_or_none('foo'))
 
+    def test_InputsParser_get_bool(self) -> None:
+        """Test InputsParser.get_all_str on strict and non-strict."""
+        for strictness in (False, True):
+            parser = InputsParser({}, strictness)
+            self.assertEqual(False, parser.get_bool('foo'))
+            parser = InputsParser({'val': ['true']}, strictness)
+            self.assertEqual(False, parser.get_bool('foo'))
+            parser = InputsParser({'val': ['True']}, strictness)
+            self.assertEqual(False, parser.get_bool('foo'))
+            parser = InputsParser({'val': ['1']}, strictness)
+            self.assertEqual(False, parser.get_bool('foo'))
+            parser = InputsParser({'val': ['foo']}, strictness)
+            self.assertEqual(False, parser.get_bool('foo'))
+            parser = InputsParser({'foo': []}, strictness)
+            self.assertEqual(False, parser.get_bool('foo'))
+            parser = InputsParser({'foo': ['None']}, strictness)
+            self.assertEqual(True, parser.get_bool('foo'))
+            parser = InputsParser({'foo': ['0']}, strictness)
+            self.assertEqual(True, parser.get_bool('foo'))
+            parser = InputsParser({'foo': ['']}, strictness)
+            self.assertEqual(True, parser.get_bool('foo'))
+            parser = InputsParser({'foo': ['bar']}, strictness)
+            self.assertEqual(True, parser.get_bool('foo'))
+            parser = InputsParser({'foo': ['bar', 'baz']}, strictness)
+            self.assertEqual(True, parser.get_bool('foo'))
+            parser = InputsParser({'foo': ['False']}, strictness)
+            self.assertEqual(True, parser.get_bool('foo'))
+
     def test_InputsParser_get_all_str(self) -> None:
         """Test InputsParser.get_all_str on strict and non-strict."""
         for strictness in (False, True):