black formatting
This commit is contained in:
200
task.py
200
task.py
@@ -15,17 +15,19 @@ from args import arg_number
|
||||
|
||||
from utils import get_pits_path
|
||||
|
||||
|
||||
class Task:
|
||||
def __init__(self, status = None, priority = None):
|
||||
def __init__(self, status=None, priority=None):
|
||||
self.name = None
|
||||
self.status = status
|
||||
self.priority = priority
|
||||
self.date = None
|
||||
self.time = None
|
||||
|
||||
|
||||
def handle_task(args, last_action, conn):
|
||||
logging.info('> handle task')
|
||||
logging.debug('args: ' + str(args))
|
||||
logging.info("> handle task")
|
||||
logging.debug("args: " + str(args))
|
||||
|
||||
if not args:
|
||||
list_task(last_action.project_id, last_action.task_id, conn)
|
||||
@@ -33,18 +35,18 @@ def handle_task(args, last_action, conn):
|
||||
|
||||
if args[0].isdigit():
|
||||
view_task_set_active(arg_number(args[0]), last_action, conn)
|
||||
elif args[0] == '-c':
|
||||
elif args[0] == "-c":
|
||||
if len(args) == 1:
|
||||
print('missing task name')
|
||||
print("missing task name")
|
||||
sys.exit(1)
|
||||
|
||||
task = Task('open', 'normal')
|
||||
task.name = get_string_arg(args[1:], 'task name')
|
||||
task = Task("open", "normal")
|
||||
task.name = get_string_arg(args[1:], "task name")
|
||||
parse_task_args(task, args[2:])
|
||||
create_task(task, last_action.project_id, conn)
|
||||
elif args[0] == '-e':
|
||||
elif args[0] == "-e":
|
||||
if len(args) == 1:
|
||||
print('nothing to update')
|
||||
print("nothing to update")
|
||||
sys.exit(1)
|
||||
|
||||
task = Task()
|
||||
@@ -53,23 +55,25 @@ def handle_task(args, last_action, conn):
|
||||
task_id = int(args[1])
|
||||
parse_task_args(task, args[2:])
|
||||
else:
|
||||
task_id = last_action.task_id # FIXME If no active tast - use incremental args_i
|
||||
task_id = (
|
||||
last_action.task_id
|
||||
) # FIXME If no active tast - use incremental args_i
|
||||
parse_task_args(task, args[1:])
|
||||
logging.debug('Task: ({}) {}'.format(task_id, task))
|
||||
logging.debug("Task: ({}) {}".format(task_id, task))
|
||||
|
||||
edit_task(task, task_id, last_action.project_id, conn)
|
||||
# TODO To set as active project, us a -a option?
|
||||
elif args[0] == '-d':
|
||||
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]), last_action.project_id, conn)
|
||||
else:
|
||||
delete_task(last_action.task_id,last_action.project_id, conn)
|
||||
elif args[0] == '-m':
|
||||
delete_task(last_action.task_id, last_action.project_id, conn)
|
||||
elif args[0] == "-m":
|
||||
# Try another logic
|
||||
args_i = 1
|
||||
if len(args) < 3:
|
||||
print('missing project number')
|
||||
print("missing project number")
|
||||
sys.exit(1)
|
||||
|
||||
task_id = last_action.task_id
|
||||
@@ -80,22 +84,23 @@ def handle_task(args, last_action, conn):
|
||||
args_i += 1
|
||||
|
||||
if not task_id:
|
||||
print('No active task to move.')
|
||||
print("No active task to move.")
|
||||
sys.exit(1)
|
||||
|
||||
# Project option - if not fail
|
||||
if args[args_i] != '-p':
|
||||
print('missing project number')
|
||||
if args[args_i] != "-p":
|
||||
print("missing project number")
|
||||
sys.exit(1)
|
||||
else:
|
||||
args_i += 1
|
||||
|
||||
moving_task(task_id, last_action.project_id, arg_number(args[args_i]), conn)
|
||||
else:
|
||||
print(f'Invalid task option: {args[0]}')
|
||||
print(f"Invalid task option: {args[0]}")
|
||||
|
||||
# TODO Cascade deleting
|
||||
|
||||
|
||||
def parse_task_args(task, args):
|
||||
if not args:
|
||||
return
|
||||
@@ -104,30 +109,30 @@ def parse_task_args(task, args):
|
||||
while i < len(args):
|
||||
logging.debug(args[i])
|
||||
|
||||
if args[i] == '-s':
|
||||
if args[i] == "-s":
|
||||
i += 1
|
||||
task.status = get_string_arg(args[i:i+1], 'task status')
|
||||
elif args[i] == '-n':
|
||||
task.status = get_string_arg(args[i : i + 1], "task status")
|
||||
elif args[i] == "-n":
|
||||
i += 1
|
||||
task.name = get_string_arg(args[i:i+1], 'task name')
|
||||
elif args[i] == '-p':
|
||||
task.name = get_string_arg(args[i : i + 1], "task name")
|
||||
elif args[i] == "-p":
|
||||
i += 1
|
||||
task.priority = get_string_arg(args[i:i+1], 'task priority')
|
||||
elif args[i] == '-d':
|
||||
task.priority = get_string_arg(args[i : i + 1], "task priority")
|
||||
elif args[i] == "-d":
|
||||
i += 1
|
||||
task.date = get_string_arg(args[i:i+1], 'task date')
|
||||
elif args[i] == '-t':
|
||||
task.date = get_string_arg(args[i : i + 1], "task date")
|
||||
elif args[i] == "-t":
|
||||
i += 1
|
||||
task.time = get_string_arg(args[i:i+1], 'task time')
|
||||
task.time = get_string_arg(args[i : i + 1], "task time")
|
||||
else:
|
||||
print(f'Invalid task option: {args[i]}')
|
||||
print(f"Invalid task option: {args[i]}")
|
||||
sys.exit(1)
|
||||
i += 1
|
||||
|
||||
|
||||
def create_task(task, project_id, conn):
|
||||
# TODO Don't create task if no project (nothing selected?) ==> foreign key is not a solution: there is no project ID :)
|
||||
logging.info('>> Create task')
|
||||
logging.info(">> Create task")
|
||||
|
||||
query = """
|
||||
INSERT INTO
|
||||
@@ -136,72 +141,101 @@ def create_task(task, project_id, conn):
|
||||
"""
|
||||
|
||||
# TODO Date & time
|
||||
date = ''
|
||||
time = ''
|
||||
date = ""
|
||||
time = ""
|
||||
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(query, (project_id, getpass.getuser(), task.name, task.status,
|
||||
task.priority, date, time, datetime.datetime.now(),))
|
||||
cursor.execute(
|
||||
query,
|
||||
(
|
||||
project_id,
|
||||
getpass.getuser(),
|
||||
task.name,
|
||||
task.status,
|
||||
task.priority,
|
||||
date,
|
||||
time,
|
||||
datetime.datetime.now(),
|
||||
),
|
||||
)
|
||||
|
||||
task_id = cursor.lastrowid
|
||||
|
||||
logging.debug('CREATE ACTION ' + str(project_id) + str(task_id) )
|
||||
logging.debug("CREATE ACTION " + str(project_id) + str(task_id))
|
||||
|
||||
task_details = [item for item in task.__dict__.items() if item[1] is not None and item[0] is not 'name']
|
||||
log_args = ', '.join("%s: %s" % (k, v) for k, v in task_details)
|
||||
task_details = [
|
||||
item
|
||||
for item in task.__dict__.items()
|
||||
if item[1] is not None and item[0] is not "name"
|
||||
]
|
||||
log_args = ", ".join("%s: %s" % (k, v) for k, v in task_details)
|
||||
|
||||
action_message = '{} ({}, project: {})'.format(task.name, log_args, project_id)
|
||||
action_message = "{} ({}, project: {})".format(task.name, log_args, project_id)
|
||||
|
||||
record_action(cursor, TypeAction.CREATE, action_message, project_id, task_id)
|
||||
print('created task {}: {}'.format(task_id, action_message))
|
||||
print("created task {}: {}".format(task_id, action_message))
|
||||
|
||||
|
||||
def edit_task(task, task_id, project_id, conn):
|
||||
logging.info('>> Edit task')
|
||||
logging.info(">> Edit task")
|
||||
|
||||
update_args = [item for item in task.__dict__.items() if item[1] is not None]
|
||||
|
||||
if not update_args:
|
||||
print("nothing to update")
|
||||
print("Tips: if you want to activate a task, just do '{} task <task_id>'"
|
||||
.format(get_pits_path()))
|
||||
print(
|
||||
"Tips: if you want to activate a task, just do '{} task <task_id>'".format(
|
||||
get_pits_path()
|
||||
)
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
logging.debug('Task update args: {}'.format(update_args))
|
||||
logging.debug("Task update args: {}".format(update_args))
|
||||
|
||||
query = 'UPDATE task SET {} WHERE id = ?'
|
||||
query = query.format(', '.join("%s = '%s'" % (k, v) for k, v in update_args))
|
||||
logging.debug('update task query: ' + query)
|
||||
query = "UPDATE task SET {} WHERE id = ?"
|
||||
query = query.format(", ".join("%s = '%s'" % (k, v) for k, v in update_args))
|
||||
logging.debug("update task query: " + query)
|
||||
|
||||
cursor = conn.cursor()
|
||||
logging.debug('Do a task update')
|
||||
logging.debug("Do a task update")
|
||||
cursor.execute(query, (task_id,))
|
||||
log_args = ', '.join("%s: %s" % (k, v) for k, v in update_args)
|
||||
log_args = ", ".join("%s: %s" % (k, v) for k, v in update_args)
|
||||
# TODO Print task name ?
|
||||
print('updated task {}: ({})'.format(task_id, log_args))
|
||||
print("updated task {}: ({})".format(task_id, log_args))
|
||||
|
||||
# TODO Remove project id ?
|
||||
logging.debug('UPDATE TASK - ACTION MESSAGE : ' + log_args)
|
||||
record_action(cursor, TypeAction.UPDATE, '({})'.format(log_args), project_id, task_id)
|
||||
logging.debug("UPDATE TASK - ACTION MESSAGE : " + log_args)
|
||||
record_action(
|
||||
cursor, TypeAction.UPDATE, "({})".format(log_args), project_id, task_id
|
||||
)
|
||||
|
||||
|
||||
def moving_task(task_id, old_project_id, project_id, conn):
|
||||
logging.info('>> Moving task')
|
||||
logging.info(">> Moving task")
|
||||
|
||||
query = """
|
||||
UPDATE task SET project_id = ? WHERE id = ?;
|
||||
"""
|
||||
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(query, (project_id, task_id,))
|
||||
cursor.execute(query, (project_id, task_id))
|
||||
|
||||
if cursor.rowcount != 1:
|
||||
print('Problem occur when moving task...')
|
||||
print("Problem occur when moving task...")
|
||||
sys.exit(1)
|
||||
|
||||
print('moved task {}: from project {} to project {}'.format(task_id, old_project_id, project_id))
|
||||
record_action(cursor, TypeAction.MOVE, '', project_id=project_id, task_id=task_id) # TODO Message
|
||||
print(
|
||||
"moved task {}: from project {} to project {}".format(
|
||||
task_id, old_project_id, project_id
|
||||
)
|
||||
)
|
||||
record_action(
|
||||
cursor, TypeAction.MOVE, "", project_id=project_id, task_id=task_id
|
||||
) # TODO Message
|
||||
|
||||
|
||||
def delete_task(task_id, project_id, conn):
|
||||
logging.info('>> Remove task')
|
||||
logging.info(">> Remove task")
|
||||
|
||||
query = """
|
||||
DELETE FROM task
|
||||
@@ -211,29 +245,40 @@ def delete_task(task_id, project_id, conn):
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(query, (task_id,))
|
||||
if cursor.rowcount != 1:
|
||||
logging.error('DELETE FAILED')
|
||||
print('could not find task {}'.format(task_id))
|
||||
logging.error("DELETE FAILED")
|
||||
print("could not find task {}".format(task_id))
|
||||
sys.exit(1)
|
||||
|
||||
print('deleted task {}: {}'.format(task_id, 'task_name'))
|
||||
record_action(cursor, TypeAction.DELETE, '', project_id, task_id) # TODO Message
|
||||
print("deleted task {}: {}".format(task_id, "task_name"))
|
||||
record_action(cursor, TypeAction.DELETE, "", project_id, task_id) # TODO Message
|
||||
# TODO Cascade deleting
|
||||
|
||||
|
||||
def list_task(active_project_id, active_task_id, conn):
|
||||
logging.info('>> No arguments')
|
||||
logging.info(">> No arguments")
|
||||
|
||||
query = """
|
||||
SELECT id, username, status, priority, name,
|
||||
(SELECT count(*) FROM note WHERE note.task_id = task.id)
|
||||
FROM task WHERE project_id = ?;
|
||||
""" #TODO Date & time
|
||||
""" # TODO Date & time
|
||||
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(query, (active_project_id,))
|
||||
for row in cursor.fetchall():
|
||||
task_id, username, status, priority, name, nb_note = row
|
||||
print('{:1} {:2d}: ({:8}) | {} | {} | {} ({} notes)'.format('*' if active_task_id == task_id else '', task_id,
|
||||
username, status, priority, name, nb_note))
|
||||
print(
|
||||
"{:1} {:2d}: ({:8}) | {} | {} | {} ({} notes)".format(
|
||||
"*" if active_task_id == task_id else "",
|
||||
task_id,
|
||||
username,
|
||||
status,
|
||||
priority,
|
||||
name,
|
||||
nb_note,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def view_task_set_active(task_id, last_action, conn):
|
||||
# FIXME duplicate with list_task / view_project_set_active
|
||||
@@ -241,16 +286,16 @@ def view_task_set_active(task_id, last_action, conn):
|
||||
SELECT id, username, status, priority, name,
|
||||
(SELECT count(*) FROM note WHERE note.task_id = task.id)
|
||||
FROM task WHERE id = ?;
|
||||
""" #TODO Date & time
|
||||
""" # TODO Date & time
|
||||
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(query, (task_id,))
|
||||
row = cursor.fetchone()
|
||||
if not row:
|
||||
print('Could not find task {}'.format(task_id))
|
||||
print("Could not find task {}".format(task_id))
|
||||
sys.exit(1)
|
||||
|
||||
print('* {:d}: ({}) {} (status: {}, {} tasks)'.format(*row))
|
||||
print("* {:d}: ({}) {} (status: {}, {} tasks)".format(*row))
|
||||
|
||||
# FIXME duplicate with list_note
|
||||
query = """
|
||||
@@ -258,13 +303,20 @@ def view_task_set_active(task_id, last_action, conn):
|
||||
"""
|
||||
cursor.execute(query, (task_id,))
|
||||
for row in cursor.fetchall():
|
||||
logging.debug('Note row: {}'.format(row))
|
||||
logging.debug("Note row: {}".format(row))
|
||||
note_id, username, message = row
|
||||
print(' {} {:d}: ({}) {}'.format('*' if last_action.note_id == row[0] else ' ',
|
||||
note_id, username, message))
|
||||
print(
|
||||
" {} {:d}: ({}) {}".format(
|
||||
"*" if last_action.note_id == row[0] else " ",
|
||||
note_id,
|
||||
username,
|
||||
message,
|
||||
)
|
||||
)
|
||||
set_active(cursor, task_id=task_id)
|
||||
|
||||
|
||||
# $ pit t 11
|
||||
# * 11: (budd) a task (project: 1, status: in progress, priority: high, date: Apr 26, 2018, time: 5:00, 2 notes)
|
||||
# 6: (budd) bonjour le monde
|
||||
# * 7: (budd) bonjour le monde
|
||||
# * 7: (budd) bonjour le monde
|
||||
|
||||
Reference in New Issue
Block a user