home · contact · privacy
46108a8fb7171e14fb7f10f2eb7c037842f827b4
[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_weight_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_weight_history = default_weight_history if default_weight_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         if 'links_history' in d.keys():
36             return cls(
37                    db,
38                    d['title_history'],
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()})
42         else:
43             return cls(
44                    db,
45                    d['title_history'],
46                    {k: set(v) for k, v in d['tags_history'].items()},
47                    d['default_weight_history'])
48
49     def to_dict(self):
50         return {
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()},
55         }
56
57     @property
58     def default_weight(self):
59         return self._last_of_history(self.default_weight_history, 1)
60
61     @default_weight.setter
62     def default_weight(self, default_weight):
63         self._set_with_history(self.default_weight_history, default_weight)
64
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':
69                 break
70             ret = default_weight
71         return ret
72
73     @property
74     def current_default_weight(self):
75         return self.default_weight_at(self.db.selected_date)
76
77     @property
78     def title(self):
79         return self._last_of_history(self.title_history, '')
80
81     @title.setter
82     def title(self, title):
83         self._set_with_history(self.title_history, title)
84
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':
89                 break
90             ret = title
91         return ret
92
93     @property
94     def current_title(self):
95         return self.title_at(self.db.selected_date)
96
97     @property
98     def tags(self):
99         return self._last_of_history(self.tags_history, set())
100
101     @tags.setter
102     def tags(self, tags):
103         self._set_with_history(self.tags_history, set(tags))
104
105     @property
106     def links(self):
107         return self._last_of_history(self.links_history, set())
108
109     @links.setter
110     def links(self, links):
111         self._set_with_history(self.links_history, set(links))
112
113     @property
114     def id_(self):
115         for k, v in self.db.tasks.items():
116             if v == self:
117                 return k
118
119
120 class Day:
121
122     def __init__(self, db, todos=None, comment=''):
123         self.db = db
124         self.todos = todos if todos else {}
125         self.comment = comment
126         self.archived = True
127
128     @classmethod
129     def from_dict(cls, db, d):
130         todos = {}
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)
135         return day
136
137     def to_dict(self):
138         d = {'comment': self.comment, 'todos': {}}
139         for task_uuid, todo in self.todos.items():
140             d['todos'][task_uuid] = todo.to_dict()
141         return d
142
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_]
146
147     def _todos_sum(self, include_undone=False):
148         s = 0
149         for todo in [todo for todo in self.todos.values() if todo.done]:
150             s += todo.weight
151         if include_undone:
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
154         return s
155
156     @property
157     def todos_sum(self):
158         return self._todos_sum()
159
160     @property
161     def todos_sum2(self):
162         return self._todos_sum(True)
163
164     @property
165     def date(self):
166         for k, v in self.db.days.items():
167             if v == self:
168                 return k
169
170 class Todo:
171
172     def __init__(self, day, done=False, day_weight=None, comment='', day_tags=None):
173         self.day = day
174         self.done = done
175         self.day_weight = day_weight
176         self.comment = comment
177         self.day_tags = day_tags if day_tags else set()
178
179     @classmethod
180     def from_dict(cls, day, d):
181         return cls(day, d['done'], d['day_weight'], d['comment'], set(d['day_tags']))
182
183     def to_dict(self):
184         return {'done': self.done, 'day_weight': self.day_weight, 'comment': self.comment, 'day_tags': list(self.day_tags)}
185
186     @property
187     def default_weight(self):
188         return self.task.default_weight_at(self.day.date)
189
190     @property
191     def weight(self):
192         if self.day_weight:
193             return self.day_weight
194         else:
195             return self.day_weight if self.day_weight else self.default_weight
196
197     @property
198     def task(self):
199         for k, v in self.day.todos.items():
200             if v == self:
201                 return self.day.db.tasks[k]
202
203     @property
204     def title(self):
205         return self.task.title_at(self.day.date)
206
207     @property
208     def tags(self):
209         return self.day_tags | self.task.tags
210
211
212 class TodoDB(PlomDB):
213
214     def __init__(self, prefix, selected_date=None, t_filter_and = None, t_filter_not = None, hide_unchosen=False, hide_done=False):
215         self.prefix = prefix
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
221         self.days = {}
222         self.tasks = {}
223         self.t_tags = set()
224         super().__init__(db_path)
225
226     def read_db_file(self, f):
227         d = json.load(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:
233                     self.t_tags.add(tag)
234         for uuid, t_dict in d['tasks'].items():
235             t = self.add_task(id_=uuid, dict_source=t_dict)
236             for tag in t.tags:
237                 self.t_tags.add(tag)
238         self.set_visibilities()
239
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))
250
251     def to_dict(self):
252         d = {
253                 # 't_filter_and': self.t_filter_and,
254                 # 't_filter_not': self.t_filter_not,
255                 'tasks': {},
256                 'days': {}
257         }
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()
262         return d
263
264     @property
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]
269
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')
273         else:
274             self.days[new_date] = self.selected_day
275             del self.days[self.selected_date]
276             self.selected_date = new_date
277
278     def write(self):
279         dates_to_purge = []
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:
284             del self.days[date]
285         self.write_text_to_db(json.dumps(self.to_dict()))
286
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())
290         self.tasks[id_] = t
291         if return_id:
292             return id_, t
293         else:
294             return t
295
296     def add_day(self, dict_source=None):
297         return Day.from_dict(self, dict_source) if dict_source else Day(self)
298
299     def show_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)
306
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()
311         days_to_show = {}
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()
324             else:
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)
328
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')
332
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]
336         else:
337             todo = self.days[date].add_todo(task_uuid)
338         todo.day_weight = float(day_weight) if len(day_weight) > 0 else None
339         todo.done = done
340         return todo
341
342     def collect_tags(self, tags_joined, tags_checked):
343         tags = set()
344         for tag in [tag.strip() for tag in tags_joined.split(';') if tag.strip() != '']:
345             tags.add(tag)
346         for tag in tags_checked:
347             tags.add(tag)
348         return tags
349
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) 
354
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')
358
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_)
361         task.title = title
362         task.default_weight = float(default_weight) if len(default_weight) > 0 else None
363         task.tags = self.collect_tags(tags_joined, tags_checked) 
364         task.links = links 
365         for link in links:
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 
372
373     def show_tasks(self):
374         return j2env.get_template('tasks.html').render(db=self, action=self.prefix+'/tasks')
375
376
377
378 class TodoHandler(PlomHandler):
379
380     def config_init(self):
381         return {
382             'cookie_name': 'todo_cookie',
383             'prefix': '',
384             'cookie_path': '/'
385         }
386
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}
392
393     def do_POST(self):
394         self.try_do(self.config_init)
395         self.try_do(self.write_db)
396
397     def write_db(self):
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]
418
419         def collect_checked(prefix, postvars):
420             tags_checked = []
421             for k in postvars.keys():
422                 if k.startswith(prefix):
423                     tags_checked += [k[len(prefix):]]
424             return tags_checked
425         
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}'
430
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}'
436
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}'
443
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}'
447
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']):
455                          t = db.tasks[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}'
468
469         db.write()
470         self.redirect(homepage)
471
472     def do_GET(self):
473         self.try_do(self.config_init)
474         self.try_do(self.show_db)
475
476     def show_db(self):
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)
481         selected_date = None
482         t_filter_and = None
483         t_filter_not = None
484         hide_unchosen = False
485         hide_done = 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 == ['-']:
496                 t_filter_and = None
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']
502                 else:
503                     t_filter_not = ['deleted']
504             elif t_filter_not == ['-']:
505                 t_filter_not = None
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']
512             else:
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']
519             else:
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':
524             page = db.show_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!'
541         else:
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']
546                 else:
547                     start_date = 'today'
548             elif start_date == '-':
549                 start_date = None
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 == '-':
555                 end_date = None
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)
560         self.send_HTML(page)
561
562
563 if __name__ == "__main__":
564     run_server(server_port, TodoHandler)