home · contact · privacy
478a41e106e23c525eb7af7d172d4e518165cf63
[misc] / todo.py
1 from plomlib import PlomDB, run_server, PlomHandler, PlomException
2 import json
3 from uuid import uuid4
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'
10 server_port = 8082
11 DATE_FORMAT = '%Y-%m-%d'
12 j2env = JinjaEnv(loader=JinjaFSLoader('todo_templates'))
13
14 class Task:
15
16     def __init__(self, db, title_history=None, tags_history=None, default_effort_history=None, links_history=None):
17         self.db = db
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 {}
22         self.visible = True
23
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
28
29     def _last_of_history(self, history, default):
30         keys = sorted(history.keys())
31         return default if 0 == len(history) else history[keys[-1]]
32
33     @classmethod
34     def from_dict(cls, db, d):
35         return cls(
36                db,
37                d['title_history'],
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()})
41
42     def to_dict(self):
43         return {
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()},
48         }
49
50     @property
51     def default_effort(self):
52         return self._last_of_history(self.default_effort_history, 1)
53
54     @default_effort.setter
55     def default_effort(self, default_effort):
56         self._set_with_history(self.default_effort_history, default_effort)
57
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':
62                 break
63             ret = default_effort
64         return ret
65
66     @property
67     def current_default_effort(self):
68         return self.default_effort_at(self.db.selected_date)
69
70     @property
71     def title(self):
72         return self._last_of_history(self.title_history, '')
73
74     @title.setter
75     def title(self, title):
76         self._set_with_history(self.title_history, title)
77
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':
82                 break
83             ret = title
84         return ret
85
86     @property
87     def current_title(self):
88         return self.title_at(self.db.selected_date)
89
90     @property
91     def tags(self):
92         return self._last_of_history(self.tags_history, set())
93
94     @tags.setter
95     def tags(self, tags):
96         self._set_with_history(self.tags_history, set(tags))
97
98     @property
99     def links(self):
100         return self._last_of_history(self.links_history, set())
101
102     @links.setter
103     def links(self, links):
104         self._set_with_history(self.links_history, set(links))
105
106     @property
107     def id_(self):
108         for k, v in self.db.tasks.items():
109             if v == self:
110                 return k
111
112
113 class Day:
114
115     def __init__(self, db, todos=None, comment=''):
116         self.db = db
117         self.todos = todos if todos else {}
118         self.comment = comment
119         self.archived = True
120
121     @classmethod
122     def from_dict(cls, db, d):
123         todos = {}
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)
128         return day
129
130     def to_dict(self):
131         d = {'comment': self.comment, 'todos': {}}
132         for task_uuid, todo in self.todos.items():
133             d['todos'][task_uuid] = todo.to_dict()
134         return d
135
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_]
139
140     def _todos_sum(self, include_undone=False):
141         s = 0
142         for todo in [todo for todo in self.todos.values() if todo.done]:
143             s += todo.effort
144         if include_undone:
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
147         return s
148
149     @property
150     def todos_sum(self):
151         return self._todos_sum()
152
153     @property
154     def todos_sum2(self):
155         return self._todos_sum(True)
156
157     @property
158     def date(self):
159         for k, v in self.db.days.items():
160             if v == self:
161                 return k
162
163 class Todo:
164
165     def __init__(self, day, done=False, day_effort=None, comment='', day_tags=None, importance=1.0):
166         self.day = day
167         self.done = done
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
172
173     @classmethod
174     def from_dict(cls, day, d):
175         return cls(day, d['done'], d['day_effort'], d['comment'], set(d['day_tags']), d['importance'])
176
177     def to_dict(self):
178         return {'done': self.done, 'day_effort': self.day_effort, 'comment': self.comment, 'day_tags': list(self.day_tags), 'importance': self.importance}
179
180     @property
181     def default_effort(self):
182         return self.task.default_effort_at(self.day.date)
183
184     @property
185     def effort(self):
186         if self.day_effort:
187             return self.day_effort
188         else:
189             return self.day_effort if self.day_effort else self.default_effort
190
191     @property
192     def task(self):
193         for k, v in self.day.todos.items():
194             if v == self:
195                 return self.day.db.tasks[k]
196
197     @property
198     def title(self):
199         return self.task.title_at(self.day.date)
200
201     @property
202     def tags(self):
203         return self.day_tags | self.task.tags
204
205     def is_empty(self):
206         return self.done or (self.day_effort is not None) or len(self.comment) > 0 or len(self.day_tags) > 0
207
208
209 class TodoDB(PlomDB):
210
211     def __init__(self, prefix, selected_date=None, t_filter_and = None, t_filter_not = None, hide_unchosen=False, hide_done=False):
212         self.prefix = prefix
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
218         self.days = {}
219         self.tasks = {}
220         self.t_tags = set()
221         super().__init__(db_path)
222
223     def read_db_file(self, f):
224         d = json.load(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:
230                     self.t_tags.add(tag)
231         for uuid, t_dict in d['tasks'].items():
232             t = self.add_task(id_=uuid, dict_source=t_dict)
233             for tag in t.tags:
234                 self.t_tags.add(tag)
235         self.set_visibilities()
236
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))
247
248     def to_dict(self):
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()
254         return d
255
256     @property
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]
261
262     def write(self):
263         dates_to_purge = []
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:
268             del self.days[date]
269         self.write_text_to_db(json.dumps(self.to_dict()))
270
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())
274         self.tasks[id_] = t
275         if return_id:
276             return id_, t
277         else:
278             return t
279
280     def add_day(self, dict_source=None):
281         return Day.from_dict(self, dict_source) if dict_source else Day(self)
282
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)
290         task_rows = []
291         for uuid, task in self.tasks.items():
292             if not task.visible:
293                 continue
294             todo = None
295             if uuid in self.selected_day.todos.keys():
296                 todo = self.selected_day.todos[uuid]
297                 if not todo.visible:
298                     continue
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)
313
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()
318         days_to_show = {}
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()
331             else:
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)
335
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]
339         else:
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')
342
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]
346         else:
347             todo = self.days[date].add_todo(task_uuid)
348         todo.day_effort = float(day_effort) if len(day_effort) > 0 else None
349         todo.done = done
350         todo.importance = float(importance)
351         return todo
352
353     def collect_tags(self, tags_joined, tags_checked):
354         tags = set()
355         for tag in [tag.strip() for tag in tags_joined.split(';') if tag.strip() != '']:
356             tags.add(tag)
357         for tag in tags_checked:
358             tags.add(tag)
359         return tags
360
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) 
365
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')
369
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_)
372         task.title = title
373         task.default_effort = float(default_effort) if len(default_effort) > 0 else None
374         task.tags = self.collect_tags(tags_joined, tags_checked) 
375         task.links = links 
376         for link in links:
377             borrowed_links = self.tasks[link].links 
378             borrowed_links.add(id_)
379             self.tasks[link].links = borrowed_links 
380
381     def show_tasks(self):
382         return j2env.get_template('tasks.html').render(db=self, action=self.prefix+'/tasks')
383
384
385
386 class TodoHandler(PlomHandler):
387
388     def config_init(self):
389         return {
390             'cookie_name': 'todo_cookie',
391             'prefix': '',
392             'cookie_path': '/'
393         }
394
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}
400
401     def do_POST(self):
402         self.try_do(self.config_init)
403         self.try_do(self.write_db)
404
405     def write_db(self):
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]
422
423         def collect_checked(prefix, postvars):
424             tags_checked = []
425             for k in postvars.keys():
426                 if k.startswith(prefix):
427                     tags_checked += [k[len(prefix):]]
428             return tags_checked
429         
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 '-')]
433
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])
439
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))
445
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']):
453                          t = db.tasks[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)]
465
466         encoded_params = urlencode(params_to_encode)
467         homepage = f'{parsed_url.path}?{encoded_params}'
468         db.write()
469         self.redirect(homepage)
470
471     def do_GET(self):
472         self.try_do(self.config_init)
473         self.try_do(self.show_db)
474
475     def show_db(self):
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)
480
481         def get_param(param_name, boolean=False, chained=False):
482             if chained:
483                 param = params.get(param_name, None)
484             else:
485                 param = params.get(param_name, [None])[0]
486             if (not chained and param == '-') or (chained and param == ['-']):
487                 param = None
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:
493                 if boolean:
494                     param = param != '0'
495                     cookie_db[param_name] = str(int(param))
496                 else:
497                     cookie_db[param_name] = param
498             elif param is boolean:
499                 param = False
500             return param
501
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!'
532         else:
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)
539         self.send_HTML(page)
540
541
542 if __name__ == "__main__":
543     run_server(server_port, TodoHandler)