Improve deleting objects

This commit is contained in:
2018-07-22 00:08:00 +02:00
parent a95d7ec051
commit 81c7b3473e
4 changed files with 75 additions and 108 deletions

View File

@@ -151,42 +151,35 @@ def edit_project(project, project_id, conn):
def delete_project(project_id, conn):
logging.info(">> Remove project")
project_name = get_project_name(project_id, conn)
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 = ?;
"""
deleted_task = 0
for row in cursor.fetchall():
task.delete_task(row[0], conn)
deleted_task += 1
query = "DELETE FROM project WHERE id = ?;"
cursor.execute(query, (project_id,))
if cursor.rowcount != 1:
logging.error("DELETE FAILED")
print("could not find project {}".format(project_id))
print("deleted project {}: {}".format(project_id, "project_name"))
record_action(cursor, TypeAction.DELETE, "", project_id)
# 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
if deleted_task:
log_message = "{} with {} task{}".format(
project_name, deleted_task, "s" if deleted_task > 1 else ""
)
else:
log_message = project_name
print("deleted project {}: {}".format(project_id, log_message))
record_action(cursor, TypeAction.DELETE, log_message, project_id)
def list_project(active_project_id, conn):
@@ -257,3 +250,11 @@ def view_project_set_active(project_id, last_action, conn):
set_active(cursor, project_id=project_id)
# TODO Add a -v option to see notes?
def get_project_name(project_id, conn):
query = "SELECT name FROM project WHERE id = ?"
cursor = conn.cursor()
cursor.execute(query, (project_id,))
return cursor.fetchone()[0]