diff --git a/dooit/api/hooks/update_hooks.py b/dooit/api/hooks/update_hooks.py index 7deecbbc..abd73cc8 100644 --- a/dooit/api/hooks/update_hooks.py +++ b/dooit/api/hooks/update_hooks.py @@ -8,15 +8,7 @@ def update_children_to_pending(_, connection, target: Todo): if not target.pending: return - to_update = [] - - def update_children(todo: Todo): - for child in todo.todos: - to_update.append(child.id) - update_children(child) - - update_children(target) - query = update(Todo).where(Todo.id.in_(to_update)).values(pending=True) + query = update(Todo).where(Todo.parent_todo_id == target.id).values(pending=True) connection.execute(query) @@ -25,59 +17,31 @@ def update_children_to_completed(_, connection, target: Todo): if target.pending: return - to_update = [] - - def update_children(todo: Todo): - for child in todo.todos: - to_update.append(child.id) - update_children(child) - - update_children(target) - query = update(Todo).where(Todo.id.in_(to_update)).values(pending=False) + query = update(Todo).where(Todo.parent_todo_id == target.id).values(pending=False) connection.execute(query) @event.listens_for(Todo, "before_update") def update_parent_to_pending(mapper, connection, target: Todo): - if not target.pending: + if not target.pending or not target.parent_todo: return - to_update = [] - - def update_parent_pending(todo: Todo): - parent = todo.parent_todo - if not parent: - return - - to_update.append(parent.id) - update_parent_pending(parent) - - update_parent_pending(target) - query = update(Todo).where(Todo.id.in_(to_update)).values(pending=True) + query = update(Todo).where(Todo.id == target.parent_todo_id).values(pending=True) connection.execute(query) @event.listens_for(Todo, "before_update") def update_parent_to_completed(mapper, connection, target: Todo): - if target.pending: + if target.pending or not target.parent_todo: return - to_update = [] - - def update_parent_completed(todo: Todo): - parent = todo.parent_todo - if not parent: - return - - all_sibling_completed = all([not sibling.pending for sibling in parent.todos]) - if all_sibling_completed: - parent.pending = False - to_update.append(parent.id) - update_parent_completed(parent) - - update_parent_completed(target) - query = update(Todo).where(Todo.id.in_(to_update)).values(pending=False) - connection.execute(query) + parent = target.parent_todo + all_sibling_completed = all([not sibling.pending for sibling in parent.todos]) + if all_sibling_completed: + query = ( + update(Todo).where(Todo.id == target.parent_todo_id).values(pending=False) + ) + connection.execute(query) @event.listens_for(Todo, "before_update") @@ -93,3 +57,4 @@ def update_due_for_recurrence(mapper, connection, todo: Todo): todo.pending = True todo.due += todo.recurrence +