From 942c977e0052d09300625e7a5c1b5fc2eede9098 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Tue, 26 Dec 2023 07:45:02 +0100 Subject: [PATCH] Imporove todo accounting. --- todo.py | 277 ++++++++++++++++++-------------------- todo_templates/day.html | 41 +++--- todo_templates/task.html | 2 +- todo_templates/tasks.html | 4 +- todo_templates/todo.html | 5 +- 5 files changed, 155 insertions(+), 174 deletions(-) diff --git a/todo.py b/todo.py index 46108a8..478a41e 100644 --- a/todo.py +++ b/todo.py @@ -13,11 +13,11 @@ j2env = JinjaEnv(loader=JinjaFSLoader('todo_templates')) class Task: - def __init__(self, db, title_history=None, tags_history=None, default_weight_history=None, links_history=None): + def __init__(self, db, title_history=None, tags_history=None, default_effort_history=None, links_history=None): self.db = db self.title_history = title_history if title_history else {} self.tags_history = tags_history if tags_history else {} - self.default_weight_history = default_weight_history if default_weight_history else {} + self.default_effort_history = default_effort_history if default_effort_history else {} self.links_history = links_history if links_history else {} self.visible = True @@ -32,47 +32,40 @@ class Task: @classmethod def from_dict(cls, db, d): - if 'links_history' in d.keys(): - return cls( - db, - d['title_history'], - {k: set(v) for k, v in d['tags_history'].items()}, - d['default_weight_history'], - {k: set(v) for k, v in d['links_history'].items()}) - else: - return cls( - db, - d['title_history'], - {k: set(v) for k, v in d['tags_history'].items()}, - d['default_weight_history']) + return cls( + db, + d['title_history'], + {k: set(v) for k, v in d['tags_history'].items()}, + d['default_effort_history'], + {k: set(v) for k, v in d['links_history'].items()}) def to_dict(self): return { 'title_history': self.title_history, - 'default_weight_history': self.default_weight_history, + 'default_effort_history': self.default_effort_history, 'tags_history': {k: list(v) for k,v in self.tags_history.items()}, 'links_history': {k: list(v) for k,v in self.links_history.items()}, } @property - def default_weight(self): - return self._last_of_history(self.default_weight_history, 1) + def default_effort(self): + return self._last_of_history(self.default_effort_history, 1) - @default_weight.setter - def default_weight(self, default_weight): - self._set_with_history(self.default_weight_history, default_weight) + @default_effort.setter + def default_effort(self, default_effort): + self._set_with_history(self.default_effort_history, default_effort) - def default_weight_at(self, queried_date): - ret = self.default_weight_history[sorted(self.default_weight_history.keys())[0]] - for date_key, default_weight in self.default_weight_history.items(): + def default_effort_at(self, queried_date): + ret = self.default_effort_history[sorted(self.default_effort_history.keys())[0]] + for date_key, default_effort in self.default_effort_history.items(): if date_key > f'{queried_date} 23:59:59': break - ret = default_weight + ret = default_effort return ret @property - def current_default_weight(self): - return self.default_weight_at(self.db.selected_date) + def current_default_effort(self): + return self.default_effort_at(self.db.selected_date) @property def title(self): @@ -147,10 +140,10 @@ class Day: def _todos_sum(self, include_undone=False): s = 0 for todo in [todo for todo in self.todos.values() if todo.done]: - s += todo.weight + s += todo.effort if include_undone: for todo in [todo for todo in self.todos.values() if not todo.done]: - s += todo.day_weight if todo.day_weight else 0 + s += todo.day_effort if todo.day_effort else 0 return s @property @@ -169,30 +162,31 @@ class Day: class Todo: - def __init__(self, day, done=False, day_weight=None, comment='', day_tags=None): + def __init__(self, day, done=False, day_effort=None, comment='', day_tags=None, importance=1.0): self.day = day self.done = done - self.day_weight = day_weight + self.day_effort = day_effort self.comment = comment self.day_tags = day_tags if day_tags else set() + self.importance = importance @classmethod def from_dict(cls, day, d): - return cls(day, d['done'], d['day_weight'], d['comment'], set(d['day_tags'])) + return cls(day, d['done'], d['day_effort'], d['comment'], set(d['day_tags']), d['importance']) def to_dict(self): - return {'done': self.done, 'day_weight': self.day_weight, 'comment': self.comment, 'day_tags': list(self.day_tags)} + return {'done': self.done, 'day_effort': self.day_effort, 'comment': self.comment, 'day_tags': list(self.day_tags), 'importance': self.importance} @property - def default_weight(self): - return self.task.default_weight_at(self.day.date) + def default_effort(self): + return self.task.default_effort_at(self.day.date) @property - def weight(self): - if self.day_weight: - return self.day_weight + def effort(self): + if self.day_effort: + return self.day_effort else: - return self.day_weight if self.day_weight else self.default_weight + return self.day_effort if self.day_effort else self.default_effort @property def task(self): @@ -208,6 +202,9 @@ class Todo: def tags(self): return self.day_tags | self.task.tags + def is_empty(self): + return self.done or (self.day_effort is not None) or len(self.comment) > 0 or len(self.day_tags) > 0 + class TodoDB(PlomDB): @@ -249,12 +246,7 @@ class TodoDB(PlomDB): and ((not self.hide_done) or (not todo.done)) def to_dict(self): - d = { - # 't_filter_and': self.t_filter_and, - # 't_filter_not': self.t_filter_not, - 'tasks': {}, - 'days': {} - } + d = {'tasks': {}, 'days': {}} for uuid, t in self.tasks.items(): d['tasks'][uuid] = t.to_dict() for date, day in self.days.items(): @@ -267,14 +259,6 @@ class TodoDB(PlomDB): self.days[self.selected_date] = self.add_day() return self.days[self.selected_date] - def change_selected_days_date(self, new_date): - if new_date in self.days.keys(): - raise PlomException('cannot use same date twice') - else: - self.days[new_date] = self.selected_day - del self.days[self.selected_date] - self.selected_date = new_date - def write(self): dates_to_purge = [] for date, day in self.days.items(): @@ -296,13 +280,36 @@ class TodoDB(PlomDB): def add_day(self, dict_source=None): return Day.from_dict(self, dict_source) if dict_source else Day(self) - def show_day(self): + def show_day(self, task_sort=None): + task_sort = task_sort if task_sort else 'title' current_date = datetime.strptime(self.selected_date, DATE_FORMAT) prev_date = current_date - timedelta(days=1) prev_date_str = prev_date.strftime(DATE_FORMAT) next_date = current_date + timedelta(days=1) next_date_str = next_date.strftime(DATE_FORMAT) - 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 = [] + for uuid, task in self.tasks.items(): + if not task.visible: + continue + todo = None + if uuid in self.selected_day.todos.keys(): + todo = self.selected_day.todos[uuid] + if not todo.visible: + continue + task_rows += [{'uuid': uuid, 'task': task, 'todo': todo}] + if task_sort == 'title': + task_rows.sort(key=lambda r: r['task'].title) + elif task_sort == 'default_effort': + task_rows.sort(key=lambda r: r['task'].default_effort, reverse=True) + elif task_sort == 'done': + 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) + elif task_sort == 'importance': + task_rows.sort(key=lambda r: 0.0 if not r['todo'] else r['todo'].importance, reverse=True) + elif task_sort == 'chosen': + task_rows.sort(key=lambda r: False if not r['todo'] else True, reverse=True) + elif task_sort == 'comment': + task_rows.sort(key=lambda r: '' if not r['todo'] else r['todo'].comment, reverse=True) + 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) def show_calendar(self, start_date_str, end_date_str): self.t_filter_and = ['calendar'] @@ -327,16 +334,20 @@ class TodoDB(PlomDB): 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) def show_todo(self, task_uuid, selected_date): - todo = self.days[selected_date].todos[task_uuid] + if task_uuid in self.days[selected_date].todos: + todo = self.days[selected_date].todos[task_uuid] + else: + todo = self.days[selected_date].add_todo(task_uuid) return j2env.get_template('todo.html').render(db=self, todo=todo, action=self.prefix+'/todo') - def update_todo_mini(self, task_uuid, date, day_weight, done): + def update_todo_mini(self, task_uuid, date, day_effort, done, importance): if task_uuid in self.days[date].todos.keys(): todo = self.days[date].todos[task_uuid] else: todo = self.days[date].add_todo(task_uuid) - todo.day_weight = float(day_weight) if len(day_weight) > 0 else None + todo.day_effort = float(day_effort) if len(day_effort) > 0 else None todo.done = done + todo.importance = float(importance) return todo def collect_tags(self, tags_joined, tags_checked): @@ -347,8 +358,8 @@ class TodoDB(PlomDB): tags.add(tag) return tags - def update_todo(self, task_uuid, date, day_weight, done, comment, day_tags_joined, day_tags_checked): - todo = self.update_todo_mini(task_uuid, date, day_weight, done) + def update_todo(self, task_uuid, date, day_effort, done, comment, day_tags_joined, day_tags_checked, importance): + todo = self.update_todo_mini(task_uuid, date, day_effort, done, importance) todo.comment = comment todo.day_tags = self.collect_tags(day_tags_joined, day_tags_checked) @@ -356,18 +367,15 @@ class TodoDB(PlomDB): task = self.tasks[id_] if id_ else self.add_task() return j2env.get_template('task.html').render(db=self, task=task, action=self.prefix+'/task') - def update_task(self, id_, title, default_weight, tags_joined, tags_checked, links): + def update_task(self, id_, title, default_effort, tags_joined, tags_checked, links): task = self.tasks[id_] if id_ in self.tasks.keys() else self.add_task(id_) task.title = title - task.default_weight = float(default_weight) if len(default_weight) > 0 else None + task.default_effort = float(default_effort) if len(default_effort) > 0 else None task.tags = self.collect_tags(tags_joined, tags_checked) task.links = links for link in links: - print("DEBUG DEBUG", links) borrowed_links = self.tasks[link].links - print("DEBUG DEBUG brorowed1", borrowed_links) borrowed_links.add(id_) - print("DEBUG DEBUG brorowed2", borrowed_links) self.tasks[link].links = borrowed_links def show_tasks(self): @@ -402,19 +410,15 @@ class TodoHandler(PlomHandler): parsed_url = urlparse(self.path) db = TodoDB(prefix=app_config['prefix']) params_to_encode = [] - if 't_and' in postvars.keys(): - for target in postvars['t_and']: - if len(target) > 0 and not target in db.t_filter_and: - db.t_filter_and += [target] - if len(db.t_filter_and) == 0: - params_to_encode += [('t_and', '-')] - if 't_not' in postvars.keys(): - for target in postvars['t_not']: - if len(target) > 0 and not target in db.t_filter_not: - db.t_filter_not += [target] - if len(db.t_filter_not) == 0: - params_to_encode += [('t_not', '-')] - params_to_encode += [('t_and', f) for f in db.t_filter_and] + [('t_not', f) for f in db.t_filter_not] + for param_name, filter_db_name in {('t_and', 't_filter_and'), ('t_not', 't_filter_not')}: + filter_db = getattr(db, filter_db_name) + if param_name in postvars.keys(): + for target in postvars[param_name]: + if len(target) > 0 and not target in filter_db: + filter_db += [target] + if len(filter_db) == 0: + params_to_encode += [(param_name, '-')] + params_to_encode += [(param_name, f) for f in filter_db] def collect_checked(prefix, postvars): tags_checked = [] @@ -424,26 +428,20 @@ class TodoHandler(PlomHandler): return tags_checked if parsed_url.path == app_config['prefix'] + '/calendar': - start = postvars['start'][0] if len(postvars['start'][0]) > 0 else '-' - end = postvars['end'][0] if len(postvars['end'][0]) > 0 else '-' - homepage = f'{app_config["prefix"]}/calendar?start={start}&end={end}' + params_to_encode += [('start', postvars['start'][0] if len(postvars['start'][0]) > 0 else '-')] + params_to_encode += [('end', postvars['end'][0] if len(postvars['end'][0]) > 0 else '-')] elif parsed_url.path == app_config['prefix'] + '/todo': task_uuid = postvars['task_uuid'][0] date = postvars['date'][0] - 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)) - homepage = f'{app_config["prefix"]}/todo?task={task_uuid}&date={date}' + params_to_encode += [('task', task_uuid), ('date', date)] + 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]) elif parsed_url.path == app_config['prefix'] + '/task': - encoded_params = urlencode(params_to_encode) id_ = postvars['id'][0] + params_to_encode += [('id', id_)] if 'title' in postvars.keys(): - db.update_task(id_, postvars['title'][0], postvars['default_weight'][0], postvars['joined_tags'][0], collect_checked('tag_', postvars), collect_checked('link_', postvars)) - homepage = f'{app_config["prefix"]}/task?id={id_}&{encoded_params}' - - elif parsed_url.path == app_config['prefix'] + '/tasks': - encoded_params = urlencode(params_to_encode) - homepage = f'{app_config["prefix"]}/tasks?{encoded_params}' + db.update_task(id_, postvars['title'][0], postvars['default_effort'][0], postvars['joined_tags'][0], collect_checked('tag_', postvars), collect_checked('link_', postvars)) elif parsed_url.path == app_config['prefix'] + '/day': if 'expect_unchosen_done' in postvars.keys(): @@ -453,19 +451,20 @@ class TodoHandler(PlomHandler): if 't_uuid' in postvars.keys(): for i, uuid in enumerate(postvars['t_uuid']): t = db.tasks[uuid] - if uuid in db.selected_day.todos.keys() and ((not 'choose' in postvars) or uuid not in postvars['choose']): + 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(): del db.selected_day.todos[uuid] if 'choose' in postvars.keys(): for i, uuid in enumerate(postvars['t_uuid']): - if uuid in postvars['choose']: + uuids = postvars['choose'] + postvars['done'] if 'done' in postvars.keys() else [] + if uuid in uuids or postvars['day_effort'][i] != '' or postvars['importance'][i] != '1.0': done = 'done' in postvars and uuid in postvars['done'] - db.update_todo_mini(uuid, db.selected_date, postvars['day_weight'][i], done) + db.update_todo_mini(uuid, db.selected_date, postvars['day_effort'][i], done, postvars['importance'][i]) if 'day_comment' in postvars.keys(): db.selected_day.comment = postvars['day_comment'][0] params_to_encode += [('selected_date', db.selected_date)] - encoded_params = urlencode(params_to_encode) - homepage = f'{app_config["prefix"]}/day?{encoded_params}' + encoded_params = urlencode(params_to_encode) + homepage = f'{parsed_url.path}?{encoded_params}' db.write() self.redirect(homepage) @@ -478,50 +477,42 @@ class TodoHandler(PlomHandler): cookie_db = self.get_cookie_db(app_config['cookie_name']) parsed_url = urlparse(self.path) params = parse_qs(parsed_url.query) - selected_date = None - t_filter_and = None - t_filter_not = None - hide_unchosen = False - hide_done = False + + def get_param(param_name, boolean=False, chained=False): + if chained: + param = params.get(param_name, None) + else: + param = params.get(param_name, [None])[0] + if (not chained and param == '-') or (chained and param == ['-']): + param = None + if param_name in cookie_db.keys(): + del cookie_db[param_name] + if param is None and param_name in cookie_db.keys(): + param = cookie_db[param_name] + if param is not None: + if boolean: + param = param != '0' + cookie_db[param_name] = str(int(param)) + else: + cookie_db[param_name] = param + elif param is boolean: + param = False + return param + + selected_date = t_filter_and = t_filter_not = None + hide_unchosen = hide_done = False if parsed_url.path in {app_config['prefix'] + '/day', app_config['prefix'] + '/tasks'}: - selected_date = params.get('selected_date', [None])[0] - if selected_date is None and 'selected_date' in cookie_db.keys(): - selected_date = cookie_db['selected_date'] - cookie_db['selected_date'] = selected_date + selected_date = get_param('selected_date') if parsed_url.path in {app_config['prefix'] + '/day', app_config['prefix'] + '/tasks', app_config['prefix'] + '/task'}: - t_filter_and = params.get('t_and', None) - if t_filter_and is None and 't_and' in cookie_db.keys(): - t_filter_and = cookie_db['t_and'] - elif t_filter_and == ['-']: - t_filter_and = None - cookie_db['t_and'] = t_filter_and - t_filter_not = params.get('t_not', None) - if t_filter_not is None: - if 't_not' in cookie_db.keys(): - t_filter_not = cookie_db['t_not'] - else: - t_filter_not = ['deleted'] - elif t_filter_not == ['-']: - t_filter_not = None - cookie_db['t_not'] = t_filter_not + t_filter_and = get_param('t_and', chained=True) + t_filter_not = get_param('t_not', chained=True) if parsed_url.path == app_config['prefix'] + '/day': - hide_unchosen_params = params.get('hide_unchosen', []) - if 0 == len(hide_unchosen_params): - if 'hide_unchosen' in cookie_db.keys(): - hide_unchosen = cookie_db['hide_unchosen'] - else: - hide_unchosen = hide_unchosen_params[0] != '0' - cookie_db['hide_unchosen'] = hide_unchosen - hide_done_params = params.get('hide_done', []) - if 0 == len(hide_done_params): - if 'hide_done' in cookie_db.keys(): - hide_done = cookie_db['hide_done'] - else: - hide_done = hide_done_params[0] != '0' - cookie_db['hide_done'] = hide_done + hide_unchosen = get_param('hide_unchosen', boolean=True) + hide_done = get_param('hide_done', boolean=True) db = TodoDB(app_config['prefix'], selected_date, t_filter_and, t_filter_not, hide_unchosen, hide_done) if parsed_url.path == app_config['prefix'] + '/day': - page = db.show_day() + task_sort = get_param('sort') + page = db.show_day(task_sort) elif parsed_url.path == app_config['prefix'] + '/todo': todo_date = params.get('date', [None])[0] task_uuid = params.get('task', [None])[0] @@ -539,21 +530,9 @@ class TodoHandler(PlomHandler): self.unset_cookie(app_config['cookie_name'], app_config['cookie_path']) page = 'cookie unset!' else: - start_date = params.get('start', [None])[0] - if start_date is None: - if 'calendar_start' in cookie_db.keys(): - start_date = cookie_db['calendar_start'] - else: - start_date = 'today' - elif start_date == '-': - start_date = None - cookie_db['calendar_start'] = start_date - end_date = params.get('end', [None])[0] - if end_date is None and 'calendar_end' in cookie_db.keys(): - end_date = cookie_db['calendar_end'] - elif end_date == '-': - end_date = None - cookie_db['calendar_end'] = end_date + start_date = get_param('start') + start_date = start_date if start_date else 'today' + end_date = get_param('end') page = db.show_calendar(start_date, end_date) if parsed_url.path != app_config['prefix'] + '/unset_cookie': self.set_cookie(app_config['cookie_name'], app_config['cookie_path'], cookie_db) diff --git a/todo_templates/day.html b/todo_templates/day.html index a6ad249..7dd8f66 100644 --- a/todo_templates/day.html +++ b/todo_templates/day.html @@ -31,40 +31,41 @@ comment:

- -{% for uuid, t in db.tasks.items() | sort(attribute='1.title') %} -{% if t.visible and (uuid not in db.selected_day.todos.keys() or db.selected_day.todos[uuid].visible) %} + +{% for row in task_rows %} - - -{% if uuid in db.selected_day.todos.keys() %} - - - - - - + + +{% if row.todo %} + + + + + + + {% else %} - - + + + + {% endif %} -{% endif %} {% endfor %}
taskchoose?done?weightedit?day tagscomment
taskchoose?done?effortimportanceedit?day tagscomment
] {{ t.current_title|e }}tags: {% for tag in t.tags | sort %}{{ tag }} {% endfor %}
edit{% for tag in db.selected_day.todos[uuid].day_tags | sort %}{{ tag }} {% endfor %}{{ db.selected_day.todos[uuid].comment|e }}
] {{ row.task.current_title|e }}tags: {% for tag in row.task.tags | sort %}{{ tag }} {% endfor %}
edit{% for tag in row.todo.day_tags | sort %}{{ tag }} {% endfor %}{{ row.todo.comment|e }} - +edit
{% include 'watch_form.html' %}