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, 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 = float(day_effort) if len(day_effort) > 0 else None
354 todo.importance = float(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 todo = self.update_todo_mini(task_uuid, date, day_effort, done, importance)
367 todo.comment = comment
368 todo.day_tags = self.collect_tags(day_tags_joined, day_tags_checked)
370 def show_task(self, id_, referer=''):
371 task = self.tasks[id_] if id_ else self.add_task()
372 return j2env.get_template('task.html').render(db=self, task=task, action=self.prefix+'/task', referer=referer)
374 def update_task(self, id_, title, default_effort, tags_joined, tags_checked, links):
375 task = self.tasks[id_] if id_ in self.tasks.keys() else self.add_task(id_)
377 task.default_effort = float(default_effort) if len(default_effort) > 0 else None
378 task.tags = self.collect_tags(tags_joined, tags_checked)
381 borrowed_links = self.tasks[link].links
382 borrowed_links.add(id_)
383 self.tasks[link].links = borrowed_links
385 def show_tasks(self):
386 return j2env.get_template('tasks.html').render(db=self, action=self.prefix+'/tasks')
390 class TodoHandler(PlomHandler):
392 def config_init(self):
394 'cookie_name': 'todo_cookie',
399 def app_init(self, handler):
400 default_path = '/todo'
401 handler.add_route('GET', default_path, self.show_db)
402 handler.add_route('POST', default_path, self.write_db)
403 return 'todo', {'cookie_name': 'todo_cookie', 'prefix': default_path, 'cookie_path': default_path}
406 self.try_do(self.config_init)
407 self.try_do(self.write_db)
410 from urllib.parse import urlencode
411 app_config = self.apps['todo'] if hasattr(self, 'apps') else self.config()
412 length = int(self.headers['content-length'])
413 postvars = parse_qs(self.rfile.read(length).decode(), keep_blank_values=1)
414 parsed_url = urlparse(self.path)
415 db = TodoDB(prefix=app_config['prefix'])
416 params_to_encode = []
417 for param_name, filter_db_name in {('t_and', 't_filter_and'), ('t_not', 't_filter_not')}:
418 filter_db = getattr(db, filter_db_name)
419 if param_name in postvars.keys():
420 for target in postvars[param_name]:
421 if len(target) > 0 and not target in filter_db:
422 filter_db += [target]
423 if len(filter_db) == 0:
424 params_to_encode += [(param_name, '-')]
425 params_to_encode += [(param_name, f) for f in filter_db]
427 def collect_checked(prefix, postvars):
429 for k in postvars.keys():
430 if k.startswith(prefix):
431 tags_checked += [k[len(prefix):]]
434 if parsed_url.path == app_config['prefix'] + '/calendar':
435 params_to_encode += [('start', postvars['start'][0] if len(postvars['start'][0]) > 0 else '-')]
436 params_to_encode += [('end', postvars['end'][0] if len(postvars['end'][0]) > 0 else '-')]
438 elif parsed_url.path == app_config['prefix'] + '/todo':
439 task_uuid = postvars['task_uuid'][0]
440 date = postvars['date'][0]
441 params_to_encode += [('task', task_uuid), ('date', date)]
442 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])
444 elif parsed_url.path == app_config['prefix'] + '/task':
445 id_ = postvars['id'][0]
446 params_to_encode += [('id', id_)]
447 if 'title' in postvars.keys():
448 db.update_task(id_, postvars['title'][0], postvars['default_effort'][0], postvars['joined_tags'][0], collect_checked('tag_', postvars), collect_checked('link_', postvars))
450 elif parsed_url.path == app_config['prefix'] + '/day':
451 if 'expect_unchosen_done' in postvars.keys():
452 params_to_encode += [('hide_unchosen', int('hide_unchosen' in postvars.keys()))] + [('hide_done', int('hide_done' in postvars.keys()))]
453 if 'selected_date' in postvars.keys():
454 db.selected_date = postvars['selected_date'][0]
455 if 't_uuid' in postvars.keys():
456 for i, uuid in enumerate(postvars['t_uuid']):
458 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():
459 del db.selected_day.todos[uuid]
460 if 'choose' in postvars.keys():
461 for i, uuid in enumerate(postvars['t_uuid']):
462 uuids = postvars['choose']
463 uuids += postvars['done'] if 'done' in postvars.keys() else []
464 if uuid in uuids or postvars['day_effort'][i] != '' or (postvars['importance'][i] not in {'1.0', ''}):
465 done = 'done' in postvars and uuid in postvars['done']
466 db.update_todo_mini(uuid, db.selected_date, postvars['day_effort'][i], done, postvars['importance'][i])
467 if 'day_comment' in postvars.keys():
468 db.selected_day.comment = postvars['day_comment'][0]
469 params_to_encode += [('selected_date', db.selected_date)]
471 if 'referer' in postvars.keys() and len(postvars['referer'][0]) > 0:
472 homepage = postvars['referer'][0]
474 encoded_params = urlencode(params_to_encode)
475 homepage = f'{parsed_url.path}?{encoded_params}'
477 self.redirect(homepage)
480 self.try_do(self.config_init)
481 self.try_do(self.show_db)
484 app_config = self.apps['todo'] if hasattr(self, 'apps') else self.config()
485 cookie_db = self.get_cookie_db(app_config['cookie_name'])
486 parsed_url = urlparse(self.path)
487 params = parse_qs(parsed_url.query)
489 def get_param(param_name, boolean=False, chained=False):
491 param = params.get(param_name, None)
493 param = params.get(param_name, [None])[0]
494 if (not chained and param == '-') or (chained and param == ['-']):
496 if param_name in cookie_db.keys():
497 del cookie_db[param_name]
498 if param is None and param_name in cookie_db.keys():
499 param = cookie_db[param_name]
500 if param is not None:
503 cookie_db[param_name] = str(int(param))
505 cookie_db[param_name] = param
506 elif param is boolean:
510 selected_date = t_filter_and = t_filter_not = None
511 hide_unchosen = hide_done = False
512 referer = params.get('referer', [''])[0]
513 if parsed_url.path in {app_config['prefix'] + '/day', app_config['prefix'] + '/tasks'}:
514 selected_date = get_param('selected_date')
515 if parsed_url.path in {app_config['prefix'] + '/day', app_config['prefix'] + '/tasks', app_config['prefix'] + '/task'}:
516 t_filter_and = get_param('t_and', chained=True)
517 t_filter_not = get_param('t_not', chained=True)
518 if parsed_url.path == app_config['prefix'] + '/day':
519 hide_unchosen = get_param('hide_unchosen', boolean=True)
520 hide_done = get_param('hide_done', boolean=True)
521 db = TodoDB(app_config['prefix'], selected_date, t_filter_and, t_filter_not, hide_unchosen, hide_done)
522 if parsed_url.path == app_config['prefix'] + '/day':
523 task_sort = get_param('sort')
524 page = db.show_day(task_sort)
525 elif parsed_url.path == app_config['prefix'] + '/todo':
526 todo_date = params.get('date', [None])[0]
527 task_uuid = params.get('task', [None])[0]
528 page = db.show_todo(task_uuid, todo_date, referer)
529 elif parsed_url.path == app_config['prefix'] + '/task':
530 id_ = params.get('id', [None])[0]
531 page = db.show_task(id_, referer)
532 elif parsed_url.path == app_config['prefix'] + '/tasks':
533 page = db.show_tasks()
534 elif parsed_url.path == app_config['prefix'] + '/add_task':
535 page = db.show_task(None)
536 elif parsed_url.path == app_config['prefix'] + '/unset_cookie':
537 page = 'no cookie to unset.'
538 if len(cookie_db) > 0:
539 self.unset_cookie(app_config['cookie_name'], app_config['cookie_path'])
540 page = 'cookie unset!'
542 start_date = get_param('start')
543 start_date = start_date if start_date else 'today'
544 end_date = get_param('end')
545 page = db.show_calendar(start_date, end_date)
546 if parsed_url.path != app_config['prefix'] + '/unset_cookie':
547 self.set_cookie(app_config['cookie_name'], app_config['cookie_path'], cookie_db)
551 if __name__ == "__main__":
552 run_server(server_port, TodoHandler)