From e06d3b744f88b2976399b0cbe08b526bb7e88907 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Wed, 15 May 2024 08:32:13 +0200
Subject: [PATCH] Add POSTing of Todo.comment in Day view.

---
 plomtask/http.py   |  7 +++++--
 templates/day.html |  8 ++++----
 tests/days.py      |  4 ++--
 tests/todos.py     | 24 ++++++++++++------------
 4 files changed, 23 insertions(+), 20 deletions(-)

diff --git a/plomtask/http.py b/plomtask/http.py
index cf3288a..15f8376 100644
--- a/plomtask/http.py
+++ b/plomtask/http.py
@@ -194,7 +194,7 @@ class TaskHandler(BaseHTTPRequestHandler):
         """Update or insert Day of date and Todos mapped to it."""
         date = self.params.get_str('date')
         day = Day.by_id(self.conn, date, create=True)
-        day.comment = self.form_data.get_str('comment')
+        day.comment = self.form_data.get_str('day_comment')
         day.save(self.conn)
         new_todos = []
         for process_id in self.form_data.get_all_int('new_todo'):
@@ -212,9 +212,12 @@ class TaskHandler(BaseHTTPRequestHandler):
                 todo.make_missing_children(self.conn)
                 todo.save(self.conn)
         done_ids = self.form_data.get_all_int('done')
-        for todo_id in self.form_data.get_all_int('todo_id'):
+        comments = self.form_data.get_all_str('comment')
+        for i, todo_id in enumerate(self.form_data.get_all_int('todo_id')):
             todo = Todo.by_id(self.conn, todo_id)
             todo.is_done = todo_id in done_ids
+            if len(comments) > 0:
+                todo.comment = comments[i]
             todo.save(self.conn)
             for condition in todo.enables:
                 condition.save(self.conn)
diff --git a/templates/day.html b/templates/day.html
index cd683da..b816f35 100644
--- a/templates/day.html
+++ b/templates/day.html
@@ -50,7 +50,7 @@ td.todo_line {
 <td class="cond_line_{{(conditions_present|length - loop.index) % 3}} {% if condition in node.todo.enables or condition in node.todo.disables %}min_width{% endif %}">{% if condition in node.todo.enables %}+{% elif condition in node.todo.disables %}!{% endif %}</td>
 {% endfor %}
 
-<td><input name="comment" /></td>
+<td><input name="comment" value="{{node.todo.comment|e}}" /></td>
 
 </tr>
 {% endif %}
@@ -71,7 +71,7 @@ td.todo_line {
 <tr>
 {% if path|length > 0 and not path[-1].todo.is_done %}
 <td>
-({% for path_node in path %}<a href="todo?id={{path_node.todo.id_}}">{{path_node.todo.process.title.newest|e}}</a> &lt;- {% endfor %})
+({% for path_node in path %}<a href="todo?id={{path_node.todo.id_}}">{{path_node.todo.process.title.newest|e}}</a>  &lt;- {% endfor %})
 </td>
 </tr>
 
@@ -82,7 +82,7 @@ td.todo_line {
 <td>
 {% for i in range(indent) %}&nbsp; {% endfor %} +
 {% endif %}
-{% if node.seen %}({% endif %}<a href="todo?id={{node.todo.id_}}">{{node.todo.process.title.newest|e}}</a>{% if node.seen %}){% endif %}
+{% if node.seen %}({% endif %}<a href="todo?id={{node.todo.id_}}">{{node.todo.process.title.newest|e}}</a> {% if node.todo.comment|length > 0 %}[{{node.todo.comment|e}}]{% endif %}{% if node.seen %}){% endif %}
 </td>
 </tr>
 
@@ -103,7 +103,7 @@ td.todo_line {
 <a href="day?date={{day.prev_date}}">prev</a> | <a href="day?date={{day.next_date}}">next</a>
 </p>
 <form action="day?date={{day.date}}" method="POST">
-comment: <input name="comment" value="{{day.comment|e}}" />
+comment: <input name="day_comment" value="{{day.comment|e}}" />
 <input type="submit" value="OK" /><br />
 add todo: <input name="new_todo" list="processes" autocomplete="off" />
 <datalist id="processes">
diff --git a/tests/days.py b/tests/days.py
index 1f0e55d..41bcf9c 100644
--- a/tests/days.py
+++ b/tests/days.py
@@ -96,7 +96,7 @@ class TestsWithDB(TestCaseWithDB):
 
     def test_Day_singularity(self) -> None:
         """Test pointers made for single object keep pointing to it."""
-        self.check_singularity('comment', 'boo')
+        self.check_singularity('day_comment', 'boo')
 
 
 class TestsWithServer(TestCaseWithServer):
@@ -115,7 +115,7 @@ class TestsWithServer(TestCaseWithServer):
 
     def test_do_POST_day(self) -> None:
         """Test POST /day."""
-        form_data = {'comment': ''}
+        form_data = {'day_comment': ''}
         self.check_post(form_data, '/day', 400)
         self.check_post(form_data, '/day?date=foo', 400)
         self.check_post(form_data, '/day?date=2024-01-01', 302)
diff --git a/tests/todos.py b/tests/todos.py
index 0633547..419d3db 100644
--- a/tests/todos.py
+++ b/tests/todos.py
@@ -237,7 +237,7 @@ class TestsWithServer(TestCaseWithServer):
         self.post_process(2)
         proc = Process.by_id(self.db_conn, 1)
         proc2 = Process.by_id(self.db_conn, 2)
-        form_data = {'comment': ''}
+        form_data = {'day_comment': ''}
         self.check_post(form_data, '/day?date=2024-01-01', 302)
         self.assertEqual(Todo.by_date(self.db_conn, '2024-01-01'), [])
         form_data['new_todo'] = str(proc.id_)
@@ -264,7 +264,7 @@ class TestsWithServer(TestCaseWithServer):
             return Todo.by_date(self.db_conn, '2024-01-01')[0]
         # test minimum
         self.post_process()
-        self.check_post({'comment': '', 'new_todo': 1},
+        self.check_post({'day_comment': '', 'new_todo': 1},
                         '/day?date=2024-01-01', 302)
         # test posting to bad URLs
         self.check_post({}, '/todo=', 404)
@@ -287,7 +287,7 @@ class TestsWithServer(TestCaseWithServer):
         self.check_post({'adopt': 1}, '/todo?id=1', 400)
         self.check_post({'adopt': 2}, '/todo?id=1', 404)
         # test posting second todo of same process
-        self.check_post({'comment': '', 'new_todo': 1},
+        self.check_post({'day_comment': '', 'new_todo': 1},
                         '/day?date=2024-01-01', 302)
         # test todo 1 adopting todo 2
         todo1 = post_and_reload({'adopt': 2})
@@ -313,7 +313,7 @@ class TestsWithServer(TestCaseWithServer):
         """Test Todos posted to Day view may adopt existing Todos."""
         form_data = self.post_process()
         form_data = self.post_process(2, form_data | {'new_top_step': 1})
-        form_data = {'comment': '', 'new_todo': 1}
+        form_data = {'day_comment': '', 'new_todo': 1}
         self.check_post(form_data, '/day?date=2024-01-01', 302)
         form_data['new_todo'] = 2
         self.check_post(form_data, '/day?date=2024-01-01', 302)
@@ -328,7 +328,7 @@ class TestsWithServer(TestCaseWithServer):
         """Test multiple Todos can be posted to Day view."""
         form_data = self.post_process()
         form_data = self.post_process(2)
-        form_data = {'comment': '', 'new_todo': [1, 2]}
+        form_data = {'day_comment': '', 'new_todo': [1, 2]}
         self.check_post(form_data, '/day?date=2024-01-01', 302)
         todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
         todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
@@ -339,7 +339,7 @@ class TestsWithServer(TestCaseWithServer):
         """Test multiple Todos can be posted to Day view w. inner adoption."""
         form_data = self.post_process()
         form_data = self.post_process(2, form_data | {'new_top_step': 1})
-        form_data = {'comment': '', 'new_todo': [1, 2]}
+        form_data = {'day_comment': '', 'new_todo': [1, 2]}
         self.check_post(form_data, '/day?date=2024-01-01', 302)
         todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
         todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
@@ -348,7 +348,7 @@ class TestsWithServer(TestCaseWithServer):
         self.assertEqual(todo2.children, [todo1])
         self.assertEqual(todo2.parents, [])
         # check process ID order does not affect end result
-        form_data = {'comment': '', 'new_todo': [2, 1]}
+        form_data = {'day_comment': '', 'new_todo': [2, 1]}
         self.check_post(form_data, '/day?date=2024-01-02', 302)
         todo1 = Todo.by_date(self.db_conn, '2024-01-02')[1]
         todo2 = Todo.by_date(self.db_conn, '2024-01-02')[0]
@@ -358,22 +358,22 @@ class TestsWithServer(TestCaseWithServer):
         self.assertEqual(todo2.parents, [])
 
     def test_do_POST_day_todo_doneness(self) -> None:
-        """Test multiple Todos can be posted to Day view."""
+        """Test Todo doneness can be posted to Day view."""
         form_data = self.post_process()
-        form_data = {'comment': '', 'new_todo': [1]}
+        form_data = {'day_comment': '', 'new_todo': [1]}
         self.check_post(form_data, '/day?date=2024-01-01', 302)
         todo = Todo.by_date(self.db_conn, '2024-01-01')[0]
-        form_data = {'comment': '', 'todo_id': [1]}
+        form_data = {'day_comment': '', 'todo_id': [1]}
         self.check_post(form_data, '/day?date=2024-01-01', 302)
         self.assertEqual(todo.is_done, False)
-        form_data = {'comment': '', 'todo_id': [1], 'done': [1]}
+        form_data = {'day_comment': '', 'todo_id': [1], 'done': [1]}
         self.check_post(form_data, '/day?date=2024-01-01', 302)
         self.assertEqual(todo.is_done, True)
 
     def test_do_GET_todo(self) -> None:
         """Test GET /todo response codes."""
         self.post_process()
-        form_data = {'comment': '', 'new_todo': 1}
+        form_data = {'day_comment': '', 'new_todo': 1}
         self.check_post(form_data, '/day?date=2024-01-01', 302)
         self.check_get('/todo', 400)
         self.check_get('/todo?id=', 400)
-- 
2.30.2