From: Christian Heller Date: Sun, 10 Dec 2023 20:47:42 +0000 (+0100) Subject: Improve todo accounting. X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/%7B%7B%20web_path%20%7D%7D/decks/condition_titles?a=commitdiff_plain;h=187d7e878767dcd4072a562e834cb7abd4349a06;p=misc Improve todo accounting. --- diff --git a/todo.py b/todo.py index 8da5f9e..a9e1807 100644 --- a/todo.py +++ b/todo.py @@ -17,16 +17,19 @@ td { border: 1px solid black; } all | edit day: choose do +coming
""" - form_footer = '\n' +form_header_tmpl = """ +
+""" archived_days_tmpl = """ -{% for date, day in db.days.items() | sort(reverse=True) %} +{% 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 %} @@ -34,9 +37,7 @@ archived_days_tmpl = """ {% endfor %}
{{ date }} ({{ day.todos_sum |round(2) }}) {{ day.comment|e }}
{{ date }} {{ day.weekday }} ({{ day.todos_sum|round(2) }}) {{ day.comment|e }}
{{ todo.title }}{% if todo.done %}✓{% endif %}{{ todo.weight }}
""" - selected_day_tmpl = """ - hiden unchosen:
mandatory tags: {% for t_tag in db.t_tags | sort %} {{ t_tag }} @@ -245,6 +246,10 @@ class TodoDB(PlomDB): d['days'][date] = day.to_dict() return d + @property + def selected_day(self): + return self.days[self.selected_day_date] + def write(self): self.write_text_to_db(json.dumps(self.to_dict())) @@ -255,8 +260,8 @@ class TodoDB(PlomDB): self.selected_day_date = date else: self.selected_day_date = str(datetime.now())[:10] - if not self.selected_day_date in self.days.keys(): - self.days[self.selected_day_date] = self.add_day() + if not self.selected_day_date in self.days.keys(): + self.days[self.selected_day_date] = self.add_day() self.selected_day.archived = False def add_task(self, id_=None, dict_source=None, return_id=False): @@ -277,14 +282,30 @@ class TodoDB(PlomDB): for date, day in self.days.items(): for task_uuid, todo in day.todos.items(): todo.title = self.tasks[task_uuid].title_at(date) - return Template(selected_day_tmpl + archived_days_tmpl + form_footer).render(db=self, action=self.prefix+'/all') - - @property - def selected_day(self): - return self.days[self.selected_day_date] + 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(selected_day_tmpl + form_footer).render(db=self, action=self.prefix+'/day') + return Template(form_header_tmpl + selected_day_tmpl + form_footer).render(db=self, action=self.prefix+'/day') + + def show_coming(self): + from datetime import timedelta + todays_date = str(datetime.now())[:10] + days_to_show = self.days.copy() + for day in days_to_show.values(): + day.archived = False + last_date = sorted(days_to_show.keys())[-1] + start_date = datetime.strptime(todays_date, '%Y-%m-%d') + end_date = datetime.strptime(last_date, '%Y-%m-%d') + 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') + if current_date not in days_to_show.keys(): + days_to_show[current_date] = self.add_day() + days_to_show[current_date].archived = True + days_to_show[current_date].weekday = datetime.strptime(current_date, '%Y-%m-%d').strftime('%A') + for task_uuid, todo in days_to_show[current_date].todos.items(): + todo.title = self.tasks[task_uuid].title_at(current_date) + return Template(form_header_tmpl + archived_days_tmpl + form_footer).render(db=self, action=self.prefix+'/day', days=days_to_show) class TodoHandler(PlomHandler): @@ -344,26 +365,30 @@ class TodoHandler(PlomHandler): 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}) - db.selected_day.comment = postvars['comment'][0] - new_selected_day_date = postvars['selected_day_date'][0] - if new_selected_day_date != db.selected_day_date: - if new_selected_day_date in db.days.keys(): - raise PlomException('cannot use same date twice') - else: - db.days[new_selected_day_date] = db.selected_day - del db.days[db.selected_day_date] - db.selected_day_date = new_selected_day_date + if 'comment' in postvars.keys(): + db.selected_day.comment = postvars['comment'][0] + if 'selected_day_date' in postvars.keys(): + new_selected_day_date = postvars['selected_day_date'][0] + try: + datetime.strptime(new_selected_day_date, '%Y-%m-%d') + except ValueError: + raise PlomException(f"{prefix} bad date string: {new_selected_day_date}") + if new_selected_day_date != db.selected_day_date: + if new_selected_day_date in db.days.keys(): + raise PlomException('cannot use same date twice') + else: + db.days[new_selected_day_date] = db.selected_day + del db.days[db.selected_day_date] + db.selected_day_date = new_selected_day_date switch_edited_day = None - for date in db.days.keys(): - if f'edit_{date}' in postvars.keys(): - switch_edited_day = date + day_edit_prefix = 'edit_' + for k in postvars.keys(): + if k.startswith(day_edit_prefix): + switch_edited_day = k[len(day_edit_prefix):] break if 'archive_day' in postvars.keys() or switch_edited_day: - if switch_edited_day: - db.switch_to_day(date) - else: - db.switch_to_day() + db.switch_to_day(switch_edited_day) 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))] encoded_params = urlencode(data) @@ -388,6 +413,8 @@ class TodoHandler(PlomHandler): db = TodoDB(prefix, t_filter_and, t_filter_not, hide_unchosen) if parsed_url.path == prefix + '/day': page = db.show_selected_day() + elif parsed_url.path == prefix + '/coming': + page = db.show_coming() else: page = db.show_all() header = Template(html_head).render(prefix=prefix)