From bd32647c67dd9a4979a39efaf31bfe63c82c72d0 Mon Sep 17 00:00:00 2001
From: Christian Heller 
Date: Sun, 17 Dec 2023 03:22:27 +0100
Subject: [PATCH] Improve accounting scripts.
---
 ledger.py |   2 -
 todo.py   | 125 ++++++++++++++++++++++++++++++++++++++----------------
 2 files changed, 88 insertions(+), 39 deletions(-)
diff --git a/ledger.py b/ledger.py
index 9ff0d3e..22e36a3 100755
--- a/ledger.py
+++ b/ledger.py
@@ -359,7 +359,6 @@ class LedgerDB(PlomDB):
         self.write_text_to_db(text)
 
     def insert_at_date(self, lines, date):
-        print("DEBUG insert_at_date")
         start_at = 0 
         if len(self.bookings) > 0:
             if date >= self.bookings[-1].date_string:
@@ -375,7 +374,6 @@ class LedgerDB(PlomDB):
         return self.write_lines_in_total_lines_at(self.real_lines, start_at, lines)
 
     def update(self, start, end, lines, date):
-        print("DEBUG update")
         total_lines = self.real_lines[:start] + self.real_lines[end:]
         n_original_lines = end - start
         start_at = len(total_lines)
diff --git a/todo.py b/todo.py
index 69cb8f5..6cade91 100644
--- a/todo.py
+++ b/todo.py
@@ -61,6 +61,7 @@ to: todo 
 task 
  
 
 
-task choose? done? weight day tags comment task choose? done? weight edit? day tags comment 
 ]  tags: {% for tag in t.tags | sort %}{{ tag }}  {% endfor %}edit {% for tag in db.selected_day.todos[uuid].day_tags | sort %}{{ tag }}  {% endfor %} 
+{{ db.selected_day.todos[uuid].comment|e }} 
+{% else %}
+ 
 {% endif %}
 {% endfor %}
@@ -237,16 +263,16 @@ class Task:
     def tags(self, tags):
         self._set_with_history(self.tags_history, set(tags))
 
-    @property
-    def tags_joined(self):
-        return ';'.join(sorted(list(self.tags)))
+    # @property
+    # def tags_joined(self):
+    #     return ';'.join(sorted(list(self.tags)))
 
-    @tags_joined.setter
-    def tags_joined(self, tags_string):
-        tags = set()
-        for tag in [tag.strip() for tag in tags_string.split(';') if tag.strip() != '']:
-            tags.add(tag)
-        self.tags = tags
+    # @tags_joined.setter
+    # def tags_joined(self, tags_string):
+    #     tags = set()
+    #     for tag in [tag.strip() for tag in tags_string.split(';') if tag.strip() != '']:
+    #         tags.add(tag)
+    #     self.tags = tags
 
     def to_dict(self):
         return {
@@ -348,16 +374,16 @@ class Todo:
     def title(self):
         return self.task.title_at(self.day.date)
 
-    @property
-    def day_tags_joined(self):
-        return ';'.join(sorted(list(self.day_tags)))
+    # @property
+    # def day_tags_joined(self):
+    #     return ';'.join(sorted(list(self.day_tags)))
 
-    @day_tags_joined.setter
-    def day_tags_joined(self, tags_string):
-        tags = set()
-        for tag in [tag.strip() for tag in tags_string.split(';') if tag.strip() != '']:
-            tags.add(tag)
-        self.day_tags = tags
+    # @day_tags_joined.setter
+    # def day_tags_joined(self, tags_string):
+    #     tags = set()
+    #     for tag in [tag.strip() for tag in tags_string.split(';') if tag.strip() != '']:
+    #         tags.add(tag)
+    #     self.day_tags = tags
 
     @property
     def tags(self):
@@ -485,25 +511,39 @@ class TodoDB(PlomDB):
         todo = self.days[selected_date].todos[task_uuid]
         return Template(form_header_tmpl + todo_tmpl + form_footer).render(db=self, todo=todo, action=self.prefix+'/todo')
 
-    def update_todo(self, task_uuid, date, day_weight, done, comment, day_tags_joined):
+    def update_todo_mini(self, task_uuid, date, day_weight, done):
         if task_uuid in self.days[date].todos.keys():
             todo = self.days[date].todos[task_uuid]
         else:
             todo = self.days[date].add_todo(task_uuid)
         todo.day_weight = float(day_weight) if len(day_weight) > 0 else None
         todo.done = done
+        return todo
+
+    def update_todo(self, task_uuid, date, day_weight, done, comment, day_tags_joined, day_tags_checked):
+        todo = self.update_todo_mini(task_uuid, date, day_weight, done)
         todo.comment = comment
-        todo.day_tags_joined = day_tags_joined
+        day_tags = set()
+        for tag in [tag.strip() for tag in day_tags_joined.split(';') if tag.strip() != '']:
+            day_tags.add(tag)
+        for tag in day_tags_checked:
+            day_tags.add(tag)
+        todo.day_tags = day_tags
 
     def show_task(self, id_):
         task = self.tasks[id_] if id_ else self.add_task()
         return Template(form_header_tmpl + task_tmpl + form_footer).render(db=self, task=task, action=self.prefix+'/task')
 
-    def update_task(self, id_, title, default_weight, tags_joined):
+    def update_task(self, id_, title, default_weight, tags_joined, tags_checked):
         task = self.tasks[id_] if id_ in self.tasks.keys() else self.add_task(id_)
         task.title = title
         task.default_weight = float(default_weight) if len(default_weight) > 0 else None
-        task.tags_joined = tags_joined
+        tags = set()
+        for tag in [tag.strip() for tag in tags_joined.split(';') if tag.strip() != '']:
+            tags.add(tag)
+        for tag in tags_checked:
+            tags.add(tag)
+        task.tags = tags
 
     def show_tasks(self):
         return Template(form_header_tmpl + tag_filters_tmpl + tasks_tmpl + form_footer).render(db=self, action=self.prefix+'/tasks')
@@ -545,12 +585,22 @@ class TodoHandler(PlomHandler):
         elif parsed_url.path == app_config['prefix'] + '/todo':
             task_uuid = postvars['task_uuid'][0]
             date = postvars['date'][0]
-            db.update_todo(task_uuid, date, postvars['day_weight'][0], 'done' in postvars.keys(), postvars['comment'][0], postvars['day_tags'][0])
+            tags_checked = []
+            prefix = 'day_tag_'
+            for k in postvars.keys():
+                if k.startswith(prefix):
+                    tags_checked += [k[len(prefix):]]
+            db.update_todo(task_uuid, date, postvars['day_weight'][0], 'done' in postvars.keys(), postvars['comment'][0], postvars['day_tags_joined'][0], tags_checked)
             homepage = f'{app_config["prefix"]}/todo?task={task_uuid}&date={date}'
 
         elif parsed_url.path == app_config['prefix'] + '/task':
             id_ = postvars['id'][0]
-            db.update_task(id_, postvars['title'][0], postvars['default_weight'][0], postvars['tags'][0])
+            tags_checked = []
+            prefix = 'tag_'
+            for k in postvars.keys():
+                if k.startswith(prefix):
+                    tags_checked += [k[len(prefix):]]
+            db.update_task(id_, postvars['title'][0], postvars['default_weight'][0], postvars['tags_joined'][0], tags_checked)
             homepage = f'{app_config["prefix"]}/task?id={id_}'
 
         elif parsed_url.path in {app_config['prefix'] + '/tasks', app_config['prefix'] + '/day'}:
@@ -592,7 +642,7 @@ class TodoHandler(PlomHandler):
                         for i, uuid in enumerate(postvars['t_uuid']):
                             if uuid in postvars['choose']:
                                 done = 'done' in postvars and uuid in postvars['done']
-                                db.update_todo(uuid, db.selected_date, postvars['day_weight'][i], done, postvars['todo_comment'][i], postvars['day_tags'][i])
+                                db.update_todo_mini(uuid, db.selected_date, postvars['day_weight'][i], done)
                 if 'day_comment' in postvars.keys():
                     db.selected_day.comment = postvars['day_comment'][0]
                 data += [('selected_date', db.selected_date)]
@@ -628,12 +678,13 @@ class TodoHandler(PlomHandler):
                 t_filter_and = None
             cookie_db['t_and'] = t_filter_and
             t_filter_not = params.get('t_not', None)
-            if t_filter_not is None and 't_not' in cookie_db.keys():
-                t_filter_not = cookie_db['t_not']
+            if t_filter_not is None:
+                if 't_not' in cookie_db.keys():
+                    t_filter_not = cookie_db['t_not']
+                else:
+                    t_filter_not = ['deleted']
             elif t_filter_not == ['-']:
                 t_filter_not = None
-            else:
-                t_filter_not = ['deleted']
             cookie_db['t_not'] = t_filter_not
         if parsed_url.path == app_config['prefix'] + '/day':
             hide_unchosen_params = params.get('hide_unchosen', [])
-- 
2.30.2