"""Web server stuff."""
from typing import Any
+from collections import namedtuple
from http.server import BaseHTTPRequestHandler
from http.server import HTTPServer
from urllib.parse import urlparse, parse_qs
def do_GET_day(self) -> dict[str, object]:
"""Show single Day of ?date=."""
date = self.params.get_str('date', todays_date())
- conditions_listing = []
- for condition in Condition.all(self.conn):
- enablers = Todo.enablers_for_at(self.conn, condition, date)
- disablers = Todo.disablers_for_at(self.conn, condition, date)
- conditions_listing += [{
- 'condition': condition,
- 'enablers': enablers,
- 'disablers': disablers}]
- return {'day': Day.by_date(self.conn, date, create=True),
- 'todos': Todo.by_date(self.conn, date),
+ top_todos = [t for t in Todo.by_date(self.conn, date) if not t.parents]
+ seen_todos: set[int] = set()
+ seen_conditions: set[int] = set()
+ todo_trees = [t.get_step_tree(seen_todos, seen_conditions)
+ for t in top_todos]
+ ConditionsNode = namedtuple('ConditionsNode',
+ ('condition', 'enablers', 'disablers'))
+ conditions_nodes: list[ConditionsNode] = []
+ for condi in Condition.all(self.conn):
+ enablers = Todo.enablers_for_at(self.conn, condi, date)
+ disablers = Todo.disablers_for_at(self.conn, condi, date)
+ conditions_nodes += [ConditionsNode(condi, enablers, disablers)]
+ return {'day': Day.by_id(self.conn, date, create=True),
+ 'todo_trees': todo_trees,
'processes': Process.all(self.conn),
- 'conditions_listing': conditions_listing}
+ 'conditions_nodes': conditions_nodes}
def do_GET_todo(self) -> dict[str, object]:
"""Show single Todo of ?id=."""
- id_ = self.params.get_int_or_none('id')
+ id_ = self.params.get_int('id')
todo = Todo.by_id(self.conn, id_)
return {'todo': todo,
- 'todo_candidates': Todo.by_date(self.conn, todo.day.date),
+ 'todo_candidates': Todo.by_date(self.conn, todo.date),
'condition_candidates': Condition.all(self.conn)}
def do_GET_conditions(self) -> dict[str, object]:
return {'process': process,
'steps': process.get_steps(self.conn),
'owners': process.used_as_step_by(self.conn),
- 'process_candidates': Process.all(self.conn),
+ 'step_candidates': Process.all(self.conn),
'condition_candidates': Condition.all(self.conn)}
def do_GET_processes(self) -> dict[str, object]:
def do_POST_day(self) -> None:
"""Update or insert Day of date and Todos mapped to it."""
date = self.params.get_str('date')
- day = Day.by_date(self.conn, date, create=True)
+ day = Day.by_id(self.conn, date, create=True)
day.comment = self.form_data.get_str('comment')
day.save(self.conn)
- process_id = self.form_data.get_int_or_none('new_todo')
- if process_id is not None:
+ existing_todos = Todo.by_date(self.conn, date)
+ for process_id in self.form_data.get_all_int('new_todo'):
process = Process.by_id(self.conn, process_id)
- todo = Todo(None, process, False, day)
+ todo = Todo(None, process, False, day.date)
+ todo.save(self.conn)
+ for step in todo.process.explicit_steps:
+ for t in [t for t in existing_todos
+ if t.process.id_ == step.step_process_id]:
+ todo.add_child(t)
+ break
todo.save(self.conn)
def do_POST_todo(self) -> None:
"""Update Todo and its children."""
- id_ = self.params.get_int_or_none('id')
+ id_ = self.params.get_int('id')
todo = Todo.by_id(self.conn, id_)
- child_id = self.form_data.get_int_or_none('adopt')
- if child_id is not None:
+ adopted_child_ids = self.form_data.get_all_int('adopt')
+ for child in todo.children:
+ if child.id_ not in adopted_child_ids:
+ assert isinstance(child.id_, int)
+ child = Todo.by_id(self.conn, child.id_)
+ todo.remove_child(child)
+ for child_id in adopted_child_ids:
+ if child_id in [c.id_ for c in todo.children]:
+ continue
child = Todo.by_id(self.conn, child_id)
todo.add_child(child)
todo.set_conditions(self.conn, self.form_data.get_all_int('condition'))
- todo.set_fulfills(self.conn, self.form_data.get_all_int('fulfills'))
- todo.set_undoes(self.conn, self.form_data.get_all_int('undoes'))
+ todo.set_enables(self.conn, self.form_data.get_all_int('enables'))
+ todo.set_disables(self.conn, self.form_data.get_all_int('disables'))
todo.is_done = len(self.form_data.get_all_str('done')) > 0
todo.save(self.conn)
- for condition in todo.fulfills:
+ for condition in todo.enables:
condition.save(self.conn)
- for condition in todo.undoes:
+ for condition in todo.disables:
condition.save(self.conn)
def do_POST_process(self) -> None:
process.effort.set(self.form_data.get_float('effort'))
process.set_conditions(self.conn,
self.form_data.get_all_int('condition'))
- process.set_fulfills(self.conn, self.form_data.get_all_int('fulfills'))
- process.set_undoes(self.conn, self.form_data.get_all_int('undoes'))
+ process.set_enables(self.conn, self.form_data.get_all_int('enables'))
+ process.set_disables(self.conn, self.form_data.get_all_int('disables'))
process.save_core(self.conn)
assert process.id_ is not None # for mypy
process.explicit_steps = []