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_weight_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_weight_history = default_weight_history if default_weight_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):
35 if 'links_history' in d.keys():
39 {k: set(v) for k, v in d['tags_history'].items()},
40 d['default_weight_history'],
41 {k: set(v) for k, v in d['links_history'].items()})
46 {k: set(v) for k, v in d['tags_history'].items()},
47 d['default_weight_history'])
51 'title_history': self.title_history,
52 'default_weight_history': self.default_weight_history,
53 'tags_history': {k: list(v) for k,v in self.tags_history.items()},
54 'links_history': {k: list(v) for k,v in self.links_history.items()},
58 def default_weight(self):
59 return self._last_of_history(self.default_weight_history, 1)
61 @default_weight.setter
62 def default_weight(self, default_weight):
63 self._set_with_history(self.default_weight_history, default_weight)
65 def default_weight_at(self, queried_date):
66 ret = self.default_weight_history[sorted(self.default_weight_history.keys())[0]]
67 for date_key, default_weight in self.default_weight_history.items():
68 if date_key > f'{queried_date} 23:59:59':
74 def current_default_weight(self):
75 return self.default_weight_at(self.db.selected_date)
79 return self._last_of_history(self.title_history, '')
82 def title(self, title):
83 self._set_with_history(self.title_history, title)
85 def title_at(self, queried_date):
86 ret = self.title_history[sorted(self.title_history.keys())[0]]
87 for date_key, title in self.title_history.items():
88 if date_key > f'{queried_date} 23:59:59':
94 def current_title(self):
95 return self.title_at(self.db.selected_date)
99 return self._last_of_history(self.tags_history, set())
102 def tags(self, tags):
103 self._set_with_history(self.tags_history, set(tags))
107 return self._last_of_history(self.links_history, set())
110 def links(self, links):
111 self._set_with_history(self.links_history, set(links))
115 for k, v in self.db.tasks.items():
122 def __init__(self, db, todos=None, comment=''):
124 self.todos = todos if todos else {}
125 self.comment = comment
129 def from_dict(cls, db, d):
131 comment = d['comment'] if 'comment' in d.keys() else ''
132 day = cls(db, todos, comment)
133 for uuid, todo_dict in d['todos'].items():
134 day.add_todo(uuid, todo_dict)
138 d = {'comment': self.comment, 'todos': {}}
139 for task_uuid, todo in self.todos.items():
140 d['todos'][task_uuid] = todo.to_dict()
143 def add_todo(self, id_, dict_source=None):
144 self.todos[id_] = Todo.from_dict(self, dict_source) if dict_source else Todo(self)
145 return self.todos[id_]
147 def _todos_sum(self, include_undone=False):
149 for todo in [todo for todo in self.todos.values() if todo.done]:
152 for todo in [todo for todo in self.todos.values() if not todo.done]:
153 s += todo.day_weight if todo.day_weight else 0
158 return self._todos_sum()
161 def todos_sum2(self):
162 return self._todos_sum(True)
166 for k, v in self.db.days.items():
172 def __init__(self, day, done=False, day_weight=None, comment='', day_tags=None):
175 self.day_weight = day_weight
176 self.comment = comment
177 self.day_tags = day_tags if day_tags else set()
180 def from_dict(cls, day, d):
181 return cls(day, d['done'], d['day_weight'], d['comment'], set(d['day_tags']))
184 return {'done': self.done, 'day_weight': self.day_weight, 'comment': self.comment, 'day_tags': list(self.day_tags)}
187 def default_weight(self):
188 return self.task.default_weight_at(self.day.date)
193 return self.day_weight
195 return self.day_weight if self.day_weight else self.default_weight
199 for k, v in self.day.todos.items():
201 return self.day.db.tasks[k]
205 return self.task.title_at(self.day.date)
209 return self.day_tags | self.task.tags
212 class TodoDB(PlomDB):
214 def __init__(self, prefix, selected_date=None, t_filter_and = None, t_filter_not = None, hide_unchosen=False, hide_done=False):
216 self.selected_date = selected_date if selected_date else str(datetime.now())[:10]
217 self.t_filter_and = t_filter_and if t_filter_and else []
218 self.t_filter_not = t_filter_not if t_filter_not else []
219 self.hide_unchosen = hide_unchosen
220 self.hide_done = hide_done
224 super().__init__(db_path)
226 def read_db_file(self, f):
228 for date, day_dict in d['days'].items():
229 self.days[date] = self.add_day(dict_source=day_dict)
230 for day in self.days.values():
231 for todo in day.todos.values():
232 for tag in todo.day_tags:
234 for uuid, t_dict in d['tasks'].items():
235 t = self.add_task(id_=uuid, dict_source=t_dict)
238 self.set_visibilities()
240 def set_visibilities(self):
241 for uuid, t in self.tasks.items():
242 t.visible = len([tag for tag in self.t_filter_and if not tag in t.tags]) == 0\
243 and len([tag for tag in self.t_filter_not if tag in t.tags]) == 0\
244 and ((not self.hide_unchosen) or uuid in self.selected_day.todos.keys())
245 for day in self.days.values():
246 for todo in day.todos.values():
247 todo.visible = len([tag for tag in self.t_filter_and if not tag in todo.day_tags | todo.task.tags ]) == 0\
248 and len([tag for tag in self.t_filter_not if tag in todo.day_tags | todo.task.tags ]) == 0\
249 and ((not self.hide_done) or (not todo.done))
253 # 't_filter_and': self.t_filter_and,
254 # 't_filter_not': self.t_filter_not,
258 for uuid, t in self.tasks.items():
259 d['tasks'][uuid] = t.to_dict()
260 for date, day in self.days.items():
261 d['days'][date] = day.to_dict()
265 def selected_day(self):
266 if not self.selected_date in self.days.keys():
267 self.days[self.selected_date] = self.add_day()
268 return self.days[self.selected_date]
270 def change_selected_days_date(self, new_date):
271 if new_date in self.days.keys():
272 raise PlomException('cannot use same date twice')
274 self.days[new_date] = self.selected_day
275 del self.days[self.selected_date]
276 self.selected_date = new_date
280 for date, day in self.days.items():
281 if len(day.todos) == 0 and len(day.comment) == 0:
282 dates_to_purge += [date]
283 for date in dates_to_purge:
285 self.write_text_to_db(json.dumps(self.to_dict()))
287 def add_task(self, id_=None, dict_source=None, return_id=False):
288 t = Task.from_dict(self, dict_source) if dict_source else Task(self)
289 id_ = id_ if id_ else str(uuid4())
296 def add_day(self, dict_source=None):
297 return Day.from_dict(self, dict_source) if dict_source else Day(self)
300 current_date = datetime.strptime(self.selected_date, DATE_FORMAT)
301 prev_date = current_date - timedelta(days=1)
302 prev_date_str = prev_date.strftime(DATE_FORMAT)
303 next_date = current_date + timedelta(days=1)
304 next_date_str = next_date.strftime(DATE_FORMAT)
305 return j2env.get_template('day.html').render(db=self, action=self.prefix+'/day', prev_date=prev_date_str, next_date=next_date_str)
307 def show_calendar(self, start_date_str, end_date_str):
308 self.t_filter_and = ['calendar']
309 self.t_filter_not = ['deleted']
310 self.set_visibilities()
312 todays_date = str(datetime.now())[:10]
313 target_start_str = start_date_str if start_date_str else sorted(self.days.keys())[0]
314 target_start = todays_date if target_start_str == 'today' else target_start_str
315 target_end_str = end_date_str if end_date_str else sorted(self.days.keys())[-1]
316 target_end = todays_date if target_end_str == 'today' else target_end_str
317 start_date = datetime.strptime(target_start, DATE_FORMAT)
318 end_date = datetime.strptime(target_end, DATE_FORMAT)
319 for n in range(int((end_date - start_date).days) + 1):
320 current_date_obj = start_date + timedelta(n)
321 current_date = current_date_obj.strftime(DATE_FORMAT)
322 if current_date not in self.days.keys():
323 days_to_show[current_date] = self.add_day()
325 days_to_show[current_date] = self.days[current_date]
326 days_to_show[current_date].weekday = datetime.strptime(current_date, DATE_FORMAT).strftime('%A')[:2]
327 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)
329 def show_todo(self, task_uuid, selected_date):
330 todo = self.days[selected_date].todos[task_uuid]
331 return j2env.get_template('todo.html').render(db=self, todo=todo, action=self.prefix+'/todo')
333 def update_todo_mini(self, task_uuid, date, day_weight, done):
334 if task_uuid in self.days[date].todos.keys():
335 todo = self.days[date].todos[task_uuid]
337 todo = self.days[date].add_todo(task_uuid)
338 todo.day_weight = float(day_weight) if len(day_weight) > 0 else None
342 def collect_tags(self, tags_joined, tags_checked):
344 for tag in [tag.strip() for tag in tags_joined.split(';') if tag.strip() != '']:
346 for tag in tags_checked:
350 def update_todo(self, task_uuid, date, day_weight, done, comment, day_tags_joined, day_tags_checked):
351 todo = self.update_todo_mini(task_uuid, date, day_weight, done)
352 todo.comment = comment
353 todo.day_tags = self.collect_tags(day_tags_joined, day_tags_checked)
355 def show_task(self, id_):
356 task = self.tasks[id_] if id_ else self.add_task()
357 return j2env.get_template('task.html').render(db=self, task=task, action=self.prefix+'/task')
359 def update_task(self, id_, title, default_weight, tags_joined, tags_checked, links):
360 task = self.tasks[id_] if id_ in self.tasks.keys() else self.add_task(id_)
362 task.default_weight = float(default_weight) if len(default_weight) > 0 else None
363 task.tags = self.collect_tags(tags_joined, tags_checked)
366 print("DEBUG DEBUG", links)
367 borrowed_links = self.tasks[link].links
368 print("DEBUG DEBUG brorowed1", borrowed_links)
369 borrowed_links.add(id_)
370 print("DEBUG DEBUG brorowed2", borrowed_links)
371 self.tasks[link].links = borrowed_links
373 def show_tasks(self):
374 return j2env.get_template('tasks.html').render(db=self, action=self.prefix+'/tasks')
378 class TodoHandler(PlomHandler):
380 def config_init(self):
382 'cookie_name': 'todo_cookie',
387 def app_init(self, handler):
388 default_path = '/todo'
389 handler.add_route('GET', default_path, self.show_db)
390 handler.add_route('POST', default_path, self.write_db)
391 return 'todo', {'cookie_name': 'todo_cookie', 'prefix': default_path, 'cookie_path': default_path}
394 self.try_do(self.config_init)
395 self.try_do(self.write_db)
398 from urllib.parse import urlencode
399 app_config = self.apps['todo'] if hasattr(self, 'apps') else self.config()
400 length = int(self.headers['content-length'])
401 postvars = parse_qs(self.rfile.read(length).decode(), keep_blank_values=1)
402 parsed_url = urlparse(self.path)
403 db = TodoDB(prefix=app_config['prefix'])
404 params_to_encode = []
405 if 't_and' in postvars.keys():
406 for target in postvars['t_and']:
407 if len(target) > 0 and not target in db.t_filter_and:
408 db.t_filter_and += [target]
409 if len(db.t_filter_and) == 0:
410 params_to_encode += [('t_and', '-')]
411 if 't_not' in postvars.keys():
412 for target in postvars['t_not']:
413 if len(target) > 0 and not target in db.t_filter_not:
414 db.t_filter_not += [target]
415 if len(db.t_filter_not) == 0:
416 params_to_encode += [('t_not', '-')]
417 params_to_encode += [('t_and', f) for f in db.t_filter_and] + [('t_not', f) for f in db.t_filter_not]
419 def collect_checked(prefix, postvars):
421 for k in postvars.keys():
422 if k.startswith(prefix):
423 tags_checked += [k[len(prefix):]]
426 if parsed_url.path == app_config['prefix'] + '/calendar':
427 start = postvars['start'][0] if len(postvars['start'][0]) > 0 else '-'
428 end = postvars['end'][0] if len(postvars['end'][0]) > 0 else '-'
429 homepage = f'{app_config["prefix"]}/calendar?start={start}&end={end}'
431 elif parsed_url.path == app_config['prefix'] + '/todo':
432 task_uuid = postvars['task_uuid'][0]
433 date = postvars['date'][0]
434 db.update_todo(task_uuid, date, postvars['day_weight'][0], 'done' in postvars.keys(), postvars['comment'][0], postvars['joined_day_tags'][0], collect_checked('day_tag_', postvars))
435 homepage = f'{app_config["prefix"]}/todo?task={task_uuid}&date={date}'
437 elif parsed_url.path == app_config['prefix'] + '/task':
438 encoded_params = urlencode(params_to_encode)
439 id_ = postvars['id'][0]
440 if 'title' in postvars.keys():
441 db.update_task(id_, postvars['title'][0], postvars['default_weight'][0], postvars['joined_tags'][0], collect_checked('tag_', postvars), collect_checked('link_', postvars))
442 homepage = f'{app_config["prefix"]}/task?id={id_}&{encoded_params}'
444 elif parsed_url.path == app_config['prefix'] + '/tasks':
445 encoded_params = urlencode(params_to_encode)
446 homepage = f'{app_config["prefix"]}/tasks?{encoded_params}'
448 elif parsed_url.path == app_config['prefix'] + '/day':
449 if 'expect_unchosen_done' in postvars.keys():
450 params_to_encode += [('hide_unchosen', int('hide_unchosen' in postvars.keys()))] + [('hide_done', int('hide_done' in postvars.keys()))]
451 if 'selected_date' in postvars.keys():
452 db.selected_date = postvars['selected_date'][0]
453 if 't_uuid' in postvars.keys():
454 for i, uuid in enumerate(postvars['t_uuid']):
456 if uuid in db.selected_day.todos.keys() and ((not 'choose' in postvars) or uuid not in postvars['choose']):
457 del db.selected_day.todos[uuid]
458 if 'choose' in postvars.keys():
459 for i, uuid in enumerate(postvars['t_uuid']):
460 if uuid in postvars['choose']:
461 done = 'done' in postvars and uuid in postvars['done']
462 db.update_todo_mini(uuid, db.selected_date, postvars['day_weight'][i], done)
463 if 'day_comment' in postvars.keys():
464 db.selected_day.comment = postvars['day_comment'][0]
465 params_to_encode += [('selected_date', db.selected_date)]
466 encoded_params = urlencode(params_to_encode)
467 homepage = f'{app_config["prefix"]}/day?{encoded_params}'
470 self.redirect(homepage)
473 self.try_do(self.config_init)
474 self.try_do(self.show_db)
477 app_config = self.apps['todo'] if hasattr(self, 'apps') else self.config()
478 cookie_db = self.get_cookie_db(app_config['cookie_name'])
479 parsed_url = urlparse(self.path)
480 params = parse_qs(parsed_url.query)
484 hide_unchosen = False
486 if parsed_url.path in {app_config['prefix'] + '/day', app_config['prefix'] + '/tasks'}:
487 selected_date = params.get('selected_date', [None])[0]
488 if selected_date is None and 'selected_date' in cookie_db.keys():
489 selected_date = cookie_db['selected_date']
490 cookie_db['selected_date'] = selected_date
491 if parsed_url.path in {app_config['prefix'] + '/day', app_config['prefix'] + '/tasks', app_config['prefix'] + '/task'}:
492 t_filter_and = params.get('t_and', None)
493 if t_filter_and is None and 't_and' in cookie_db.keys():
494 t_filter_and = cookie_db['t_and']
495 elif t_filter_and == ['-']:
497 cookie_db['t_and'] = t_filter_and
498 t_filter_not = params.get('t_not', None)
499 if t_filter_not is None:
500 if 't_not' in cookie_db.keys():
501 t_filter_not = cookie_db['t_not']
503 t_filter_not = ['deleted']
504 elif t_filter_not == ['-']:
506 cookie_db['t_not'] = t_filter_not
507 if parsed_url.path == app_config['prefix'] + '/day':
508 hide_unchosen_params = params.get('hide_unchosen', [])
509 if 0 == len(hide_unchosen_params):
510 if 'hide_unchosen' in cookie_db.keys():
511 hide_unchosen = cookie_db['hide_unchosen']
513 hide_unchosen = hide_unchosen_params[0] != '0'
514 cookie_db['hide_unchosen'] = hide_unchosen
515 hide_done_params = params.get('hide_done', [])
516 if 0 == len(hide_done_params):
517 if 'hide_done' in cookie_db.keys():
518 hide_done = cookie_db['hide_done']
520 hide_done = hide_done_params[0] != '0'
521 cookie_db['hide_done'] = hide_done
522 db = TodoDB(app_config['prefix'], selected_date, t_filter_and, t_filter_not, hide_unchosen, hide_done)
523 if parsed_url.path == app_config['prefix'] + '/day':
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)
529 elif parsed_url.path == app_config['prefix'] + '/task':
530 id_ = params.get('id', [None])[0]
531 page = db.show_task(id_)
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 = params.get('start', [None])[0]
543 if start_date is None:
544 if 'calendar_start' in cookie_db.keys():
545 start_date = cookie_db['calendar_start']
548 elif start_date == '-':
550 cookie_db['calendar_start'] = start_date
551 end_date = params.get('end', [None])[0]
552 if end_date is None and 'calendar_end' in cookie_db.keys():
553 end_date = cookie_db['calendar_end']
554 elif end_date == '-':
556 cookie_db['calendar_end'] = end_date
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)