home · contact · privacy
Fix todo.py task deselection bugs.
authorChristian Heller <c.heller@plomlompom.de>
Sun, 7 Jan 2024 17:29:59 +0000 (18:29 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Sun, 7 Jan 2024 17:29:59 +0000 (18:29 +0100)
todo.py

diff --git a/todo.py b/todo.py
index 7b8b83e5ddfe34aa42cb9e6b6e3021b3ceadde71..8c911fb1a9247325fcd603a2e65ed6b9269f41d0 100644 (file)
--- a/todo.py
+++ b/todo.py
@@ -3,7 +3,6 @@ import json
 from uuid import uuid4
 from datetime import datetime, timedelta
 from urllib.parse import parse_qs
-from jinja2 import Template
 from jinja2 import Environment as JinjaEnv, FileSystemLoader as JinjaFSLoader 
 from urllib.parse import urlparse
 db_path = '/home/plom/org/todo_new.json'
@@ -160,6 +159,7 @@ class Day:
             if v == self:
                 return k
 
+
 class Todo:
 
     def __init__(self, day, done=False, day_effort=None, comment='', day_tags=None, importance=1.0):
@@ -202,8 +202,8 @@ class Todo:
     def tags(self):
         return self.day_tags | self.task.tags
 
-    def is_empty(self):
-        return self.done or (self.day_effort is not None) or len(self.comment) > 0 or len(self.day_tags) > 0
+    def internals_empty(self):
+        return len(self.comment) == 0 and len(self.day_tags) == 0
 
 
 class TodoDB(PlomDB):
@@ -349,9 +349,9 @@ class TodoDB(PlomDB):
             todo = self.days[date].todos[task_uuid]
         else:
             todo = self.days[date].add_todo(task_uuid)
-        todo.day_effort = float(day_effort) if len(day_effort) > 0 else None
+        todo.day_effort = day_effort
         todo.done = done
-        todo.importance = float(importance)
+        todo.importance = importance
         return todo
 
     def collect_tags(self, tags_joined, tags_checked):
@@ -363,6 +363,8 @@ class TodoDB(PlomDB):
         return tags
 
     def update_todo(self, task_uuid, date, day_effort, done, comment, day_tags_joined, day_tags_checked, importance):
+        day_effort = float(day_effort) if len(day_effort) > 0 else None
+        importance = float(importance)
         todo = self.update_todo_mini(task_uuid, date, day_effort, done, importance)
         todo.comment = comment
         todo.day_tags = self.collect_tags(day_tags_joined, day_tags_checked) 
@@ -448,25 +450,35 @@ class TodoHandler(PlomHandler):
                 db.update_task(id_, postvars['title'][0], postvars['default_effort'][0], postvars['joined_tags'][0], collect_checked('tag_', postvars), collect_checked('link_', postvars))
 
         elif parsed_url.path == app_config['prefix'] + '/day':
+            # always store the two hide params in the URL if possible … TODO: find out if really necessary
             if 'expect_unchosen_done' in postvars.keys():
                 params_to_encode += [('hide_unchosen', int('hide_unchosen' in postvars.keys()))] + [('hide_done', int('hide_done' in postvars.keys()))]
+
             if 'selected_date' in postvars.keys():
-                 db.selected_date = postvars['selected_date'][0]
-                 if 't_uuid' in postvars.keys():
-                     for i, uuid in enumerate(postvars['t_uuid']):
-                         t = db.tasks[uuid]
-                         if uuid in db.selected_day.todos.keys() and ((not 'choose' in postvars) or uuid not in postvars['choose']) and not db.selected_day.todos[uuid].is_empty():
-                             del db.selected_day.todos[uuid]
-                     if 'choose' in postvars.keys():
-                         for i, uuid in enumerate(postvars['t_uuid']):
-                             uuids = postvars['choose']
-                             uuids += postvars['done'] if 'done' in postvars.keys() else []
-                             if uuid in uuids or postvars['day_effort'][i] != '' or (postvars['importance'][i] not in {'1.0', ''}):
-                                 done = 'done' in postvars and uuid in postvars['done']
-                                 db.update_todo_mini(uuid, db.selected_date, postvars['day_effort'][i], done, postvars['importance'][i])
-                 if 'day_comment' in postvars.keys():
-                     db.selected_day.comment = postvars['day_comment'][0]
-                 params_to_encode += [('selected_date', db.selected_date)]
+                db.selected_date = postvars['selected_date'][0]
+                if 'day_comment' in postvars.keys():
+                    db.selected_day.comment = postvars['day_comment'][0]
+                params_to_encode += [('selected_date', db.selected_date)]
+
+                # handle todo list updates via task UUIDs
+                if 't_uuid' in postvars.keys():
+                    for i, uuid in enumerate(postvars['t_uuid']):
+                        task = db.tasks[uuid]
+                        old_todo = None if not uuid in db.selected_day.todos.keys() else db.selected_day.todos[uuid]
+                        selects_as_todo = 'choose' in postvars and uuid in postvars['choose']
+                        too_much_keepworthy_data = ('done' in postvars and uuid in postvars['done']) or postvars['day_effort'][i] != '' or (old_todo and not old_todo.internals_empty())
+                        if old_todo and too_much_keepworthy_data and not selects_as_todo:
+                            raise PlomException('cannot deselect task as todo of preserve-worthy values')
+                        elif old_todo and not selects_as_todo:
+                            del db.selected_day.todos[uuid]
+                        else:
+                            done = ('done' in postvars) and (uuid in postvars['done'])
+                            day_effort_input = postvars['day_effort'][i]
+                            day_effort = float(day_effort_input) if len(day_effort_input) > 0 else None
+                            importance = float(postvars['importance'][i])
+                            if old_todo and old_todo.done == done and old_todo.day_effort == day_effort and old_todo.importance == importance:
+                                continue
+                            db.update_todo_mini(uuid, db.selected_date, day_effort, done, importance)
 
         if 'referer' in postvars.keys() and len(postvars['referer'][0]) > 0:
             homepage = postvars['referer'][0]