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 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) (limited to 'todo/db/task_tags.py') 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) -- cgit v1.2.3