From fa927e56c36522f1148635c28b1133fd370cff80 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Fri, 9 Aug 2024 16:51:35 +0200
Subject: [PATCH] Minor improvements to query parameter handling/defaulting.

---
 plomtask/http.py | 18 ++++++++++++++----
 tests/days.py    |  3 ++-
 tests/utils.py   | 10 +++++-----
 3 files changed, 21 insertions(+), 10 deletions(-)

diff --git a/plomtask/http.py b/plomtask/http.py
index ef9438a..c2e1f5d 100644
--- a/plomtask/http.py
+++ b/plomtask/http.py
@@ -327,6 +327,7 @@ class TaskHandler(BaseHTTPRequestHandler):
         start = self._params.get_str_or_fail('start', '')
         end = self._params.get_str_or_fail('end', '')
         end = end if end != '' else date_in_n_days(366)
+        #
         days, start, end = Day.by_date_range_with_limits(self._conn,
                                                          (start, end), 'id')
         days = Day.with_filled_gaps(days, start, end)
@@ -344,8 +345,9 @@ class TaskHandler(BaseHTTPRequestHandler):
     def do_GET_day(self) -> dict[str, object]:
         """Show single Day of ?date=."""
         date = self._params.get_str_or_fail('date', date_in_n_days(0))
+        make_type = self._params.get_str_or_fail('make_type', 'full')
+        #
         day = Day.by_id_or_create(self._conn, date)
-        make_type = self._params.get_str_or_fail('make_type', '')
         conditions_present = []
         enablers_for = {}
         disablers_for = {}
@@ -444,11 +446,12 @@ class TaskHandler(BaseHTTPRequestHandler):
 
     def do_GET_todos(self) -> dict[str, object]:
         """Show Todos from ?start= to ?end=, of ?process=, ?comment= pattern"""
-        sort_by = self._params.get_str_or_fail('sort_by', '')
+        sort_by = self._params.get_str_or_fail('sort_by', 'title')
         start = self._params.get_str_or_fail('start', '')
         end = self._params.get_str_or_fail('end', '')
         process_id = self._params.get_int_or_none('process_id')
         comment_pattern = self._params.get_str_or_fail('comment_pattern', '')
+        #
         todos = []
         ret = Todo.by_date_range_with_limits(self._conn, (start, end))
         todos_by_date_range, start, end = ret
@@ -463,7 +466,8 @@ class TaskHandler(BaseHTTPRequestHandler):
     def do_GET_conditions(self) -> dict[str, object]:
         """Show all Conditions."""
         pattern = self._params.get_str_or_fail('pattern', '')
-        sort_by = self._params.get_str_or_fail('sort_by', '')
+        sort_by = self._params.get_str_or_fail('sort_by', 'title')
+        #
         conditions = Condition.matching(self._conn, pattern)
         sort_by = Condition.sort_by(conditions, sort_by)
         return {'conditions': conditions,
@@ -496,6 +500,7 @@ class TaskHandler(BaseHTTPRequestHandler):
         owner_ids = self._params.get_all_int('step_to')
         owned_ids = self._params.get_all_int('has_step')
         title_64 = self._params.get_str('title_b64')
+        #
         if title_64:
             try:
                 title = b64decode(title_64.encode()).decode()
@@ -536,7 +541,8 @@ class TaskHandler(BaseHTTPRequestHandler):
     def do_GET_processes(self) -> dict[str, object]:
         """Show all Processes."""
         pattern = self._params.get_str_or_fail('pattern', '')
-        sort_by = self._params.get_str_or_fail('sort_by', '')
+        sort_by = self._params.get_str_or_fail('sort_by', 'title')
+        #
         processes = Process.matching(self._conn, pattern)
         sort_by = Process.sort_by(processes, sort_by)
         return {'processes': processes, 'sort_by': sort_by, 'pattern': pattern}
@@ -592,6 +598,7 @@ class TaskHandler(BaseHTTPRequestHandler):
         comments = self._form.get_all_str('comment')
         efforts = self._form.get_all_floats_or_nones('effort')
         done_todos = self._form.get_all_int('done')
+        #
         for _ in [id_ for id_ in done_todos if id_ not in old_todos]:
             raise BadFormatException('"done" field refers to unknown Todo')
         is_done = [t_id in done_todos for t_id in old_todos]
@@ -638,6 +645,7 @@ class TaskHandler(BaseHTTPRequestHandler):
         cond_rels = [self._form.get_all_int(name) for name in
                      ['conditions', 'blockers', 'enables', 'disables']]
         effort_or_not = self._form.get_str('effort')
+        #
         if effort_or_not is not None:
             if effort_or_not == '':
                 to_update['effort'] = None
@@ -719,6 +727,7 @@ class TaskHandler(BaseHTTPRequestHandler):
         for step_id in kept_steps:
             name = f'new_step_to_{step_id}'
             new_steps_to[step_id] = self._form.get_all_int(name)
+        #
         for k, v in versioned.items():
             getattr(process, k).set(v)
         process.set_condition_relations(self._conn, *cond_rels)
@@ -784,6 +793,7 @@ class TaskHandler(BaseHTTPRequestHandler):
         title = self._form.get_str_or_fail('title')
         description = self._form.get_str_or_fail('description')
         is_active = self._form.get_bool_or_none('is_active')
+        #
         if is_active is not None:
             condition.is_active = is_active
         condition.title.set(title)
diff --git a/tests/days.py b/tests/days.py
index 0c6ee72..aac150b 100644
--- a/tests/days.py
+++ b/tests/days.py
@@ -123,7 +123,7 @@ class ExpectedGetCalendar(Expected):
 
 class ExpectedGetDay(Expected):
     """Builder of expectations for GET /day."""
-    _default_dict = {'make_type': ''}
+    _default_dict = {'make_type': 'full'}
     _on_empty_make_temp = ('Day', 'day_as_dict')
 
     def __init__(self, date: str, *args: Any, **kwargs: Any) -> None:
@@ -163,6 +163,7 @@ class TestsWithServer(TestCaseWithServer):
     def test_basic_GET_day(self) -> None:
         """Test basic (no Processes/Conditions/Todos) GET /day basics."""
         # check illegal date parameters
+        self.check_get('/day?date=', 400)
         self.check_get('/day?date=foo', 400)
         self.check_get('/day?date=2024-02-30', 400)
         # check undefined day
diff --git a/tests/utils.py b/tests/utils.py
index e04fba6..71da9fb 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -1015,9 +1015,9 @@ class TestCaseWithServer(TestCaseWithDB):
         try:
             self.assertEqual(cmp, retrieved)
         except AssertionError as e:
-            print('EXPECTED:')
-            pprint(cmp)
-            print('RETRIEVED:')
-            pprint(retrieved)
-            walk_diffs('', cmp, retrieved)
+            # print('EXPECTED:')
+            # pprint(cmp)
+            # print('RETRIEVED:')
+            # pprint(retrieved)
+            # walk_diffs('', cmp, retrieved)
             raise e
-- 
2.30.2