home · contact · privacy
Improve accounting scripts.
[misc] / todo.py
diff --git a/todo.py b/todo.py
index 08fb8b82f6e3921ce6d75c3e86428d31c6398902..8c7029cab8eda5683b668857a220d7faa283b4fe 100644 (file)
--- a/todo.py
+++ b/todo.py
@@ -23,7 +23,7 @@ forbidden tags: {% for t_tag in db.t_tags | sort %}
 {% endfor %}
 <table>
 <tr><th colspan=2></th><th>date</th><td colspan=2><input name="today_date" value="{{ db.today_date }}" size=8 /></td><th>archive?</th><td><input type="checkbox" name="archive_today" /></td><td>{{ db.today.todos_sum|round(2) }}</td><th>comment:</th><td><input name="comment" value="{{ db.today.comment|e }}"></td></tr>
-<tr><th>weight</th><th>title</th><th>tags</th><th>today?</th><th>done?</th><th colspan=2>day weight</th></tr>
+<tr><th>default<br />weight</th><th>title</th><th>tags</th><th>today?</th><th>done?</th><th colspan=2>day<br />weight</th></tr>
 {% for uuid, t in db.tasks.items() | sort(attribute='1.title', reverse=True) %}
 {% if t.visible %}
 <input name="t_uuid" value="{{ uuid }}" type="hidden" >
@@ -33,7 +33,7 @@ forbidden tags: {% for t_tag in db.t_tags | sort %}
 <td><input name="t_tags" value="{{ t.tags_joined|e }}" >
 <td><input name="do_today" type="checkbox" value="{{ uuid }}" {% if uuid in db.today.todos.keys() %}checked{% endif %} ></td>
 <td><input name="done" type="checkbox" value="{{ uuid }}" {% if uuid in db.today.todos.keys() and db.today.todos[uuid].done %}checked{% endif %} ></td>
-<td colspan=2><input name="day_weight" type="number" step=0.1 size=5 value="{% if uuid in db.today.todos.keys() and db.today.todos[uuid].day_weight %}{{ db.today.todos[uuid].day_weight }}{% endif %}" ></td>
+<td colspan=2><input name="weight" type="number" step=0.1 size=5 value="{% if uuid in db.today.todos.keys() and db.today.todos[uuid].weight %}{{ db.today.todos[uuid].weight }}{% endif %}" ></td>
 </tr>
 {% endif %}
 {% endfor %}
@@ -99,6 +99,14 @@ class Task:
     def title(self):
         return self._last_of_history(self.title_history, '')
 
+    def title_at(self, queried_date):
+        ret = self.title_history[sorted(self.title_history.keys())[0]] 
+        for date_key, title in self.title_history.items():
+            if date_key > f'{queried_date} 23:59:59':
+                break
+            ret = title
+        return ret
+
     def set_tags(self, tags):
         self._set_with_history(self.tags_history, set(tags))
 
@@ -130,7 +138,7 @@ class Day:
     @property
     def todos_sum(self):
         s = 0
-        for todo in [todo for todo in self.todos.values() if (todo.done or todo.day_weight)]:
+        for todo in [todo for todo in self.todos.values() if todo.done]:
             s += todo.weight
         return s
 
@@ -143,16 +151,16 @@ class Day:
 
 class Todo:
 
-    def __init__(self, done=False, day_weight=None):
+    def __init__(self, done=False, weight=None):
         self.done = done
-        self.day_weight = day_weight
+        self.weight = weight
 
     @classmethod
     def from_dict(cls, d):
-        return cls(d['done'], d['day_weight'])
+        return cls(d['done'], d['weight'])
 
     def to_dict(self):
-        return {'done': self.done, 'day_weight': self.day_weight}
+        return {'done': self.done, 'weight': self.weight}
 
 
 class TodoDB(PlomDB):
@@ -198,6 +206,14 @@ class TodoDB(PlomDB):
     def write(self):
         self.write_text_to_db(json.dumps(self.to_dict()))
 
+    def save_today(self):
+        if self.today_date in self.old_days.keys():
+            raise PlomException('cannot use same date twice')
+        for task_uuid, todo in [(task_uuid, todo) for task_uuid, todo in self.today.todos.items()
+                                if not todo.weight]:
+            todo.weight = self.tasks[task_uuid].default_weight
+        self.old_days[self.today_date] = self.today
+
     def reset_today(self, date=None):
         if date:
             self.today_date = date 
@@ -263,8 +279,8 @@ class TodoHandler(PlomHandler):
                 for i, uuid in enumerate(postvars['t_uuid']):
                     if uuid in postvars['do_today']:
                         done = 'done' in postvars and uuid in postvars['done']
-                        day_weight = float(postvars['day_weight'][i]) if postvars['day_weight'][i] else None
-                        db.today.todos[uuid] = Todo(done, day_weight)
+                        weight = float(postvars['weight'][i]) if postvars['weight'][i] else None
+                        db.today.todos[uuid] = Todo(done, weight)
         db.today_date = postvars['today_date'][0]
         db.today.comment = postvars['comment'][0]
         switch_edited_day = None
@@ -273,9 +289,7 @@ class TodoHandler(PlomHandler):
                 switch_edited_day = date
                 break
         if 'archive_today' in postvars.keys() or switch_edited_day:
-            if db.today_date in db.old_days.keys():
-                raise PlomException('cannot use same date twice')
-            db.old_days[db.today_date] = db.today
+            db.save_today()
             if switch_edited_day:
                 db.reset_today(date) 
             else:
@@ -300,12 +314,9 @@ class TodoHandler(PlomHandler):
         db = TodoDB(t_filter_and, t_filter_not)
         for i in range(10):
             db.tasks[f'new{i}'] = Task()
-        for task_uuid, todo in db.today.todos.items():
-            todo.weight = todo.day_weight if todo.day_weight else db.tasks[task_uuid].default_weight 
         for date, day in db.old_days.items():
             for task_uuid, todo in day.todos.items():
-                todo.title = db.tasks[task_uuid].title
-                todo.weight = todo.day_weight if todo.day_weight else db.tasks[task_uuid].default_weight 
+                todo.title = db.tasks[task_uuid].title_at(date)
         page = Template(tmpl).render(db=db)
         self.send_HTML(page)