From 4e923888896798da6db7b50c75ee31fcc5119e16 Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 14 Jun 2020 12:01:36 +0200 Subject: Improve tag update --- todo/db/task_tags.py | 31 ++++++++++++++++++++----------- todo/gui/tasks/dialog.py | 2 +- todo/service/tasks.py | 12 ++++++++---- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/todo/db/task_tags.py b/todo/db/task_tags.py index 0fae5f9..2f064e0 100644 --- a/todo/db/task_tags.py +++ b/todo/db/task_tags.py @@ -18,22 +18,31 @@ def get(cursor: Cursor) -> List[TaskTag]: return [TaskTag(r[0], r[1]) for r in cursor.fetchall()] def insert_many(cursor: Cursor, task_id: int, tag_ids: List[int]) -> List[TaskTag] : - now = int(time.time()) + if len(tag_ids) >= 1: + now = int(time.time()) - task_tags = [TaskTag(task_id = task_id, tag_id = tag) for tag in tag_ids] + task_tags = [TaskTag(task_id = task_id, tag_id = tag) for tag in tag_ids] - cursor.executemany( - " INSERT INTO task_tags(" - " task_id," - " tag_id," - " created_at" - " ) VALUES (?, ?, ?)", - [(t.task_id, t.tag_id, now) for t in task_tags]) + cursor.executemany( + " INSERT INTO task_tags(" + " task_id," + " tag_id," + " created_at" + " ) VALUES (?, ?, ?)", + [(t.task_id, t.tag_id, now) for t in task_tags]) - return task_tags + return task_tags + else: + return [] -def delete(cursor: Cursor, task_ids: List[int]): +def delete_from_tasks(cursor: Cursor, task_ids: List[int]): if len(task_ids) >= 1: cursor.execute( "DELETE FROM task_tags WHERE task_id IN (%s)" % ",".join("?"*len(task_ids)), task_ids) + +def delete_from_tags(cursor: Cursor, task_id: int, tag_ids: List[int]): + if len(tag_ids) >= 1: + cursor.execute( + "DELETE FROM task_tags WHERE task_id = ? AND tag_id IN (%s)" % ",".join("?"*len(tag_ids)), + [task_id] + tag_ids) diff --git a/todo/gui/tasks/dialog.py b/todo/gui/tasks/dialog.py index c9e3734..2bed6af 100644 --- a/todo/gui/tasks/dialog.py +++ b/todo/gui/tasks/dialog.py @@ -19,7 +19,7 @@ def add(parent_widget, status: Status, add_task_signal): def update(parent_widget, update_task_signal, row: int, task: Task, tags: List[int]): def on_update(task_form: ValidTaskForm): - updated_task = todo.service.tasks.update(todo.database.cursor(), task, task_form) + updated_task = todo.service.tasks.update(todo.database.cursor(), task, tags, task_form) update_task_signal.emit(row, updated_task, task_form.tags) return widget(parent_widget, "Modify a task", "modify", task, tags, on_update) diff --git a/todo/service/tasks.py b/todo/service/tasks.py index 2cebf00..c01ab8a 100644 --- a/todo/service/tasks.py +++ b/todo/service/tasks.py @@ -15,15 +15,19 @@ def create(cursor, status: Status, task_form: ValidTaskForm) -> Task: todo.database.commit() return task -def update(cursor, task: Task, task_form: ValidTaskForm) -> Task: - todo.db.task_tags.delete(cursor, [task.id]) +def update(cursor, task: Task, tags: List[int], task_form: ValidTaskForm) -> Task: + old_tags = [t for t in tags if t not in task_form.tags] + todo.db.task_tags.delete_from_tags(cursor, task.id, old_tags) + + new_tags = [t for t in task_form.tags if t not in tags] + todo.db.task_tags.insert_many(cursor, task.id, new_tags) + updated_task = todo.db.tasks.update(cursor, task, task_form) - todo.db.task_tags.insert_many(cursor, task.id, task_form.tags) todo.database.commit() return updated_task def delete(cursor, task_ids: List[int]): - todo.db.task_tags.delete(cursor, task_ids) + todo.db.task_tags.delete_from_tasks(cursor, task_ids) todo.db.tasks.delete(cursor, task_ids) todo.database.commit() -- cgit v1.2.3