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 db_path = '/home/plom/org/todo_new.json'
10 DATE_FORMAT = '%Y-%m-%d'
11 j2env = JinjaEnv(loader=JinjaFSLoader('todo_templates'))
15 def __init__(self, db, title_history=None, tags_history=None, default_effort_history=None, links_history=None):
17 self.title_history = title_history if title_history else {}
18 self.tags_history = tags_history if tags_history else {}
19 self.default_effort_history = default_effort_history if default_effort_history else {}
20 self.links_history = links_history if links_history else {}
23 def _set_with_history(self, history, value):
24 keys = sorted(history.keys())
25 if len(history) == 0 or value != history[keys[-1]]:
26 history[str(datetime.now())[:19]] = value
28 def _last_of_history(self, history, default):
29 keys = sorted(history.keys())
30 return default if 0 == len(history) else history[keys[-1]]
33 def from_dict(cls, db, d):
37 {k: set(v) for k, v in d['tags_history'].items()},
38 d['default_effort_history'],
39 {k: set(v) for k, v in d['links_history'].items()})
43 'title_history': self.title_history,
44 'default_effort_history': self.default_effort_history,
45 'tags_history': {k: list(v) for k,v in self.tags_history.items()},
46 'links_history': {k: list(v) for k,v in self.links_history.items()},
50 def default_effort(self):
51 return self._last_of_history(self.default_effort_history, 1)
53 @default_effort.setter
54 def default_effort(self, default_effort):
55 self._set_with_history(self.default_effort_history, default_effort)
57 def default_effort_at(self, queried_date):
58 ret = self.default_effort_history[sorted(self.default_effort_history.keys())[0]]
59 for date_key, default_effort in self.default_effort_history.items():
60 if date_key > f'{queried_date} 23:59:59':
66 def current_default_effort(self):
67 return self.default_effort_at(self.db.selected_date)
71 return self._last_of_history(self.title_history, '')
74 def title(self, title):
75 self._set_with_history(self.title_history, title)
77 def title_at(self, queried_date):
78 ret = self.title_history[sorted(self.title_history.keys())[0]]
79 for date_key, title in self.title_history.items():
80 if date_key > f'{queried_date} 23:59:59':
86 def current_title(self):
87 return self.title_at(self.db.selected_date)
91 return self._last_of_history(self.tags_history, set())
95 self._set_with_history(self.tags_history, set(tags))
99 return self._last_of_history(self.links_history, set())
102 def links(self, links):
103 self._set_with_history(self.links_history, set(links))
107 for k, v in self.db.tasks.items():
114 def __init__(self, db, todos=None, comment=''):
116 self.todos = todos if todos else {}
117 self.comment = comment
121 def from_dict(cls, db, d):
123 comment = d['comment'] if 'comment' in d.keys() else ''
124 day = cls(db, todos, comment)
125 for uuid, todo_dict in d['todos'].items():
126 day.add_todo(uuid, todo_dict)
130 d = {'comment': self.comment, 'todos': {}}
131 for task_uuid, todo in self.todos.items():
132 d['todos'][task_uuid] = todo.to_dict()
135 def add_todo(self, id_, dict_source=None):
136 self.todos[id_] = Todo.from_dict(self, dict_source) if dict_source else Todo(self)
137 return self.todos[id_]
139 def _todos_sum(self, include_undone=False):
141 for todo in [todo for todo in self.todos.values() if todo.done]:
144 for todo in [todo for todo in self.todos.values() if not todo.done]:
145 s += todo.day_effort if todo.day_effort else 0
150 return self._todos_sum()
153 def todos_sum2(self):
154 return self._todos_sum(True)
158 for k, v in self.db.days.items():
165 def __init__(self, day, done=False, day_effort=None, comment='', day_tags=None, importance=1.0):
168 self.day_effort = day_effort
169 self.comment = comment
170 self.day_tags = day_tags if day_tags else set()
171 self.importance = importance
174 def from_dict(cls, day, d):
175 return cls(day, d['done'], d['day_effort'], d['comment'], set(d['day_tags']), d['importance'])
178 return {'done': self.done, 'day_effort': self.day_effort, 'comment': self.comment, 'day_tags': list(self.day_tags), 'importance': self.importance}
181 def default_effort(self):
182 return self.task.default_effort_at(self.day.date)
187 return self.day_effort
189 return self.day_effort if self.day_effort else self.default_effort
193 for k, v in self.day.todos.items():
195 return self.day.db.tasks[k]
199 return self.task.title_at(self.day.date)
203 return self.day_tags | self.task.tags
205 def internals_empty(self):
206 return len(self.comment) == 0 and len(self.day_tags) == 0
209 class TodoDB(PlomDB):
211 def __init__(self, prefix, selected_date=None, t_filter_and = None, t_filter_not = None, hide_unchosen=False, hide_done=False):
213 self.selected_date = selected_date if selected_date else str(datetime.now())[:10]
214 self.t_filter_and = t_filter_and if t_filter_and else []
215 self.t_filter_not = t_filter_not if t_filter_not else []
216 self.hide_unchosen = hide_unchosen
217 self.hide_done = hide_done
221 super().__init__(db_path)
223 def read_db_file(self, f):
225 for date, day_dict in d['days'].items():
226 self.days[date] = self.add_day(dict_source=day_dict)
227 for day in self.days.values():
228 for todo in day.todos.values():
229 for tag in todo.day_tags:
231 for uuid, t_dict in d['tasks'].items():
232 t = self.add_task(id_=uuid, dict_source=t_dict)
235 self.set_visibilities()
237 def set_visibilities(self):
238 for uuid, t in self.tasks.items():
239 t.visible = len([tag for tag in self.t_filter_and if not tag in t.tags]) == 0\
240 and len([tag for tag in self.t_filter_not if tag in t.tags]) == 0\
241 and ((not self.hide_unchosen) or uuid in self.selected_day.todos.keys())
242 for day in self.days.values():
243 for todo in day.todos.values():
244 todo.visible = len([tag for tag in self.t_filter_and if not tag in todo.day_tags | todo.task.tags ]) == 0\
245 and len([tag for tag in self.t_filter_not if tag in todo.day_tags | todo.task.tags ]) == 0\
246 and ((not self.hide_done) or (not todo.done))
249 d = {'tasks': {}, 'days': {}}
250 for uuid, t in self.tasks.items():
251 d['tasks'][uuid] = t.to_dict()
252 for date, day in self.days.items():
253 d['days'][date] = day.to_dict()
257 def selected_day(self):
258 if not self.selected_date in self.days.keys():
259 self.days[self.selected_date] = self.add_day()
260 return self.days[self.selected_date]
264 for date, day in self.days.items():
265 if len(day.todos) == 0 and len(day.comment) == 0:
266 dates_to_purge += [date]
267 for date in dates_to_purge:
269 self.write_text_to_db(json.dumps(self.to_dict()))
271 def add_task(self, id_=None, dict_source=None, return_id=False):
272 t = Task.from_dict(self, dict_source) if dict_source else Task(self)
273 id_ = id_ if id_ else str(uuid4())
280 def add_day(self, dict_source=None):
281 return Day.from_dict(self, dict_source) if dict_source else Day(self)
283 def show_day(self, task_sort=None):
284 task_sort = task_sort if task_sort else 'title'
285 current_date = datetime.strptime(self.selected_date, DATE_FORMAT)
286 prev_date = current_date - timedelta(days=1)
287 prev_date_str = prev_date.strftime(DATE_FORMAT)
288 next_date = current_date + timedelta(days=1)
289 next_date_str = next_date.strftime(DATE_FORMAT)
291 for uuid, task in self.tasks.items():
295 if uuid in self.selected_day.todos.keys():
296 todo = self.selected_day.todos[uuid]
299 task_rows += [{'uuid': uuid, 'task': task, 'todo': todo}]
300 if task_sort == 'title':
301 task_rows.sort(key=lambda r: r['task'].title)
302 elif task_sort == 'default_effort':
303 task_rows.sort(key=lambda r: r['task'].default_effort, reverse=True)
304 elif task_sort == 'done':
305 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)
306 elif task_sort == 'importance':
307 task_rows.sort(key=lambda r: 0.0 if not r['todo'] else r['todo'].importance, reverse=True)
308 elif task_sort == 'chosen':
309 task_rows.sort(key=lambda r: False if not r['todo'] else True, reverse=True)
310 elif task_sort == 'comment':
311 task_rows.sort(key=lambda r: '' if not r['todo'] else r['todo'].comment, reverse=True)
312 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)
314 def show_calendar(self, start_date_str, end_date_str):
315 self.t_filter_and = ['calendar']
316 self.t_filter_not = ['deleted']
317 self.set_visibilities()
319 todays_date = str(datetime.now())[:10]
320 target_start_str = start_date_str if start_date_str else sorted(self.days.keys())[0]
321 target_start = todays_date if target_start_str == 'today' else target_start_str
322 target_end_str = end_date_str if end_date_str else sorted(self.days.keys())[-1]
323 target_end = todays_date if target_end_str == 'today' else target_end_str
324 start_date = datetime.strptime(target_start, DATE_FORMAT)
325 end_date = datetime.strptime(target_end, DATE_FORMAT)
326 for n in range(int((end_date - start_date).days) + 1):
327 current_date_obj = start_date + timedelta(n)
328 current_date = current_date_obj.strftime(DATE_FORMAT)
329 if current_date not in self.days.keys():
330 days_to_show[current_date] = self.add_day()
332 days_to_show[current_date] = self.days[current_date]
333 days_to_show[current_date].weekday = datetime.strptime(current_date, DATE_FORMAT).strftime('%A')[:2]
334 return j2env.get_template('calendar.html').render(db=self, days=days_to_show, action=self.prefix+'/calendar', today=str(datetime.now())[:10], start_date=start_date_str, end_date=end_date_str)
336 def show_todo(self, task_uuid, selected_date, referer):
337 if selected_date not in self.days.keys():
338 self.days[selected_date] = self.add_day()
339 if task_uuid in self.days[selected_date].todos:
340 todo = self.days[selected_date].todos[task_uuid]
342 todo = self.days[selected_date].add_todo(task_uuid)
343 return j2env.get_template('todo.html').render(db=self, todo=todo, action=self.prefix+'/todo', referer=referer)
345 def update_todo_mini(self, task_uuid, date, day_effort, done, importance):
346 if date not in self.days.keys():
347 self.days[date] = self.add_day()
348 if task_uuid in self.days[date].todos.keys():
349 todo = self.days[date].todos[task_uuid]
351 todo = self.days[date].add_todo(task_uuid)
352 todo.day_effort = day_effort
354 todo.importance = importance
357 def collect_tags(self, tags_joined, tags_checked):
359 for tag in [tag.strip() for tag in tags_joined.split(';') if tag.strip() != '']:
361 for tag in tags_checked:
365 def update_todo(self, task_uuid, date, day_effort, done, comment, day_tags_joined, day_tags_checked, importance):
366 day_effort = float(day_effort) if len(day_effort) > 0 else None
367 importance = float(importance)
368 todo = self.update_todo_mini(task_uuid, date, day_effort, done, importance)
369 todo.comment = comment
370 todo.day_tags = self.collect_tags(day_tags_joined, day_tags_checked)
372 def show_task(self, id_, referer=''):
373 task = self.tasks[id_] if id_ else self.add_task()
374 return j2env.get_template('task.html').render(db=self, task=task, action=self.prefix+'/task', referer=referer)
376 def update_task(self, id_, title, default_effort, tags_joined, tags_checked, links):
377 task = self.tasks[id_] if id_ in self.tasks.keys() else self.add_task(id_)
379 task.default_effort = float(default_effort) if len(default_effort) > 0 else None
380 task.tags = self.collect_tags(tags_joined, tags_checked)
383 borrowed_links = self.tasks[link].links
384 borrowed_links.add(id_)
385 self.tasks[link].links = borrowed_links
387 def show_tasks(self):
388 return j2env.get_template('tasks.html').render(db=self, action=self.prefix+'/tasks')
392 class TodoHandler(PlomHandler):
394 def config_init(self):
396 'cookie_name': 'todo_cookie',
401 def app_init(self, handler):
402 default_path = '/todo'
403 handler.add_route('GET', default_path, self.show_db)
404 handler.add_route('POST', default_path, self.write_db)
405 return 'todo', {'cookie_name': 'todo_cookie', 'prefix': default_path, 'cookie_path': default_path}
408 self.try_do(self.config_init)
409 self.try_do(self.write_db)
412 from urllib.parse import urlencode
413 app_config = self.apps['todo'] if hasattr(self, 'apps') else self.config()
414 length = int(self.headers['content-length'])
415 postvars = parse_qs(self.rfile.read(length).decode(), keep_blank_values=1)
416 parsed_url = urlparse(self.path)
417 db = TodoDB(prefix=app_config['prefix'])
418 params_to_encode = []
419 for param_name, filter_db_name in {('t_and', 't_filter_and'), ('t_not', 't_filter_not')}:
420 filter_db = getattr(db, filter_db_name)
421 if param_name in postvars.keys():
422 for target in postvars[param_name]:
423 if len(target) > 0 and not target in filter_db:
424 filter_db += [target]
425 if len(filter_db) == 0:
426 params_to_encode += [(param_name, '-')]
427 params_to_encode += [(param_name, f) for f in filter_db]
429 def collect_checked(prefix, postvars):
431 for k in postvars.keys():
432 if k.startswith(prefix):
433 tags_checked += [k[len(prefix):]]
436 if parsed_url.path == app_config['prefix'] + '/calendar':
437 params_to_encode += [('start', postvars['start'][0] if len(postvars['start'][0]) > 0 else '-')]
438 params_to_encode += [('end', postvars['end'][0] if len(postvars['end'][0]) > 0 else '-')]
440 elif parsed_url.path == app_config['prefix'] + '/todo':
441 task_uuid = postvars['task_uuid'][0]
442 date = postvars['date'][0]
443 params_to_encode += [('task', task_uuid), ('date', date)]
444 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])
446 elif parsed_url.path == app_config['prefix'] + '/task':
447 id_ = postvars['id'][0]
448 params_to_encode += [('id', id_)]
449 if 'title' in postvars.keys():
450 db.update_task(id_, postvars['title'][0], postvars['default_effort'][0], postvars['joined_tags'][0], collect_checked('tag_', postvars), collect_checked('link_', postvars))
452 elif parsed_url.path == app_config['prefix'] + '/day':
453 # always store the two hide params in the URL if possible … TODO: find out if really necessary
454 if 'expect_unchosen_done' in postvars.keys():
455 params_to_encode += [('hide_unchosen', int('hide_unchosen' in postvars.keys()))] + [('hide_done', int('hide_done' in postvars.keys()))]
457 if 'selected_date' in postvars.keys():
458 db.selected_date = postvars['selected_date'][0]
459 if 'day_comment' in postvars.keys():
460 db.selected_day.comment = postvars['day_comment'][0]
461 params_to_encode += [('selected_date', db.selected_date)]
463 # handle todo list updates via task UUIDs
464 if 't_uuid' in postvars.keys():
465 for i, uuid in enumerate(postvars['t_uuid']):
466 task = db.tasks[uuid]
467 old_todo = None if not uuid in db.selected_day.todos.keys() else db.selected_day.todos[uuid]
468 selects_as_todo = 'choose' in postvars and uuid in postvars['choose']
469 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())
470 if old_todo and too_much_keepworthy_data and not selects_as_todo:
471 raise PlomException('cannot deselect task as todo of preserve-worthy values')
472 elif old_todo and not selects_as_todo:
473 del db.selected_day.todos[uuid]
475 done = ('done' in postvars) and (uuid in postvars['done'])
476 day_effort_input = postvars['day_effort'][i]
477 day_effort = float(day_effort_input) if len(day_effort_input) > 0 else None
478 importance = float(postvars['importance'][i])
479 if old_todo and old_todo.done == done and old_todo.day_effort == day_effort and old_todo.importance == importance:
481 db.update_todo_mini(uuid, db.selected_date, day_effort, done, importance)
483 if 'referer' in postvars.keys() and len(postvars['referer'][0]) > 0:
484 homepage = postvars['referer'][0]
486 encoded_params = urlencode(params_to_encode)
487 homepage = f'{parsed_url.path}?{encoded_params}'
489 self.redirect(homepage)
492 self.try_do(self.config_init)
493 self.try_do(self.show_db)
496 app_config = self.apps['todo'] if hasattr(self, 'apps') else self.config()
497 cookie_db = self.get_cookie_db(app_config['cookie_name'])
498 parsed_url = urlparse(self.path)
499 params = parse_qs(parsed_url.query)
501 def get_param(param_name, boolean=False, chained=False):
503 param = params.get(param_name, None)
505 param = params.get(param_name, [None])[0]
506 if (not chained and param == '-') or (chained and param == ['-']):
508 if param_name in cookie_db.keys():
509 del cookie_db[param_name]
510 if param is None and param_name in cookie_db.keys():
511 param = cookie_db[param_name]
512 if param is not None:
515 cookie_db[param_name] = str(int(param))
517 cookie_db[param_name] = param
518 elif param is boolean:
522 selected_date = t_filter_and = t_filter_not = None
523 hide_unchosen = hide_done = False
524 referer = params.get('referer', [''])[0]
525 if parsed_url.path in {app_config['prefix'] + '/day', app_config['prefix'] + '/tasks'}:
526 selected_date = get_param('selected_date')
527 if parsed_url.path in {app_config['prefix'] + '/day', app_config['prefix'] + '/tasks', app_config['prefix'] + '/task'}:
528 t_filter_and = get_param('t_and', chained=True)
529 t_filter_not = get_param('t_not', chained=True)
530 if parsed_url.path == app_config['prefix'] + '/day':
531 hide_unchosen = get_param('hide_unchosen', boolean=True)
532 hide_done = get_param('hide_done', boolean=True)
533 db = TodoDB(app_config['prefix'], selected_date, t_filter_and, t_filter_not, hide_unchosen, hide_done)
534 if parsed_url.path == app_config['prefix'] + '/day':
535 task_sort = get_param('sort')
536 page = db.show_day(task_sort)
537 elif parsed_url.path == app_config['prefix'] + '/todo':
538 todo_date = params.get('date', [None])[0]
539 task_uuid = params.get('task', [None])[0]
540 page = db.show_todo(task_uuid, todo_date, referer)
541 elif parsed_url.path == app_config['prefix'] + '/task':
542 id_ = params.get('id', [None])[0]
543 page = db.show_task(id_, referer)
544 elif parsed_url.path == app_config['prefix'] + '/tasks':
545 page = db.show_tasks()
546 elif parsed_url.path == app_config['prefix'] + '/add_task':
547 page = db.show_task(None)
548 elif parsed_url.path == app_config['prefix'] + '/unset_cookie':
549 page = 'no cookie to unset.'
550 if len(cookie_db) > 0:
551 self.unset_cookie(app_config['cookie_name'], app_config['cookie_path'])
552 page = 'cookie unset!'
554 start_date = get_param('start')
555 start_date = start_date if start_date else 'today'
556 end_date = get_param('end')
557 page = db.show_calendar(start_date, end_date)
558 if parsed_url.path != app_config['prefix'] + '/unset_cookie':
559 self.set_cookie(app_config['cookie_name'], app_config['cookie_path'], cookie_db)
563 if __name__ == "__main__":
564 run_server(server_port, TodoHandler)