1 from plomlib import PlomDB, run_server, PlomHandler, PlomException
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'
11 DATE_FORMAT = '%Y-%m-%d'
12 j2env = JinjaEnv(loader=JinjaFSLoader('todo_templates'))
16 def today_date(with_time=False):
17 length = 19 if with_time else 10
18 return str(datetime.now())[:length]
22 class AttributeWithHistory:
24 def __init__(self, parent, name, default_if_empty, history=None, then_date='2000-01-01'):
27 self.default = default_if_empty
28 self.then_date = then_date
29 self.history = history if history else {}
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
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):
43 ret = self.history[sorted_dates[0]]
44 for date_key, item in self.history.items():
45 if date_key > end_of_queried_day:
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
57 return self.history[keys[-1]]
61 return self.at(self.then_date)
67 def __init__(self, db, id_, comment):
70 self.comment = comment
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
78 return visible_by_and and visible_by_not
82 if len(self.deps) == 0:
84 return 1 + max([d.deps_depth for d in self.deps])
89 def add_deps(node, all_deps):
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)
99 loop_msg = "can't set dep, would create loop"
101 for dep in node.deps:
103 raise PlomException(loop_msg)
110 class Task(TaskLike):
117 default_effort_history=None,
118 dep_ids_history=None,
119 adoptivity_history=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)
131 def from_dict(cls, db, d, id_):
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'],
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,
156 def forked_task(self):
157 return self.db.tasks[self.forks_id] if self.forks_id else None
162 for id_ in self.dep_ids.now:
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_]]
171 def deps_weight(self):
172 def count_weight(task):
174 for dep in task.deps:
175 sub_count += count_weight(dep)
177 return count_weight(self)
181 return [t for t in self.db.tasks.values() if self in t.deps]
183 def matches(self, search):
187 return search in self.title.now or search in self.comment or search in '$'.join(self.tags.now)
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)
194 return todos[-1].latest_date
202 def __init__(self, db, date, comment=''):
205 self.comment = comment
206 self.linked_todos_as_list = []
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)
215 d = {'comment': self.comment}
219 def linked_todos(self):
221 for todo in self.linked_todos_as_list:
222 linked_todos[todo.id_] = todo
225 def _todos_sum(self, include_undone=False):
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]
231 s += day_effort if day_effort else todo.task.default_effort.at(self.date)
233 s += day_effort if day_effort else 0
238 return self._todos_sum()
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]
243 sort_column = sort_order[:]
244 if sort_order and '-' == sort_order[0]:
246 sort_column = sort_order[1:]
247 if sort_column in legal_keys:
248 todos.sort(key=lambda t: getattr(t, sort_column))
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)
257 trunk = [t for t in todos if len([td for td in t.dependers if td.done]) == 0]
259 trunk = [t for t in todos if len(t.dependers) == 0]
261 walk_tree(node, todos)
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
271 class Todo(TaskLike):
283 super().__init__(db, id_, comment)
286 for k in efforts.keys():
288 datetime.strptime(k, DATE_FORMAT)
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
299 def from_dict(cls, db, d, id_):
314 'task': self.task.id_,
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)}
324 return self.task.title.at(self.db.selected_date)
327 def dated_title(self):
328 return f'{self.earliest_date}:{self.title}'
332 return [self.db.todos[id_] for id_ in self.dep_ids]
335 def deps(self, deps):
336 self.dep_ids = {dep.id_ for dep in deps}
339 def default_effort(self):
340 return self.task.default_effort.at(self.earliest_date)
344 return self.deps_done and self._done
347 def done(self, doneness):
348 self._done = doneness
352 if len(self.deps) > 0:
353 for dep in self.deps:
359 def dep_efforts(self):
362 for dep in self.deps:
363 dep_efforts += dep.all_days_effort
364 dep_efforts += dep.dep_efforts
367 def depender_path(self, dep):
369 return f'Y{depender.depender_path()}:{self.title}Y'
371 return f'X{self.title}X'
374 def depender_paths(self):
376 for depender in self.dependers:
377 if len(depender.depender_paths) == 0:
378 paths += [[depender]]
380 for path in depender.depender_paths:
381 paths += [path + [depender]]
385 def shortened_depender_paths(self):
387 for path in self.depender_paths:
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:
395 new_paths += [new_path]
399 def all_days_effort(self):
401 for effort in self.efforts.values():
402 total += effort if effort else 0
404 total = max(total, self.task.default_effort.at(self.latest_date))
407 def matches(self, search):
411 return search in self.comment or search in '$'.join(self.tags) or search in self.title
413 def is_effort_removable(self, date):
414 if not date in self.efforts.keys():
416 if self.efforts[date]:
418 if self.done and date == self.latest_date:
424 return self.day_tags | self.task.tags.now
427 def day_effort(self):
428 return self.efforts[self.db.selected_date]
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
435 return self.task.default_effort.then
439 return self.db.days[self.earliest_date]
442 def sorted_effort_dates(self):
443 dates = list(self.efforts.keys())
448 def earliest_date(self):
449 return self.sorted_effort_dates[0]
452 def latest_date(self):
453 return self.sorted_effort_dates[-1]
456 def has_dependers(self):
457 return len(self.dependers) > 0
461 return [t for t in self.db.todos.values() if self.id_ in t.dep_ids]
465 return len(self.deps) > 0
469 return self.day_effort if self.day_effort else 0
472 def family_effort(self):
473 return self.effort_at_selected_date + self.dep_efforts
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)
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_]
509 self.been_observed = True
514 class TodoDB(PlomDB):
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 []
528 super().__init__(db_path)
532 def read_db_file(self, f):
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:
539 # for task in self.tasks.values():
540 # for dep in task.deps:
541 # dep.dependers += [task]
543 for id_, todo_dict in d['todos'].items():
544 todo = self.add_todo(todo_dict, id_)
545 for tag in todo.day_tags:
547 # for todo in self.todos.values():
548 # for dep in todo.deps:
549 # dep.dependers += [todo]
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():
557 self.days[date].linked_todos_as_list += [todo]
559 # self.set_visibilities()
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()
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:
578 self.write_text_to_db(json.dumps(self.to_dict()))
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]
588 # table manipulations
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
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_)
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
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)
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)
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())
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
643 creator.deps += [todo]
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
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)
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]
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]
671 def _update_todo_shared(self, id_, done, comment, importance):
672 todo = self.todos[id_]
674 todo.comment = comment
675 todo.importance = importance
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
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]
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_)
702 def delete_todo(self, id_):
703 todo = self.todos[id_]
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_)
713 def delete_task(self, id_):
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]
724 def get_message(self, message):
725 return j2env.get_template('message.html').render(message=message)
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)
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,
736 start_date=start_date_str,
737 end_date=end_date_str)
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'}
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)
749 adoptable_past_todos = []
750 for todo in [t for t in self.todos.values()
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)
760 return j2env.get_template('day_todos.html').render(
761 day=self.selected_day,
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)
776 def get_todo(self, id_, parenthood):
777 todo = self.todos[id_]
778 filtered_tasks = [t for t in self.tasks.values()
780 filtered_todos = [t for t in self.todos.values()
782 and t not in todo.deps]
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:
790 if date in filtered_todo.efforts.keys():
791 date_filtered_todos += [filtered_todo]
794 for dep in todo.task.deps:
795 dep_slots += [{'task': dep,
796 'todos': [t for t in todo.deps if t.task == dep]}]
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]
803 return j2env.get_template('todo.html').render(
806 filtered_todos=date_filtered_todos,
807 filtered_tasks=filtered_tasks,
809 suggested_todos=suggested_todos,
810 additional_deps=additional_deps,
811 parentood=parenthood,
814 def get_task(self, id_):
816 if not id_ in self.tasks.keys():
817 raise PlomException('no Task for ID')
818 task = self.tasks[id_]
820 task = self.add_task()
822 task.default_effort.set(1.0)
823 filtered_tasks = [t for t in self.tasks.values()
825 and (t not in task.deps)]
827 return j2env.get_template('task.html').render(
828 selected_date=self.selected_date,
830 filtered_tasks=filtered_tasks,
833 def get_tasks(self, search, sort_order=None):
835 for task in [t for t in self.tasks.values() if (not search) or t.matches(search)]:
836 filtered_tasks += [task]
838 sort_column = sort_order
839 if sort_order and '-' == sort_order[0]:
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)
851 filtered_tasks.reverse()
852 return j2env.get_template('tasks.html').render(
854 tasks=filtered_tasks,
856 filter_and=self.tag_filter_and,
857 filter_not=self.tag_filter_not,
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
868 for i, date in enumerate(postvars.get_all('effort_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', [])\
880 raise PlomException('will not remove todo of preserve-worthy values')
881 self.delete_todo(id_)
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)
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_,
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),
906 depender_ids=depender_ids)
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_)
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(
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),
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_)
945 def post_day_todos(self, postvars, todo_parenthood):
946 if not postvars.has('update'):
948 self.selected_date = postvars.get('date')
949 self.selected_day.comment = postvars.get('day_comment', '')
950 task_id = postvars.get('choose_task', None)
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)
968 self.delete_todo(todo_id)
970 importance = float(postvars.get_at_index('importance', i))
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:
977 self.update_todo_for_day(
987 def init_calendar_items(self, start_date_str, end_date_str):
988 self.tag_filter_and = ['calendar']
989 self.tag_filter_not = ['deleted']
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
999 date_obj = datetime.strptime(day_limit_string, DATE_FORMAT)
1001 start_date_obj = get_day_limit_obj(0, start_date_str)
1002 end_date_obj = get_day_limit_obj(-1, end_date_str)
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)
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]
1020 def __init__(self, parsed_url_query, cookie_db):
1021 self.params = parse_qs(parsed_url_query)
1022 self.cookie_db = cookie_db
1024 def get(self, key, default=None, as_bool=False):
1025 param = self.params.get(key, [default])[0]
1029 elif param is not None:
1033 def cookie_key_from_params_key(self, prefix, key):
1034 return f'{prefix}:{key}' if prefix else key
1036 def _get_cookied(self, cookie_key, params):
1037 if params in [['-'], '-']:
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
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)
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)
1059 class PostvarsParser:
1061 def __init__(self, postvars):
1062 self.postvars = postvars
1065 return key in self.postvars.keys()
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)
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]
1075 if float_if_possible and val is not None:
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]
1085 def set(self, key, value):
1086 self.postvars[key] = [value]
1090 class TodoHandler(PlomHandler):
1092 def config_init(self):
1094 'cookie_name': 'todo_cookie',
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}
1105 self.try_do(self.write_db)
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))
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')
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_)]
1144 elif 'task' == site:
1145 ret = db.post_task(id_, postvars)
1147 redir_params += [('id', ret)]
1150 elif 'day_todos' == site:
1151 db.post_day_todos(postvars, todo_parenthood)
1152 redir_params += [('date', db.selected_date)]
1154 encoded_params = urlencode(redir_params)
1155 homepage = f'{site}?{encoded_params}'
1157 self.redirect(homepage)
1160 self.try_do(self.show_db)
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]
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')
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)
1212 self.set_cookie(config['cookie_name'], config['cookie_path'], cookie_db)
1213 self.send_HTML(page)
1217 if __name__ == "__main__":
1218 run_server(server_port, TodoHandler)