102 lines
3.3 KiB
Python
102 lines
3.3 KiB
Python
import sqlite3
|
|
import os
|
|
import argparse
|
|
import json
|
|
import getpass
|
|
import datetime
|
|
import logging
|
|
import action
|
|
|
|
db_filename = 'pits.db'
|
|
schema_filename = 'pits_schema.sql'
|
|
|
|
def handle_project(args, last_action):
|
|
logging.info('> handle project')
|
|
logging.debug('args: args')
|
|
|
|
status = 'active'
|
|
if args.status:
|
|
status = args.status
|
|
|
|
do_something = False
|
|
|
|
if args.create_name:
|
|
do_something = True
|
|
# TODO Project is same name is forbidden
|
|
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:
|
|
do_something = True
|
|
logging.info('>> Remove project')
|
|
|
|
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:
|
|
project_id = args.edit_id
|
|
do_something = True
|
|
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))
|
|
|
|
if not do_something:
|
|
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 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
|
|
#created project 3: toto (status: active) |