Split project class
This commit is contained in:
154
project.py
154
project.py
@@ -12,91 +12,97 @@ schema_filename = 'pits_schema.sql'
|
|||||||
|
|
||||||
def handle_project(args, last_action):
|
def handle_project(args, last_action):
|
||||||
logging.info('> handle project')
|
logging.info('> handle project')
|
||||||
logging.debug('args: args')
|
logging.debug('args: ' + str(args))
|
||||||
|
|
||||||
status = 'active'
|
|
||||||
if args.status:
|
|
||||||
status = args.status
|
|
||||||
|
|
||||||
do_something = False
|
do_something = False
|
||||||
|
|
||||||
if args.create_name:
|
if args.create_name:
|
||||||
do_something = True
|
do_something = True
|
||||||
# TODO Project is same name is forbidden
|
create_project(args)
|
||||||
logging.info('>> Create project')
|
|
||||||
|
|
||||||
query = """
|
|
||||||
INSERT INTO project (username, name, status, created_at)
|
|
||||||
VALUES (?, ?, ?, ?);
|
|
||||||
"""
|
|
||||||
|
|
||||||
with sqlite3.connect(db_filename) as conn:
|
|
||||||
cursor = conn.cursor()
|
|
||||||
cursor.execute(query, (getpass.getuser(), args.create_name, status, datetime.datetime.now(),))
|
|
||||||
|
|
||||||
project_id = cursor.lastrowid
|
|
||||||
|
|
||||||
action.create_action(cursor, project_id=project_id)
|
|
||||||
|
|
||||||
print('created project {}: {} (status: {})'.format(project_id, args.create_name, status))
|
|
||||||
|
|
||||||
if args.delete_id:
|
if args.delete_id:
|
||||||
do_something = True
|
do_something = True
|
||||||
logging.info('>> Remove project')
|
delete_project(args.delete_id)
|
||||||
|
|
||||||
query = """
|
|
||||||
DELETE FROM project
|
|
||||||
WHERE id = ?;
|
|
||||||
"""
|
|
||||||
|
|
||||||
with sqlite3.connect(db_filename) as conn:
|
|
||||||
cursor = conn.cursor()
|
|
||||||
cursor.execute(query, (args.delete_id,))
|
|
||||||
if cursor.rowcount != 1:
|
|
||||||
logging.error('DELETE FAILED')
|
|
||||||
print('deleted project {}: {}'.format(args.delete_id, 'project_name'))
|
|
||||||
|
|
||||||
if args.edit_id:
|
if args.edit_id:
|
||||||
project_id = args.edit_id
|
|
||||||
do_something = True
|
do_something = True
|
||||||
logging.info('>> Edit project')
|
edit_project(args, args.edit_id)
|
||||||
|
|
||||||
update_args = {}
|
|
||||||
if args.status:
|
|
||||||
update_args['status'] = args.status
|
|
||||||
if args.edit_name:
|
|
||||||
update_args['name'] = args.edit_name
|
|
||||||
|
|
||||||
query = 'UPDATE project SET {} WHERE id = ?'
|
|
||||||
query = query.format(', '.join("%s = '%s'" % (k, v) for k, v in update_args.items()))
|
|
||||||
logging.debug('update project query: ' + query)
|
|
||||||
|
|
||||||
with sqlite3.connect(db_filename) as conn:
|
|
||||||
cursor = conn.cursor()
|
|
||||||
if update_args:
|
|
||||||
logging.debug('Do a project update')
|
|
||||||
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))
|
|
||||||
else:
|
|
||||||
print('updated project {}: set active project')
|
|
||||||
|
|
||||||
action.create_action(cursor, project_id, message = 'update ' + str(update_args))
|
|
||||||
|
|
||||||
if not do_something:
|
if not do_something:
|
||||||
logging.info('>> No arguments')
|
list_project(last_action[0])
|
||||||
query = "SELECT id, username, name, status, created_at FROM project;"
|
|
||||||
with sqlite3.connect(db_filename) as conn:
|
|
||||||
cursor = conn.cursor()
|
|
||||||
cursor.execute(query)
|
|
||||||
for row in cursor.fetchall():
|
|
||||||
project_id, username, name, status, date = row
|
|
||||||
nb_task = 0
|
|
||||||
current_project = ' '
|
|
||||||
if last_action and last_action[0] == project_id: current_project = '*'
|
|
||||||
print('{} {:2d}: ({:16}) | {} | {} ({} tasks )'.format(current_project, project_id, username, status, name, nb_task))
|
|
||||||
# TODO Print creation date
|
|
||||||
|
|
||||||
# desired output
|
def create_project(args):
|
||||||
#created project 3: toto (status: active)
|
# TODO Project is same name is forbidden
|
||||||
|
logging.info('>> Create project')
|
||||||
|
|
||||||
|
query = """
|
||||||
|
INSERT INTO project (username, name, status, created_at)
|
||||||
|
VALUES (?, ?, ?, ?);
|
||||||
|
"""
|
||||||
|
|
||||||
|
status = 'active'
|
||||||
|
if args.status: status = args.status
|
||||||
|
|
||||||
|
with sqlite3.connect(db_filename) as conn:
|
||||||
|
cursor = conn.cursor()
|
||||||
|
cursor.execute(query, (getpass.getuser(), args.create_name, status, datetime.datetime.now(),))
|
||||||
|
|
||||||
|
project_id = cursor.lastrowid
|
||||||
|
|
||||||
|
action.create_action(cursor, project_id=project_id)
|
||||||
|
|
||||||
|
print('created project {}: {} (status: {})'.format(project_id, args.create_name, status))
|
||||||
|
|
||||||
|
def edit_project(args, project_id):
|
||||||
|
logging.info('>> Edit project')
|
||||||
|
|
||||||
|
update_args = {}
|
||||||
|
if args.status:
|
||||||
|
update_args['status'] = args.status
|
||||||
|
if args.edit_name:
|
||||||
|
update_args['name'] = args.edit_name
|
||||||
|
|
||||||
|
query = 'UPDATE project SET {} WHERE id = ?'
|
||||||
|
query = query.format(', '.join("%s = '%s'" % (k, v) for k, v in update_args.items()))
|
||||||
|
logging.debug('update project query: ' + query)
|
||||||
|
|
||||||
|
with sqlite3.connect(db_filename) as conn:
|
||||||
|
cursor = conn.cursor()
|
||||||
|
if update_args:
|
||||||
|
logging.debug('Do a project update')
|
||||||
|
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))
|
||||||
|
else:
|
||||||
|
print('updated project {}: set active project')
|
||||||
|
|
||||||
|
action.create_action(cursor, project_id, message = 'update ' + str(update_args))
|
||||||
|
|
||||||
|
def delete_project(project_id):
|
||||||
|
logging.info('>> Remove project')
|
||||||
|
|
||||||
|
query = """
|
||||||
|
DELETE FROM project
|
||||||
|
WHERE id = ?;
|
||||||
|
"""
|
||||||
|
|
||||||
|
with sqlite3.connect(db_filename) as conn:
|
||||||
|
cursor = conn.cursor()
|
||||||
|
cursor.execute(query, (project_id,))
|
||||||
|
if cursor.rowcount != 1:
|
||||||
|
logging.error('DELETE FAILED')
|
||||||
|
print('deleted project {}: {}'.format(project_id, 'project_name'))
|
||||||
|
|
||||||
|
def list_project(active_project_id):
|
||||||
|
logging.info('>> No arguments')
|
||||||
|
query = "SELECT id, username, name, status, created_at FROM project;"
|
||||||
|
with sqlite3.connect(db_filename) as conn:
|
||||||
|
cursor = conn.cursor()
|
||||||
|
cursor.execute(query)
|
||||||
|
for row in cursor.fetchall():
|
||||||
|
project_id, username, name, status, date = row
|
||||||
|
nb_task = 0
|
||||||
|
current_project = ''
|
||||||
|
if active_project_id and active_project_id == project_id: current_project = '*'
|
||||||
|
print('{:1} {:2d}: ({:16}) | {} | {} ({} tasks )'.format(current_project, project_id, username, status, name, nb_task))
|
||||||
Reference in New Issue
Block a user