176 lines
4.6 KiB
Python
176 lines
4.6 KiB
Python
# import sqlite3
|
|
import getpass
|
|
import logging
|
|
import sys
|
|
|
|
from action import record_action
|
|
from action import TypeAction
|
|
|
|
from args import get_string_arg
|
|
from args import arg_number
|
|
|
|
|
|
class Note:
|
|
def __init__(self):
|
|
self.message = None
|
|
|
|
|
|
def handle_note(args, last_action, conn):
|
|
logging.info("> handle note")
|
|
logging.debug("args: %s", args)
|
|
|
|
if not args:
|
|
list_note(
|
|
last_action.project_id, last_action.task_id, last_action.note_id, conn
|
|
)
|
|
sys.exit(0)
|
|
|
|
if args[0] == "-c":
|
|
if len(args) == 1:
|
|
print("missing note message")
|
|
sys.exit(1)
|
|
|
|
note = Note()
|
|
note.message = get_string_arg(args[1:], "note message")
|
|
create_note(note, last_action.project_id, last_action.task_id, conn)
|
|
elif args[0] == "-e":
|
|
if len(args) == 1:
|
|
print("nothing to update")
|
|
|
|
note = Note()
|
|
args_i = 1
|
|
note_id = last_action.note_id
|
|
if args[args_i].isdigit():
|
|
note_id = arg_number(args[args_i])
|
|
args_i += 1
|
|
|
|
if not note_id:
|
|
print("No active note to update")
|
|
sys.exit(1)
|
|
|
|
note.message = get_string_arg(args[args_i:], "note message")
|
|
edit_note(note, note_id, last_action, conn)
|
|
elif args[0] == "-d":
|
|
# FIXME Duplicate code
|
|
if len(args) > 1:
|
|
delete_note(arg_number(args[1]), conn)
|
|
else:
|
|
delete_note(last_action.note_id, conn)
|
|
else:
|
|
print(f"Invalid note option: {args[0]}")
|
|
|
|
|
|
def create_note(note, active_project_id, active_task_id, conn):
|
|
logging.info(">> Create note")
|
|
|
|
if active_task_id == 0:
|
|
print("No task selected")
|
|
sys.exit(1)
|
|
|
|
query = """
|
|
INSERT INTO note (project_id, task_id, username, message)
|
|
VALUES (?, ?, ?, ?);
|
|
"""
|
|
|
|
cursor = conn.cursor()
|
|
cursor.execute(
|
|
query, (active_project_id, active_task_id, getpass.getuser(), note.message)
|
|
)
|
|
|
|
note_id = cursor.lastrowid
|
|
|
|
action_message = "{} (task: {})".format(note.message, active_task_id)
|
|
record_action(
|
|
cursor,
|
|
TypeAction.CREATE,
|
|
action_message,
|
|
active_project_id,
|
|
active_task_id,
|
|
note_id,
|
|
)
|
|
logging.debug(action_message)
|
|
|
|
print("created note {}: {} (task {})".format(note_id, note.message, active_task_id))
|
|
|
|
|
|
def edit_note(note, note_id, last_action, conn):
|
|
logging.info(">> Edit note")
|
|
|
|
query = "UPDATE note SET message = ? WHERE id = ?"
|
|
|
|
cursor = conn.cursor()
|
|
cursor.execute(query, (note.message, note_id))
|
|
|
|
if cursor.rowcount != 1:
|
|
logging.error("UPDATE FAILED")
|
|
print("could not find note {}".format(note_id))
|
|
sys.exit(1)
|
|
|
|
record_action(
|
|
cursor,
|
|
TypeAction.UPDATE,
|
|
note.message,
|
|
last_action.project_id,
|
|
last_action.task_id,
|
|
last_action.note_id,
|
|
)
|
|
|
|
print(
|
|
"updated note {}: (message: {}, task {})".format(
|
|
note_id, note.message, last_action.task_id
|
|
)
|
|
)
|
|
|
|
|
|
def delete_note(note_id, conn):
|
|
logging.info(">> Remove note")
|
|
|
|
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)
|
|
|
|
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):
|
|
logging.info(">> No arguments")
|
|
query = (
|
|
"SELECT id, username, message FROM note WHERE project_id = ? AND task_id = ?;"
|
|
) # TODO Date & time
|
|
|
|
cursor = conn.cursor()
|
|
cursor.execute(query, (active_project_id, active_task_id))
|
|
for row in cursor.fetchall():
|
|
note_id, username, message = row
|
|
print(
|
|
"{:1} {:2d}: ({:8}) {}".format(
|
|
"*" if active_note_id == note_id else "", note_id, username, message
|
|
)
|
|
)
|