Cascade deleting

Fix bad note deleting when no ID specified
This commit is contained in:
2018-07-21 16:11:32 +02:00
parent f426b616fd
commit fa25b86a2d
3 changed files with 73 additions and 9 deletions

18
note.py
View File

@@ -55,7 +55,7 @@ def handle_note(args, last_action, conn):
if len(args) > 1:
delete_note(arg_number(args[1]), conn)
else:
delete_note(last_action.task_id, conn)
delete_note(last_action.note_id, conn)
else:
print(f"Invalid note option: {args[0]}")
@@ -137,6 +137,22 @@ def delete_note(note_id, conn):
record_action(cursor, TypeAction.DELETE, "", note_id=note_id)
print("deleted note {}: {}".format(note_id, "note_name"))
# TODO Good Output
# $ pit p -d
# deleted task 1: efzf (project: 1)
# deleted note 1: fzef (task 3)
# deleted note 2: efz (task 3)
# deleted task 3: efzfzefzef with 2 notes (project: 1)
# deleted note 4: gtrezgze (task 7)
# deleted note 5: fgzegfz (task 7)
# deleted task 7: test with 2 notes (project: 1)
# deleted task 9: a task (project: 1)
# deleted task 10: a task (project: 1)
# deleted note 6: test (task 11)
# deleted note 7: test (task 11)
# deleted note 8: test (task 11)
# deleted task 11: new name with 3 notes (project: 1)
# deleted project 1: test with 6 tasks
def list_note(active_project_id, active_task_id, active_note_id, conn):

View File

@@ -4,6 +4,8 @@ import datetime
import logging
import sys
import task
from action import record_action
from action import set_active
from action import TypeAction
@@ -149,12 +151,19 @@ def edit_project(project, project_id, conn):
def delete_project(project_id, conn):
logging.info(">> Remove project")
cursor = conn.cursor()
# Cascade deleting: task
query = "SELECT id FROM task WHERE project_id = ?;"
cursor.execute(query, (project_id,))
for row in cursor.fetchall():
task.delete_task(row[0], project_id, conn)
query = """
DELETE FROM project
WHERE id = ?;
"""
cursor = conn.cursor()
cursor.execute(query, (project_id,))
if cursor.rowcount != 1:
logging.error("DELETE FAILED")
@@ -162,7 +171,22 @@ def delete_project(project_id, conn):
print("deleted project {}: {}".format(project_id, "project_name"))
record_action(cursor, TypeAction.DELETE, "", project_id)
# TODO Cascade deleting
# TODO Good Output
# $ pit p -d
# deleted task 1: efzf (project: 1)
# deleted note 1: fzef (task 3)
# deleted note 2: efz (task 3)
# deleted task 3: efzfzefzef with 2 notes (project: 1)
# deleted note 4: gtrezgze (task 7)
# deleted note 5: fgzegfz (task 7)
# deleted task 7: test with 2 notes (project: 1)
# deleted task 9: a task (project: 1)
# deleted task 10: a task (project: 1)
# deleted note 6: test (task 11)
# deleted note 7: test (task 11)
# deleted note 8: test (task 11)
# deleted task 11: new name with 3 notes (project: 1)
# deleted project 1: test with 6 tasks
def list_project(active_project_id, conn):

36
task.py
View File

@@ -12,6 +12,8 @@ from args import arg_number
from utils import get_pits_path
import note
class Task:
def __init__(self, status=None, priority=None):
@@ -233,12 +235,19 @@ def moving_task(task_id, old_project_id, project_id, conn):
def delete_task(task_id, project_id, conn):
logging.info(">> Remove task")
query = """
DELETE FROM task
WHERE id = ?;
"""
cursor = conn.cursor()
# Cascade deleting: note
query = "SELECT id FROM note WHERE task_id = ?;"
cursor.execute(query, (task_id,))
for row in cursor.fetchall():
note_id = row[0]
note.delete_note(note_id, conn)
# Remove task
query = "DELETE FROM task WHERE id = ?;"
cursor.execute(query, (task_id,))
if cursor.rowcount != 1:
logging.error("DELETE FAILED")
@@ -247,7 +256,22 @@ def delete_task(task_id, project_id, conn):
print("deleted task {}: {}".format(task_id, "task_name"))
record_action(cursor, TypeAction.DELETE, "", project_id, task_id) # TODO Message
# TODO Cascade deleting
# TODO Good Output
# $ pit p -d
# deleted task 1: efzf (project: 1)
# deleted note 1: fzef (task 3)
# deleted note 2: efz (task 3)
# deleted task 3: efzfzefzef with 2 notes (project: 1)
# deleted note 4: gtrezgze (task 7)
# deleted note 5: fgzegfz (task 7)
# deleted task 7: test with 2 notes (project: 1)
# deleted task 9: a task (project: 1)
# deleted task 10: a task (project: 1)
# deleted note 6: test (task 11)
# deleted note 7: test (task 11)
# deleted note 8: test (task 11)
# deleted task 11: new name with 3 notes (project: 1)
# deleted project 1: test with 6 tasks
def list_task(active_project_id, active_task_id, conn):