From: Christian Heller Date: Mon, 11 Dec 2023 06:14:26 +0000 (+0100) Subject: Further improvements to todo accounting. X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/%22https:/validator.w3.org/%7B%7Btodo.date%7D%7D?a=commitdiff_plain;h=5c55641640539a8a2628959d4bbbf3c0a7c8ae19;p=misc Further improvements to todo accounting. --- diff --git a/todo.py b/todo.py index d4ca1e7..88684ac 100644 --- a/todo.py +++ b/todo.py @@ -8,20 +8,23 @@ from urllib.parse import urlparse db_path = '/home/plom/org/todo_new.json' # db_path = '/home/plom/public_repos/misc/todo_new.json' server_port = 8082 +DATE_FORMAT = '%Y-%m-%d' html_head = """ @@ -32,14 +35,19 @@ tasks: list a
""" form_footer = '\n' - form_header_tmpl = """
""" calendar_tmpl = """ +

+from: +to: + +

-{% for date, day in days.items() | sort(reverse=True) %} - +{% for date, day in days.items() | sort() %} +{% if day.weekday == "Monday" %}{% endif %} + {% for task, todo in day.todos.items() | sort(attribute='1.title', reverse=True) %} {% if todo.visible %} @@ -70,38 +78,19 @@ task_tmpl = """
{{ date }} {{ day.weekday }} ({{ day.todos_sum|round(2) }}) {{ day.comment|e }}
{{ date }} {{ day.weekday }} ({{ day.todos_sum|round(2) }}) {{ day.comment|e }}
{% if todo.done %}✓{% else %}  {% endif %}{{ todo.title }}{{ todo.comment|e }}
""" -# archived_days_tmpl = """ -# -# {% for date, day in days.items() | sort(reverse=True) %} -# {% if day.archived %} -# -# {% for task, todo in day.todos.items() | sort(attribute='1.title', reverse=True) %} -# -# {% endfor %} -# {% endif %} -# {% endfor %} -#
{{ date }} {{ day.weekday }} ({{ day.todos_sum|round(2) }}) {{ day.comment|e }}
{{ todo.title }}{% if todo.done %}✓{% endif %}{{ todo.weight }}
-# """ day_tmpl = """ - -hide unchosen:
-mandatory tags: {% for t_tag in db.t_tags | sort %} -{{ t_tag }} -{% endfor %} -
-forbidden tags: {% for t_tag in db.t_tags | sort %} -{{ t_tag }} -{% endfor %} -

-

+ hide unchosen | +prev next | -prev next date: {{ db.selected_day.todos_sum|round(2) }} ({{ db.selected_day.todos_sum2|round(2)}}) comment: +date: | +{{ db.selected_day.todos_sum|round(2) }} ({{ db.selected_day.todos_sum2|round(2)}}) | +comment:

- +
-{% for uuid, t in db.tasks.items() | sort(attribute='1.title', reverse=True) %} +{% for uuid, t in db.tasks.items() | sort(attribute='1.title') %} {% if t.visible %} @@ -116,19 +105,48 @@ forbidden tags: {% for t_tag in db.t_tags | sort %}
taskchoose?done?weightcomment
""" -tasks_tmpl = """ +tag_filters_tmpl = """ +

+ +

- mandatory tags: {% for t_tag in db.t_tags | sort %} -{{ t_tag }} +mandatory tags: +{% for and_filter in db.t_filter_and %} + +{% endfor %} +
-forbidden tags: {% for t_tag in db.t_tags | sort %} -{{ t_tag }} +forbidden tags: +{% for not_filter in db.t_filter_not %} + +{% endfor %} +

- +""" +tasks_tmpl = """ +
-{% for uuid, t in db.tasks.items() | sort(attribute='1.title', reverse=True) %} +{% for uuid, t in db.tasks.items() | sort(attribute='1.title') %} {% if t.visible %} @@ -138,41 +156,6 @@ forbidden tags: {% for t_tag in db.t_tags | sort %} {% endfor %}
default
weight
tasktags
{{ t.default_weight }}
""" -# selected_day_tmpl = """ -#

-# hide unchosen:
-# mandatory tags: {% for t_tag in db.t_tags | sort %} -# {{ t_tag }} -# {% endfor %} -#
-# forbidden tags: {% for t_tag in db.t_tags | sort %} -# {{ t_tag }} -# {% endfor %} -#

-# -#

-# -# date: {{ db.selected_day.todos_sum|round(2) }} ({{ db.selected_day.todos_sum2|round(2)}}) comment: -#

-# -# -# -# {% for uuid, t in db.tasks.items() | sort(attribute='1.title', reverse=True) %} -# {% if t.visible %} -# -# -# -# -# -# -# -# -# {% endif %} -# {% endfor %} -#
default
weight
titletagschoose?done?day
weight
-#
-# -# """ class Task: @@ -364,14 +347,13 @@ class TodoDB(PlomDB): def __init__(self, prefix, selected_date=None, t_filter_and = None, t_filter_not = None, hide_unchosen=False): self.prefix = prefix self.selected_date = selected_date if selected_date else str(datetime.now())[:10] - self.t_filter_and = t_filter_and if t_filter_and else set() - self.t_filter_not = t_filter_not if t_filter_not else set() + self.t_filter_and = t_filter_and if t_filter_and else [] + self.t_filter_not = t_filter_not if t_filter_not else [] self.hide_unchosen = hide_unchosen self.days = {} self.tasks = {} self.t_tags = set() super().__init__(db_path) - self.switch_to_day() def read_db_file(self, f): d = json.load(f) @@ -387,8 +369,8 @@ class TodoDB(PlomDB): def to_dict(self): d = { - 't_filter_and': list(self.t_filter_and), - 't_filter_not': list(self.t_filter_not), + 't_filter_and': self.t_filter_and, + 't_filter_not': self.t_filter_not, 'tasks': {}, 'days': {} } @@ -400,20 +382,27 @@ class TodoDB(PlomDB): @property def selected_day(self): + if not self.selected_date in self.days.keys(): + 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(): + if len(day.todos) == 0 and len(day.comment) == 0: + dates_to_purge += [date] + for date in dates_to_purge: + del self.days[date] self.write_text_to_db(json.dumps(self.to_dict())) - def switch_to_day(self, date=None): - if date: - self.selected_date = date - # if self.selected_date in self.days.keys(): - # self.selected_day.archived = True - if not self.selected_date in self.days.keys(): - self.days[self.selected_date] = self.add_day() - # self.selected_day.archived = False - def add_task(self, id_=None, dict_source=None, return_id=False): t = Task.from_dict(self, dict_source) if dict_source else Task(self) id_ = id_ if id_ else str(uuid4()) @@ -426,41 +415,33 @@ 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_all(self): - # for i in range(10): - # self.add_task(id_=f'new{i}') - # return Template(form_header_tmpl + selected_day_tmpl + archived_days_tmpl + form_footer).render(db=self, action=self.prefix+'/all', days=self.days) - - #def show_selected_day(self): - # return Template(form_header_tmpl + selected_day_tmpl + form_footer).render(db=self, action=self.prefix+'/day') - def show_day(self): - current_date = datetime.strptime(self.selected_date, '%Y-%m-%d') + current_date = datetime.strptime(self.selected_date, DATE_FORMAT) prev_date = current_date - timedelta(days=1) - prev_date_str = prev_date.strftime('%Y-%m-%d') + prev_date_str = prev_date.strftime(DATE_FORMAT) next_date = current_date + timedelta(days=1) - next_date_str = next_date.strftime('%Y-%m-%d') - return Template(form_header_tmpl + day_tmpl + form_footer).render(db=self, action=self.prefix+'/day', prev_date=prev_date_str, next_date=next_date_str) + next_date_str = next_date.strftime(DATE_FORMAT) + return Template(form_header_tmpl + tag_filters_tmpl + day_tmpl + form_footer).render(db=self, action=self.prefix+'/day', prev_date=prev_date_str, next_date=next_date_str) def show_calendar(self, start_date_str, end_date_str): days_to_show = {} target_start = start_date_str if start_date_str else sorted(self.days.keys())[0] - target_start = str(datetime.now())[:10] if 'today' == target_start else target_start + # target_start = str(datetime.now())[:10] if 'today' == target_start else target_start target_end = end_date_str if end_date_str else sorted(self.days.keys())[-1] - todays_date = str(datetime.now())[:10] - start_date = datetime.strptime(target_start, '%Y-%m-%d') - end_date = datetime.strptime(target_end, '%Y-%m-%d') + # todays_date = str(datetime.now())[:10] + start_date = datetime.strptime(target_start, DATE_FORMAT) + end_date = datetime.strptime(target_end, DATE_FORMAT) for n in range(int((end_date - start_date).days) + 1): current_date_obj = start_date + timedelta(n) - current_date = current_date_obj.strftime('%Y-%m-%d') + current_date = current_date_obj.strftime(DATE_FORMAT) if current_date not in self.days.keys(): days_to_show[current_date] = self.add_day() else: days_to_show[current_date] = self.days[current_date] - days_to_show[current_date].weekday = datetime.strptime(current_date, '%Y-%m-%d').strftime('%A') + days_to_show[current_date].weekday = datetime.strptime(current_date, DATE_FORMAT).strftime('%A') for task_uuid, todo in days_to_show[current_date].todos.items(): todo.visible = self.tasks[task_uuid].visible - return Template(calendar_tmpl).render(db=self, days=days_to_show) + return Template(form_header_tmpl + calendar_tmpl + form_footer).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] @@ -471,7 +452,6 @@ class TodoDB(PlomDB): todo = self.days[date].todos[task_uuid] else: todo = self.days[date].add_todo(task_uuid) - # todo = self.days[date].todos[task_uuid] todo.day_weight = float(day_weight) if len(day_weight) > 0 else None todo.done = done todo.comment = comment @@ -487,7 +467,7 @@ class TodoDB(PlomDB): task.tags_joined = tags_joined def show_tasks(self): - return Template(form_header_tmpl + tasks_tmpl + form_footer).render(db=self, action=self.prefix+'/tasks') + return Template(form_header_tmpl + tag_filters_tmpl + tasks_tmpl + form_footer).render(db=self, action=self.prefix+'/tasks') @@ -509,130 +489,65 @@ class TodoHandler(PlomHandler): postvars = parse_qs(self.rfile.read(length).decode(), keep_blank_values=1) parsed_url = urlparse(self.path) db = TodoDB(prefix=prefix) - if parsed_url.path == prefix + '/todo': + + if parsed_url.path == 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'{prefix}/calendar?t_and=calendar&start={start}&end={end}' + + elif parsed_url.path == 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]) homepage = f'{prefix}/todo?task={task_uuid}&date={date}' + elif parsed_url.path == prefix + '/task': id_ = postvars['id'][0] db.update_task(id_, postvars['title'][0], postvars['default_weight'][0], postvars['tags'][0]) homepage = f'{prefix}/task?id={id_}' + elif parsed_url.path in {prefix + '/tasks', prefix + '/day'}: - if 't_filter_and' in postvars.keys(): - for target in postvars['t_filter_and']: - db.t_filter_and.add(target) - if 't_filter_not' in postvars.keys(): - for target in postvars['t_filter_not']: - db.t_filter_not.add(target) + for target in postvars['t_and']: + if not target in db.t_filter_and: + db.t_filter_and += [target] + for target in postvars['t_not']: + if not target in db.t_filter_not: + db.t_filter_not += [target] if 'hide_unchosen' in postvars.keys(): db.hide_unchosen = True data = [('t_and', f) for f in db.t_filter_and] + [('t_not', f) for f in db.t_filter_not] + [('hide_unchosen', int(db.hide_unchosen))] + if parsed_url.path == prefix + '/tasks': encoded_params = urlencode(data) homepage = f'{prefix}/tasks?{encoded_params}' + elif parsed_url.path == prefix + '/day': - db.switch_to_day(postvars['original_selected_date'][0]) + db.selected_date = postvars['original_selected_date'][0] new_selected_date = postvars['new_selected_date'][0] try: - datetime.strptime(new_selected_date, '%Y-%m-%d') + datetime.strptime(new_selected_date, DATE_FORMAT) except ValueError: raise PlomException(f"{prefix} bad date string: {new_selected_date}") if new_selected_date != db.selected_date: - if new_selected_date in db.days.keys(): - raise PlomException('cannot use same date twice') - else: - db.days[new_selected_date] = db.selected_day - del db.days[db.selected_date] - db.selected_date = new_selected_date - 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']): - del db.selected_day.todos[uuid] - if 'choose' in postvars.keys(): + db.change_selected_days_date(new_selected_date) + if 't_uuid' in postvars.keys(): for i, uuid in enumerate(postvars['t_uuid']): - if uuid in postvars['choose']: - done = 'done' in postvars and uuid in postvars['done'] - db.update_todo(uuid, db.selected_date, postvars['day_weight'][i], done, postvars['todo_comment'][i]) - if 'comment' in postvars.keys(): - db.selected_day.comment = postvars['comment'][0] + t = db.tasks[uuid] + if uuid in db.selected_day.todos.keys() and ((not 'choose' in postvars) or uuid not in postvars['choose']): + del db.selected_day.todos[uuid] + if 'choose' in postvars.keys(): + for i, uuid in enumerate(postvars['t_uuid']): + if uuid in postvars['choose']: + done = 'done' in postvars and uuid in postvars['done'] + db.update_todo(uuid, db.selected_date, postvars['day_weight'][i], done, postvars['todo_comment'][i]) + if 'day_comment' in postvars.keys(): + db.selected_day.comment = postvars['day_comment'][0] data += [('date', db.selected_date)] encoded_params = urlencode(data) homepage = f'{prefix}/day?{encoded_params}' + db.write() self.redirect(homepage) - # db = TodoDB(prefix=prefix, selected_date=postvars['original_selected_date'][0]) - # # import pprint - # # pp = pprint.PrettyPrinter(indent=4) - # # pp.pprint(postvars) - # db.t_filter_and = set() - # db.t_filter_not = set() - # if 't_filter_and' in postvars.keys(): - # for target in postvars['t_filter_and']: - # db.t_filter_and.add(target) - # if 't_filter_not' in postvars.keys(): - # for target in postvars['t_filter_not']: - # db.t_filter_not.add(target) - # if 'hide_unchosen' in postvars.keys(): - # db.hide_unchosen = True - # if 't_uuid' in postvars.keys(): - # new_postvars_t_uuid = postvars['t_uuid'].copy() - # for i, uuid in enumerate(postvars['t_uuid']): - # if len(uuid) < 36 and len(postvars['t_title'][i]) > 0: - # new_uuid, t = db.add_task(return_id=True) - # new_postvars_t_uuid[i] = new_uuid - # for key in [k for k in postvars.keys() if not k == 't_uuid']: - # if uuid in postvars[key]: - # uuid_index = postvars[key].index(uuid) - # postvars[key][uuid_index] = new_uuid - # postvars['t_uuid'] = new_postvars_t_uuid - # for i, uuid in enumerate(postvars['t_uuid']): - # if len(uuid) < 36: - # continue - # t = db.tasks[uuid] - # t.set_title(postvars['t_title'][i]) - # t.tags_from_joined_string(postvars['t_tags'][i]) - # t.set_default_weight(float(postvars['t_default_weight'][i])) - # if uuid in db.selected_day.todos.keys() and ((not 'choose' in postvars) or uuid not in postvars['choose']): - # del db.selected_day.todos[uuid] - # if 'choose' in postvars.keys(): - # for i, uuid in enumerate(postvars['t_uuid']): - # if uuid in postvars['choose']: - # done = 'done' in postvars and uuid in postvars['done'] - # day_weight = float(postvars['day_weight'][i]) if postvars['day_weight'][i] else None - # db.selected_day.add_todo(uuid, {'done': done, 'day_weight': day_weight}) - - # if 'comment' in postvars.keys(): - # db.selected_day.comment = postvars['comment'][0] - # if 'new_selected_date' in postvars.keys(): - # new_selected_date = postvars['new_selected_date'][0] - # try: - # datetime.strptime(new_selected_date, '%Y-%m-%d') - # except ValueError: - # raise PlomException(f"{prefix} bad date string: {new_selected_date}") - # if new_selected_date != db.selected_date: - # if new_selected_date in db.days.keys(): - # raise PlomException('cannot use same date twice') - # else: - # db.days[new_selected_date] = db.selected_day - # del db.days[db.selected_date] - # db.selected_date = new_selected_date - - # switch_edited_day = None - # day_edit_prefix = 'edit_' - # for k in postvars.keys(): - # if k.startswith(day_edit_prefix): - # switch_edited_day = k[len(day_edit_prefix):] - # db.switch_to_day(switch_edited_day) - # break - # db.write() - # data = [('t_and', f) for f in db.t_filter_and] + [('t_not', f) for f in db.t_filter_not] + [('hide_unchosen', int(db.hide_unchosen))] + [('date', db.selected_date)] - # encoded_params = urlencode(data) - # if prefix + '/day' == parsed_url.path: - # homepage = f'{prefix}/day?{encoded_params}' - # else: - # homepage = f'{prefix}/all?{encoded_params}' - # self.redirect(homepage) def do_GET(self): self.try_do(self.show_db) @@ -642,8 +557,8 @@ class TodoHandler(PlomHandler): parsed_url = urlparse(self.path) params = parse_qs(parsed_url.query) selected_date = params.get('date', [None])[0] - t_filter_and = set(params.get('t_and', [])) - t_filter_not = set(params.get('t_not', ['deleted'])) + t_filter_and = params.get('t_and', []) + t_filter_not = params.get('t_not', ['deleted']) hide_unchosen_params = params.get('hide_unchosen', []) hide_unchosen = len(hide_unchosen_params) > 0 and hide_unchosen_params[0] != '0' db = TodoDB(prefix, selected_date, t_filter_and, t_filter_not, hide_unchosen)