From: Christian Heller Date: Fri, 9 Aug 2024 14:14:55 +0000 (+0200) Subject: Private TaskHandler.conn to ._conn. X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/%7B%7Bprefix%7D%7D/copy_free?a=commitdiff_plain;h=1af7e2710a2af8c8e03e5e679921399825a4407a;p=plomtask Private TaskHandler.conn to ._conn. --- diff --git a/plomtask/http.py b/plomtask/http.py index c7897e8..ef9438a 100644 --- a/plomtask/http.py +++ b/plomtask/http.py @@ -123,7 +123,7 @@ class TaskHandler(BaseHTTPRequestHandler): """Handles single HTTP request.""" # pylint: disable=too-many-public-methods server: TaskServer - conn: DatabaseConnection + _conn: DatabaseConnection _site: str _form: InputsParser _params: InputsParser @@ -240,7 +240,7 @@ class TaskHandler(BaseHTTPRequestHandler): # (because pylint here fails to detect the use of wrapper as a # method to self with respective access privileges) try: - self.conn = DatabaseConnection(self.server.db) + self._conn = DatabaseConnection(self.server.db) parsed_url = urlparse(self.path) self._site = path_split(parsed_url.path)[1] params = parse_qs(parsed_url.query, @@ -266,7 +266,7 @@ class TaskHandler(BaseHTTPRequestHandler): ctx = {'msg': error} self._send_page(ctx, 'msg', error.http_code) finally: - self.conn.close() + self._conn.close() return wrapper return decorator @@ -289,7 +289,7 @@ class TaskHandler(BaseHTTPRequestHandler): keep_blank_values=True) self._form = InputsParser(postvars) redir_target = handler() - self.conn.commit() + self._conn.commit() return redir_target # GET handlers @@ -306,9 +306,9 @@ class TaskHandler(BaseHTTPRequestHandler): # method to self with respective access privileges) id_ = self._params.get_int_or_none('id') if target_class.can_create_by_id: - item = target_class.by_id_or_create(self.conn, id_) + item = target_class.by_id_or_create(self._conn, id_) else: - item = target_class.by_id(self.conn, id_) + item = target_class.by_id(self._conn, id_) return f(self, item) return wrapper return decorator @@ -327,7 +327,7 @@ class TaskHandler(BaseHTTPRequestHandler): start = self._params.get_str_or_fail('start', '') end = self._params.get_str_or_fail('end', '') end = end if end != '' else date_in_n_days(366) - days, start, end = Day.by_date_range_with_limits(self.conn, + days, start, end = Day.by_date_range_with_limits(self._conn, (start, end), 'id') days = Day.with_filled_gaps(days, start, end) today = date_in_n_days(0) @@ -344,7 +344,7 @@ class TaskHandler(BaseHTTPRequestHandler): def do_GET_day(self) -> dict[str, object]: """Show single Day of ?date=.""" date = self._params.get_str_or_fail('date', date_in_n_days(0)) - day = Day.by_id_or_create(self.conn, date) + day = Day.by_id_or_create(self._conn, date) make_type = self._params.get_str_or_fail('make_type', '') conditions_present = [] enablers_for = {} @@ -354,10 +354,10 @@ class TaskHandler(BaseHTTPRequestHandler): if condition not in conditions_present: conditions_present += [condition] enablers_for[condition.id_] = [p for p in - Process.all(self.conn) + Process.all(self._conn) if condition in p.enables] disablers_for[condition.id_] = [p for p in - Process.all(self.conn) + Process.all(self._conn) if condition in p.disables] seen_todos: set[int] = set() top_nodes = [t.get_step_tree(seen_todos) @@ -368,7 +368,7 @@ class TaskHandler(BaseHTTPRequestHandler): 'enablers_for': enablers_for, 'disablers_for': disablers_for, 'conditions_present': conditions_present, - 'processes': Process.all(self.conn)} + 'processes': Process.all(self._conn)} @_get_item(Todo) def do_GET_todo(self, todo: Todo) -> dict[str, object]: @@ -379,7 +379,7 @@ class TaskHandler(BaseHTTPRequestHandler): steps_nodes: list[TodoOrProcStepNode]) -> int: for process_step_node in process_step_nodes: node_id += 1 - proc = Process.by_id(self.conn, + proc = Process.by_id(self._conn, process_step_node.step.step_process_id) node = TodoOrProcStepNode(node_id, None, proc, []) steps_nodes += [node] @@ -420,7 +420,7 @@ class TaskHandler(BaseHTTPRequestHandler): return ids todo_steps = [step.todo for step in todo.get_step_tree(set()).children] - process_tree = todo.process.get_steps(self.conn, None) + process_tree = todo.process.get_steps(self._conn, None) steps_todo_to_process: list[TodoOrProcStepNode] = [] last_node_id = walk_process_steps(0, process_tree, steps_todo_to_process) @@ -428,8 +428,8 @@ class TaskHandler(BaseHTTPRequestHandler): steps_node.fillable = True walk_todo_steps(last_node_id, todo_steps, steps_todo_to_process) adoptables: dict[int, list[Todo]] = {} - any_adoptables = [Todo.by_id(self.conn, t.id_) - for t in Todo.by_date(self.conn, todo.date) + any_adoptables = [Todo.by_id(self._conn, t.id_) + for t in Todo.by_date(self._conn, todo.date) if t.id_ is not None and t != todo] for id_ in collect_adoptables_keys(steps_todo_to_process): @@ -438,9 +438,9 @@ class TaskHandler(BaseHTTPRequestHandler): return {'todo': todo, 'steps_todo_to_process': steps_todo_to_process, 'adoption_candidates_for': adoptables, - 'process_candidates': sorted(Process.all(self.conn)), + 'process_candidates': sorted(Process.all(self._conn)), 'todo_candidates': any_adoptables, - 'condition_candidates': Condition.all(self.conn)} + 'condition_candidates': Condition.all(self._conn)} def do_GET_todos(self) -> dict[str, object]: """Show Todos from ?start= to ?end=, of ?process=, ?comment= pattern""" @@ -450,7 +450,7 @@ class TaskHandler(BaseHTTPRequestHandler): process_id = self._params.get_int_or_none('process_id') comment_pattern = self._params.get_str_or_fail('comment_pattern', '') todos = [] - ret = Todo.by_date_range_with_limits(self.conn, (start, end)) + ret = Todo.by_date_range_with_limits(self._conn, (start, end)) todos_by_date_range, start, end = ret todos = [t for t in todos_by_date_range if comment_pattern in t.comment @@ -458,13 +458,13 @@ class TaskHandler(BaseHTTPRequestHandler): sort_by = Todo.sort_by(todos, sort_by) return {'start': start, 'end': end, 'process_id': process_id, 'comment_pattern': comment_pattern, 'todos': todos, - 'all_processes': Process.all(self.conn), 'sort_by': sort_by} + 'all_processes': Process.all(self._conn), 'sort_by': sort_by} def do_GET_conditions(self) -> dict[str, object]: """Show all Conditions.""" pattern = self._params.get_str_or_fail('pattern', '') sort_by = self._params.get_str_or_fail('sort_by', '') - conditions = Condition.matching(self.conn, pattern) + conditions = Condition.matching(self._conn, pattern) sort_by = Condition.sort_by(conditions, sort_by) return {'conditions': conditions, 'sort_by': sort_by, @@ -473,7 +473,7 @@ class TaskHandler(BaseHTTPRequestHandler): @_get_item(Condition) def do_GET_condition(self, c: Condition) -> dict[str, object]: """Show Condition of ?id=.""" - ps = Process.all(self.conn) + ps = Process.all(self._conn) return {'condition': c, 'is_new': c.id_ is None, 'enabled_processes': [p for p in ps if c in p.conditions], 'disabled_processes': [p for p in ps if c in p.blockers], @@ -504,19 +504,19 @@ class TaskHandler(BaseHTTPRequestHandler): raise BadFormatException(msg) from exc process.title.set(title) preset_top_step = None - owners = process.used_as_step_by(self.conn) + owners = process.used_as_step_by(self._conn) for step_id in owner_ids: - owners += [Process.by_id(self.conn, step_id)] + owners += [Process.by_id(self._conn, step_id)] for process_id in owned_ids: - Process.by_id(self.conn, process_id) # to ensure ID exists + Process.by_id(self._conn, process_id) # to ensure ID exists preset_top_step = process_id return {'process': process, 'is_new': process.id_ is None, 'preset_top_step': preset_top_step, - 'steps': process.get_steps(self.conn), + 'steps': process.get_steps(self._conn), 'owners': owners, - 'n_todos': len(Todo.by_process_id(self.conn, process.id_)), - 'process_candidates': Process.all(self.conn), - 'condition_candidates': Condition.all(self.conn)} + 'n_todos': len(Todo.by_process_id(self._conn, process.id_)), + 'process_candidates': Process.all(self._conn), + 'condition_candidates': Condition.all(self._conn)} @_get_item(Process) def do_GET_process_titles(self, p: Process) -> dict[str, object]: @@ -537,7 +537,7 @@ class TaskHandler(BaseHTTPRequestHandler): """Show all Processes.""" pattern = self._params.get_str_or_fail('pattern', '') sort_by = self._params.get_str_or_fail('sort_by', '') - processes = Process.matching(self.conn, pattern) + processes = Process.matching(self._conn, pattern) sort_by = Process.sort_by(processes, sort_by) return {'processes': processes, 'sort_by': sort_by, 'pattern': pattern} @@ -558,13 +558,13 @@ class TaskHandler(BaseHTTPRequestHandler): msg = 'trying to delete non-saved ' +\ f'{target_class.__name__}' raise NotFoundException(msg) - item = target_class.by_id(self.conn, id_) - item.remove(self.conn) + item = target_class.by_id(self._conn, id_) + item.remove(self._conn) return redir_target if target_class.can_create_by_id: - item = target_class.by_id_or_create(self.conn, id_) + item = target_class.by_id_or_create(self._conn, id_) else: - item = target_class.by_id(self.conn, id_) + item = target_class.by_id(self._conn, id_) return f(self, item) return wrapper return decorator @@ -572,13 +572,13 @@ class TaskHandler(BaseHTTPRequestHandler): def _change_versioned_timestamps(self, cls: Any, attr_name: str) -> str: """Update history timestamps for VersionedAttribute.""" id_ = self._params.get_int_or_none('id') - item = cls.by_id(self.conn, id_) + item = cls.by_id(self._conn, id_) attr = getattr(item, attr_name) for k, v in self._form.get_firsts_of_key_prefixed('at:').items(): old = k[3:] if old[19:] != v: attr.reset_timestamp(old, f'{v}.0') - attr.save(self.conn) + attr.save(self._conn) return f'/{cls.name_lowercase()}_{attr_name}s?id={item.id_}' def do_POST_day(self) -> str: @@ -600,24 +600,24 @@ class TaskHandler(BaseHTTPRequestHandler): msg = 'not equal number each of number of todo_id, comments, ' +\ 'and efforts inputs' raise BadFormatException(msg) - day = Day.by_id_or_create(self.conn, date) + day = Day.by_id_or_create(self._conn, date) day.comment = day_comment - day.save(self.conn) + day.save(self._conn) new_todos = [] for process_id in sorted(new_todos_by_process): - process = Process.by_id(self.conn, process_id) + process = Process.by_id(self._conn, process_id) todo = Todo(None, process, False, date) - todo.save(self.conn) + todo.save(self._conn) new_todos += [todo] if 'full' == make_type: for todo in new_todos: - todo.ensure_children(self.conn) + todo.ensure_children(self._conn) for i, todo_id in enumerate(old_todos): - todo = Todo.by_id(self.conn, todo_id) + todo = Todo.by_id(self._conn, todo_id) todo.is_done = is_done[i] todo.comment = comments[i] todo.effort = efforts[i] - todo.save(self.conn) + todo.save(self._conn) return f'/day?date={date}&make_type={make_type}' @_delete_or_post(Todo, '/') @@ -647,7 +647,7 @@ class TaskHandler(BaseHTTPRequestHandler): except ValueError as e: msg = 'cannot float form field value for key: effort' raise BadFormatException(msg) from e - todo.set_condition_relations(self.conn, *cond_rels) + todo.set_condition_relations(self._conn, *cond_rels) for filler in [f for f in step_fillers if f != 'ignore']: target_id: int to_int = filler @@ -670,23 +670,23 @@ class TaskHandler(BaseHTTPRequestHandler): if child.id_ and (child.id_ not in adopted_child_ids): to_remove += [child.id_] for id_ in to_remove: - child = Todo.by_id(self.conn, id_) + child = Todo.by_id(self._conn, id_) todo.remove_child(child) for child_id in adopted_child_ids: if child_id not in [c.id_ for c in todo.children]: - todo.add_child(Todo.by_id(self.conn, child_id)) + todo.add_child(Todo.by_id(self._conn, child_id)) todo.update_attrs(**to_update) for approach, proc_ids in to_make.items(): for process_id in proc_ids: - process = Process.by_id(self.conn, process_id) + process = Process.by_id(self._conn, process_id) made = Todo(None, process, False, todo.date) - made.save(self.conn) + made.save(self._conn) if 'full' == approach: - made.ensure_children(self.conn) + made.ensure_children(self._conn) todo.add_child(made) # todo.save() may destroy Todo if .effort < 0, so retrieve .id_ early url = f'/todo?id={todo.id_}' - todo.save(self.conn) + todo.save(self._conn) return url def do_POST_process_descriptions(self) -> str: @@ -721,10 +721,10 @@ class TaskHandler(BaseHTTPRequestHandler): new_steps_to[step_id] = self._form.get_all_int(name) for k, v in versioned.items(): getattr(process, k).set(v) - process.set_condition_relations(self.conn, *cond_rels) + process.set_condition_relations(self._conn, *cond_rels) if calendarize is not None: process.calendarize = calendarize - process.save(self.conn) + process.save(self._conn) assert isinstance(process.id_, int) # set relations to, and if non-existant yet: create, other Processes # pylint: disable=fixme @@ -740,10 +740,10 @@ class TaskHandler(BaseHTTPRequestHandler): owners_to_set += [int(owner_identifier)] except ValueError: new_owner_title = owner_identifier - process.set_owners(self.conn, owners_to_set) + process.set_owners(self._conn, owners_to_set) # 2. owneds (downwards) new_step_title = None - steps: list[ProcessStep] = [ProcessStep.by_id(self.conn, step_id) + steps: list[ProcessStep] = [ProcessStep.by_id(self._conn, step_id) for step_id in kept_steps] for step_id in kept_steps: new_sub_steps = [ @@ -757,8 +757,8 @@ class TaskHandler(BaseHTTPRequestHandler): steps += [step] except ValueError: new_step_title = step_id_or_new_title - process.set_steps(self.conn, steps) - process.set_step_suppressions(self.conn, suppresses) + process.set_steps(self._conn, steps) + process.set_step_suppressions(self._conn, suppresses) # encode titles for potentially newly created Processes up or down params = f'id={process.id_}' if new_step_title: @@ -767,7 +767,7 @@ class TaskHandler(BaseHTTPRequestHandler): elif new_owner_title: title_b64_encoded = b64encode(new_owner_title.encode()).decode() params = f'has_step={process.id_}&title_b64={title_b64_encoded}' - process.save(self.conn) + process.save(self._conn) return f'/process?{params}' def do_POST_condition_descriptions(self) -> str: @@ -788,5 +788,5 @@ class TaskHandler(BaseHTTPRequestHandler): condition.is_active = is_active condition.title.set(title) condition.description.set(description) - condition.save(self.conn) + condition.save(self._conn) return f'/condition?id={condition.id_}'