197 lines
5.6 KiB
Python
197 lines
5.6 KiB
Python
# import sqlite3
|
|
import os
|
|
import argparse
|
|
import json
|
|
import getpass
|
|
import datetime
|
|
import logging
|
|
import sys
|
|
|
|
import action
|
|
|
|
class Project:
|
|
def __init__(self, status = None):
|
|
self.name = None
|
|
self.status = status
|
|
|
|
def __str__(self):
|
|
return str(self.__dict__)
|
|
|
|
def handle_project(args, last_action, conn):
|
|
logging.info('> handle project')
|
|
logging.debug('args: ' + str(args))
|
|
|
|
if not args:
|
|
list_project(last_action.project_id, conn)
|
|
sys.exit(0)
|
|
|
|
if args[0].isdigit():
|
|
view_project()
|
|
sys.exit(0)
|
|
|
|
# Use sys.exit when no add/update in Database
|
|
|
|
if args[0] == '-c':
|
|
if len(args) == 1:
|
|
print('missing project name')
|
|
sys.exit(1)
|
|
|
|
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':
|
|
if len(args) == 1:
|
|
print('nothing to update')
|
|
sys.exit(1)
|
|
|
|
project = Project()
|
|
|
|
if args[1].isdigit():
|
|
project_id = int(args[1])
|
|
parse_project_args(project, args[2:])
|
|
else:
|
|
project_id = last_action.project_id
|
|
parse_project_args(project, args[1:])
|
|
logging.debug('Project: ({}) {}'.format(project_id, project))
|
|
|
|
edit_project(project, project_id, 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
|
|
sys.exit('1')
|
|
|
|
return int(arg)
|
|
|
|
def create_project(project, conn):
|
|
# TODO Project is same name is forbidden
|
|
logging.info('>> Create project')
|
|
|
|
query = """
|
|
INSERT INTO project (username, name, status, created_at)
|
|
VALUES (?, ?, ?, ?);
|
|
"""
|
|
|
|
cursor = conn.cursor()
|
|
cursor.execute(query, (getpass.getuser(), project.name, project.status, datetime.datetime.now(),))
|
|
project_id = cursor.lastrowid
|
|
action_message = '{} (status: {})'.format(project.name, project.status)
|
|
action.create_action(cursor, project_id=project_id, message=action_message)
|
|
logging.debug(action_message)
|
|
|
|
print('created project {}: {} (status: {})'.format(project_id, project.name, project.status))
|
|
|
|
def edit_project(project, project_id, conn):
|
|
logging.info('>> Edit project')
|
|
|
|
update_args = [item for item in project.__dict__.items() if item[1] is not None]
|
|
|
|
logging.debug('Project update args: {}'.format(update_args))
|
|
|
|
query = 'UPDATE project SET {} WHERE id = ?'
|
|
query = query.format(', '.join("%s = '%s'" % (k, v) for k, v in update_args))
|
|
logging.debug('update project query: ' + query)
|
|
|
|
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)
|
|
print('updated project {}: ({})'.format(project_id, log_args))
|
|
else:
|
|
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))
|
|
|
|
def delete_project(project_id, conn):
|
|
logging.info('>> Remove project')
|
|
|
|
query = """
|
|
DELETE FROM project
|
|
WHERE id = ?;
|
|
"""
|
|
|
|
cursor = conn.cursor()
|
|
cursor.execute(query, (project_id,))
|
|
if cursor.rowcount != 1:
|
|
logging.error('DELETE FAILED')
|
|
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,
|
|
(SELECT count(*) FROM task WHERE task.project_id = project.id)
|
|
FROM project;
|
|
"""
|
|
|
|
cursor = conn.cursor()
|
|
cursor.execute(query)
|
|
for row in cursor.fetchall():
|
|
logging.debug('Project row: {}'.format(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))
|
|
|
|
def view_project():
|
|
print("TODO View Project")
|
|
#TODO |