home · contact · privacy
Various small fixes.
[misc] / todo.py
1 from plomlib import PlomDB, run_server, PlomHandler, PlomException
2 import json
3 from uuid import uuid4
4 from datetime import datetime, timedelta
5 from urllib.parse import parse_qs
6 from jinja2 import Environment as JinjaEnv, FileSystemLoader as JinjaFSLoader 
7 from urllib.parse import urlparse
8 from os.path import split as path_split
9 db_path = '/home/plom/org/todo_new.json'
10 server_port = 8082
11 DATE_FORMAT = '%Y-%m-%d'
12 j2env = JinjaEnv(loader=JinjaFSLoader('todo_templates'))
13
14 class Task:
15
16     def __init__(self, db, id_, title_history=None, tags_history=None, default_effort_history=None, links_history=None, comment=''):
17         self.id_ = id_
18         self.db = db
19         self.title_history = title_history if title_history else {}
20         self.tags_history = tags_history if tags_history else {}
21         self.default_effort_history = default_effort_history if default_effort_history else {}
22         self.links_history = links_history if links_history else {}
23         self.comment = comment
24         self.visible = True
25
26     def _set_with_history(self, history, value):
27         keys = sorted(history.keys())
28         if len(history) == 0 or value != history[keys[-1]]:
29             history[str(datetime.now())[:19]] = value
30
31     def _last_of_history(self, history, default):
32         keys = sorted(history.keys())
33         return default if 0 == len(history) else history[keys[-1]]
34
35     @classmethod
36     def from_dict(cls, db, d, id_):
37         t = cls(
38                db,
39                id_,
40                d['title_history'],
41                {k: set(v) for k, v in d['tags_history'].items()},
42                d['default_effort_history'],
43                {k: set(v) for k, v in d['links_history'].items()},
44                d['comment'])
45         return t
46
47     def to_dict(self):
48         return {
49             'title_history': self.title_history,
50             'default_effort_history': self.default_effort_history,
51             'tags_history': {k: list(v) for k,v in self.tags_history.items()},
52             'links_history': {k: list(v) for k,v in self.links_history.items()},
53             'comment': self.comment,
54         }
55
56     @property
57     def default_effort(self):
58         return self._last_of_history(self.default_effort_history, 1)
59
60     @default_effort.setter
61     def default_effort(self, default_effort):
62         self._set_with_history(self.default_effort_history, default_effort)
63
64     def default_effort_at(self, queried_date):
65         ret = self.default_effort_history[sorted(self.default_effort_history.keys())[0]]
66         for date_key, default_effort in self.default_effort_history.items():
67             if date_key > f'{queried_date} 23:59:59':
68                 break
69             ret = default_effort
70         return ret
71
72     # @property
73     # def current_default_effort(self):
74     #     return self.default_effort_at(self.db.selected_date)
75
76     def matches(self, search):
77         if search is None:
78             return False
79         else:
80             return search in self.title or search in self.comment or search in '$'.join(self.tags) or search in self.title
81
82     @property
83     def title(self):
84         return self._last_of_history(self.title_history, '')
85
86     @title.setter
87     def title(self, title):
88         self._set_with_history(self.title_history, title)
89
90     def title_at(self, queried_date):
91         ret = self.title_history[sorted(self.title_history.keys())[0]]
92         for date_key, title in self.title_history.items():
93             if date_key > f'{queried_date} 23:59:59':
94                 break
95             ret = title
96         return ret
97
98     @property
99     def current_title(self):
100         return self.title_at(self.db.selected_date)
101
102     @property
103     def tags(self):
104         return self._last_of_history(self.tags_history, set())
105
106     @tags.setter
107     def tags(self, tags):
108         self._set_with_history(self.tags_history, set(tags))
109
110     @property
111     def links(self):
112         return self._last_of_history(self.links_history, set())
113
114     @links.setter
115     def links(self, links):
116         self._set_with_history(self.links_history, set(links))
117
118     # @property
119     # def id_(self):
120     #     for k, v in self.db.tasks.items():
121     #         if v == self:
122     #             return k
123
124
125 class Day:
126
127     # def __init__(self, db, todos=None, comment='', date=None):
128     def __init__(self, db, date, comment=''):
129         self.date = date 
130         self.db = db
131         self.comment = comment
132         self.archived = True
133         self.todos = {} # legacy
134         self.linked_todos_as_list = []
135         # if todos:
136         #     for id_, todo_dict in todos.items():
137         #         self.add_todo(id_, todo_dict)
138
139     @classmethod
140     def from_dict(cls, db, d, date=None):
141         # todos = {}
142         comment = d['comment'] if 'comment' in d.keys() else ''
143         day = cls(db, date, comment)
144         # if 'todos' in d.keys():
145         #     for uuid, todo_dict in d['todos'].items():
146         #         day.add_todo(uuid, todo_dict)
147         # if 'linked_todos' in d.keys():
148         for id_ in d['linked_todos']:
149             # if id_ in day.linked_todos.keys():
150             #     continue
151             # if id_ is None:
152             #     continue
153             # linked_todo = db.todos[id_]
154             # linked_todo._day = day 
155             # day.linked_todos_as_list += [linked_todo]
156             day.linked_todos_as_list += [db.todos[id_]]
157         return day
158
159     def to_dict(self):
160         d = {'comment': self.comment, 'linked_todos': []}
161         for todo_id in self.linked_todos.keys():
162             d['linked_todos'] += [todo_id]
163         # for task_uuid, todo in self.todos.items():
164         # for id_, todo in self.todos.items():
165         # #     d['todos'][task_uuid] = todo.to_dict()
166         #     new_type_todo_id = f'{self.date}_{task_uuid}'
167         #     if not new_type_todo_id in d['linked_todos']:
168         #         # d['linked_todos'] += [todo.id_] 
169         #         d['linked_todos'] += [new_type_todo_id] 
170         return d
171
172     # def add_todo(self, task_id, dict_source=None):
173     #     new_type_todo_id = f'{self.date}_{task_id}'
174     #     task = self.db.tasks[task_id]
175     #     if new_type_todo_id in self.db.todos.keys():
176     #         todo = self.db.todos[new_type_todo_id]
177     #     # elif task_id in self.db.keys():
178     #     #     todo = self.db.todos[task_id]
179     #     else:
180     #         # todo = Todo.from_dict(self.db, dict_source, id_=new_type_todo_id) if dict_source else Todo(db=self.db, day=self)
181     #         todo = Todo.from_dict(self.db, dict_source)
182     #     # todo._id = new_type_todo_id
183     #     todo._task = self.db.tasks[task_id] 
184     #     todo._id = new_type_todo_id
185     #     todo._day = self 
186     #     # self.todos[task_id] = todo 
187     #     self.linked_todos_as_list += [todo]
188     #     self.db.todos[new_type_todo_id] = todo
189     #     # self.db.todos[todo.task_id] = [todo]
190     #     # self.db.todos_as_list += [todo]
191     #     # self.db.all_todos[todo.task_id] = todo
192     #     return todo 
193
194     @property
195     def linked_todos(self):
196         linked_todos = {}
197         for todo in self.linked_todos_as_list:
198             linked_todos[todo.id_] = todo
199         return linked_todos
200
201     def _todos_sum(self, include_undone=False):
202         s = 0
203         for todo in [todo for todo in self.linked_todos.values()
204                      if self.date in todo.efforts.keys()]:
205             day_effort = todo.efforts[self.date]
206             if todo.done:
207                 s += day_effort if day_effort else todo.task.default_effort_at(self.date)
208             elif include_undone:
209                 s += day_effort if day_effort else 0 
210         return s
211
212     @property
213     def todos_sum(self):
214         return self._todos_sum()
215
216     # @property
217     # def todos_sum2(self):
218     #     return self._todos_sum(True)
219
220     # @property
221     # def date(self):
222     #     if self._date:
223     #         return self._date
224     #     else:
225     #         for k, v in self.db.days.items():
226     #             # print("DEBUG date", k, v)
227     #             if v == self:
228     #                 return k
229     #     print("DEBUG FAIL", self.test_date, self)
230
231
232 class Todo:
233
234     # def __init__(self, db, id_, task, done=False, day_effort=None, comment='', day_tags=None, importance=1.0, efforts=None):
235     def __init__(self, db, id_, task, done=False, comment='', day_tags=None, importance=1.0, efforts=None, children=None):
236         self.id_ = id_
237         self.db = db 
238         self.task = task 
239         self.done = done
240         # self.day_effort = day_effort
241         self.efforts = efforts if efforts else {}
242         self.comment = comment
243         self.day_tags = day_tags if day_tags else set()
244         self.importance = importance
245         self.children = children if children else [] 
246         self.parent = None 
247
248     @classmethod
249     def from_dict(cls, db, d, id_):
250         # todo = cls(db, None, None, d['done'], d['day_effort'], d['comment'], set(d['day_tags']), d['importance'])
251         # todo._task = db.tasks[d['task']] if 'task' in d.keys() else None
252         # todo._efforts = d['efforts'] if 'efforts' in d.keys() else None
253         # todo = cls(db, id_, db.tasks[d['task']], d['done'], d['day_effort'], d['comment'], set(d['day_tags']), d['importance'], d['efforts'])
254         todo = cls(db, id_, db.tasks[d['task']], d['done'], d['comment'], set(d['day_tags']), d['importance'], d['efforts'])
255         if 'children' in d:
256             todo.children = d['children']
257         return todo
258
259     # @classmethod
260     # def OLD_from_dict(cls, day, d):
261     #     todo = cls(day, d['done'], d['day_effort'], d['comment'], set(d['day_tags']), d['importance'])
262     #     if 'efforts' in d.keys():
263     #         todo._efforts = d['efforts']
264     #     return todo
265
266     def to_dict(self):
267         # return {'task': self.task.id_, 'done': self.done, 'day_effort': self.day_effort, 'comment': self.comment, 'day_tags': list(self.day_tags), 'importance': self.importance, 'efforts': self.efforts}
268         return {'task': self.task.id_, 'done': self.done, 'comment': self.comment, 'day_tags': list(self.day_tags), 'importance': self.importance, 'efforts': self.efforts, 'children': self.children}
269
270     @property
271     def default_effort(self):
272         return self.task.default_effort_at(self.sorted_effort_dates[0])
273
274     # @property
275     # def effort(self):
276     #     if self.day_effort:
277     #         return self.day_effort
278     #     else:
279     #         return self.day_effort if self.day_effort else self.default_effort
280
281     # @property
282     # def task(self):
283     #     if self._task:
284     #         return self._task
285     #     # else:
286     #     #     for k, v in self.day.todos.items():
287     #     #         if v == self:
288     #     #             return self.db.tasks[k]
289
290     def is_effort_removable(self, date):
291         if not date in self.efforts.keys():
292             return False
293         # if len(self.efforts) == 1:
294         #     return False
295         if self.efforts[date]:
296             return False
297         if self.done and date == self.sorted_effort_dates[-1]:
298             return False
299         return True
300
301     @property
302     def total_effort(self):
303         total = 0
304         for effort in self.efforts.values():
305             total += effort if effort else 0
306         if self.done:
307             total = max(total, self.task.default_effort_at(self.sorted_effort_dates[-1]))
308         return total
309
310     def matches(self, search):
311         if search is None:
312             return False
313         else:
314             return search in self.comment or search in '$'.join(self.tags) or search in self.title
315
316     @property
317     def title(self):
318         return self.task.title_at(self.sorted_effort_dates[0])
319
320     @property
321     def path(self):
322         path = ''
323         if self.parent:
324             path = f'{self.parent.path}{self.parent.title}:'
325         return path
326
327     @property
328     def tags(self):
329         return self.day_tags | self.task.tags
330
331     # def internals_empty(self):
332     #     return len(self.comment) == 0 and len(self.day_tags) == 0
333
334     # def ensure_day_efforts_table(self):
335     #     # We don't do this yet at __init__ because self.day.date is unknown, since Todo may be imported with Day, and during the import process the Day is not yet keyed in TodoDB.days.
336     #     if not hasattr(self, '_efforts'):
337     #         self._efforts = {} # {self.day.date: self.day_effort}
338
339     # def set_day_effort(self, date, effort):
340     #     self.ensure_day_efforts_table()
341     #     self._efforts[date] = self.day_effort
342
343     @property
344     def day_effort(self):
345         return self.efforts[self.db.selected_date]
346
347     @property
348     def sorted_effort_dates(self):
349         dates = list(self.efforts.keys())
350         dates.sort()
351         return dates
352
353     @property
354     def day(self):
355         if len(self.efforts) == 0:
356             return None
357         todo_start_date = self.sorted_effort_dates[0]
358         return self.db.days[todo_start_date]
359
360     # @property
361     # def efforts(self):
362     #     self.ensure_day_efforts_table()
363     #     return self._efforts
364
365     # @property
366     # def id_(self):
367     #     if self._id:
368     #         return self._id
369     #     for k, v in self.db.todos.items():
370     #         if v == self:
371     #             return k
372     #     # return f'{self.day.date}_{self.task.id_}'
373
374
375 class TodoDB(PlomDB):
376
377     def __init__(self, prefix, selected_date=None, t_filter_and = None, t_filter_not = None, hide_unchosen=False, hide_done=False):
378         self.prefix = prefix
379         self.selected_date = selected_date if selected_date else str(datetime.now())[:10]
380         self.t_filter_and = t_filter_and if t_filter_and else []
381         self.t_filter_not = t_filter_not if t_filter_not else []
382         self.hide_unchosen = hide_unchosen
383         self.hide_done = hide_done
384         self.days = {}
385         self.tasks = {}
386         self.t_tags = set()
387         self.todos = {}
388         super().__init__(db_path)
389
390     def read_db_file(self, f):
391         d = json.load(f)
392         for id_, t_dict in d['tasks'].items():
393             t = self.add_task(id_=id_, dict_source=t_dict)
394             for tag in t.tags:
395                 self.t_tags.add(tag)
396         # if 'todos' in d.keys():
397         for id_, todo_dict in d['todos'].items():
398             # todo = Todo.from_dict(self, todo_dict, id_)
399             # todo._id = id_ 
400             # self.todos[id_] = todo
401             todo = self.add_todo(todo_dict, id_) # Todo.from_dict(self, todo_dict, id_)
402             self.todos[id_] = todo 
403             for tag in todo.day_tags:
404                 self.t_tags.add(tag)
405         for date, day_dict in d['days'].items():
406             self.add_day(dict_source=day_dict, date=date)
407         for todo in self.todos.values():
408             for child_id in todo.children:
409                 self.todos[child_id].parent = todo
410             # if len(todo.efforts) == 0:
411             #     print("DEBUG", todo.id_, todo.task.title, todo.efforts)
412             for date in todo.efforts.keys():
413                 if not date in self.days.keys():
414                     self.add_day(date)
415                 if not todo in self.days[date].linked_todos_as_list:
416                     self.days[date].linked_todos_as_list += [todo]
417                     # print("DEBUG", todo.title, date)
418                 # if type(todo.efforts[date]) == str:
419                 #     todo.efforts[date] = float(todo.efforts[date])
420                 #     print("DEBUG", todo.title, date, todo.efforts[date])
421         # for todo in self.todos.values():
422         # for day in self.days.values():
423         #     for todo in day.todos.values():
424         #         for tag in todo.day_tags:
425         #             self.t_tags.add(tag)
426         # delete = None
427         # for todo in self.todos.values():
428         #     if len(todo.efforts) == 0:
429         #         print("DEBUG", todo.task.title)
430         #         delete = todo.id_ 
431         # self.delete_todo(delete)
432         self.set_visibilities()
433
434     def set_visibilities(self):
435         for uuid, t in self.tasks.items():
436             t.visible = len([tag for tag in self.t_filter_and if not tag in t.tags]) == 0\
437                     and len([tag for tag in self.t_filter_not if tag in t.tags]) == 0\
438                     and ((not self.hide_unchosen) or uuid in self.selected_day.todos.keys())
439         # for day in self.days.values():
440         #     for todo in day.todos.values():
441         #         todo.visible = len([tag for tag in self.t_filter_and if not tag in todo.day_tags | todo.task.tags ]) == 0\
442         #             and len([tag for tag in self.t_filter_not if tag in todo.day_tags | todo.task.tags ]) == 0\
443         #             and ((not self.hide_done) or (not todo.done))
444         for todo in self.todos.values():
445             todo.visible = len([tag for tag in self.t_filter_and if not tag in todo.day_tags | todo.task.tags ]) == 0\
446                 and len([tag for tag in self.t_filter_not if tag in todo.day_tags | todo.task.tags ]) == 0\
447                 and ((not self.hide_done) or (not todo.done))
448
449     def to_dict(self):
450         d = {'tasks': {}, 'days': {}, 'todos': {}}
451         for uuid, t in self.tasks.items():
452              d['tasks'][uuid] = t.to_dict()
453         for date, day in self.days.items():
454             d['days'][date] = day.to_dict()
455             for todo in day.todos.values():
456                 d['todos'][todo.id_] = todo.to_dict()
457         for id_, todo in self.todos.items():
458             d['todos'][id_] = todo.to_dict()
459         return d
460
461     # @property
462     # def all_todos(self):
463     #     todos = {}
464     #     for todo in self.todos_as_list:
465     #         todos[todo.id_] = todo
466     #     return todos 
467
468     @property
469     def selected_day(self):
470         if not self.selected_date in self.days.keys():
471             self.days[self.selected_date] = self.add_day(date=self.selected_date)
472             # print("DEBUG selected_day", self.days[self.selected_date].date)
473         return self.days[self.selected_date]
474
475     def write(self):
476         dates_to_purge = []
477         for date, day in self.days.items():
478             if len(day.linked_todos) == 0 and len(day.comment) == 0:
479                 dates_to_purge += [date]
480         for date in dates_to_purge:
481             del self.days[date]
482         self.write_text_to_db(json.dumps(self.to_dict()))
483
484     def add_task(self, id_=None, dict_source=None, return_id=False):
485         id_ = id_ if id_ else str(uuid4())
486         t = Task.from_dict(self, dict_source, id_) if dict_source else Task(self, id_)
487         self.tasks[id_] = t
488         if return_id:
489             return id_, t
490         else:
491             return t
492
493     def add_todo(self, todo_dict=None, id_=None, task=None, efforts=None):
494         id_ = id_ if id_ else str(uuid4())
495         if todo_dict:
496             todo = Todo.from_dict(self, todo_dict, id_)
497         elif task and efforts:
498             todo = Todo(self, id_, task, efforts=efforts)
499             children = []
500             for child_id in task.links:
501                 child_task = self.tasks[child_id]
502                 children += [self.add_todo(task=child_task, efforts=efforts)]
503             todo.children = [child.id_ for child in children]
504         self.todos[id_] = todo 
505         return todo
506
507     def add_day(self, date, dict_source=None):
508         day = Day.from_dict(self, dict_source, date) if dict_source else Day(self, date)
509         self.days[date] = day 
510         return day
511
512     # def show_day(self, task_sort=None):
513     #     task_sort = task_sort if task_sort else 'title' 
514     #     current_date = datetime.strptime(self.selected_date, DATE_FORMAT)
515     #     prev_date = current_date - timedelta(days=1)
516     #     prev_date_str = prev_date.strftime(DATE_FORMAT)
517     #     next_date = current_date + timedelta(days=1)
518     #     next_date_str = next_date.strftime(DATE_FORMAT)
519     #     task_rows = []
520     #     for uuid, task in self.tasks.items():
521     #         if not task.visible:
522     #             continue
523     #         todo = None
524     #         if uuid in self.selected_day.todos.keys():
525     #             todo = self.selected_day.todos[uuid]
526     #             if not todo.visible:
527     #                 continue
528     #         task_rows += [{'uuid': uuid, 'task': task, 'todo': todo}] 
529     #     if task_sort == 'title':
530     #         task_rows.sort(key=lambda r: r['task'].title)
531     #     elif task_sort == 'default_effort':
532     #         task_rows.sort(key=lambda r: r['task'].default_effort, reverse=True)
533     #     elif task_sort == 'done':
534     #         task_rows.sort(key=lambda r: 0 if not r['todo'] else r['todo'].day_effort if r['todo'].day_effort else r['task'].default_effort if r['todo'].done else 0, reverse=True)
535     #     elif task_sort == 'importance':
536     #         task_rows.sort(key=lambda r: 0.0 if not r['todo'] else r['todo'].importance, reverse=True)
537     #     elif task_sort == 'chosen':
538     #         task_rows.sort(key=lambda r: False if not r['todo'] else True, reverse=True)
539     #     elif task_sort == 'comment':
540     #         task_rows.sort(key=lambda r: '' if not r['todo'] else r['todo'].comment, reverse=True)
541     #     done_tasks = []
542     #     for uuid, task in self.tasks.items():
543     #         if uuid in self.selected_day.todos.keys():
544     #             todo = self.selected_day.todos[uuid]
545     #             if todo.done:
546     #                 done_tasks += [todo]
547     #     done_tasks.sort(key=lambda t: t.effort, reverse=True)
548     #     return j2env.get_template('day.html').render(db=self, action=self.prefix+'/day', prev_date=prev_date_str, next_date=next_date_str, task_rows=task_rows, sort=task_sort, done_tasks=done_tasks)
549
550     def neighbor_dates(self):
551         current_date = datetime.strptime(self.selected_date, DATE_FORMAT)
552         prev_date = current_date - timedelta(days=1)
553         prev_date_str = prev_date.strftime(DATE_FORMAT)
554         next_date = current_date + timedelta(days=1)
555         next_date_str = next_date.strftime(DATE_FORMAT)
556         return prev_date_str, next_date_str
557
558     def show_do_day(self, sort_order=None):
559         prev_date_str, next_date_str = self.neighbor_dates()
560         todos = [t for t in self.selected_day.linked_todos_as_list if t.visible]
561         if sort_order == 'title':
562             todos.sort(key=lambda t: t.task.title)
563         elif sort_order == 'done':
564             todos.sort(key=lambda t: t.day_effort if t.day_effort else t.default_effort if t.done else 0, reverse=True)
565         elif sort_order == 'default_effort':
566             todos.sort(key=lambda t: t.default_effort, reverse=True)
567         elif sort_order == 'importance':
568             todos.sort(key=lambda t: t.importance, reverse=True)
569         return j2env.get_template('do_day.html').render(
570                 day=self.selected_day,
571                 tags=self.t_tags,
572                 filter_and=self.t_filter_and,
573                 filter_not=self.t_filter_not,
574                 prev_date=prev_date_str,
575                 next_date=next_date_str,
576                 todos=todos,
577                 sort=sort_order,
578                 hide_done=self.hide_done)
579
580     def show_calendar(self, start_date_str, end_date_str):
581         self.t_filter_and = ['calendar']
582         self.t_filter_not = ['deleted']
583         self.set_visibilities()
584         days_to_show = {}
585         todays_date_str = str(datetime.now())[:10]
586         todays_date_obj = datetime.strptime(todays_date_str, DATE_FORMAT) 
587         yesterdays_date_obj = todays_date_obj - timedelta(1)
588         yesterdays_date_str = yesterdays_date_obj.strftime(DATE_FORMAT) 
589         start_date_obj = datetime.strptime(sorted(self.days.keys())[0], DATE_FORMAT)
590         if start_date_str and len(start_date_str) > 0:
591             if start_date_str in {'today', 'yesterday'}:
592                 start_date_obj = todays_date_obj if start_date_str == 'today' else yesterdays_date_obj
593             else:
594                 start_date_obj = datetime.strptime(start_date_str, DATE_FORMAT)
595         end_date_obj = datetime.strptime(sorted(self.days.keys())[-1], DATE_FORMAT)
596         if end_date_str and len(end_date_str) > 0:
597             if end_date_str in {'today', 'yesterday'}:
598                 end_date_obj = todays_date_obj if end_date_str == 'today' else yesterdays_date_obj
599             else:
600                 end_date_obj = datetime.strptime(end_date_str, DATE_FORMAT)
601         for n in range(int((end_date_obj - start_date_obj).days) + 1):
602             current_date_obj = start_date_obj + timedelta(n)
603             current_date_str = current_date_obj.strftime(DATE_FORMAT)
604             if current_date_str not in self.days.keys():
605                 days_to_show[current_date_str] = self.add_day(current_date_str)
606             else:
607                 days_to_show[current_date_str] = self.days[current_date_str]
608             days_to_show[current_date_str].weekday = datetime.strptime(current_date_str, DATE_FORMAT).strftime('%A')[:2]
609         return j2env.get_template('calendar.html').render(db=self, days=days_to_show, action=self.prefix+'/calendar', start_date=start_date_str, end_date=end_date_str)
610
611     def show_todo(self, id_, return_to):
612         todo = self.todos[id_]
613         # if selected_date not in self.days.keys():
614         #     self.days[selected_date] = self.add_day(test_date=f'3:{selected_date}') 
615         #     # print("DEBUG show_todo", self.days[selected_date].date)
616         # if task_uuid in self.days[selected_date].todos:
617         #     todo = self.days[selected_date].todos[task_uuid]
618         # else:
619         #     todo = self.days[selected_date].add_todo(task_uuid)
620         linked_todos = [self.todos[c] for c in todo.children]
621         filtered_todos = [] #[t for t in self.todos.values() if t.visible and t != self and (t not in linked_todos) and (len(search) == 0 or t.matches(search))] 
622         return j2env.get_template('todo.html').render(
623                 db=self,
624                 todo=todo,
625                 filtered_todos=filtered_todos,
626                 linked_todos=linked_todos,
627                 return_to=return_to)
628
629     # def update_todo_mini(self, task_uuid, date, day_effort, done, importance):
630     #     if date not in self.days.keys():
631     #         self.days[date] = self.add_day(test_date=f'Y:{date}') 
632     #         # print("DEBUG update_todo_min", self.days[date].date)
633     #     if task_uuid in self.days[date].todos.keys():
634     #         todo = self.days[date].todos[task_uuid]
635     #     else:
636     #         todo = self.days[date].add_todo(task_uuid)
637     #     todo.day_effort = day_effort
638     #     todo.done = done
639     #     todo.importance = importance
640     #     return todo
641
642     def collect_tags(self, tags_joined, tags_checked):
643         tags = set()
644         for tag in [tag.strip() for tag in tags_joined.split(';') if tag.strip() != '']:
645             tags.add(tag)
646         for tag in tags_checked:
647             tags.add(tag)
648         return tags
649
650     def update_todo_for_day(self, id_, date, effort, done, comment, importance):
651         todo = self.todos[id_]
652         todo.done = done
653         todo.efforts[date] = effort 
654         todo.comment = comment 
655         todo.importance = importance 
656
657     def update_todo(self, id_, efforts, done, comment, day_tags_joined, day_tags_checked, importance):
658         if len(efforts) == 0:
659             raise PlomException('todo must have at least one effort!')
660         todo = self.todos[id_]
661         todo.done = done
662         todo.efforts = efforts 
663         for date in todo.efforts:
664             if not date in self.days.keys():
665                 self.add_day(date=date) 
666             if not self in self.days[date].linked_todos_as_list:
667                 self.days[date].linked_todos_as_list += [todo]
668         todo.comment = comment 
669         todo.day_tags = self.collect_tags(day_tags_joined, day_tags_checked) 
670         todo.importance = importance 
671
672     def delete_todo(self, id_):
673         todo = self.todos[id_]
674         dates_to_delete = []
675         for date in todo.efforts.keys():
676             dates_to_delete += [date]
677         for date in dates_to_delete:
678             self.delete_effort(todo, date, force=True)
679         del self.todos[id_]
680
681     def delete_effort(self, todo, date, force=False):
682         if (not force) and len(todo.efforts) == 1:
683             raise PlomException('todo must retain at least one effort!')
684         self.days[date].linked_todos_as_list.remove(todo)
685         del todo.efforts[date]
686
687
688     # def update_todo(self, task_uuid, date, day_effort, done, comment, day_tags_joined, day_tags_checked, importance):
689     #     day_effort = float(day_effort) if len(day_effort) > 0 else None
690     #     importance = float(importance)
691     #     todo = self.update_todo_mini(task_uuid, date, day_effort, done, importance)
692     #     todo.comment = comment
693     #     todo.day_tags = self.collect_tags(day_tags_joined, day_tags_checked) 
694
695     # def link_day_with_todo(self, date, todo_id):
696     #     print("DEBUG link", date, todo_id)
697     #     todo_creation_date, task_uuid = todo_id.split('_')
698     #     todo = self.days[todo_creation_date].todos[task_uuid]
699     #     if date in todo.efforts.keys():
700     #         raise PlomException('todo already linked to respective day')
701     #     todo.set_day_effort(date, None)
702     #     if date not in self.days.keys():
703     #         print("DEBUG link_day_with_todo", date)
704     #         self.days[date] = self.add_day(test_date=f'Z:{date}') 
705     #     self.days[date].linked_todos_as_list += [todo]
706     #     print("DEBUG", date, self.days[date].linked_todos)
707
708     def show_task(self, id_, return_to='', search=''):
709         task = self.tasks[id_] if id_ else self.add_task()
710         selected = task.id_ in self.selected_day.todos.keys()
711         linked_tasks = [self.tasks[l] for l in task.links]
712         filtered_tasks = [t for t in self.tasks.values() if t.visible and t != self and (t not in linked_tasks) and (len(search) == 0 or t.matches(search))] 
713         return j2env.get_template('task.html').render(
714                 db=self,
715                 search=search,
716                 tags=self.t_tags, 
717                 filter_and=self.t_filter_and,
718                 filter_not=self.t_filter_not,
719                 filtered_tasks=filtered_tasks,
720                 linked_tasks=linked_tasks,
721                 task=task,
722                 return_to=return_to)
723
724     def update_task(self, id_, title, default_effort, tags_joined, tags_checked, links, comment):
725         task = self.tasks[id_] if id_ in self.tasks.keys() else self.add_task(id_)
726         task.title = title
727         task.default_effort = float(default_effort) if len(default_effort) > 0 else None
728         task.tags = self.collect_tags(tags_joined, tags_checked) 
729         task.links = links
730         task.comment = comment 
731
732     def show_tasks(self, expand_uuid):
733         expanded_tasks = {}
734         if expand_uuid:
735             for uuid in self.tasks[expand_uuid].links:
736                 expanded_tasks[uuid] = self.tasks[uuid]
737         return j2env.get_template('tasks.html').render(db=self, action=self.prefix+'/tasks', expand_uuid=expand_uuid, expanded_tasks=expanded_tasks)
738
739     def show_new_day(self, search, hide_chosen_tasks, sort_order=None):
740         prev_date_str, next_date_str = self.neighbor_dates()
741         chosen_todos = self.selected_day.linked_todos_as_list
742         relevant_todos = []
743         for todo in self.todos.values():
744             if todo.done or (not todo.visible) or (not todo.matches(search)) or todo in self.selected_day.linked_todos_as_list: # or todo.day.date == self.selected_day.date:  # TODO or todo is linked by day  
745                 continue
746             relevant_todos += [todo] 
747         tasks = []
748         chosen_tasks = [todo.task for todo in self.selected_day.linked_todos_as_list]
749         for uuid, task in self.tasks.items():
750             if (not task.visible) or (not task.matches(search)) or (hide_chosen_tasks and task in chosen_tasks):
751                 continue
752             tasks += [task]
753         if sort_order == 'title':
754             chosen_todos.sort(key=lambda t: t.title)
755             relevant_todos.sort(key=lambda t: t.title)
756             tasks.sort(key=lambda t: t.title)
757         elif sort_order == 'effort':
758             chosen_todos.sort(key=lambda t: t.day_effort if t.day_effort else t.default_effort if t.done else 0, reverse=True)
759             relevant_todos.sort(key=lambda t: t.total_effort, reverse=True)
760             tasks.sort(key=lambda t: t.default_effort, reverse=True)
761         return j2env.get_template('new_day.html').render(
762                 sort=sort_order,
763                 tags=self.t_tags,
764                 chosen_todos=chosen_todos,
765                 filter_and=self.t_filter_and,
766                 filter_not=self.t_filter_not,
767                 day=self.selected_day,
768                 prev_date=prev_date_str,
769                 next_date=next_date_str,
770                 tasks=tasks,
771                 hide_chosen_tasks=hide_chosen_tasks,
772                 relevant_todos=relevant_todos,
773                 search=search)
774
775
776 class ParamsParser:
777
778     def __init__(self, parsed_url_query, cookie_db):
779         self.params = parse_qs(parsed_url_query)
780         self.cookie_db = cookie_db
781
782     def get(self, key, default=None):
783         boolean = bool == type(default) 
784         param = self.params.get(key, [default])[0]
785         if boolean:
786             param = param != '0' 
787         return param 
788
789     def get_cookied(self, key, default=None):
790         param = self.get(key, default)
791         if param == '-':
792             param = None
793             if key in self.cookie_db.keys():
794                 del self.cookie_db[key]
795         if param is None and key in self.cookie_db.keys():
796             param = self.cookie_db[key]
797         if param is not None:
798             self.cookie_db[key] = param
799         return param
800
801     def get_cookied_chain(self, key, default=None):
802         # default = default if default else ['-']
803         params = self.params.get(key, default)
804         if params == ['-']:
805             params = None 
806             if key in self.cookie_db.keys():
807                 del self.cookie_db[key]
808         if params is None and key in self.cookie_db.keys():
809             params = self.cookie_db[key]
810         if params is not None:
811             self.cookie_db[key] = params
812         return params
813
814
815 class TodoHandler(PlomHandler):
816
817     def config_init(self):
818         return {
819             'cookie_name': 'todo_cookie',
820             'prefix': '',
821             'cookie_path': '/'
822         }
823
824     def app_init(self, handler):
825         default_path = '/todo'
826         handler.add_route('GET', default_path, self.show_db)
827         handler.add_route('POST', default_path, self.write_db)
828         return 'todo', {'cookie_name': 'todo_cookie', 'prefix': default_path, 'cookie_path': default_path}
829
830     def do_POST(self):
831         self.try_do(self.config_init)
832         self.try_do(self.write_db)
833
834     def write_db(self):
835         from urllib.parse import urlencode
836         config = self.apps['todo'] if hasattr(self, 'apps') else self.config_init()
837         length = int(self.headers['content-length'])
838         postvars = parse_qs(self.rfile.read(length).decode(), keep_blank_values=1)
839         parsed_url = urlparse(self.path)
840         site = path_split(urlparse(self.path).path)[1]
841         # site = path_split(parsed_url.path)[1]
842         db = TodoDB(prefix=config['prefix'])
843         redir_params = []
844         for param_name, filter_db_name in {('t_and', 't_filter_and'), ('t_not', 't_filter_not')}:
845             filter_db = getattr(db, filter_db_name)
846             if param_name in postvars.keys():
847                 for target in postvars[param_name]:
848                     if len(target) > 0 and not target in filter_db:
849                         filter_db += [target]
850                 if len(filter_db) == 0:
851                     redir_params += [(param_name, '-')]
852             redir_params += [(param_name, f) for f in filter_db]
853
854         def collect_checked(prefix, postvars):
855             tags_checked = []
856             for k in postvars.keys():
857                 if k.startswith(prefix):
858                     tags_checked += [k[len(prefix):]]
859             return tags_checked
860         
861         if 'calendar' == site:
862             redir_params += [('start', postvars['start'][0] if len(postvars['start'][0]) > 0 else '-')]
863             redir_params += [('end', postvars['end'][0] if len(postvars['end'][0]) > 0 else '-')]
864
865         elif 'todo' == site:
866             # task_uuid = postvars['task_uuid'][0]
867             todo_id = postvars['todo_id'][0]
868             # date = postvars['date'][0]
869             old_todo = db.todos[todo_id] 
870             efforts = {}
871             for i, date in enumerate(postvars['effort_date']):
872                 if '' == date:
873                     continue
874                 efforts[date] = float(postvars['effort'][i]) if len(postvars['effort'][i]) > 0 else None 
875             if 'delete_effort' in postvars.keys():
876                 for date in postvars['delete_effort']:
877                     del efforts[date]
878             if 'delete' in postvars.keys():
879                 # old_todo = db.days[date].todos[task_uuid]
880                 if 'done' in postvars or postvars['comment'][0] != '' or len(collect_checked('day_tag_', postvars)) > 0 or postvars['joined_day_tags'][0] != '' or len(efforts) > 0:
881                     raise PlomException('will not remove todo of preserve-worthy values')
882                 db.delete_todo(todo_id) 
883                 # db.write()
884                 # self.redirect('new_day')
885             else:
886                 # redir_params += [('task', task_uuid), ('date', date)]
887                 redir_params += [('id', todo_id)]
888                 # db.update_todo(task_uuid, date, postvars['day_effort'][0], 'done' in postvars.keys(), postvars['comment'][0], postvars['joined_day_tags'][0], collect_checked('day_tag_', postvars), postvars['importance'][0])
889                 # efforts = {}
890                 # for i, date in enumerate(postvars['effort_date']):
891                 #     if '' == date:
892                 #         continue
893                 #     efforts[date] = postvars['effort'][i] if len(postvars['effort'][i]) > 0 else None 
894                 if 'delete_effort' in postvars.keys():
895                     for date in postvars['delete_effort']:
896                         db.delete_effort(old_todo, date)
897                 db.update_todo(id_=todo_id,
898                                efforts=efforts,
899                                done='done' in postvars.keys(),
900                                comment=postvars['comment'][0],
901                                day_tags_joined=postvars['joined_day_tags'][0],
902                                day_tags_checked=collect_checked('day_tag_', postvars),
903                                importance=float(postvars['importance'][0]))
904
905         elif 'task' == site:
906             id_ = postvars['id'][0]
907             redir_params += [('id', id_)]
908             if 'filter' in postvars.keys():
909                 redir_params += [('search', postvars['search'][0])]
910             elif 'title' in postvars.keys():
911                 links = [] if not 'link_task' in postvars.keys() else postvars['link_task']
912                 # db.update_task(id_, postvars['title'][0], postvars['default_effort'][0], postvars['joined_tags'][0], collect_checked('tag_', postvars), collect_checked('link_', postvars), postvars['comment'][0])
913                 db.update_task(id_, postvars['title'][0], postvars['default_effort'][0], postvars['joined_tags'][0], collect_checked('tag_', postvars), links, postvars['comment'][0])
914                 # if 'as_todo' in postvars.keys() and id_ not in db.selected_day.todos.keys():
915                 #     db.update_todo_mini(id_, db.selected_date, None, False, 1.0)
916                 # elif 'as_todo' not in postvars.keys() and id_ in db.selected_day.todos.keys():
917                 #     todo = db.selected_day.todos[id_]
918                 #     if todo.internals_empty() and (not todo.done) and todo.day_effort is None:
919                 #         del db.selected_day.todos[id_]
920                 #     else:
921                 #         raise PlomException('cannot deselect task as todo of preserve-worthy values')
922
923         elif 'new_day' == site:
924             if 'filter' in postvars.keys():
925                 redir_params += [('search', postvars['search'][0])]
926                 redir_params += [('hide_chosen_tasks', int('hide_chosen_tasks' in postvars.keys()))]
927             else:
928                 db.selected_date = postvars['date'][0]
929                 todos_to_shrink = []
930                 todos_to_delete = []
931                 for todo in db.selected_day.linked_todos_as_list:
932                     if todo.visible and not ('chosen_todo' in postvars.keys() and todo.id_ in postvars['chosen_todo']):
933                         if len(todo.comment) > 0 or len(todo.day_tags) > 0 or not todo.is_effort_removable(db.selected_date):
934                             # print("DEBUG", len(todo.comment) > 0, len(todo.day_tags) > 0, todo.is_effort_removable(db.selected_date))
935                             raise PlomException('will not remove effort of preserve-worthy values')
936                         if len(todo.efforts) > 1:
937                             todos_to_shrink += [todo]
938                             # db.delete_effort(todo, db.selected_date)
939                         else:
940                             todos_to_delete += [todo]
941                             # db.delete_todo(todo.id_)
942                 for todo in todos_to_shrink:
943                     db.delete_effort(todo, db.selected_date)
944                 for todo in todos_to_delete:
945                     db.delete_todo(todo.id_)
946                 if 'choose_task' in postvars.keys():
947                     for id_ in postvars['choose_task']:
948                         db.add_todo(task=db.tasks[id_], efforts={db.selected_date: None})
949                 if 'choose_todo' in postvars.keys():
950                     for id_ in postvars['choose_todo']:
951                         todo = db.todos[id_]
952                         todo.efforts[db.selected_date] = None
953
954             # if 'choose_task' in postvars.keys():
955             #     for i, uuid in enumerate(postvars['choose_task']):
956             #          if not uuid in db.selected_day.todos.keys():
957             #             # task = db.tasks[uuid]
958             #              db.update_todo_mini(uuid, db.selected_date, None, False, 1.0)
959             # if 'choose_todo' in postvars.keys():
960             #     for i, id_ in enumerate(postvars['choose_todo']):
961             #         if not id_ in [todo.id_ for todo in db.selected_day.linked_todos_as_list]:
962             #             db.link_day_with_todo(db.selected_date, id_)
963
964         elif 'do_day' == site:
965             if 'filter' in postvars.keys():
966                 redir_params += [('hide_done', int('hide_done' in postvars.keys()))]
967             else:
968                 db.selected_date = postvars['date'][0]
969                 redir_params += [('date', db.selected_date)]
970                 db.selected_day.comment = postvars['day_comment'][0]
971                 if 'todo_id' in postvars.keys():
972                     for i, todo_id in enumerate(postvars['todo_id']):
973                         old_todo = None if not todo_id in db.todos.keys() else db.todos[todo_id]
974                         done = ('done' in postvars) and (todo_id in postvars['done'])
975                         day_effort_input = postvars['effort'][i]
976                         day_effort = float(day_effort_input) if len(day_effort_input) > 0 else None
977                         comment = postvars['effort_comment'][i]
978                         importance = float(postvars['importance'][i])
979                         if old_todo and old_todo.done == done and old_todo.day_effort == day_effort and comment == old_todo.comment and old_todo.importance == importance:
980                             continue
981                         db.update_todo_for_day(todo_id, db.selected_date, day_effort, done, comment, importance)
982
983         elif 'day' == site:
984             # always store the two hide params in the URL if possible … TODO: find out if really necessary
985             if 'expect_unchosen_done' in postvars.keys():
986                 redir_params += [('hide_unchosen', int('hide_unchosen' in postvars.keys()))] + [('hide_done', int('hide_done' in postvars.keys()))]
987
988             if 'date' in postvars.keys():
989                 db.selected_date = postvars['date'][0]
990                 if 'day_comment' in postvars.keys():
991                     db.selected_day.comment = postvars['day_comment'][0]
992                 redir_params += [('date', db.selected_date)]
993
994                 # handle todo list updates via task UUIDs
995                 if 't_uuid' in postvars.keys():
996                     for i, uuid in enumerate(postvars['t_uuid']):
997                         task = db.tasks[uuid]
998                         old_todo = None if not uuid in db.selected_day.todos.keys() else db.selected_day.todos[uuid]
999                         selects_as_todo = 'choose' in postvars and uuid in postvars['choose']
1000                         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())
1001                         if old_todo and too_much_keepworthy_data and not selects_as_todo:
1002                             raise PlomException('cannot deselect task as todo of preserve-worthy values')
1003                         elif old_todo and not selects_as_todo:
1004                             del db.selected_day.todos[uuid]
1005                         elif too_much_keepworthy_data or selects_as_todo:
1006                             done = ('done' in postvars) and (uuid in postvars['done'])
1007                             day_effort_input = postvars['day_effort'][i]
1008                             day_effort = float(day_effort_input) if len(day_effort_input) > 0 else None
1009                             importance = float(postvars['importance'][i])
1010                             if old_todo and old_todo.done == done and old_todo.day_effort == day_effort and old_todo.importance == importance:
1011                                 continue
1012                             db.update_todo_mini(uuid, db.selected_date, day_effort, done, importance)
1013
1014         if 'return_to' in postvars.keys() and len(postvars['return_to'][0]) > 0:
1015             homepage = postvars['return_to'][0]
1016         else:
1017             encoded_params = urlencode(redir_params)
1018             # homepage = f'{parsed_url.path}?{encoded_params}'
1019             homepage = f'{site}?{encoded_params}'
1020         db.write()
1021         self.redirect(homepage)
1022
1023     def do_GET(self):
1024         self.try_do(self.config_init)
1025         self.try_do(self.show_db)
1026
1027     def show_db(self):
1028         config = self.apps['todo'] if hasattr(self, 'apps') else self.config_init()
1029         cookie_db = self.get_cookie_db(config['cookie_name'])
1030         parsed_url = urlparse(self.path)
1031         site = path_split(parsed_url.path)[1]
1032         # params = parse_qs(parsed_url.query)
1033         params = ParamsParser(parsed_url.query, cookie_db)
1034
1035         # def get_param(param_name, boolean=False, chained=False, none_as_empty_string=False):
1036         #     if chained:
1037         #         param = params.get(param_name, None)
1038         #     elif none_as_empty_string:
1039         #         param = params.get(param_name, [''])[0]
1040         #     else: 
1041         #         param = params.get(param_name, [None])[0]
1042         #     if (not chained and param == '-') or (chained and param == ['-']):
1043         #         param = None
1044         #         if param_name in cookie_db.keys():
1045         #             del cookie_db[param_name]
1046         #     if param is None and param_name in cookie_db.keys():
1047         #         param = cookie_db[param_name]
1048         #     if param is not None:
1049         #         if boolean:
1050         #             param = param != '0'
1051         #             cookie_db[param_name] = str(int(param))
1052         #         else:
1053         #             cookie_db[param_name] = param
1054         #     elif param is boolean:
1055         #         param = False
1056         #     return param
1057
1058         selected_date = t_filter_and = t_filter_not = None
1059         hide_unchosen = hide_done = False
1060         # return_to = params.get('return_to', [''])[0]
1061         return_to = params.get('return_to', '')
1062         if site in {'do_day', 'new_day'}:
1063             selected_date = params.get_cookied('date')
1064         if site in {'do_day', 'new_day', 'task'}:
1065             t_filter_and = params.get_cookied_chain('t_and')
1066             # t_filter_not = params.get_cookied_chain('t_not', ['deleted', 'cancelled'])
1067             t_filter_not = params.get_cookied_chain('t_not')
1068         # if site in {'day','tasks', 'task', 'new_day'}:
1069         #     t_filter_and = get_param('t_and', chained=True)
1070         #     t_filter_not = get_param('t_not', chained=True)
1071         if 'do_day' == site:
1072             # hide_unchosen = get_param('hide_unchosen', boolean=True)
1073             hide_done = params.get('hide_done', False) 
1074         db = TodoDB(config['prefix'], selected_date, t_filter_and, t_filter_not, hide_unchosen, hide_done)
1075         if 'do_day' == site:
1076             sort_order = params.get_cookied('sort')
1077             page = db.show_do_day(sort_order)
1078         elif 'new_day' == site:
1079             sort_order = params.get_cookied('sort')
1080             hide_chosen_tasks = params.get('hide_chosen_tasks', False)
1081             search = params.get('search', '')
1082             page = db.show_new_day(search, hide_chosen_tasks, sort_order)
1083         elif site == 'todo':
1084             todo_id = params.get('id')
1085             page = db.show_todo(todo_id, return_to)
1086             # todo_id = params.get('id')
1087             # if todo_id:
1088             #     todo_date, task_uuid = todo_id.split('_') 
1089             # else:
1090             #     todo_date = params.get('date')
1091             #     task_uuid = params.get('task')
1092             # page = db.show_todo(task_uuid, todo_date, return_to)
1093         elif 'task' == site:
1094             id_ = params.get('id')
1095             search = params.get('search', '')
1096             page = db.show_task(id_, return_to, search)
1097         elif 'tasks' == site:
1098             expand_uuid = params.get('expand_uuid')
1099             page = db.show_tasks(expand_uuid)
1100         elif 'add_task' == site:
1101             page = db.show_task(None)
1102         elif 'unset_cookie' == site:
1103             page = 'no cookie to unset.'
1104             if len(cookie_db) > 0:
1105                 self.unset_cookie(config['cookie_name'], config['cookie_path'])
1106                 page = 'cookie unset!'
1107         else:
1108             start_date = params.get_cookied('start')
1109             end_date = params.get_cookied('end')
1110             page = db.show_calendar(start_date, end_date)
1111         if 'unset_cookie' != site:
1112             self.set_cookie(config['cookie_name'], config['cookie_path'], cookie_db)
1113         self.send_HTML(page)
1114
1115
1116 if __name__ == "__main__":
1117     run_server(server_port, TodoHandler)