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 Template
7 from jinja2 import Environment as JinjaEnv, FileSystemLoader as JinjaFSLoader
8 from urllib.parse import urlparse
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, title_history=None, tags_history=None, default_effort_history=None, links_history=None):
18 self.title_history = title_history if title_history else {}
19 self.tags_history = tags_history if tags_history else {}
20 self.default_effort_history = default_effort_history if default_effort_history else {}
21 self.links_history = links_history if links_history else {}
24 def _set_with_history(self, history, value):
25 keys = sorted(history.keys())
26 if len(history) == 0 or value != history[keys[-1]]:
27 history[str(datetime.now())[:19]] = value
29 def _last_of_history(self, history, default):
30 keys = sorted(history.keys())
31 return default if 0 == len(history) else history[keys[-1]]
34 def from_dict(cls, db, d):
38 {k: set(v) for k, v in d['tags_history'].items()},
39 d['default_effort_history'],
40 {k: set(v) for k, v in d['links_history'].items()})
44 'title_history': self.title_history,
45 'default_effort_history': self.default_effort_history,
46 'tags_history': {k: list(v) for k,v in self.tags_history.items()},
47 'links_history': {k: list(v) for k,v in self.links_history.items()},
51 def default_effort(self):
52 return self._last_of_history(self.default_effort_history, 1)
54 @default_effort.setter
55 def default_effort(self, default_effort):
56 self._set_with_history(self.default_effort_history, default_effort)
58 def default_effort_at(self, queried_date):
59 ret = self.default_effort_history[sorted(self.default_effort_history.keys())[0]]
60 for date_key, default_effort in self.default_effort_history.items():
61 if date_key > f'{queried_date} 23:59:59':
67 def current_default_effort(self):
68 return self.default_effort_at(self.db.selected_date)
72 return self._last_of_history(self.title_history, '')
75 def title(self, title):
76 self._set_with_history(self.title_history, title)
78 def title_at(self, queried_date):
79 ret = self.title_history[sorted(self.title_history.keys())[0]]
80 for date_key, title in self.title_history.items():
81 if date_key > f'{queried_date} 23:59:59':
87 def current_title(self):
88 return self.title_at(self.db.selected_date)
92 return self._last_of_history(self.tags_history, set())
96 self._set_with_history(self.tags_history, set(tags))
100 return self._last_of_history(self.links_history, set())
103 def links(self, links):
104 self._set_with_history(self.links_history, set(links))
108 for k, v in self.db.tasks.items():
115 def __init__(self, db, todos=None, comment=''):
117 self.todos = todos if todos else {}
118 self.comment = comment
122 def from_dict(cls, db, d):
124 comment = d['comment'] if 'comment' in d.keys() else ''
125 day = cls(db, todos, comment)
126 for uuid, todo_dict in d['todos'].items():
127 day.add_todo(uuid, todo_dict)
131 d = {'comment': self.comment, 'todos': {}}
132 for task_uuid, todo in self.todos.items():
133 d['todos'][task_uuid] = todo.to_dict()
136 def add_todo(self, id_, dict_source=None):
137 self.todos[id_] = Todo.from_dict(self, dict_source) if dict_source else Todo(self)
138 return self.todos[id_]
140 def _todos_sum(self, include_undone=False):
142 for todo in [todo for todo in self.todos.values() if todo.done]:
145 for todo in [todo for todo in self.todos.values() if not todo.done]:
146 s += todo.day_effort if todo.day_effort else 0
151 return self._todos_sum()
154 def todos_sum2(self):
155 return self._todos_sum(True)
159 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
206 return self.done or (self.day_effort is not None) or len(self.comment) > 0 or 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)
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):
337 if task_uuid in self.days[selected_date].todos:
338 todo = self.days[selected_date].todos[task_uuid]
340 todo = self.days[selected_date].add_todo(task_uuid)
341 return j2env.get_template('todo.html').render(db=self, todo=todo, action=self.prefix+'/todo')
343 def update_todo_mini(self, task_uuid, date, day_effort, done, importance):
344 if task_uuid in self.days[date].todos.keys():
345 todo = self.days[date].todos[task_uuid]
347 todo = self.days[date].add_todo(task_uuid)
348 todo.day_effort = float(day_effort) if len(day_effort) > 0 else None
350 todo.importance = float(importance)
353 def collect_tags(self, tags_joined, tags_checked):
355 for tag in [tag.strip() for tag in tags_joined.split(';') if tag.strip() != '']:
357 for tag in tags_checked:
361 def update_todo(self, task_uuid, date, day_effort, done, comment, day_tags_joined, day_tags_checked, importance):
362 todo = self.update_todo_mini(task_uuid, date, day_effort, done, importance)
363 todo.comment = comment
364 todo.day_tags = self.collect_tags(day_tags_joined, day_tags_checked)
366 def show_task(self, id_):
367 task = self.tasks[id_] if id_ else self.add_task()
368 return j2env.get_template('task.html').render(db=self, task=task, action=self.prefix+'/task')
370 def update_task(self, id_, title, default_effort, tags_joined, tags_checked, links):
371 task = self.tasks[id_] if id_ in self.tasks.keys() else self.add_task(id_)
373 task.default_effort = float(default_effort) if len(default_effort) > 0 else None
374 task.tags = self.collect_tags(tags_joined, tags_checked)
377 borrowed_links = self.tasks[link].links
378 borrowed_links.add(id_)
379 self.tasks[link].links = borrowed_links
381 def show_tasks(self):
382 return j2env.get_template('tasks.html').render(db=self, action=self.prefix+'/tasks')
386 class TodoHandler(PlomHandler):
388 def config_init(self):
390 'cookie_name': 'todo_cookie',
395 def app_init(self, handler):
396 default_path = '/todo'
397 handler.add_route('GET', default_path, self.show_db)
398 handler.add_route('POST', default_path, self.write_db)
399 return 'todo', {'cookie_name': 'todo_cookie', 'prefix': default_path, 'cookie_path': default_path}
402 self.try_do(self.config_init)
403 self.try_do(self.write_db)
406 from urllib.parse import urlencode
407 app_config = self.apps['todo'] if hasattr(self, 'apps') else self.config()
408 length = int(self.headers['content-length'])
409 postvars = parse_qs(self.rfile.read(length).decode(), keep_blank_values=1)
410 parsed_url = urlparse(self.path)
411 db = TodoDB(prefix=app_config['prefix'])
412 params_to_encode = []
413 for param_name, filter_db_name in {('t_and', 't_filter_and'), ('t_not', 't_filter_not')}:
414 filter_db = getattr(db, filter_db_name)
415 if param_name in postvars.keys():
416 for target in postvars[param_name]:
417 if len(target) > 0 and not target in filter_db:
418 filter_db += [target]
419 if len(filter_db) == 0:
420 params_to_encode += [(param_name, '-')]
421 params_to_encode += [(param_name, f) for f in filter_db]
423 def collect_checked(prefix, postvars):
425 for k in postvars.keys():
426 if k.startswith(prefix):
427 tags_checked += [k[len(prefix):]]
430 if parsed_url.path == app_config['prefix'] + '/calendar':
431 params_to_encode += [('start', postvars['start'][0] if len(postvars['start'][0]) > 0 else '-')]
432 params_to_encode += [('end', postvars['end'][0] if len(postvars['end'][0]) > 0 else '-')]
434 elif parsed_url.path == app_config['prefix'] + '/todo':
435 task_uuid = postvars['task_uuid'][0]
436 date = postvars['date'][0]
437 params_to_encode += [('task', task_uuid), ('date', date)]
438 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])
440 elif parsed_url.path == app_config['prefix'] + '/task':
441 id_ = postvars['id'][0]
442 params_to_encode += [('id', id_)]
443 if 'title' in postvars.keys():
444 db.update_task(id_, postvars['title'][0], postvars['default_effort'][0], postvars['joined_tags'][0], collect_checked('tag_', postvars), collect_checked('link_', postvars))
446 elif parsed_url.path == app_config['prefix'] + '/day':
447 if 'expect_unchosen_done' in postvars.keys():
448 params_to_encode += [('hide_unchosen', int('hide_unchosen' in postvars.keys()))] + [('hide_done', int('hide_done' in postvars.keys()))]
449 if 'selected_date' in postvars.keys():
450 db.selected_date = postvars['selected_date'][0]
451 if 't_uuid' in postvars.keys():
452 for i, uuid in enumerate(postvars['t_uuid']):
454 if uuid in db.selected_day.todos.keys() and ((not 'choose' in postvars) or uuid not in postvars['choose']) and not db.selected_day.todos[uuid].is_empty():
455 del db.selected_day.todos[uuid]
456 if 'choose' in postvars.keys():
457 for i, uuid in enumerate(postvars['t_uuid']):
458 uuids = postvars['choose'] + postvars['done'] if 'done' in postvars.keys() else []
459 if uuid in uuids or postvars['day_effort'][i] != '' or postvars['importance'][i] != '1.0':
460 done = 'done' in postvars and uuid in postvars['done']
461 db.update_todo_mini(uuid, db.selected_date, postvars['day_effort'][i], done, postvars['importance'][i])
462 if 'day_comment' in postvars.keys():
463 db.selected_day.comment = postvars['day_comment'][0]
464 params_to_encode += [('selected_date', db.selected_date)]
466 encoded_params = urlencode(params_to_encode)
467 homepage = f'{parsed_url.path}?{encoded_params}'
469 self.redirect(homepage)
472 self.try_do(self.config_init)
473 self.try_do(self.show_db)
476 app_config = self.apps['todo'] if hasattr(self, 'apps') else self.config()
477 cookie_db = self.get_cookie_db(app_config['cookie_name'])
478 parsed_url = urlparse(self.path)
479 params = parse_qs(parsed_url.query)
481 def get_param(param_name, boolean=False, chained=False):
483 param = params.get(param_name, None)
485 param = params.get(param_name, [None])[0]
486 if (not chained and param == '-') or (chained and param == ['-']):
488 if param_name in cookie_db.keys():
489 del cookie_db[param_name]
490 if param is None and param_name in cookie_db.keys():
491 param = cookie_db[param_name]
492 if param is not None:
495 cookie_db[param_name] = str(int(param))
497 cookie_db[param_name] = param
498 elif param is boolean:
502 selected_date = t_filter_and = t_filter_not = None
503 hide_unchosen = hide_done = False
504 if parsed_url.path in {app_config['prefix'] + '/day', app_config['prefix'] + '/tasks'}:
505 selected_date = get_param('selected_date')
506 if parsed_url.path in {app_config['prefix'] + '/day', app_config['prefix'] + '/tasks', app_config['prefix'] + '/task'}:
507 t_filter_and = get_param('t_and', chained=True)
508 t_filter_not = get_param('t_not', chained=True)
509 if parsed_url.path == app_config['prefix'] + '/day':
510 hide_unchosen = get_param('hide_unchosen', boolean=True)
511 hide_done = get_param('hide_done', boolean=True)
512 db = TodoDB(app_config['prefix'], selected_date, t_filter_and, t_filter_not, hide_unchosen, hide_done)
513 if parsed_url.path == app_config['prefix'] + '/day':
514 task_sort = get_param('sort')
515 page = db.show_day(task_sort)
516 elif parsed_url.path == app_config['prefix'] + '/todo':
517 todo_date = params.get('date', [None])[0]
518 task_uuid = params.get('task', [None])[0]
519 page = db.show_todo(task_uuid, todo_date)
520 elif parsed_url.path == app_config['prefix'] + '/task':
521 id_ = params.get('id', [None])[0]
522 page = db.show_task(id_)
523 elif parsed_url.path == app_config['prefix'] + '/tasks':
524 page = db.show_tasks()
525 elif parsed_url.path == app_config['prefix'] + '/add_task':
526 page = db.show_task(None)
527 elif parsed_url.path == app_config['prefix'] + '/unset_cookie':
528 page = 'no cookie to unset.'
529 if len(cookie_db) > 0:
530 self.unset_cookie(app_config['cookie_name'], app_config['cookie_path'])
531 page = 'cookie unset!'
533 start_date = get_param('start')
534 start_date = start_date if start_date else 'today'
535 end_date = get_param('end')
536 page = db.show_calendar(start_date, end_date)
537 if parsed_url.path != app_config['prefix'] + '/unset_cookie':
538 self.set_cookie(app_config['cookie_name'], app_config['cookie_path'], cookie_db)
542 if __name__ == "__main__":
543 run_server(server_port, TodoHandler)