Skip to content

Commit

Permalink
simplify update hooks (bonus: fixed pytest warnings)
Browse files Browse the repository at this point in the history
  • Loading branch information
kraanzu committed Dec 19, 2024
1 parent 17d685e commit 0add32a
Showing 1 changed file with 13 additions and 48 deletions.
61 changes: 13 additions & 48 deletions dooit/api/hooks/update_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand All @@ -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")
Expand All @@ -93,3 +57,4 @@ def update_due_for_recurrence(mapper, connection, todo: Todo):

todo.pending = True
todo.due += todo.recurrence

0 comments on commit 0add32a

Please sign in to comment.