From: Christian Heller <c.heller@plomlompom.de>
Date: Thu, 14 Dec 2023 05:55:12 +0000 (+0100)
Subject: Improve todo accounting.
X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/static/%7B%7Bdb.prefix%7D%7D/%7B%7Bprefix%7D%7D/processes?a=commitdiff_plain;h=520a7a19656bb44f43b405fcd65fc495fac62748;p=misc

Improve todo accounting.
---

diff --git a/plomlib.py b/plomlib.py
index d95a35c..a94eb54 100644
--- a/plomlib.py
+++ b/plomlib.py
@@ -1,5 +1,7 @@
 import os
-from http.server import BaseHTTPRequestHandler 
+from http.server import BaseHTTPRequestHandler
+from http.cookies import SimpleCookie
+import json
 
 
 class PlomException(Exception):
@@ -50,10 +52,10 @@ class PlomDB:
         # timedelta, keep the newest file that's older
         ages_to_keep = [timedelta(minutes=4**i) for i in range(0, 8)]
         print(f'DEBUG ages_to_keep: {ages_to_keep}')
-        now = datetime.now() 
+        now = datetime.now()
         to_save = {}
         for age in ages_to_keep:
-            limit = now - age 
+            limit = now - age
             for mtime in reversed(sorted(mtimes_to_paths.keys())):
                 print(f'DEBUG checking if {mtime} < {limit} ({now} - {age})')
                 if datetime.strptime(mtime, '%Y-%m-%d %H:%M:%S.%f') < limit:
@@ -75,7 +77,7 @@ class PlomDB:
                 shutil.move(source, target)
             i += 1
 
-        # put copy of current state at end of bak list 
+        # put copy of current state at end of bak list
         print(f'DEBUG saving current state to {bak_prefix}{i}')
         shutil.copy(self.db_file, f'{bak_prefix}{i}')
 
@@ -87,11 +89,11 @@ class PlomDB:
         self.unlock()
 
 
-class PlomHandler(BaseHTTPRequestHandler): 
+class PlomHandler(BaseHTTPRequestHandler):
     homepage = '/'
     html_head = '<!DOCTYPE html>\n<html>\n<meta charset="UTF-8">'
     html_foot = '</body>\n</html>'
-    
+
     def fail_400(self, e):
         self.send_HTML(f'ERROR: {e}', 400)
 
@@ -99,8 +101,39 @@ class PlomHandler(BaseHTTPRequestHandler):
         self.send_code_and_headers(code, [('Content-type', 'text/html')])
         self.wfile.write(bytes(f'{self.html_head}\n{html}\n{self.html_foot}', 'utf-8'))
 
+    def ensure_cookies_to_set(self):
+        if not hasattr(self, 'cookies_to_set'):
+            self.cookies_to_set = []
+
+    def set_cookie(self, cookie_name, cookie_path, cookie_db):
+        self.ensure_cookies_to_set()
+        cookie = SimpleCookie()
+        cookie[cookie_name] = json.dumps(cookie_db)
+        cookie[cookie_name]['path'] = cookie_path
+        self.cookies_to_set += [cookie]
+
+    def unset_cookie(self, cookie_name, cookie_path):
+        self.ensure_cookies_to_set()
+        cookie = SimpleCookie()
+        cookie[cookie_name] = ''
+        cookie[cookie_name]['path'] = cookie_path
+        cookie[cookie_name]['expires'] = 'Thu, 01 Jan 1970 00:00:00 GMT'
+        self.cookies_to_set += [cookie]
+
+    def get_cookie_db(self, cookie_name):
+        cookie_db = {}
+        if 'Cookie' in self.headers:
+            cookie = SimpleCookie(self.headers['Cookie'])
+            if cookie_name in cookie:
+                cookie_db = json.loads(cookie[cookie_name].value)
+        return cookie_db
+
     def send_code_and_headers(self, code, headers=[]):
+        self.ensure_cookies_to_set()
         self.send_response(code)
+        for cookie in self.cookies_to_set:
+            for morsel in cookie.values():
+                self.send_header('Set-Cookie', morsel.OutputString())
         for fieldname, content in headers:
             self.send_header(fieldname, content)
         self.end_headers()
@@ -110,7 +143,7 @@ class PlomHandler(BaseHTTPRequestHandler):
 
     def try_do(self, do_method):
         try:
-            do_method() 
+            do_method()
         except PlomException as e:
             self.fail_400(e)
 
diff --git a/todo.py b/todo.py
index 42639a1..69cb8f5 100644
--- a/todo.py
+++ b/todo.py
@@ -1,4 +1,4 @@
-from plomlib import PlomDB, run_server, PlomHandler, PlomException 
+from plomlib import PlomDB, run_server, PlomHandler, PlomException
 import json
 from uuid import uuid4
 from datetime import datetime, timedelta
@@ -31,10 +31,11 @@ input[type="number"] { font-family: monospace; text-align: right; }
 input[type="text"] { width: 100%; box-sizing: border-box; }
 </style>
 <body>
-tasks: <a href="{{db.prefix}}/tasks">list</a> <a href="{{db.prefix}}/add_task">add</a> | day: 
-<a href="{{db.prefix}}/day{% if date %}?date={{date}}{% endif %}">choose tasks</a>
-<a href="{{db.prefix}}/day?{% if date %}date={{date}}&{% endif %}hide_unchosen=1&hide_done=1">do tasks</a>
-| <a href="{{db.prefix}}/calendar?t_and=calendar">calendar</a>
+tasks: <a href="{{db.prefix}}/tasks">list</a> <a href="{{db.prefix}}/add_task">add</a> | day:
+<a href="{{db.prefix}}/day?hide_unchosen=0&hide_done=0">choose tasks</a>
+<a href="{{db.prefix}}/day?hide_unchosen=1&hide_done=1">do tasks</a>
+| <a href="{{db.prefix}}/calendar">calendar</a>
+| <a href="{{db.prefix}}/unset_cookie">unset cookie</a>
 <hr />
 """
 form_footer = '\n</form>'
@@ -50,10 +51,10 @@ to: <input name="end" {% if end_date %}value="{{ end_date }}"{% endif %} placeho
 <table>
 {% for date, day in days.items() | sort() %}
 {% if day.weekday == "Mo" %}<tr class="week_row"><td colspan=3></td></tr>{% endif %}
-<tr class="day_row"><td colspan=3><a href="{{db.prefix}}/day?date={{date}}&hide_unchosen=1">{{ day.weekday }} {{ date }}</a> |{{ '%04.1f' % day.todos_sum|round(2) }}| {{ day.comment|e }}</td></tr>
+<tr class="day_row"><td colspan=3><a href="{{db.prefix}}/day?selected_date={{date}}&hide_unchosen=1">{{ day.weekday }} {{ date }}</a> |{{ '%04.1f' % day.todos_sum|round(2) }}| {{ day.comment|e }}</td></tr>
 {% for task, todo in day.todos.items() | sort(attribute='1.title', reverse=True)  %}
 {% if todo.visible %}
-<tr><td class="checkbox">{% if todo.done %}✓{% else %}&nbsp;&nbsp;{% endif %}</td><td><a href="{{db.prefix}}/todo?task={{ todo.task.id_ }}&date={{ date }}">{{ todo.title }}</a></td><td>{{ todo.comment|e }}</td></tr>
+<tr><td class="checkbox">{% if todo.done %}✓{% else %}&nbsp;&nbsp;{% endif %}</td><td><a href="{{db.prefix}}/todo?task={{ todo.task.id_ }}&date={{ date }}">{%if "cancelled" in todo.tags%}<s>{% endif %}{% if "deadline" in todo.tags %}DEADLINE: {% endif %}{{ todo.title|e }}{%if "cancelled" in todo.tags%}</s>{% endif %}</a></td><td>{{ todo.comment|e }}</td></tr>
 {% endif %}
 {% endfor %}
 {% endfor %}
@@ -69,6 +70,7 @@ todo_tmpl = """
 <tr><th>day weight</th><td class="input"><input type="number" name="day_weight" step=0.1 size=8 value="{{ todo.day_weight }}" /></td></tr>
 <tr><th>comment</th><td class="input"><input type="text" name="comment" value="{{todo.comment|e}}" /></td></tr>
 <tr><th>done</th><td class="input"><input type="checkbox" name="done" {% if todo.done %}checked{% endif %}/></td></tr>
+<tr><th>day tags</th><td class="input"><input name="day_tags" type="text" value="{{ todo.day_tags_joined|e }}" ></td></tr>
 </table>
 <input type="submit" value="update" />
 """
@@ -83,8 +85,8 @@ task_tmpl = """
 """
 day_tmpl = """
 <p>
-<input name="hide_unchosen" type="checkbox" {% if db.hide_unchosen %}checked{% endif %} /> hide unchosen <input name="hide_done" type="checkbox" {% if db.hide_done %}checked{% endif %} /> hide done | 
-<a href="{{db.prefix}}/day?date={{prev_date}}{% if db.hide_unchosen %}&hide_unchosen=1{% endif %}{% if db.hide_done %}&hide_done=1{% endif %}">prev</a> <a href="{{db.prefix}}/day?date={{next_date}}{% if db.hide_unchosen %}&hide_unchosen=1{% endif %}{% if db.hide_done %}&hide_done=1{% endif %}">next</a> | 
+<input name="hide_unchosen" type="checkbox" {% if db.hide_unchosen %}checked{% endif %} /> hide unchosen <input name="hide_done" type="checkbox" {% if db.hide_done %}checked{% endif %} /> hide done |
+<a href="{{db.prefix}}/day?selected_date={{prev_date}}">prev</a> <a href="{{db.prefix}}/day?selected_date={{next_date}}">next</a> |
 <input type="hidden" name="original_selected_date" value="{{ db.selected_date }}" />
 date: <input name="new_selected_date" value="{{ db.selected_date }}" size=10 /> |
 {{ db.selected_day.todos_sum|round(2) }} ({{ db.selected_day.todos_sum2|round(2)}}) |
@@ -92,15 +94,16 @@ comment: <input name="day_comment" value="{{ db.selected_day.comment|e }}">
 </p>
 
 <table class="alternating">
-<tr><th>task</th><th class="checkbox">choose?</th><th class="checkbox">done?</th><th>weight</th><th>comment</th></tr>
+<tr><th>task</th><th class="checkbox">choose?</th><th class="checkbox">done?</th><th>weight</th><th>day tags</th><th>comment</th></tr>
 {% for uuid, t in db.tasks.items() | sort(attribute='1.title') %}
-{% if t.visible %}
+{% if t.visible and (uuid not in db.selected_day.todos.keys() or db.selected_day.todos[uuid].visible) %}
 <tr>
 <input name="t_uuid" value="{{ uuid }}" type="hidden" >
-<td><details><summary>] <a href="{{db.prefix}}/task?id={{ uuid }}" />{{ t.current_title|e }}</a></summary>tags: {% for tag in t.tags | sort %}<a href="{{db.prefix}}/day?date={{ db.selected_date }}&t_and={{tag|e}}">{{ tag }}</a> {% endfor %}</details></td>
+<td><details><summary>] <a href="{{db.prefix}}/task?id={{ uuid }}" />{{ t.current_title|e }}</a></summary>tags: {% for tag in t.tags | sort %}<a href="{{db.prefix}}/day?t_and={{tag|e}}">{{ tag }}</a> {% endfor %}</details></td>
 <td class="checkbox"><input name="choose" type="checkbox" value="{{ uuid }}" {% if uuid in db.selected_day.todos.keys() %}checked{% endif %} ></td>
 <td class="checkbox"><input name="done" type="checkbox" value="{{ uuid }}" {% if uuid in db.selected_day.todos.keys() and db.selected_day.todos[uuid].done %}checked{% endif %} ></td>
 <td class="checkbox"><input name="day_weight" type="number" step=0.1 size=8 value="{% if uuid in db.selected_day.todos.keys() and db.selected_day.todos[uuid].day_weight %}{{ db.selected_day.todos[uuid].day_weight }}{% endif %}" placeholder={{ t.current_default_weight }} ></td>
+<td type="input"><input name="day_tags" type="text" value="{% if uuid in db.selected_day.todos.keys() %}{{ db.selected_day.todos[uuid].day_tags_joined|e }}{% endif %}" ></td>
 <td type="input"><input name="todo_comment" type="text" value="{% if uuid in db.selected_day.todos.keys() %}{{ db.selected_day.todos[uuid].comment|e }}{% endif %}" ></td>
 </tr>
 {% endif %}
@@ -120,14 +123,14 @@ mandatory tags:
 {% for tag in db.t_tags | sort %}
 <option value="{{tag|e}}" {% if and_filter == tag %}selected{% endif %}>{{tag|e}}</option>
 {% endfor %}
-</select> 
-{% endfor %} 
+</select>
+{% endfor %}
 <select name="t_and">
 <option></option>
 {% for tag in db.t_tags | sort %}
 <option value="{{tag|e}}">{{tag|e}}</option>
 {% endfor %}
-</select> 
+</select>
 <br />
 forbidden tags:
 {% for not_filter in db.t_filter_not %}
@@ -136,14 +139,14 @@ forbidden tags:
 {% for tag in db.t_tags | sort %}
 <option value="{{tag|e}}" {% if not_filter == tag %}selected{% endif %}>{{tag|e}}</option>
 {% endfor %}
-</select> 
-{% endfor %} 
+</select>
+{% endfor %}
 <select name="t_not">
 <option></option>
 {% for tag in db.t_tags | sort %}
 <option value="{{tag|e}}">{{tag|e}}</option>
 {% endfor %}
-</select> 
+</select>
 </p>
 """
 tasks_tmpl = """
@@ -176,7 +179,7 @@ class Task:
 
     def _last_of_history(self, history, default):
         keys = sorted(history.keys())
-        return default if 0 == len(history) else history[keys[-1]] 
+        return default if 0 == len(history) else history[keys[-1]]
 
     @classmethod
     def from_dict(cls, db, d):
@@ -195,11 +198,11 @@ class Task:
         self._set_with_history(self.default_weight_history, default_weight)
 
     def default_weight_at(self, queried_date):
-        ret = self.default_weight_history[sorted(self.default_weight_history.keys())[0]] 
+        ret = self.default_weight_history[sorted(self.default_weight_history.keys())[0]]
         for date_key, default_weight in self.default_weight_history.items():
             if date_key > f'{queried_date} 23:59:59':
                 break
-            ret = default_weight 
+            ret = default_weight
         return ret
 
     @property
@@ -215,7 +218,7 @@ class Task:
         self._set_with_history(self.title_history, title)
 
     def title_at(self, queried_date):
-        ret = self.title_history[sorted(self.title_history.keys())[0]] 
+        ret = self.title_history[sorted(self.title_history.keys())[0]]
         for date_key, title in self.title_history.items():
             if date_key > f'{queried_date} 23:59:59':
                 break
@@ -262,9 +265,9 @@ class Day:
 
     def __init__(self, db, todos=None, comment=''):
         self.db = db
-        self.todos = todos if todos else {} 
+        self.todos = todos if todos else {}
         self.comment = comment
-        self.archived = True 
+        self.archived = True
 
     @classmethod
     def from_dict(cls, db, d):
@@ -310,18 +313,19 @@ class Day:
 
 class Todo:
 
-    def __init__(self, day, done=False, day_weight=None, comment=''):
-        self.day = day 
+    def __init__(self, day, done=False, day_weight=None, comment='', day_tags=None):
+        self.day = day
         self.done = done
         self.day_weight = day_weight
         self.comment = comment
+        self.day_tags = day_tags if day_tags else set()
 
     @classmethod
     def from_dict(cls, day, d):
-        return cls(day, d['done'], d['day_weight'], d['comment'])
+        return cls(day, d['done'], d['day_weight'], d['comment'], set(d['day_tags']))
 
     def to_dict(self):
-        return {'done': self.done, 'day_weight': self.day_weight, 'comment': self.comment}
+        return {'done': self.done, 'day_weight': self.day_weight, 'comment': self.comment, 'day_tags': list(self.day_tags)}
 
     @property
     def default_weight(self):
@@ -344,33 +348,60 @@ class Todo:
     def title(self):
         return self.task.title_at(self.day.date)
 
+    @property
+    def day_tags_joined(self):
+        return ';'.join(sorted(list(self.day_tags)))
+
+    @day_tags_joined.setter
+    def day_tags_joined(self, tags_string):
+        tags = set()
+        for tag in [tag.strip() for tag in tags_string.split(';') if tag.strip() != '']:
+            tags.add(tag)
+        self.day_tags = tags
+
+    @property
+    def tags(self):
+        return self.day_tags | self.task.tags
+
 
 class TodoDB(PlomDB):
 
     def __init__(self, prefix, selected_date=None, t_filter_and = None, t_filter_not = None, hide_unchosen=False, hide_done=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 [] 
+        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 []
         self.t_filter_not = t_filter_not if t_filter_not else []
-        self.hide_unchosen = hide_unchosen 
-        self.hide_done = hide_done 
+        self.hide_unchosen = hide_unchosen
+        self.hide_done = hide_done
         self.days = {}
         self.tasks = {}
-        self.t_tags = set() 
+        self.t_tags = set()
         super().__init__(db_path)
 
     def read_db_file(self, f):
         d = json.load(f)
         for date, day_dict in d['days'].items():
             self.days[date] = self.add_day(dict_source=day_dict)
+        for day in self.days.values():
+            for todo in day.todos.values():
+                for tag in todo.day_tags:
+                    self.t_tags.add(tag)
         for uuid, t_dict in d['tasks'].items():
             t = self.add_task(id_=uuid, dict_source=t_dict)
-            t.visible = len([tag for tag in self.t_filter_and if not tag in t.tags]) == 0\
-                    and len([tag for tag in self.t_filter_not if tag in t.tags]) == 0\
-                    and ((not self.hide_unchosen) or uuid in self.selected_day.todos)\
-                    and ((not self.hide_done) or (uuid in self.selected_day.todos and not self.selected_day.todos[uuid].done))
             for tag in t.tags:
                 self.t_tags.add(tag)
+        self.set_visibilities()
+
+    def set_visibilities(self):
+        for uuid, t in self.tasks.items():
+            t.visible = len([tag for tag in self.t_filter_and if not tag in t.tags]) == 0\
+                    and len([tag for tag in self.t_filter_not if tag in t.tags]) == 0\
+                    and ((not self.hide_unchosen) or uuid in self.selected_day.todos.keys())
+        for day in self.days.values():
+            for todo in day.todos.values():
+                todo.visible = len([tag for tag in self.t_filter_and if not tag in todo.day_tags | todo.task.tags ]) == 0\
+                    and len([tag for tag in self.t_filter_not if tag in todo.day_tags | todo.task.tags ]) == 0\
+                    and ((not self.hide_done) or (not todo.done))
 
     def to_dict(self):
         d = {
@@ -397,13 +428,13 @@ class TodoDB(PlomDB):
         else:
             self.days[new_date] = self.selected_day
             del self.days[self.selected_date]
-            self.selected_date = new_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] 
+                dates_to_purge += [date]
         for date in dates_to_purge:
             del self.days[date]
         self.write_text_to_db(json.dumps(self.to_dict()))
@@ -414,52 +445,55 @@ class TodoDB(PlomDB):
         self.tasks[id_] = t
         if return_id:
             return id_, t
-        else: 
+        else:
             return t
 
     def add_day(self, dict_source=None):
         return Day.from_dict(self, dict_source) if dict_source else Day(self)
 
     def show_day(self):
-        current_date = datetime.strptime(self.selected_date, DATE_FORMAT) 
-        prev_date = current_date - timedelta(days=1) 
+        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 = current_date + timedelta(days=1)
         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):
+        self.t_filter_and = ['calendar']
+        self.t_filter_not = ['deleted']
+        self.set_visibilities()
         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_end = end_date_str if end_date_str else sorted(self.days.keys())[-1]
-        # todays_date = str(datetime.now())[:10]
+        todays_date = str(datetime.now())[:10]
+        target_start_str = start_date_str if start_date_str else sorted(self.days.keys())[0]
+        target_start = todays_date if target_start_str == 'today' else target_start_str
+        target_end_str = end_date_str if end_date_str else sorted(self.days.keys())[-1]
+        target_end = todays_date if target_end_str == 'today' else target_end_str
         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(DATE_FORMAT) 
+            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] = self.days[current_date]
             days_to_show[current_date].weekday = datetime.strptime(current_date, DATE_FORMAT).strftime('%A')[:2]
-            for task_uuid, todo in days_to_show[current_date].todos.items():
-                todo.visible = self.tasks[task_uuid].visible
         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]
         return Template(form_header_tmpl + todo_tmpl + form_footer).render(db=self, todo=todo, action=self.prefix+'/todo')
 
-    def update_todo(self, task_uuid, date, day_weight, done, comment):
+    def update_todo(self, task_uuid, date, day_weight, done, comment, day_tags_joined):
         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.done = done 
-        todo.comment = comment 
+        todo.done = done
+        todo.comment = comment
+        todo.day_tags_joined = day_tags_joined
 
     def show_task(self, id_):
         task = self.tasks[id_] if id_ else self.add_task()
@@ -477,64 +511,76 @@ class TodoDB(PlomDB):
 
 
 class TodoHandler(PlomHandler):
-    
+
+    def config_init(self):
+        return {
+            'cookie_name': 'todo_cookie',
+            'prefix': '',
+            'cookie_path': '/'
+        }
+
     def app_init(self, handler):
         default_path = '/todo'
-        handler.add_route('GET', default_path, self.show_db) 
-        handler.add_route('POST', default_path, self.write_db) 
-        return 'todo', default_path 
+        handler.add_route('GET', default_path, self.show_db)
+        handler.add_route('POST', default_path, self.write_db)
+        return 'todo', {'cookie_name': 'todo_cookie', 'prefix': default_path, 'cookie_path': default_path}
 
     def do_POST(self):
+        self.try_do(self.config_init)
         self.try_do(self.write_db)
 
     def write_db(self):
         from urllib.parse import urlencode
-        prefix = self.apps['todo'] if hasattr(self, 'apps') else '' 
+        app_config = self.apps['todo'] if hasattr(self, 'apps') else self.config()
         length = int(self.headers['content-length'])
         postvars = parse_qs(self.rfile.read(length).decode(), keep_blank_values=1)
         parsed_url = urlparse(self.path)
-        db = TodoDB(prefix=prefix)
+        db = TodoDB(prefix=app_config['prefix'])
 
-        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}'
+        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}'
 
-        elif parsed_url.path == prefix + '/todo':
+        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])
-            homepage = f'{prefix}/todo?task={task_uuid}&date={date}'
+            db.update_todo(task_uuid, date, postvars['day_weight'][0], 'done' in postvars.keys(), postvars['comment'][0], postvars['day_tags'][0])
+            homepage = f'{app_config["prefix"]}/todo?task={task_uuid}&date={date}'
 
-        elif parsed_url.path == prefix + '/task':
+        elif parsed_url.path == app_config['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_}'
+            homepage = f'{app_config["prefix"]}/task?id={id_}'
 
-        elif parsed_url.path in {prefix + '/tasks', prefix + '/day'}:
+        elif parsed_url.path in {app_config['prefix'] + '/tasks', app_config['prefix'] + '/day'}:
+            data = []
             for target in postvars['t_and']:
-                if not target in db.t_filter_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:
+                data += [('t_and', '-')]
             for target in postvars['t_not']:
-                if not target in db.t_filter_not:
+                if len(target) > 0 and not target in db.t_filter_not:
                     db.t_filter_not += [target]
-            if 'hide_unchosen' in postvars.keys():
-                db.hide_unchosen = True
-            if 'hide_done' in postvars.keys():
-                db.hide_done = 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))] + [('hide_done', int(db.hide_done))]
-
-            if parsed_url.path == prefix + '/tasks':
+            if len(db.t_filter_not) == 0:
+                data += [('t_not', '-')]
+            data += [('t_and', f) for f in db.t_filter_and] + [('t_not', f) for f in db.t_filter_not]
+            if parsed_url.path == app_config['prefix'] + '/tasks':
                 encoded_params = urlencode(data)
-                homepage = f'{prefix}/tasks?{encoded_params}'
+                homepage = f'{app_config["prefix"]}/tasks?{encoded_params}'
+
+            elif parsed_url.path == app_config['prefix'] + '/day':
+                db.hide_unchosen = 'hide_unchosen' in postvars.keys()
+                db.hide_done = 'hide_done' in postvars.keys()
+                data += [('hide_unchosen', int(db.hide_unchosen))] + [('hide_done', int(db.hide_done))]
 
-            elif parsed_url.path == prefix + '/day':
                 db.selected_date = postvars['original_selected_date'][0]
                 new_selected_date = postvars['new_selected_date'][0]
                 try:
                     datetime.strptime(new_selected_date, DATE_FORMAT)
                 except ValueError:
-                    raise PlomException(f"{prefix} bad date string: {new_selected_date}")
+                    raise PlomException(f'{app_config["prefix"]} bad date string: {new_selected_date}')
                 if new_selected_date != db.selected_date:
                     db.change_selected_days_date(new_selected_date)
                 if 't_uuid' in postvars.keys():
@@ -546,50 +592,105 @@ class TodoHandler(PlomHandler):
                         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])
+                                db.update_todo(uuid, db.selected_date, postvars['day_weight'][i], done, postvars['todo_comment'][i], postvars['day_tags'][i])
                 if 'day_comment' in postvars.keys():
                     db.selected_day.comment = postvars['day_comment'][0]
-                data += [('date', db.selected_date)]
+                data += [('selected_date', db.selected_date)]
                 encoded_params = urlencode(data)
-                homepage = f'{prefix}/day?{encoded_params}'
+                homepage = f'{app_config["prefix"]}/day?{encoded_params}'
 
         db.write()
         self.redirect(homepage)
 
     def do_GET(self):
+        self.try_do(self.config_init)
         self.try_do(self.show_db)
 
     def show_db(self):
-        prefix = self.apps['todo'] if hasattr(self, 'apps') else '' 
+        app_config = self.apps['todo'] if hasattr(self, 'apps') else self.config()
+        cookie_db = self.get_cookie_db(app_config['cookie_name'])
         parsed_url = urlparse(self.path)
         params = parse_qs(parsed_url.query)
-        selected_date = params.get('date', [None])[0]
-        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'
-        hide_done_params = params.get('hide_done', [])
-        hide_done = len(hide_done_params) > 0 and hide_done_params[0] != '0'
-        db = TodoDB(prefix, selected_date, t_filter_and, t_filter_not, hide_unchosen, hide_done)
-        if parsed_url.path == prefix + '/day':
+        selected_date = None
+        t_filter_and = None
+        t_filter_not = None
+        hide_unchosen = False
+        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
+            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 and 't_not' in cookie_db.keys():
+                t_filter_not = cookie_db['t_not']
+            elif t_filter_not == ['-']:
+                t_filter_not = None
+            else:
+                t_filter_not = ['deleted']
+            cookie_db['t_not'] = t_filter_not
+        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
+        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()
-        elif parsed_url.path == prefix + '/todo':
+        elif parsed_url.path == app_config['prefix'] + '/todo':
+            todo_date = params.get('date', [None])[0]
             task_uuid = params.get('task', [None])[0]
-            page = db.show_todo(task_uuid, selected_date)
-        elif parsed_url.path == prefix + '/task':
+            page = db.show_todo(task_uuid, todo_date)
+        elif parsed_url.path == app_config['prefix'] + '/task':
             id_ = params.get('id', [None])[0]
             page = db.show_task(id_)
-        elif parsed_url.path == prefix + '/tasks':
+        elif parsed_url.path == app_config['prefix'] + '/tasks':
             page = db.show_tasks()
-        elif parsed_url.path == prefix + '/add_task':
+        elif parsed_url.path == app_config['prefix'] + '/add_task':
             page = db.show_task(None)
-        else: 
+        elif parsed_url.path == app_config['prefix'] + '/unset_cookie':
+            page = 'no cookie to unset.'
+            if len(cookie_db) > 0:
+                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
             page = db.show_calendar(start_date, end_date)
-        header = Template(html_head).render(db=db, prefix=prefix, date=selected_date)
+        header = Template(html_head).render(db=db, prefix=app_config['prefix'], date=selected_date)
+        if parsed_url.path != app_config['prefix'] + '/unset_cookie':
+            self.set_cookie(app_config['cookie_name'], app_config['cookie_path'], cookie_db)
         self.send_HTML(header + page)
 
 
-if __name__ == "__main__":  
+if __name__ == "__main__":
     run_server(server_port, TodoHandler)