From: Christian Heller Date: Sun, 21 Jul 2024 07:10:53 +0000 (+0200) Subject: Improve readability of POST /todo code. X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/%7B%7B%20web_path%20%7D%7D/decks?a=commitdiff_plain;h=f682d99bcb4e09017aeb1204fd12229f830248e9;p=plomtask Improve readability of POST /todo code. --- diff --git a/plomtask/http.py b/plomtask/http.py index 43e4550..15c17db 100644 --- a/plomtask/http.py +++ b/plomtask/http.py @@ -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') diff --git a/tests/misc.py b/tests/misc.py index 1713432..ce5f87a 100644 --- a/tests/misc.py +++ b/tests/misc.py @@ -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):