X-Git-Url: https://plomlompom.com/repos/berlin_corona.txt?a=blobdiff_plain;f=plomtask%2Fhttp.py;h=c8486003ce48fb785474432f512b08a380193a32;hb=7b6b8d0b93b1d4dd85152e49e7105aacc647327c;hp=e541057ed793ebe2a53c55b50a11be7133c5884f;hpb=54e6c8bccace28583cf9926aa00917a796628a00;p=plomtask diff --git a/plomtask/http.py b/plomtask/http.py index e541057..c848600 100644 --- a/plomtask/http.py +++ b/plomtask/http.py @@ -1,5 +1,5 @@ """Web server stuff.""" -from typing import Any +from typing import Any, NamedTuple from http.server import BaseHTTPRequestHandler from http.server import HTTPServer from urllib.parse import urlparse, parse_qs @@ -118,24 +118,28 @@ class TaskHandler(BaseHTTPRequestHandler): def do_GET_day(self) -> dict[str, object]: """Show single Day of ?date=.""" + + class ConditionListing(NamedTuple): + """Listing of Condition augmented with its enablers, disablers.""" + condition: Condition + enablers: list[Todo] + disablers: list[Todo] + date = self.params.get_str('date', todays_date()) - conditions_listing = [] 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] - 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}] + condition_listings: list[ConditionListing] = [] + for cond in Condition.all(self.conn): + enablers = Todo.enablers_for_at(self.conn, cond, date) + disablers = Todo.disablers_for_at(self.conn, cond, date) + condition_listings += [ConditionListing(cond, 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} + 'condition_listings': condition_listings} def do_GET_todo(self) -> dict[str, object]: """Show single Todo of ?id=.""" @@ -161,7 +165,7 @@ class TaskHandler(BaseHTTPRequestHandler): 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]: @@ -195,18 +199,28 @@ class TaskHandler(BaseHTTPRequestHandler): 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.date) todo.save(self.conn) + todo.adopt_from(existing_todos) + todo.make_missing_children(self.conn) + todo.save(self.conn) def do_POST_todo(self) -> None: """Update Todo and its children.""" 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')) @@ -231,7 +245,6 @@ class TaskHandler(BaseHTTPRequestHandler): 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 = [] steps: list[tuple[int | None, int, int | None]] = [] for step_id in self.form_data.get_all_int('steps'):