Improve deleting objects
This commit is contained in:
14
action.py
14
action.py
@@ -56,21 +56,9 @@ def handle_action(_1, _2, conn):
|
||||
object_type = "note"
|
||||
id_object = note_id
|
||||
|
||||
if taction == TypeAction.DELETE.value:
|
||||
print(
|
||||
"{} ({}): {} {} {}".format(
|
||||
formated_date, username, taction, object_type, id_object
|
||||
)
|
||||
)
|
||||
else:
|
||||
print(
|
||||
"{} ({}): {} {} {}: {}".format(
|
||||
formated_date,
|
||||
username,
|
||||
taction,
|
||||
object_type,
|
||||
id_object,
|
||||
message,
|
||||
formated_date, username, taction, object_type, id_object, message
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
49
note.py
49
note.py
@@ -79,7 +79,7 @@ def create_note(note, active_project_id, active_task_id, conn):
|
||||
|
||||
note_id = cursor.lastrowid
|
||||
|
||||
action_message = "{} (task: {})".format(note.message, active_task_id)
|
||||
action_message = "{} (task {})".format(note.message, active_task_id)
|
||||
record_action(
|
||||
cursor,
|
||||
TypeAction.CREATE,
|
||||
@@ -125,44 +125,33 @@ def edit_note(note, note_id, last_action, conn):
|
||||
def delete_note(note_id, conn):
|
||||
logging.info(">> Remove note")
|
||||
|
||||
query = """
|
||||
DELETE FROM note
|
||||
WHERE id = ?;
|
||||
"""
|
||||
note_name, task_id = get_note_name_task_id(note_id, conn)
|
||||
|
||||
query = "DELETE FROM note WHERE id = ?;"
|
||||
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(query, (note_id,))
|
||||
|
||||
if cursor.rowcount != 1:
|
||||
logging.error("DELETE FAILED")
|
||||
print("could not find note {}".format(note_id))
|
||||
sys.exit(1)
|
||||
|
||||
record_action(cursor, TypeAction.DELETE, "", note_id=note_id)
|
||||
log_message = "{} (task {})".format(note_name, task_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
|
||||
record_action(cursor, TypeAction.DELETE, log_message, note_id=note_id)
|
||||
|
||||
print("deleted note {}: {}".format(note_id, log_message))
|
||||
|
||||
|
||||
def list_note(active_project_id, active_task_id, active_note_id, conn):
|
||||
logging.info(">> No arguments")
|
||||
query = (
|
||||
"SELECT id, username, message FROM note WHERE project_id = ? AND task_id = ?;"
|
||||
) # TODO Date & time
|
||||
query = """
|
||||
SELECT id, username, message
|
||||
FROM note
|
||||
WHERE project_id = ?
|
||||
AND task_id = ?;
|
||||
"""
|
||||
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(query, (active_project_id, active_task_id))
|
||||
@@ -173,3 +162,11 @@ def list_note(active_project_id, active_task_id, active_note_id, conn):
|
||||
"*" if active_note_id == note_id else "", note_id, username, message
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def get_note_name_task_id(note_id, conn):
|
||||
query = "SELECT message, task_id FROM note WHERE id = ?"
|
||||
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(query, (note_id,))
|
||||
return cursor.fetchone()
|
||||
|
||||
49
project.py
49
project.py
@@ -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]
|
||||
|
||||
65
task.py
65
task.py
@@ -64,9 +64,9 @@ def handle_task(args, last_action, conn):
|
||||
elif args[0] == "-d":
|
||||
# FIXME Duplicate code
|
||||
if len(args) > 1:
|
||||
delete_task(arg_number(args[1]), last_action.project_id, conn)
|
||||
delete_task(arg_number(args[1]), conn)
|
||||
else:
|
||||
delete_task(last_action.task_id, last_action.project_id, conn)
|
||||
delete_task(last_action.task_id, conn)
|
||||
elif args[0] == "-m":
|
||||
# Try another logic
|
||||
args_i = 1
|
||||
@@ -194,7 +194,7 @@ def edit_task(task, task_id, conn):
|
||||
# Retrieve name if not updated
|
||||
task_name = None
|
||||
if "name" not in update_args[0]:
|
||||
task_name = get_task_name(task_id, conn)
|
||||
task_name, _ = get_task_name_project_id(task_id, conn)
|
||||
|
||||
query = "UPDATE task SET {} WHERE id = ?"
|
||||
query = query.format(", ".join("%s = '%s'" % (k, v) for k, v in update_args))
|
||||
@@ -223,7 +223,7 @@ def edit_task(task, task_id, conn):
|
||||
def moving_task(task_id, dest_project_id, conn):
|
||||
logging.info(">> Moving task")
|
||||
|
||||
or_project_id = get_task_project(task_id, conn)
|
||||
_, or_project_id = get_task_name_project_id(task_id, conn)
|
||||
|
||||
if or_project_id == dest_project_id:
|
||||
print("Task already in project {} - nothing to do".format(dest_project_id))
|
||||
@@ -252,11 +252,10 @@ def moving_task(task_id, dest_project_id, conn):
|
||||
)
|
||||
|
||||
|
||||
def delete_task(task_id, project_id, conn):
|
||||
def delete_task(task_id, conn):
|
||||
logging.info(">> Remove task")
|
||||
|
||||
task_name = get_task_name(task_id, conn)
|
||||
task_project = get_task_project(task_id, conn)
|
||||
task_name, project_id = get_task_name_project_id(task_id, conn)
|
||||
|
||||
cursor = conn.cursor()
|
||||
|
||||
@@ -264,9 +263,12 @@ def delete_task(task_id, project_id, conn):
|
||||
query = "SELECT id FROM note WHERE task_id = ?;"
|
||||
cursor.execute(query, (task_id,))
|
||||
|
||||
deleted_note = 0
|
||||
for row in cursor.fetchall():
|
||||
note_id = row[0]
|
||||
note.delete_note(note_id, conn)
|
||||
deleted_note += 1
|
||||
# FIXME Use a delete_note_for_task function in note module?
|
||||
|
||||
# Remove task
|
||||
query = "DELETE FROM task WHERE id = ?;"
|
||||
@@ -277,30 +279,15 @@ def delete_task(task_id, project_id, conn):
|
||||
print("could not find task {}".format(task_id))
|
||||
sys.exit(1)
|
||||
|
||||
print("deleted task {}: {} (project: {})".format(task_id, task_name, project_id))
|
||||
record_action(
|
||||
cursor,
|
||||
TypeAction.DELETE,
|
||||
"{} (project: {})".format(task_name, project_id),
|
||||
project_id,
|
||||
task_id,
|
||||
) # TODO Message
|
||||
# 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_note:
|
||||
log_message = "{} with {} note{} (project: {})".format(
|
||||
task_name, deleted_note, "s" if deleted_note > 1 else "", project_id
|
||||
)
|
||||
else:
|
||||
log_message = "{} (project: {})".format(task_name, project_id)
|
||||
|
||||
print("deleted task {}: {}".format(task_id, log_message))
|
||||
record_action(cursor, TypeAction.DELETE, log_message, project_id, task_id)
|
||||
|
||||
|
||||
def list_task(active_project_id, active_task_id, conn):
|
||||
@@ -365,20 +352,14 @@ def view_task_set_active(task_id, last_action, conn):
|
||||
set_active(cursor, task_id=task_id)
|
||||
|
||||
|
||||
def get_task_name(task_id, conn):
|
||||
query = "SELECT name FROM task WHERE id = ?"
|
||||
def get_task_name_project_id(task_id, conn):
|
||||
""" Return a tuple with task name and project id of task"""
|
||||
|
||||
query = "SELECT name, project_id FROM task WHERE id = ?"
|
||||
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(query, (task_id,))
|
||||
return cursor.fetchone()[0]
|
||||
|
||||
|
||||
def get_task_project(task_id, conn):
|
||||
query = "SELECT project_id FROM task WHERE id = ?"
|
||||
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(query, (task_id,))
|
||||
return cursor.fetchone()[0]
|
||||
return cursor.fetchone()
|
||||
|
||||
|
||||
# $ pit t 11
|
||||
|
||||
Reference in New Issue
Block a user