home · contact · privacy
Add forgotten files.
[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
15
16 def today_date(with_time=False):
17     length = 19 if with_time else 10
18     return str(datetime.now())[:length]
19
20
21
22 class AttributeWithHistory:
23
24     def __init__(self, parent, name, default_if_empty, history=None, then_date='2000-01-01'):
25         self.parent = parent 
26         self.name = name
27         self.default = default_if_empty
28         self.then_date = then_date
29         self.history = history if history else {}
30
31     def set(self, value):
32         keys = sorted(self.history.keys())
33         if len(self.history) == 0 or value != self.history[keys[-1]]:
34             self.history[today_date(with_time=True)] = value
35
36     def at(self, queried_date):
37         end_of_queried_day = f'{queried_date} 23:59:59'
38         sorted_dates = sorted(self.history.keys())
39         if self.parent.forked_task and (0 == len(sorted_dates) or sorted_dates[0] > end_of_queried_day):
40             return getattr(self.parent.forked_task, self.name).at(queried_date)
41         elif 0 == len(sorted_dates):
42             return self.default
43         ret = self.history[sorted_dates[0]]
44         for date_key, item in self.history.items():
45             if date_key > end_of_queried_day:
46                 break
47             ret = item
48         return ret
49
50     @property
51     def now(self):
52         keys = sorted(self.history.keys())
53         if 0 == len(self.history):
54             if self.parent.forked_task:
55                 return getattr(self.parent.forked_task, self.name).now
56             return self.default
57         return self.history[keys[-1]]
58
59     @property
60     def then(self):
61         return self.at(self.then_date)
62
63
64
65 class TaskLike:
66
67     def __init__(self, db, id_, comment):
68         self.db = db
69         self.id_ = id_
70         self.comment = comment
71
72     @property
73     def visible(self):
74         visible_by_and = 0 == len([tag for tag in self.db.tag_filter_and
75                                    if not tag in self.tags])
76         visible_by_not = 0 == len([tag for tag in self.db.tag_filter_not
77                                    if tag in self.tags])
78         return visible_by_and and visible_by_not
79
80     @property
81     def deps_depth(self):
82         if len(self.deps) == 0:
83             return 0
84         return 1 + max([d.deps_depth for d in self.deps])
85
86     @property
87     def deps_chain(self):
88         all_deps = set()
89         def add_deps(node, all_deps):
90             for dep in node.deps:
91                 all_deps.add(dep)
92                 add_deps(dep, all_deps)
93         add_deps(self, all_deps)
94         chain = list(all_deps)
95         chain.sort(key=lambda t: t.deps_depth)
96         return chain
97
98     def loop_check(self):
99         loop_msg = "can't set dep, would create loop"
100         def f(node, id_):
101             for dep in node.deps:
102                 if id_ == dep.id_:
103                     raise PlomException(loop_msg)
104                 f(dep, id_)
105         f(self, self.id_)
106
107
108
109
110 class Task(TaskLike):
111
112     def __init__(self,
113             db,
114             id_,
115             title_history=None,
116             tags_history=None,
117             default_effort_history=None,
118             dep_ids_history=None,
119             adoptivity_history=None,
120             comment='',
121             forks_id=None):
122         super().__init__(db, id_, comment)
123         self.forks_id = forks_id 
124         self.title = AttributeWithHistory(self, 'title', '', title_history, self.db.selected_date)
125         self.tags = AttributeWithHistory(self, 'tags', set(), tags_history, self.db.selected_date)
126         self.default_effort = AttributeWithHistory(self, 'default_effort', 0.0, default_effort_history, self.db.selected_date)
127         self.dep_ids = AttributeWithHistory(self, 'dep_ids', set(), dep_ids_history, self.db.selected_date)
128         self.fences_adoptions = AttributeWithHistory(self, 'fences_adoptions', False, adoptivity_history, self.db.selected_date)
129
130     @classmethod
131     def from_dict(cls, db, d, id_):
132         t = cls(
133                db,
134                id_,
135                d['title_history'],
136                {k: set(v) for k, v in d['tags_history'].items()},
137                d['default_effort_history'],
138                {k: set(v) for k, v in d['deps_history'].items()},
139                d['adoptivity_history'],
140                comment=d['comment'],
141                forks_id=d['forks'])
142         return t
143
144     def to_dict(self):
145         return {
146             'title_history': self.title.history,
147             'default_effort_history': self.default_effort.history,
148             'tags_history': {k: list(v) for k, v in self.tags.history.items()},
149             'deps_history': {k: list(v) for k, v in self.dep_ids.history.items()},
150             'adoptivity_history': self.fences_adoptions.history,
151             'comment': self.comment,
152             'forks': self.forks_id,
153         }
154
155     @property
156     def forked_task(self):
157         return self.db.tasks[self.forks_id] if self.forks_id else None
158
159     @property
160     def deps(self):
161         deps = []
162         for id_ in self.dep_ids.now:
163             if len(id_) == 0:
164                 continue
165             elif id_ not in self.db.tasks.keys():
166                 raise PlomException(f'dep referenced on Task {self.id_} not found: {id_}')
167             deps += [self.db.tasks[id_]]
168         return deps
169
170     @property
171     def deps_weight(self):
172         def count_weight(task):
173             sub_count = 1
174             for dep in task.deps:
175                 sub_count += count_weight(dep)
176             return sub_count
177         return count_weight(self) 
178
179     @property
180     def dependers(self):
181         return [t for t in self.db.tasks.values() if self in t.deps]
182
183     def matches(self, search):
184         if search is None:
185             return False
186         else:
187             return search in self.title.now or search in self.comment or search in '$'.join(self.tags.now)
188
189     @property
190     def latest_effort_date(self):
191         todos = [t for t in self.db.todos.values() if t.task.id_ == self.id_]
192         todos.sort(key=lambda t: t.latest_date)
193         if len(todos) > 0:
194             return todos[-1].latest_date 
195         else:
196             return '' 
197
198
199
200 class Day:
201
202     def __init__(self, db, date, comment=''):
203         self.date = date 
204         self.db = db
205         self.comment = comment
206         self.linked_todos_as_list = []
207
208     @classmethod
209     def from_dict(cls, db, d, date=None):
210         comment = d['comment'] if 'comment' in d.keys() else ''
211         day = cls(db, date, comment)
212         return day
213
214     def to_dict(self):
215         d = {'comment': self.comment}
216         return d
217
218     @property
219     def linked_todos(self):
220         linked_todos = {}
221         for todo in self.linked_todos_as_list:
222             linked_todos[todo.id_] = todo
223         return linked_todos
224
225     def _todos_sum(self, include_undone=False):
226         s = 0
227         for todo in [todo for todo in self.linked_todos.values()
228                      if self.date in todo.efforts.keys()]:
229             day_effort = todo.efforts[self.date]
230             if todo.done:
231                 s += day_effort if day_effort else todo.task.default_effort.at(self.date)
232             elif include_undone:
233                 s += day_effort if day_effort else 0 
234         return s
235
236     @property
237     def todos_sum(self):
238         return self._todos_sum()
239
240     def sorted_todos(self, done, is_tree_shaped, sort_order, legal_keys):
241         todos = [t for t in self.linked_todos_as_list if t.visible and t.done == done]
242         reverse = False
243         sort_column = sort_order[:]
244         if sort_order and '-' == sort_order[0]:
245             reverse = True
246             sort_column = sort_order[1:]
247         if sort_column in legal_keys:
248             todos.sort(key=lambda t: getattr(t, sort_column))
249         if reverse:
250             todos.reverse()
251         if is_tree_shaped:
252             def walk_tree(todo, sorted_todos):
253                 todo.deps = [t for t in sorted_todos if t in todo.deps]
254                 for dep in [t for t in sorted_todos if t in todo.deps]:
255                     walk_tree(dep, sorted_todos)
256             if done:
257                 trunk = [t for t in todos if len([td for td in t.dependers if td.done]) == 0]
258             else:
259                 trunk = [t for t in todos if len(t.dependers) == 0]
260             for node in trunk:
261                 walk_tree(node, todos)
262             todos = trunk 
263         return todos
264
265     @property
266     def visible_in_export(self):
267         return len(self.comment) + len([t for t in self.linked_todos_as_list if t.visible]) > 0
268
269
270
271 class Todo(TaskLike):
272
273     def __init__(self,
274                  db,
275                  id_,
276                  task,
277                  done=False,
278                  comment='',
279                  day_tags=None,
280                  importance=1.0,
281                  efforts=None,
282                  dep_ids=None):
283         super().__init__(db, id_, comment)
284         self.task = task 
285         self._done = done
286         for k in efforts.keys():
287             try:
288                 datetime.strptime(k, DATE_FORMAT)
289             except ValueError:
290                 raise PlomException(f'Todo creation: bad date string: {k}')
291         self.efforts = efforts if efforts else {}
292         self.day_tags = day_tags if day_tags else set()
293         self.importance = importance
294         self.dep_ids = dep_ids if dep_ids else set() 
295         self.already_listed = False
296         self.been_observed = False 
297
298     @classmethod
299     def from_dict(cls, db, d, id_):
300         todo = cls(
301                 db,
302                 id_,
303                 db.tasks[d['task']],
304                 d['done'],
305                 d['comment'],
306                 set(d['day_tags']),
307                 d['importance'],
308                 d['efforts'],
309                 set(d['deps']))
310         return todo
311
312     def to_dict(self):
313         return {
314                 'task': self.task.id_,
315                 'done': self.done,
316                 'comment': self.comment,
317                 'day_tags': list(self.day_tags),
318                 'importance': self.importance,
319                 'efforts': self.efforts,
320                 'deps': list(self.dep_ids)}
321
322     @property
323     def title(self):
324         return self.task.title.at(self.db.selected_date)
325
326     @property
327     def dated_title(self):
328         return f'{self.earliest_date}:{self.title}'
329
330     @property
331     def deps(self):
332         return [self.db.todos[id_] for id_ in self.dep_ids]
333
334     @deps.setter
335     def deps(self, deps):
336         self.dep_ids = {dep.id_ for dep in deps}
337
338     @property
339     def default_effort(self):
340         return self.task.default_effort.at(self.earliest_date)
341
342     @property
343     def done(self):
344         return self.deps_done and self._done
345
346     @done.setter
347     def done(self, doneness):
348         self._done = doneness
349
350     @property
351     def deps_done(self):
352         if len(self.deps) > 0:
353             for dep in self.deps:
354                 if not dep.done:
355                     return False
356         return True
357
358     @property
359     def dep_efforts(self):
360         dep_efforts = 0
361         if self.deps:
362             for dep in self.deps: 
363                 dep_efforts += dep.all_days_effort
364                 dep_efforts += dep.dep_efforts
365         return dep_efforts
366
367     def depender_path(self, dep):
368         if self.dependers:
369             return f'Y{depender.depender_path()}:{self.title}Y'
370         else:
371             return f'X{self.title}X'
372
373     @property
374     def depender_paths(self):
375         paths = []
376         for depender in self.dependers:
377             if len(depender.depender_paths) == 0:
378                  paths += [[depender]]
379             else:
380                 for path in depender.depender_paths:
381                     paths += [path + [depender]] 
382         return paths
383
384     @property
385     def shortened_depender_paths(self):
386         new_paths = []
387         for path in self.depender_paths:
388             new_path = []
389             for i, step in enumerate(path):
390                 next_step = None if len(path) <= i+1 else path[i+1]
391                 for distinct_path in new_paths:
392                     if len(distinct_path) >= i+2 and distinct_path[i+1] == next_step:
393                         step = None
394                 new_path += [step]
395             new_paths += [new_path]
396         return new_paths
397
398     @property
399     def all_days_effort(self):
400         total = 0
401         for effort in self.efforts.values():
402             total += effort if effort else 0
403         if self.done:
404             total = max(total, self.task.default_effort.at(self.latest_date))
405         return total
406
407     def matches(self, search):
408         if search is None:
409             return False
410         else:
411             return search in self.comment or search in '$'.join(self.tags) or search in self.title
412
413     def is_effort_removable(self, date):
414         if not date in self.efforts.keys():
415             return False
416         if self.efforts[date]:
417             return False
418         if self.done and date == self.latest_date:
419             return False
420         return True
421
422     @property
423     def tags(self):
424         return self.day_tags | self.task.tags.now
425
426     @property
427     def day_effort(self):
428         return self.efforts[self.db.selected_date]
429
430     @property
431     def effort_at_selected_date(self):
432         if self.db.selected_date in self.efforts.keys() and self.day_effort is not None:
433             return self.day_effort 
434         else:
435             return self.task.default_effort.then
436
437     @property
438     def day(self):
439         return self.db.days[self.earliest_date]
440
441     @property
442     def sorted_effort_dates(self):
443         dates = list(self.efforts.keys())
444         dates.sort()
445         return dates
446
447     @property
448     def earliest_date(self):
449         return self.sorted_effort_dates[0]
450
451     @property
452     def latest_date(self):
453         return self.sorted_effort_dates[-1]
454
455     @property
456     def has_dependers(self):
457         return len(self.dependers) > 0
458
459     @property
460     def dependers(self):
461         return [t for t in self.db.todos.values() if self.id_ in t.dep_ids]
462
463     @property
464     def has_deps(self):
465         return len(self.deps) > 0
466
467     @property
468     def sort_done(self):
469         return self.day_effort if self.day_effort else 0 
470
471     @property
472     def family_effort(self):
473         return self.effort_at_selected_date + self.dep_efforts
474
475     def closest_adoptable(self, task_id):
476         print("DEBUG closest_adoptable", self.id_)
477         ranked_adoptables = {self.id_: 0} 
478         def collect_downwards(node, ranked_adoptables, n_steps):
479             print("debug called collect_downwards", node.title, n_steps)
480             if not node.id_ in ranked_adoptables.keys():
481                 ranked_adoptables[node.id_] = n_steps
482             for dep in node.deps:
483                 collect_downwards(dep, ranked_adoptables, n_steps+1)
484         def collect_upwards(node, adoptables, n_steps):
485             print("debug called collect_upwards", node.title, n_steps)
486             collect_downwards(node, ranked_adoptables, n_steps+1)
487             if not node.task.fences_adoptions.now:
488                 print("debug not blocking", node.dependers)
489                 for depender in node.dependers:
490                     collect_upwards(depender, ranked_adoptables, n_steps+1)
491                 if len(node.dependers) == 0:
492                     print("debug NO DEPENDERS, calling day")
493                     for t in self.db.days[self.latest_date].linked_todos_as_list:
494                         print("debug linked_todos_as_list:", t.title, t.id_)
495                         collect_downwards(t, ranked_adoptables, n_steps)
496             else:
497                 print("debug BLOCKING")
498         collect_upwards(self, ranked_adoptables, 0)
499         # for depender in self.dependers:
500         #     collect_upwards(depender, ranked_adoptables, 0)
501         del ranked_adoptables[self.id_]
502         for id_, distance in sorted(ranked_adoptables.items(), key=lambda i:i[1], reverse=True):
503             if self.db.todos[id_].task.id_ == task_id:
504                 return self.db.todos[id_]
505         return None
506
507
508     def observe(self):
509         self.been_observed = True
510         return '' 
511
512
513
514 class TodoDB(PlomDB):
515
516     def __init__(self,
517             selected_date=None,
518             tag_filter_and = None,
519             tag_filter_not = None):
520         self.selected_date = selected_date if selected_date else today_date()
521         self.tag_filter_and = tag_filter_and if tag_filter_and else []
522         self.tag_filter_not = tag_filter_not if tag_filter_not else []
523
524         self.days = {}
525         self.tasks = {}
526         self.t_tags = set()
527         self.todos = {}
528         super().__init__(db_path)
529
530     # savefile I/O
531
532     def read_db_file(self, f):
533         d = json.load(f)
534
535         for id_, t_dict in d['tasks'].items():
536             t = self.add_task(id_=id_, dict_source=t_dict)
537             for tag in t.tags.now:
538                 self.t_tags.add(tag)
539         # for task in self.tasks.values():
540         #     for dep in task.deps:
541         #         dep.dependers += [task]
542
543         for id_, todo_dict in d['todos'].items():
544             todo = self.add_todo(todo_dict, id_)
545             for tag in todo.day_tags:
546                 self.t_tags.add(tag)
547         # for todo in self.todos.values():
548         #     for dep in todo.deps:
549         #         dep.dependers += [todo]
550
551         for date, day_dict in d['days'].items():
552             self.add_day(dict_source=day_dict, date=date)
553         for todo in self.todos.values():
554             for date in todo.efforts.keys():
555                 if not date in self.days.keys():
556                     self.add_day(date)
557                 self.days[date].linked_todos_as_list += [todo]
558
559         # self.set_visibilities()
560
561     def to_dict(self):
562         d = {'tasks': {}, 'days': {}, 'todos': {}}
563         for uuid, t in self.tasks.items():
564              d['tasks'][uuid] = t.to_dict()
565         for date, day in self.days.items():
566             d['days'][date] = day.to_dict()
567         for id_, todo in self.todos.items():
568             d['todos'][id_] = todo.to_dict()
569         return d
570
571     def write(self):
572         dates_to_purge = []
573         for date, day in self.days.items():
574             if len(day.linked_todos) == 0 and len(day.comment) == 0:
575                 dates_to_purge += [date]
576         for date in dates_to_purge:
577             del self.days[date]
578         self.write_text_to_db(json.dumps(self.to_dict()))
579
580     # properties 
581
582     @property
583     def selected_day(self):
584         if not self.selected_date in self.days.keys():
585             self.days[self.selected_date] = self.add_day(date=self.selected_date)
586         return self.days[self.selected_date]
587
588     # table manipulations 
589
590     def add_day(self, date, dict_source=None):
591         day = Day.from_dict(self, dict_source, date) if dict_source else Day(self, date)
592         self.days[date] = day 
593         return day
594
595     def add_task(self, id_=None, dict_source=None):
596         id_ = id_ if id_ else str(uuid4())
597         t = Task.from_dict(self, dict_source, id_) if dict_source else Task(self, id_)
598         self.tasks[id_] = t
599         return t
600
601     def fork_task(self, id_):
602         origin = self.tasks[id_]
603         now = today_date(with_time=True)
604         fork_id = str(uuid4())
605         fork = Task(self, fork_id, {}, {}, {}, {}, origin.comment, id_)
606         self.tasks[fork_id] = fork
607         return fork
608
609     def update_task(self, id_, title, default_effort, tags, fences_adoptions, comment, dep_ids, depender_ids):
610         task = self.tasks[id_] if id_ in self.tasks.keys() else self.add_task(id_)
611         task.title.set(title)
612         task.default_effort.set(default_effort)
613         task.tags.set(tags)
614         task.fences_adoptions.set(fences_adoptions)
615         task.comment = comment 
616         task.dep_ids.set(dep_ids)
617         for depender in task.dependers:
618             if not depender.id_ in depender_ids:
619                 depender_dep_ids = depender.dep_ids.now
620                 depender_dep_ids.remove(task.id_)
621                 depender.dep_ids.set(depender_dep_ids)
622         for depender_id in depender_ids:
623             depender = self.tasks[depender_id]
624             depender_dep_ids = depender.dep_ids.now
625             depender_dep_ids.add(task.id_)
626             depender.dep_ids.set(depender_dep_ids)
627         task.loop_check()
628         return task
629
630     def add_todo(self, todo_dict=None, id_=None, task=None, efforts=None, parenthood='', force_new=False, creator=None):
631         make_children = parenthood != 'childless'
632         adopt_if_possible = parenthood == 'adoption'
633         id_ = id_ if id_ else str(uuid4())
634         if todo_dict:
635             todo = Todo.from_dict(self, todo_dict, id_)
636             self.todos[id_] = todo
637         elif task and efforts:
638             todo = Todo(self, id_, task, efforts=efforts)
639             print("DEBUG calling add_todo for", task.title.now, id_) 
640             print("debug creator:", creator) 
641             self.todos[id_] = todo
642             if creator:
643                 creator.deps += [todo]
644             # deps = []
645             if make_children and todo.latest_date in self.days.keys():
646                 for dep_task in task.deps:
647                     # if Todo expects dependencies, adopt any compatibles found in DB.selected_date
648                     # before creating new depended Todos
649                     dep_todo = None
650                     # if not force_new:
651                     if adopt_if_possible:
652                         print("debug trying adoption for", dep_task.title.now)
653                         # adoptable_todos = [t for t in self.days[todo.latest_date].linked_todos_as_list
654                         #                    if t.task.id_ == dep_task.id_]
655                         # if len(adoptable_todos) > 0:
656                         #     dep_todo = adoptable_todos[0]
657                         dep_todo = todo.closest_adoptable(dep_task.id_)
658                         print("debug GOT ADOPTION", dep_todo)
659                     if not dep_todo:
660                         # dep_todo = self.add_todo(task=dep_task, efforts=efforts, force_new)
661                         print("debug no adoption for", dep_task.title.now, id_)
662                         dep_todo = self.add_todo(task=dep_task, efforts=efforts, parenthood=parenthood, creator=todo)
663                     todo.deps += [dep_todo]
664                     # deps += [dep_todo]
665             # todo.deps = deps 
666         for date in todo.efforts.keys():
667             if date in self.days.keys():  # possibly not all Days have been initialized yet
668                 self.days[date].linked_todos_as_list += [todo] 
669         return todo
670
671     def _update_todo_shared(self, id_, done, comment, importance):
672         todo = self.todos[id_]
673         todo.done = done
674         todo.comment = comment 
675         todo.importance = importance 
676         return todo
677
678     def update_todo_for_day(self, id_, date, effort, done, comment, importance):
679         todo = self._update_todo_shared(id_, done, comment, importance)
680         todo.efforts[date] = effort 
681
682     def update_todo(self, id_, efforts, done, comment, tags, importance, deps, depender_ids):
683         todo = self._update_todo_shared(id_, done, comment, importance)
684         if len(efforts) == 0 and not todo.deps:
685             raise PlomException('todo must have at least one effort!')
686         todo.efforts = efforts 
687         for date in todo.efforts.keys():
688             if not date in self.days.keys():
689                 self.add_day(date=date) 
690             if not self in self.days[date].linked_todos_as_list:
691                 self.days[date].linked_todos_as_list += [todo]
692         todo.day_tags = tags
693         todo.deps = deps 
694         for depender_id in depender_ids:
695             depender = self.todos[depender_id]
696             depender.dep_ids.add(todo.id_)
697         for depender in todo.dependers:
698             if not depender.id_ in depender_ids:
699                 depender.dep_ids.remove(todo.id_)
700         todo.loop_check()
701
702     def delete_todo(self, id_):
703         todo = self.todos[id_]
704         dates_to_delete = []
705         for date in todo.efforts.keys():
706             dates_to_delete += [date]
707         for date in dates_to_delete:
708             self.delete_effort(todo, date, force=True)
709         for depender in todo.dependers:
710             depender.dep_ids.remove(todo.id_)
711         del self.todos[id_]
712
713     def delete_task(self, id_):
714         del self.tasks[id_]
715
716     def delete_effort(self, todo, date, force=False):
717         if (not force) and len(todo.efforts) == 1:
718             raise PlomException('todo must retain at least one effort!')
719         self.days[date].linked_todos_as_list.remove(todo)
720         del todo.efforts[date]
721
722     # views
723
724     def get_message(self, message):
725         return j2env.get_template('message.html').render(message=message)
726
727     def get_calendar_export(self, start_date_str, end_date_str):
728         days_to_show = self.init_calendar_items(start_date_str, end_date_str)
729         return j2env.get_template('calendar_export.html').render(days=days_to_show)
730
731     def get_calendar(self, start_date_str, end_date_str):
732         days_to_show = self.init_calendar_items(start_date_str, end_date_str)
733         return j2env.get_template('calendar.html').render(
734                 selected_date=self.selected_date,
735                 days=days_to_show,
736                 start_date=start_date_str,
737                 end_date=end_date_str)
738
739     def get_day_todos(self, undone_sort_order=None, done_sort_order=None, is_tree_shaped=False, todo_parenthood=None):
740         legal_undone_sort_keys = {'title', 'sort_done', 'default_effort', 'importance'} 
741         legal_done_sort_keys = {'title', 'effort_at_selected_date', 'family_effort'} 
742
743         current_date = datetime.strptime(self.selected_date, DATE_FORMAT)
744         prev_date = current_date - timedelta(days=1)
745         prev_date_str = prev_date.strftime(DATE_FORMAT)
746         next_date = current_date + timedelta(days=1)
747         next_date_str = next_date.strftime(DATE_FORMAT)
748
749         adoptable_past_todos = []
750         for todo in [t for t in self.todos.values()
751                      if t.visible
752                      and (not t.done)
753                      and t.earliest_date < self.selected_date]:
754              adoptable_past_todos += [todo] 
755         undone_todos = self.selected_day.sorted_todos(False, is_tree_shaped, undone_sort_order,
756                                                       legal_undone_sort_keys)
757         done_todos = self.selected_day.sorted_todos(True, is_tree_shaped, done_sort_order,
758                                                     legal_done_sort_keys)
759
760         return j2env.get_template('day_todos.html').render(
761                 day=self.selected_day,
762                 tags=self.t_tags,
763                 filter_and=self.tag_filter_and,
764                 filter_not=self.tag_filter_not,
765                 prev_date=prev_date_str,
766                 adoptable_past_todos=adoptable_past_todos,
767                 next_date=next_date_str,
768                 all_tasks=[t for t in self.tasks.values()],
769                 undone_todos=undone_todos,
770                 done_todos=done_todos,
771                 is_tree_shaped=is_tree_shaped,
772                 undone_sort=undone_sort_order,
773                 done_sort=done_sort_order,
774                 parenthood=todo_parenthood)
775
776     def get_todo(self, id_, parenthood):
777         todo = self.todos[id_]
778         filtered_tasks = [t for t in self.tasks.values()
779                           if t != todo.task] 
780         filtered_todos = [t for t in self.todos.values()
781                           if t != todo
782                           and t not in todo.deps]
783
784         legal_dates = list(todo.efforts.keys())
785         date_filtered_todos = [] 
786         for date in legal_dates:
787             for filtered_todo in filtered_todos:
788                 if filtered_todo in date_filtered_todos:
789                     continue
790                 if date in filtered_todo.efforts.keys():
791                     date_filtered_todos += [filtered_todo]
792
793         dep_slots = []
794         for dep in todo.task.deps:
795             dep_slots += [{'task': dep,
796                                'todos': [t for t in todo.deps if t.task == dep]}]
797
798         suggested_todos = {}
799         for dep in todo.task.deps:
800             suggested_todos[dep.id_] = [t for t in date_filtered_todos if t.task.id_ == dep.id_]
801         additional_deps = [t for t in todo.deps if not t.task in todo.task.deps]
802
803         return j2env.get_template('todo.html').render(
804                 tags=self.t_tags,
805                 todo=todo,
806                 filtered_todos=date_filtered_todos,
807                 filtered_tasks=filtered_tasks,
808                 dep_slots=dep_slots,
809                 suggested_todos=suggested_todos,
810                 additional_deps=additional_deps,
811                 parentood=parenthood, 
812                 dep_todos=todo.deps)
813
814     def get_task(self, id_):
815         if id_:
816             if not id_ in self.tasks.keys():
817                 raise PlomException('no Task for ID')
818             task = self.tasks[id_]
819         else:
820             task = self.add_task()
821         if not id_:
822             task.default_effort.set(1.0)
823         filtered_tasks = [t for t in self.tasks.values()
824                           if t != task
825                           and (t not in task.deps)]
826
827         return j2env.get_template('task.html').render(
828                 selected_date=self.selected_date,
829                 tags=self.t_tags, 
830                 filtered_tasks=filtered_tasks,
831                 task=task)
832
833     def get_tasks(self, search, sort_order=None):
834         filtered_tasks = [] 
835         for task in [t for t in self.tasks.values() if (not search) or t.matches(search)]:
836             filtered_tasks += [task]
837         reverse = False
838         sort_column = sort_order 
839         if sort_order and '-' == sort_order[0]:
840             reverse = True
841             sort_column = sort_order[1:]
842         if sort_column == 'title':
843             filtered_tasks.sort(key=lambda t: t.title.now)
844         elif sort_column == 'default_effort':
845             filtered_tasks.sort(key=lambda t: t.default_effort.now)
846         elif sort_column == 'weight':
847             filtered_tasks.sort(key=lambda t: t.deps_weight)
848         elif sort_column == 'latest_effort_date':
849             filtered_tasks.sort(key=lambda t: t.latest_effort_date)
850         if reverse:
851             filtered_tasks.reverse()
852         return j2env.get_template('tasks.html').render(
853                 sort=sort_order,
854                 tasks=filtered_tasks,
855                 tags=self.t_tags,
856                 filter_and=self.tag_filter_and,
857                 filter_not=self.tag_filter_not,
858                 search=search)
859
860     # posts
861
862     def post_todo(self, id_, postvars, todo_parenthood):
863         if postvars.has('delete') and (not id_ in self.todos.keys()):
864             raise PlomException('can only do this on Todo that already exists')
865         old_todo = self.todos[id_] if id_ in self.todos.keys() else None
866         latest_date = self.selected_date 
867         efforts = {}
868         for i, date in enumerate(postvars.get_all('effort_date', [])):
869             if '' == date:
870                 continue
871             latest_date = date
872             efforts[date] = postvars.get_at_index('effort', i, float_if_possible=True)
873         if postvars.has('delete'): 
874             raise PlomException('can only do this on Task that already exists')
875             has_day_effort = len([e for e in efforts.values() if e is not None]) > 0
876             if postvars.has('done')\
877                     or postvars.get('comment')\
878                     or postvars.get_all('tag', [])\
879                     or has_day_effort:
880                 raise PlomException('will not remove todo of preserve-worthy values')
881             self.delete_todo(id_) 
882             return False
883         elif postvars.has('update'):
884             if postvars.has('delete_effort'):
885                 for date in postvars.get_all('delete_effort'):
886                     self.delete_effort(old_todo, date)
887                     del efforts[date]
888             deps = [self.todos[adopt_id] for adopt_id in postvars.get_all('adopt_dep', [])
889                     if adopt_id in self.todos.keys()] 
890             birth_dep_ids = postvars.get_all('birth_dep', [])
891             for bad_id in [bad_id for bad_id in birth_dep_ids if not bad_id in self.tasks.keys()]:
892                 raise PlomException('submitted illegal dep ID')
893             tasks_to_birth = [self.tasks[dep_id] for dep_id in birth_dep_ids]
894             for task in tasks_to_birth:
895                 deps += [self.add_todo(task=task,
896                                        efforts={latest_date: None},
897                                        parenthood=todo_parenthood)]
898             depender_ids = postvars.get_all('depender', [])
899             self.update_todo(id_=id_,
900                              efforts=efforts,
901                              done=postvars.has('done'),
902                              comment=postvars.get('comment', ''),
903                              tags=postvars.get_all('tag', []),
904                              importance=postvars.get('importance', float_if_possible=True),
905                              deps=deps,
906                              depender_ids=depender_ids)
907         return True 
908
909     def post_task(self, id_, postvars):
910            if (postvars.has('delete') or postvars.has('fork')) and (not id_ in self.tasks.keys()):
911                raise PlomException('can only do this on Task that already exists')
912            if postvars.has('delete'):
913                if [t for t in self.todos.values() if id_ == t.task.id_]:
914                    raise PlomException('will not remove Task describing existing Todos')
915                if postvars.get('title', '')\
916                        or postvars.get_all('tag', [])\
917                        or postvars.get_all('dep', [])\
918                        or postvars.get('comment', ''):
919                    raise PlomException('will not remove Task of preserve-worthy values')
920                self.delete_task(id_)
921                return None 
922            elif postvars.has('update'):
923                dep_ids = postvars.get_all('dep', [])
924                for bad_id in [bad_id for bad_id in dep_ids if not bad_id in self.tasks.keys()]:
925                    raise PlomException('submitted illegal dep ID')
926                depender_ids = postvars.get_all('depender', [])
927                for bad_id in [bad_id_ for bad_id in depender_ids if not bad_id in self.tasks.keys()]:
928                    raise PlomException('submitted illegal dep ID')
929                task = self.update_task(
930                        id_=id_,
931                        title=postvars.get('title', ''),
932                        default_effort=postvars.get('default_effort', float_if_possible=True), 
933                        tags=postvars.get_all('tag', []),
934                        comment=postvars.get('comment', ''),
935                        fences_adoptions=postvars.get('fences_adoptions', False),
936                        dep_ids=dep_ids,
937                        depender_ids=depender_ids)
938                if postvars.has('add_as_todo'):
939                    self.add_todo(task=task, efforts={postvars.get('new_todo_date'): None})
940            elif postvars.has('fork'):
941                t = self.fork_task(id_)
942                return t.id_
943            return id_
944
945     def post_day_todos(self, postvars, todo_parenthood):
946         if not postvars.has('update'):
947             return
948         self.selected_date = postvars.get('date') 
949         self.selected_day.comment = postvars.get('day_comment', '') 
950         task_id = postvars.get('choose_task', None)
951         if task_id:
952             if task_id not in self.tasks.keys():
953                 raise PlomException('illegal task ID entered')
954             self.add_todo(task=self.tasks[task_id], efforts={self.selected_date: None},
955                           parenthood=todo_parenthood)
956         for todo_id in postvars.get_all('choose_todo', []):
957             self.todos[todo_id].efforts[self.selected_date] = None
958         for i, todo_id in enumerate(postvars.get_all('todo_id', [])):
959             old_todo = self.todos[todo_id]
960             done = todo_id in postvars.get_all('done', [])
961             day_effort_input = postvars.get_at_index('effort', i, '') 
962             day_effort = float(day_effort_input) if len(day_effort_input) > 0 else None
963             comment = postvars.get_at_index('effort_comment', i, '') 
964             if (day_effort is not None) and (not done) and day_effort < 0 and 0 == len(comment):
965                 if len(old_todo.efforts) > 1:
966                     self.delete_effort(old_todo, self.selected_date) 
967                 else:
968                     self.delete_todo(todo_id)
969                 continue
970             importance = float(postvars.get_at_index('importance', i))
971             if old_todo\
972                     and old_todo.done == done\
973                     and old_todo.day_effort == day_effort\
974                     and comment == old_todo.comment\
975                     and old_todo.importance == importance:
976                 continue
977             self.update_todo_for_day(
978                     todo_id,
979                     self.selected_date,
980                     day_effort,
981                     done,
982                     comment,
983                     importance)
984
985     # helpers
986
987     def init_calendar_items(self, start_date_str, end_date_str):
988         self.tag_filter_and = ['calendar']
989         self.tag_filter_not = ['deleted']
990
991         todays_date_obj = datetime.strptime(today_date(), DATE_FORMAT) 
992         yesterdays_date_obj = todays_date_obj - timedelta(1)
993         def get_day_limit_obj(index, day_limit_string):
994             date_obj = datetime.strptime(sorted(self.days.keys())[index], DATE_FORMAT)
995             if day_limit_string and len(day_limit_string) > 0:
996                 if day_limit_string in {'today', 'yesterday'}:
997                     date_obj = todays_date_obj if day_limit_string == 'today' else yesterdays_date_obj
998                 else:
999                     date_obj = datetime.strptime(day_limit_string, DATE_FORMAT)
1000             return date_obj
1001         start_date_obj = get_day_limit_obj(0, start_date_str)
1002         end_date_obj = get_day_limit_obj(-1, end_date_str)
1003
1004         days_to_show = {}
1005         for n in range(int((end_date_obj - start_date_obj).days) + 1):
1006             date_obj = start_date_obj + timedelta(n)
1007             date_str = date_obj.strftime(DATE_FORMAT)
1008             if date_str not in self.days.keys():
1009                 days_to_show[date_str] = self.add_day(date_str)
1010             else:
1011                 days_to_show[date_str] = self.days[date_str]
1012             days_to_show[date_str].month_title = date_obj.strftime('%B') if date_obj.day == 1 else None 
1013             days_to_show[date_str].weekday = datetime.strptime(date_str, DATE_FORMAT).strftime('%A')[:2]
1014         return days_to_show
1015
1016
1017
1018 class ParamsParser:
1019
1020     def __init__(self, parsed_url_query, cookie_db):
1021         self.params = parse_qs(parsed_url_query)
1022         self.cookie_db = cookie_db
1023
1024     def get(self, key, default=None, as_bool=False):
1025         param = self.params.get(key, [default])[0]
1026         if as_bool:
1027             if param == '0':
1028                 param = False
1029             elif param is not None:
1030                 param = True
1031         return param 
1032
1033     def cookie_key_from_params_key(self, prefix, key):
1034         return f'{prefix}:{key}' if prefix else key
1035
1036     def _get_cookied(self, cookie_key, params):
1037         if params in [['-'], '-']:
1038             params = None 
1039             if cookie_key in self.cookie_db.keys():
1040                 del self.cookie_db[cookie_key]
1041         if params is None and cookie_key in self.cookie_db.keys():
1042             params = self.cookie_db[cookie_key]
1043         if params is not None:
1044             self.cookie_db[cookie_key] = params
1045         return params
1046
1047     def get_cookied(self, key, default=None, prefix=None, as_bool=False):
1048         cookie_key = self.cookie_key_from_params_key(prefix, key) 
1049         param = self.get(key, default, as_bool)
1050         return self._get_cookied(cookie_key, param)
1051
1052     def get_cookied_chain(self, key, default=None, prefix=None):
1053         cookie_key = self.cookie_key_from_params_key(prefix, key) 
1054         params = self.params.get(key, default)
1055         return self._get_cookied(cookie_key, params)
1056
1057
1058
1059 class PostvarsParser:
1060
1061     def __init__(self, postvars):
1062         self.postvars = postvars
1063
1064     def has(self, key):
1065         return key in self.postvars.keys()
1066
1067     def get(self, key, on_empty=None, float_if_possible=False):
1068         return self.get_at_index(key, 0, on_empty, float_if_possible)
1069
1070     def get_at_index(self, key, i, on_empty=None, float_if_possible=False):
1071         if self.has(key) and len(self.postvars[key][i]) > 0:
1072             val = self.postvars[key][i] 
1073         else:
1074             val = on_empty 
1075         if float_if_possible and val is not None:
1076             return float(val)
1077         else:
1078             return val 
1079
1080     def get_all(self, key, on_empty=None):
1081         if self.has(key) and len(self.postvars[key]) > 0:
1082             return [v for v in self.postvars[key] if len(v) > 0]
1083         return on_empty
1084
1085     def set(self, key, value):
1086         self.postvars[key] = [value]
1087
1088
1089
1090 class TodoHandler(PlomHandler):
1091
1092     def config_init(self):
1093         return {
1094             'cookie_name': 'todo_cookie',
1095             'cookie_path': '/'
1096         }
1097
1098     def app_init(self, handler):
1099         default_path = '/todo'
1100         handler.add_route('GET', default_path, self.show_db)
1101         handler.add_route('POST', default_path, self.write_db)
1102         return 'todo', {'cookie_name': 'todo_cookie', 'cookie_path': default_path}
1103
1104     def do_POST(self):
1105         self.try_do(self.write_db)
1106
1107     def write_db(self):
1108         from urllib.parse import urlencode
1109         config = self.apps['todo'] if hasattr(self, 'apps') else self.config_init()
1110         parsed_url = urlparse(self.path)
1111         site = path_split(parsed_url.path)[1]
1112         length = int(self.headers['content-length'])
1113         postvars = PostvarsParser(parse_qs(self.rfile.read(length).decode(), keep_blank_values=1))
1114
1115         db = TodoDB()
1116         redir_params = []
1117         # if we do encounter a filter post, we repost it (and if empty, the emptying command '-')
1118         for param_name, filter_db_name in {('and_tag', 'tag_filter_and'),
1119                                            ('not_tag', 'tag_filter_not')}:
1120             filter_db = getattr(db, filter_db_name)
1121             if postvars.has(param_name): 
1122                 for target in postvars.get_all(param_name, []):
1123                     if len(target) > 0 and not target in filter_db:
1124                         filter_db += [target]
1125                 if len(filter_db) == 0:
1126                     redir_params += [(param_name, '-')]
1127             redir_params += [(param_name, f) for f in filter_db]
1128         if site in {'calendar', 'todo'}:
1129             redir_params += [('end', postvars.get('end', '-'))]
1130             redir_params += [('start', postvars.get('start', '-'))]
1131         if site in {'day_todos', 'todo'}:
1132             todo_parenthood = postvars.get('parenthood')
1133             redir_params += [('parenthood', todo_parenthood)]
1134         if site in {'todo', 'task'}:
1135             id_ = postvars.get('id')
1136
1137         if 'tasks' == site:
1138             redir_params += [('search', postvars.get('search', ''))]
1139         elif 'todo' == site:
1140             if db.post_todo(id_, postvars, todo_parenthood):
1141                 redir_params += [('id', id_)]
1142             else:
1143                 site = 'calendar'
1144         elif 'task' == site:
1145             ret = db.post_task(id_, postvars)
1146             if ret:
1147                 redir_params += [('id', ret)]
1148             else:
1149                 site = 'calendar'
1150         elif 'day_todos' == site:
1151             db.post_day_todos(postvars, todo_parenthood)
1152             redir_params += [('date', db.selected_date)]
1153
1154         encoded_params = urlencode(redir_params)
1155         homepage = f'{site}?{encoded_params}'
1156         db.write()
1157         self.redirect(homepage)
1158
1159     def do_GET(self):
1160         self.try_do(self.show_db)
1161
1162     def show_db(self):
1163         config = self.apps['todo'] if hasattr(self, 'apps') else self.config_init()
1164         parsed_url = urlparse(self.path)
1165         site = path_split(parsed_url.path)[1]
1166
1167         cookie_db = self.get_cookie_db(config['cookie_name'])
1168         params = ParamsParser(parsed_url.query, cookie_db)
1169         selected_date = tag_filter_and = tag_filter_not = None
1170         if site in {'day_todos', 'calendar', 'task'}:
1171             selected_date = params.get_cookied('date')
1172         if site in {'day_todos', 'tasks'}:
1173             tag_filter_and = params.get_cookied_chain('and_tag', prefix=site)
1174             tag_filter_not = params.get_cookied_chain('not_tag', prefix=site)
1175         if site in {'day_todos', 'todo'}:
1176             todo_parenthood = params.get_cookied('parenthood', prefix=site)
1177         if site in {'calendar', 'calendar_export', ''}:
1178             start_date = params.get_cookied('start', prefix=site)
1179             end_date = params.get_cookied('end', prefix=site)
1180         if site in {'todo', 'task'}:
1181             id_ = params.get('id')
1182
1183         db = TodoDB(selected_date, tag_filter_and, tag_filter_not)
1184         if 'reset_cookie' == site:
1185             cookie_db = {  # sensible defaults
1186                 params.cookie_key_from_params_key('day_todos', 'tree'): True,
1187                 params.cookie_key_from_params_key('todo', 'and_tag'): ['default'],
1188                 params.cookie_key_from_params_key('todo', 'not_tag'): ['ignore'],
1189                 params.cookie_key_from_params_key('todo', 'start'): 'yesterday',
1190                 params.cookie_key_from_params_key('todo', 'end'): 'today'}
1191             page = db.get_message('cookie unset!')
1192         elif 'day_todos' == site:
1193             tree_view = params.get_cookied('tree', prefix=site, as_bool=True)
1194             undone_sort_order = params.get_cookied('undone_sort', prefix=site)
1195             done_sort_order = params.get_cookied('done_sort', prefix=site)
1196             page = db.get_day_todos(undone_sort_order, done_sort_order, tree_view, todo_parenthood)
1197         elif site == 'todo':
1198             page = db.get_todo(id_, parenthood=todo_parenthood)
1199         elif 'task' == site:
1200             page = db.get_task(id_)
1201         elif 'tasks' == site:
1202             sort_order = params.get_cookied('sort', prefix=site)
1203             search = params.get('search', '')
1204             page = db.get_tasks(search, sort_order)
1205         elif 'add_task' == site:
1206             page = db.get_task(None)
1207         elif 'calendar_export' == site:
1208             page = db.get_calendar_export(start_date, end_date)
1209         else:  # 'calendar' == site
1210             page = db.get_calendar(start_date, end_date)
1211
1212         self.set_cookie(config['cookie_name'], config['cookie_path'], cookie_db)
1213         self.send_HTML(page)
1214
1215
1216
1217 if __name__ == "__main__":
1218     run_server(server_port, TodoHandler)