+ return t
+
+ def add_todo(self, todo_dict, id_=None):
+ id_ = id_ if id_ else str(uuid4())
+ todo = Todo.from_dict(self, todo_dict, id_)
+ self.todos[id_] = todo
+ return todo
+
+ def add_day(self, date, dict_source=None):
+ day = Day.from_dict(self, dict_source, date) if dict_source else Day(self, date)
+ self.days[date] = day
+ return day
+
+ 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)
+ 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)
+ done_tasks = []
+ for uuid, task in self.tasks.items():
+ if uuid in self.selected_day.todos.keys():
+ todo = self.selected_day.todos[uuid]
+ if todo.done:
+ done_tasks += [todo]
+ done_tasks.sort(key=lambda t: t.effort, 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, sort=task_sort, done_tasks=done_tasks)
+
+ def neighbor_dates(self):
+ 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 prev_date_str, next_date_str
+
+ def show_do_day(self, sort_order=None):
+ prev_date_str, next_date_str = self.neighbor_dates()
+ # 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)
+ todos = [t for t in self.selected_day.linked_todos_as_list if t.visible]
+ if sort_order == 'title':
+ todos.sort(key=lambda t: t.task.title)
+ elif sort_order == 'done':
+ todos.sort(key=lambda t: t.day_effort if t.day_effort else t.default_effort if t.done else 0, reverse=True)
+ elif sort_order == 'default_effort':
+ todos.sort(key=lambda t: t.task.default_effort, reverse=True)
+ elif sort_order == 'importance':
+ todos.sort(key=lambda t: t.importance, reverse=True)
+ return j2env.get_template('do_day.html').render(
+ day=self.selected_day,
+ prev_date=prev_date_str,
+ next_date=next_date_str,
+ todos=todos,
+ sort=sort_order,
+ hide_done=self.hide_done)
+
+ 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 = {}
+ todays_date_str = str(datetime.now())[:10]
+ todays_date_obj = datetime.strptime(todays_date_str, DATE_FORMAT)
+ yesterdays_date_obj = todays_date_obj - timedelta(1)
+ yesterdays_date_str = yesterdays_date_obj.strftime(DATE_FORMAT)
+ start_date_obj = datetime.strptime(sorted(self.days.keys())[0], DATE_FORMAT)
+ if start_date_str and len(start_date_str) > 0:
+ if start_date_str in {'today', 'yesterday'}:
+ start_date_obj = todays_date_obj if start_date_str == 'today' else yesterdays_date_obj
+ else:
+ start_date_obj = datetime.strptime(start_date_str, DATE_FORMAT)
+ end_date_obj = datetime.strptime(sorted(self.days.keys())[-1], DATE_FORMAT)
+ if end_date_str and len(end_date_str) > 0:
+ if end_date_str in {'today', 'yesterday'}:
+ end_date_obj = todays_date_obj if end_date_str == 'today' else yesterdays_date_obj
+ else:
+ end_date_obj = datetime.strptime(end_date_str, DATE_FORMAT)
+ for n in range(int((end_date_obj - start_date_obj).days) + 1):
+ current_date_obj = start_date_obj + timedelta(n)
+ current_date_str = current_date_obj.strftime(DATE_FORMAT)
+ if current_date_str not in self.days.keys():
+ days_to_show[current_date_str] = self.add_day(current_date_str)
+ else:
+ days_to_show[current_date_str] = self.days[current_date_str]
+ days_to_show[current_date_str].weekday = datetime.strptime(current_date_str, DATE_FORMAT).strftime('%A')[:2]
+ return j2env.get_template('calendar.html').render(db=self, days=days_to_show, action=self.prefix+'/calendar', start_date=start_date_str, end_date=end_date_str)
+
+ def show_todo(self, id_, return_to):
+ todo = self.todos[id_]
+ # if selected_date not in self.days.keys():
+ # self.days[selected_date] = self.add_day(test_date=f'3:{selected_date}')
+ # # print("DEBUG show_todo", self.days[selected_date].date)
+ # 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', return_to=return_to)
+
+ def update_todo_mini(self, task_uuid, date, day_effort, done, importance):
+ if date not in self.days.keys():
+ self.days[date] = self.add_day(test_date=f'Y:{date}')
+ # print("DEBUG update_todo_min", self.days[date].date)
+ 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_effort = day_effort
+ todo.done = done
+ todo.importance = importance
+ return todo
+
+ def collect_tags(self, tags_joined, tags_checked):
+ tags = set()
+ for tag in [tag.strip() for tag in tags_joined.split(';') if tag.strip() != '']:
+ tags.add(tag)
+ for tag in tags_checked:
+ tags.add(tag)
+ return tags
+
+ def update_todo_for_day(self, id_, date, effort, done, comment, importance):
+ todo = self.todos[id_]
+ todo.done = done
+ todo.efforts[date] = effort
+ todo.comment = comment
+ todo.importance = importance
+
+ def update_todo(self, id_, efforts, done, comment, day_tags_joined, day_tags_checked, importance):
+ if len(efforts) == 0:
+ raise PlomException('todo must have at least one effort!')
+ todo = self.todos[id_]
+ todo.done = done
+ todo.efforts = efforts
+ for date in todo.efforts:
+ if not date in self.days.keys():
+ self.add_day(date=date)
+ if not self in self.days[date].linked_todos_as_list:
+ self.days[date].linked_todos_as_list += [todo]
+ todo.comment = comment
+ todo.day_tags = self.collect_tags(day_tags_joined, day_tags_checked)
+ todo.importance = importance
+
+ def delete_todo(self, id_):
+ todo = self.todos[id_]
+ for date in todo.efforts.keys():
+ self.delete_effort(todo, date)
+ del self.todos[id_]
+
+ def delete_effort(self, todo, date):
+ if todo in self.days[date].linked_todos_as_list:
+ self.days[date].linked_todos_as_list.remove(todo)
+
+
+ # def update_todo(self, task_uuid, date, day_effort, done, comment, day_tags_joined, day_tags_checked, importance):
+ # day_effort = float(day_effort) if len(day_effort) > 0 else None
+ # importance = float(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)
+
+ def link_day_with_todo(self, date, todo_id):
+ print("DEBUG link", date, todo_id)
+ todo_creation_date, task_uuid = todo_id.split('_')
+ todo = self.days[todo_creation_date].todos[task_uuid]
+ if date in todo.efforts.keys():
+ raise PlomException('todo already linked to respective day')
+ todo.set_day_effort(date, None)
+ if date not in self.days.keys():
+ print("DEBUG link_day_with_todo", date)
+ self.days[date] = self.add_day(test_date=f'Z:{date}')
+ self.days[date].linked_todos_as_list += [todo]
+ print("DEBUG", date, self.days[date].linked_todos)
+
+ def show_task(self, id_, return_to=''):
+ task = self.tasks[id_] if id_ else self.add_task()
+ selected = id_ in self.selected_day.todos.keys()
+ return j2env.get_template('task.html').render(db=self, task=task, action=self.prefix+'/task', return_to=return_to, selected=selected)
+
+ def update_task(self, id_, title, default_effort, tags_joined, tags_checked, links, comment):
+ task = self.tasks[id_] if id_ in self.tasks.keys() else self.add_task(id_)
+ task.title = title
+ 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:
+ borrowed_links = self.tasks[link].links
+ borrowed_links.add(id_)
+ self.tasks[link].links = borrowed_links
+ task.comment = comment
+
+ def show_tasks(self, expand_uuid):
+ expanded_tasks = {}
+ if expand_uuid:
+ for uuid in self.tasks[expand_uuid].links:
+ expanded_tasks[uuid] = self.tasks[uuid]
+ return j2env.get_template('tasks.html').render(db=self, action=self.prefix+'/tasks', expand_uuid=expand_uuid, expanded_tasks=expanded_tasks)
+
+ def new_day(self, search):
+ prev_date_str, next_date_str = self.neighbor_dates()
+ relevant_todos = []
+ for todo in self.todos.values():
+ # if todo.done or (not todo.visible) or (not todo.matches(search)) or todo.day.date == self.selected_day.date: # TODO or todo is linked by day
+ if todo.done or (not todo.visible) or (not todo.matches(search)): # or todo.day.date == self.selected_day.date: # TODO or todo is linked by day
+ continue
+ relevant_todos += [todo]
+ tasks = []
+ for uuid, task in self.tasks.items():
+ if not task.visible or (not task.matches(search)):
+ continue
+ tasks += [task]
+ return j2env.get_template('new_day.html').render(
+ day=self.selected_day,
+ prev_date=prev_date_str,
+ next_date=next_date_str,
+ tasks=tasks,
+ relevant_todos=relevant_todos,
+ search=search)
+
+
+class ParamsParser:
+
+ def __init__(self, parsed_url_query, cookie_db):
+ self.params = parse_qs(parsed_url_query)
+ self.cookie_db = cookie_db
+
+ def get(self, key, default=None):
+ boolean = bool == type(default)
+ param = self.params.get(key, [default])[0]
+ if boolean:
+ param = param != '0'
+ return param
+
+ def get_cookied(self, key, default=None):
+ param = self.get(key, default)
+ if param == '-':
+ param = None
+ if key in self.cookie_db.keys():
+ del self.cookie_db[key]
+ if param is None and key in self.cookie_db.keys():
+ param = self.cookie_db[key]
+ if param is not None:
+ self.cookie_db[key] = param
+ return param