WIP Task
This commit is contained in:
124
task.py
124
task.py
@@ -11,10 +11,12 @@ import action
|
|||||||
from args import get_string_arg
|
from args import get_string_arg
|
||||||
|
|
||||||
class Task:
|
class Task:
|
||||||
def __init__(self):
|
def __init__(self, status = None, priority = None):
|
||||||
self.name = ''
|
self.name = None
|
||||||
self.status = 'open'
|
self.status = status
|
||||||
self.priority = 'normal'
|
self.priority = priority
|
||||||
|
self.date = None
|
||||||
|
self.time = None
|
||||||
|
|
||||||
def handle_task(args, last_action, conn):
|
def handle_task(args, last_action, conn):
|
||||||
logging.info('> handle task')
|
logging.info('> handle task')
|
||||||
@@ -25,19 +27,66 @@ def handle_task(args, last_action, conn):
|
|||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
if args[0].isdigit():
|
if args[0].isdigit():
|
||||||
view_task()
|
view_task(last_action.task_id, conn)
|
||||||
sys.exit(0)
|
elif args[0] == '-c':
|
||||||
|
|
||||||
if args[0] == '-c':
|
|
||||||
if len(args) == 1:
|
if len(args) == 1:
|
||||||
print('missing task name')
|
print('missing task name')
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
task = Task()
|
task = Task('open', 'normal')
|
||||||
task.name = project.arg_string(args[1], 'task name')
|
task.name = get_string_arg(args[1:], 'task name')
|
||||||
|
parse_task_args(task, args[2:])
|
||||||
create_task(task, last_action.project_id, conn)
|
create_task(task, last_action.project_id, conn)
|
||||||
|
elif args[0] == '-e':
|
||||||
|
if len(args) == 1:
|
||||||
|
print('nothing to update')
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
def create_task(task, active_project_id, conn):
|
task = Task()
|
||||||
|
|
||||||
|
if args[1].isdigit():
|
||||||
|
task_id = int(args[1])
|
||||||
|
parse_task_args(task, args[2:])
|
||||||
|
else:
|
||||||
|
task_id = last_action.task_id
|
||||||
|
parse_task_args(task, args[1:])
|
||||||
|
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?
|
||||||
|
else:
|
||||||
|
print(f'Invalid project option: {args[0]}')
|
||||||
|
|
||||||
|
def parse_task_args(task, args):
|
||||||
|
if not args:
|
||||||
|
return
|
||||||
|
|
||||||
|
i = 0
|
||||||
|
while i < len(args):
|
||||||
|
logging.debug(args[i])
|
||||||
|
|
||||||
|
if args[i] == '-s':
|
||||||
|
i += 1
|
||||||
|
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':
|
||||||
|
i += 1
|
||||||
|
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':
|
||||||
|
i += 1
|
||||||
|
task.time = get_string_arg(args[i:i+1], 'task time')
|
||||||
|
else:
|
||||||
|
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 :)
|
# 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')
|
||||||
|
|
||||||
@@ -52,48 +101,49 @@ def create_task(task, active_project_id, conn):
|
|||||||
time = ''
|
time = ''
|
||||||
|
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute(query, (active_project_id, getpass.getuser(), task.name, task.status,
|
cursor.execute(query, (project_id, getpass.getuser(), task.name, task.status,
|
||||||
task.priority, date, time, datetime.datetime.now(),))
|
task.priority, date, time, datetime.datetime.now(),))
|
||||||
|
|
||||||
task_id = cursor.lastrowid
|
task_id = cursor.lastrowid
|
||||||
|
|
||||||
logging.debug('CREATE ACTION ' + str(active_project_id) + str(task_id) )
|
logging.debug('CREATE ACTION ' + str(project_id) + str(task_id) )
|
||||||
|
|
||||||
action_message = '{} (status: {}, priority: {}, project: {})'.format(task.name,
|
task_details = [item for item in task.__dict__.items() if item[1] is not None and item[0] is not 'name']
|
||||||
task.status, task.priority, active_project_id)
|
log_args = ', '.join("%s: %s" % (k, v) for k, v in task_details)
|
||||||
# TODO if date/time/other => add to message
|
|
||||||
|
|
||||||
action.create_action(cursor, project_id=active_project_id, task_id=task_id, message=action_message)
|
action_message = '{} ({}, project: {})'.format(task.name, log_args, project_id)
|
||||||
print('created task {}: {} (status: {})'.format(task_id, task.name, task.status))
|
|
||||||
|
|
||||||
def edit_task(args, task_id, conn):
|
action.create_action(cursor, project_id=project_id, task_id=task_id, message=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 = {}
|
update_args = [item for item in task.__dict__.items() if item[1] is not None]
|
||||||
if args.status:
|
|
||||||
update_args['status'] = args.status
|
if not update_args:
|
||||||
if args.edit_name:
|
print("nothing to update")
|
||||||
update_args['name'] = args.edit_name
|
print("Tips: if you want to set an active task, just do '{} task <task_id>'"
|
||||||
|
.format(os.path.basename(sys.argv[0])))
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
logging.debug('Task update args: {}'.format(update_args))
|
||||||
|
|
||||||
query = 'UPDATE task SET {} WHERE id = ?'
|
query = 'UPDATE task SET {} WHERE id = ?'
|
||||||
query = query.format(', '.join("%s = '%s'" % (k, v) for k, v in update_args.items()))
|
query = query.format(', '.join("%s = '%s'" % (k, v) for k, v in update_args))
|
||||||
logging.debug('update task query: ' + query)
|
logging.debug('update task query: ' + query)
|
||||||
|
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
if update_args:
|
logging.debug('Do a task update')
|
||||||
logging.debug('Do a task update')
|
cursor.execute(query, (task_id,))
|
||||||
cursor.execute(query, (task_id,))
|
log_args = ', '.join("%s: %s" % (k, v) for k, v in update_args)
|
||||||
|
action_message = '{} ({})'.format(task_id, ', '.join("%s: %s" % (k, v) for k, v in update_args))
|
||||||
|
# TODO Review action message (at the end)
|
||||||
|
print('updated task {}: ({})'.format(task_id, log_args))
|
||||||
|
|
||||||
log_args = ', '.join("%s: '%s'" % (k, v) for k, v in update_args.items())
|
|
||||||
print('updated task {}: ({})'.format(task_id, log_args))
|
|
||||||
else:
|
|
||||||
print('updated task {}: set active task')
|
|
||||||
|
|
||||||
# TODO If not edit name... => retrieve name ?
|
|
||||||
# TODO Retrieve project id ?
|
# TODO Retrieve project id ?
|
||||||
action_message = '{} ({})'.format(args.edit_name, ', '.join("%s: %s" % (k, v) for k, v in update_args.items()))
|
|
||||||
logging.debug('UPDATE TASK - ACTION MESSAGE : ' + action_message)
|
logging.debug('UPDATE TASK - ACTION MESSAGE : ' + action_message)
|
||||||
action.create_action(cursor, task_id=task_id, action=action.TypeAction.UPDATE, message = action_message)
|
action.create_action(cursor, project_id=project_id, task_id=task_id, action=action.TypeAction.UPDATE, message = action_message)
|
||||||
|
|
||||||
def moving_task(args):
|
def moving_task(args):
|
||||||
print('moving')
|
print('moving')
|
||||||
@@ -138,5 +188,5 @@ def list_task(active_project_id, active_task_id, conn):
|
|||||||
# 2: (budd) |open| |normal| test (0 notes)
|
# 2: (budd) |open| |normal| test (0 notes)
|
||||||
# * 3: (budd) |open| |normal| Dec 31, 2018 tet (0 notes)
|
# * 3: (budd) |open| |normal| Dec 31, 2018 tet (0 notes)
|
||||||
|
|
||||||
def view_task():
|
def view_task(task_id, conn):
|
||||||
print('TODO View Task')
|
print('TODO View Task')
|
||||||
Reference in New Issue
Block a user