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 __init__(self, db, id_, title_history=None, tags_history=None, default_effort_history=None, links_history=None, comment=''):
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
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
31 def _last_of_history(self, history, default):
32 keys = sorted(history.keys())
33 return default if 0 == len(history) else history[keys[-1]]
36 def from_dict(cls, db, d, id_):
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()},
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,
57 def default_effort(self):
58 return self._last_of_history(self.default_effort_history, 1)
60 @default_effort.setter
61 def default_effort(self, default_effort):
62 self._set_with_history(self.default_effort_history, default_effort)
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':
73 # def current_default_effort(self):
74 # return self.default_effort_at(self.db.selected_date)
76 def matches(self, search):
80 return search in self.title or search in self.comment or search in '$'.join(self.tags) or search in self.title
84 return self._last_of_history(self.title_history, '')
87 def title(self, title):
88 self._set_with_history(self.title_history, title)
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':
99 def current_title(self):
100 return self.title_at(self.db.selected_date)
104 return self._last_of_history(self.tags_history, set())
107 def tags(self, tags):
108 self._set_with_history(self.tags_history, set(tags))
112 return self._last_of_history(self.links_history, set())
115 def links(self, links):
116 self._set_with_history(self.links_history, set(links))
120 # for k, v in self.db.tasks.items():
127 # def __init__(self, db, todos=None, comment='', date=None):
128 def __init__(self, db, date, comment=''):
131 self.comment = comment
133 self.todos = {} # legacy
134 self.linked_todos_as_list = []
136 # for id_, todo_dict in todos.items():
137 # self.add_todo(id_, todo_dict)
140 def from_dict(cls, db, d, date=None):
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():
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_]]
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]
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]
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
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
195 def linked_todos(self):
197 for todo in self.linked_todos_as_list:
198 linked_todos[todo.id_] = todo
201 def _todos_sum(self, include_undone=False):
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]
207 s += day_effort if day_effort else todo.task.default_effort_at(self.date)
209 s += day_effort if day_effort else 0
214 return self._todos_sum()
217 # def todos_sum2(self):
218 # return self._todos_sum(True)
225 # for k, v in self.db.days.items():
226 # # print("DEBUG date", k, v)
229 # print("DEBUG FAIL", self.test_date, self)
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):
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 []
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'])
256 todo.children = d['children']
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']
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}
271 def default_effort(self):
272 return self.task.default_effort_at(self.sorted_effort_dates[0])
276 # if self.day_effort:
277 # return self.day_effort
279 # return self.day_effort if self.day_effort else self.default_effort
286 # # for k, v in self.day.todos.items():
288 # # return self.db.tasks[k]
290 def is_effort_removable(self, date):
291 if not date in self.efforts.keys():
293 # if len(self.efforts) == 1:
295 if self.efforts[date]:
297 if self.done and date == self.sorted_effort_dates[-1]:
302 def total_effort(self):
304 for effort in self.efforts.values():
305 total += effort if effort else 0
307 total = max(total, self.task.default_effort_at(self.sorted_effort_dates[-1]))
310 def matches(self, search):
314 return search in self.comment or search in '$'.join(self.tags) or search in self.title
318 return self.task.title_at(self.sorted_effort_dates[0])
324 path = f'{self.parent.path}{self.parent.title}:'
329 return self.day_tags | self.task.tags
331 # def internals_empty(self):
332 # return len(self.comment) == 0 and len(self.day_tags) == 0
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}
339 # def set_day_effort(self, date, effort):
340 # self.ensure_day_efforts_table()
341 # self._efforts[date] = self.day_effort
344 def day_effort(self):
345 return self.efforts[self.db.selected_date]
348 def sorted_effort_dates(self):
349 dates = list(self.efforts.keys())
355 if len(self.efforts) == 0:
357 todo_start_date = self.sorted_effort_dates[0]
358 return self.db.days[todo_start_date]
362 # self.ensure_day_efforts_table()
363 # return self._efforts
369 # for k, v in self.db.todos.items():
372 # # return f'{self.day.date}_{self.task.id_}'
375 class TodoDB(PlomDB):
377 def __init__(self, prefix, selected_date=None, t_filter_and = None, t_filter_not = None, hide_unchosen=False, hide_done=False):
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
388 super().__init__(db_path)
390 def read_db_file(self, f):
392 for id_, t_dict in d['tasks'].items():
393 t = self.add_task(id_=id_, dict_source=t_dict)
396 # if 'todos' in d.keys():
397 for id_, todo_dict in d['todos'].items():
398 # todo = Todo.from_dict(self, todo_dict, 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:
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():
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)
427 # for todo in self.todos.values():
428 # if len(todo.efforts) == 0:
429 # print("DEBUG", todo.task.title)
431 # self.delete_todo(delete)
432 self.set_visibilities()
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))
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()
462 # def all_todos(self):
464 # for todo in self.todos_as_list:
465 # todos[todo.id_] = todo
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]
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:
482 self.write_text_to_db(json.dumps(self.to_dict()))
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_)
493 def add_todo(self, todo_dict=None, id_=None, task=None, efforts=None):
494 id_ = id_ if id_ else str(uuid4())
496 todo = Todo.from_dict(self, todo_dict, id_)
497 elif task and efforts:
498 todo = Todo(self, id_, task, efforts=efforts)
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
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
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)
520 # for uuid, task in self.tasks.items():
521 # if not task.visible:
524 # if uuid in self.selected_day.todos.keys():
525 # todo = self.selected_day.todos[uuid]
526 # if not todo.visible:
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)
542 # for uuid, task in self.tasks.items():
543 # if uuid in self.selected_day.todos.keys():
544 # todo = self.selected_day.todos[uuid]
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)
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
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,
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,
578 hide_done=self.hide_done)
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()
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
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
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)
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)
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]
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(
625 filtered_todos=filtered_todos,
626 linked_todos=linked_todos,
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]
636 # todo = self.days[date].add_todo(task_uuid)
637 # todo.day_effort = day_effort
639 # todo.importance = importance
642 def collect_tags(self, tags_joined, tags_checked):
644 for tag in [tag.strip() for tag in tags_joined.split(';') if tag.strip() != '']:
646 for tag in tags_checked:
650 def update_todo_for_day(self, id_, date, effort, done, comment, importance):
651 todo = self.todos[id_]
653 todo.efforts[date] = effort
654 todo.comment = comment
655 todo.importance = importance
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_]
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
672 def delete_todo(self, id_):
673 todo = self.todos[id_]
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)
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]
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)
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)
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(
717 filter_and=self.t_filter_and,
718 filter_not=self.t_filter_not,
719 filtered_tasks=filtered_tasks,
720 linked_tasks=linked_tasks,
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_)
727 task.default_effort = float(default_effort) if len(default_effort) > 0 else None
728 task.tags = self.collect_tags(tags_joined, tags_checked)
730 task.comment = comment
732 def show_tasks(self, 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)
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
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
746 relevant_todos += [todo]
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):
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(
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,
771 hide_chosen_tasks=hide_chosen_tasks,
772 relevant_todos=relevant_todos,
778 def __init__(self, parsed_url_query, cookie_db):
779 self.params = parse_qs(parsed_url_query)
780 self.cookie_db = cookie_db
782 def get(self, key, default=None):
783 boolean = bool == type(default)
784 param = self.params.get(key, [default])[0]
789 def get_cookied(self, key, default=None):
790 param = self.get(key, default)
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
801 def get_cookied_chain(self, key, default=None):
802 # default = default if default else ['-']
803 params = self.params.get(key, default)
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
815 class TodoHandler(PlomHandler):
817 def config_init(self):
819 'cookie_name': 'todo_cookie',
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}
831 self.try_do(self.config_init)
832 self.try_do(self.write_db)
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'])
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]
854 def collect_checked(prefix, postvars):
856 for k in postvars.keys():
857 if k.startswith(prefix):
858 tags_checked += [k[len(prefix):]]
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 '-')]
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]
871 for i, date in enumerate(postvars['effort_date']):
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']:
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)
884 # self.redirect('new_day')
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])
890 # for i, date in enumerate(postvars['effort_date']):
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,
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]))
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_]
921 # raise PlomException('cannot deselect task as todo of preserve-worthy values')
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()))]
928 db.selected_date = postvars['date'][0]
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)
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']:
952 todo.efforts[db.selected_date] = None
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_)
964 elif 'do_day' == site:
965 if 'filter' in postvars.keys():
966 redir_params += [('hide_done', int('hide_done' in postvars.keys()))]
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:
981 db.update_todo_for_day(todo_id, db.selected_date, day_effort, done, comment, importance)
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()))]
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)]
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:
1012 db.update_todo_mini(uuid, db.selected_date, day_effort, done, importance)
1014 if 'return_to' in postvars.keys() and len(postvars['return_to'][0]) > 0:
1015 homepage = postvars['return_to'][0]
1017 encoded_params = urlencode(redir_params)
1018 # homepage = f'{parsed_url.path}?{encoded_params}'
1019 homepage = f'{site}?{encoded_params}'
1021 self.redirect(homepage)
1024 self.try_do(self.config_init)
1025 self.try_do(self.show_db)
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)
1035 # def get_param(param_name, boolean=False, chained=False, none_as_empty_string=False):
1037 # param = params.get(param_name, None)
1038 # elif none_as_empty_string:
1039 # param = params.get(param_name, [''])[0]
1041 # param = params.get(param_name, [None])[0]
1042 # if (not chained and param == '-') or (chained and param == ['-']):
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:
1050 # param = param != '0'
1051 # cookie_db[param_name] = str(int(param))
1053 # cookie_db[param_name] = param
1054 # elif param is boolean:
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')
1088 # todo_date, task_uuid = todo_id.split('_')
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!'
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)
1116 if __name__ == "__main__":
1117 run_server(server_port, TodoHandler)