Complete project
This commit is contained in:
101
project.py
101
project.py
@@ -10,9 +10,13 @@ import sys
|
||||
import action
|
||||
|
||||
class Project:
|
||||
name = ''
|
||||
status = 'active'
|
||||
def __init__(self, status = ''):
|
||||
self.id = 0
|
||||
self.name = ''
|
||||
self.status = status
|
||||
|
||||
def __str__(self):
|
||||
return str(self.__dict__)
|
||||
|
||||
def handle_project(args, last_action, conn):
|
||||
logging.info('> handle project')
|
||||
@@ -33,24 +37,84 @@ def handle_project(args, last_action, conn):
|
||||
print('missing project name')
|
||||
sys.exit(1)
|
||||
|
||||
project = Project()
|
||||
project.name = arg_string(args[1], 'project name')
|
||||
project = Project('active')
|
||||
project.name = get_string_arg(args[1:], 'project name')
|
||||
parse_project_args(project, args[2:])
|
||||
create_project(project, conn)
|
||||
elif args[0] == '-e':
|
||||
print('TODO Update project')
|
||||
if len(args) == 1:
|
||||
print('nothing to update')
|
||||
sys.exit(1)
|
||||
|
||||
project = Project()
|
||||
project.id = last_action.project_id
|
||||
|
||||
if args[1].isdigit():
|
||||
if int(args[1]) == project.id:
|
||||
print('nothing to update')
|
||||
sys.exit(1)
|
||||
project.id = int(args[1])
|
||||
parse_project_args(project, args[2:])
|
||||
else:
|
||||
parse_project_args(project, args[1:])
|
||||
logging.debug('Project: {}'.format(project))
|
||||
|
||||
edit_project(project, conn)
|
||||
# TODO To set as active project, us a -a option?
|
||||
|
||||
elif args[0] == '-d':
|
||||
if len(args) > 1:
|
||||
delete_project(arg_number(args[1]), conn)
|
||||
else:
|
||||
delete_project(last_action.project_id, conn)
|
||||
else:
|
||||
print('Nothing to do... And this is not normalous')
|
||||
|
||||
def parse_project_args(project, args):
|
||||
if not args:
|
||||
return
|
||||
|
||||
i = 0
|
||||
while i < len(args):
|
||||
logging.debug(args[i])
|
||||
|
||||
if args[i] == '-s':
|
||||
i += 1
|
||||
project.status = get_string_arg(args[i:i+1], 'project status')
|
||||
elif args[i] == '-n':
|
||||
i += 1
|
||||
project.name = get_string_arg(args[i:i+1], 'project name')
|
||||
else:
|
||||
print(f'Invalid project option: {args[i]}')
|
||||
sys.exit(1)
|
||||
i += 1
|
||||
|
||||
def get_string_arg(args, required):
|
||||
"""
|
||||
Take an array of element, take first element and return it stripped.
|
||||
If array is empty or None, print an error message with required and exit
|
||||
in error.
|
||||
|
||||
:param args: an array of arguments. Can be empty or None
|
||||
:param required: the message to print in no args (or None)
|
||||
:return: The arg stripped
|
||||
"""
|
||||
if not args:
|
||||
print(f'Missing {required}')
|
||||
sys.exit(1)
|
||||
|
||||
logging.debug(args)
|
||||
return args[0].strip()
|
||||
|
||||
# TODO Move this function
|
||||
def arg_string(arg, required = ''):
|
||||
# TODO To meld with get_string_args
|
||||
if required and arg.startswith('-'):
|
||||
print('required ', required)
|
||||
sys.exit('1')
|
||||
else:
|
||||
return arg.strip()
|
||||
|
||||
def arg_number(arg, required = ''):
|
||||
if not arg.isdigit():
|
||||
print('Invalid value') # TODO
|
||||
@@ -76,14 +140,14 @@ def create_project(project, conn):
|
||||
|
||||
print('created project {}: {} (status: {})'.format(project_id, project.name, project.status))
|
||||
|
||||
def edit_project(args, project_id, conn):
|
||||
def edit_project(project, conn):
|
||||
logging.info('>> Edit project')
|
||||
|
||||
update_args = {}
|
||||
if args.status:
|
||||
update_args['status'] = args.status
|
||||
if args.edit_name:
|
||||
update_args['name'] = args.edit_name
|
||||
if project.status:
|
||||
update_args['status'] = project.status
|
||||
if project.name:
|
||||
update_args['name'] = project.name
|
||||
|
||||
query = 'UPDATE project SET {} WHERE id = ?'
|
||||
query = query.format(', '.join("%s = '%s'" % (k, v) for k, v in update_args.items()))
|
||||
@@ -92,14 +156,15 @@ def edit_project(args, project_id, conn):
|
||||
cursor = conn.cursor()
|
||||
if update_args:
|
||||
logging.debug('Do a project update')
|
||||
cursor.execute(query, (project_id,))
|
||||
cursor.execute(query, (project.id,))
|
||||
|
||||
log_args = ', '.join("%s: '%s'" % (k, v) for k, v in update_args.items())
|
||||
print('updated project {}: ({})'.format(project_id, log_args))
|
||||
print('updated project {}: ({})'.format(project.id, log_args))
|
||||
else:
|
||||
print('updated project {}: set active project')
|
||||
print('updated project {}: set as active project'.format(project.id))
|
||||
# TODO If not project ID to set active
|
||||
|
||||
action.create_action(cursor, project_id, message = 'update ' + str(update_args))
|
||||
action.create_action(cursor, project.id, message = 'update ' + str(update_args))
|
||||
|
||||
def delete_project(project_id, conn):
|
||||
logging.info('>> Remove project')
|
||||
@@ -113,12 +178,14 @@ def delete_project(project_id, conn):
|
||||
cursor.execute(query, (project_id,))
|
||||
if cursor.rowcount != 1:
|
||||
logging.error('DELETE FAILED')
|
||||
print('deleted project {}: {}'.format(project_id, 'project_name'))
|
||||
print('could not find current project')
|
||||
else:
|
||||
print('deleted project {}: {}'.format(project_id, 'project_name'))
|
||||
|
||||
def list_project(active_project_id, conn):
|
||||
logging.info('>> No arguments')
|
||||
query = """
|
||||
SELECT id, username, name, status, created_at,
|
||||
SELECT id, username, name, status,
|
||||
(SELECT count(*) FROM task WHERE task.project_id = project.id)
|
||||
FROM project;
|
||||
"""
|
||||
@@ -127,7 +194,7 @@ def list_project(active_project_id, conn):
|
||||
cursor.execute(query)
|
||||
for row in cursor.fetchall():
|
||||
logging.debug('Project row: {}'.format(row))
|
||||
project_id, username, name, status, date, nb_task = row
|
||||
project_id, username, name, status, nb_task = row
|
||||
print('{:1} {:2d}: ({:8}) | {} | {} ({} tasks)'.format('*' if active_project_id == project_id else '',
|
||||
project_id, username, status, name, nb_task))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user