120 lines
3.5 KiB
Python
120 lines
3.5 KiB
Python
# import sqlite3
|
|
import os
|
|
import argparse
|
|
import json
|
|
import getpass
|
|
import datetime
|
|
import logging
|
|
|
|
import action
|
|
|
|
def handle_task(args, last_action, conn):
|
|
logging.info('> handle task')
|
|
logging.debug('args: ' + str(args))
|
|
|
|
logging.debug('Last action: {}'.format(last_action))
|
|
|
|
do_something = False
|
|
|
|
if args.create_name:
|
|
do_something = True
|
|
create_task(args, last_action.project_id, conn)
|
|
|
|
if args.delete_id:
|
|
do_something = True
|
|
delete_task(args.delete_id, conn)
|
|
|
|
if args.moving_id:
|
|
do_something = True
|
|
moving_task(args)
|
|
|
|
if args.edit_id:
|
|
do_something = True
|
|
edit_task(args, args.edit_id, conn)
|
|
|
|
if not do_something:
|
|
list_task(last_action.project_id, last_action.task_id, conn)
|
|
|
|
def create_task(args, active_project_id, conn):
|
|
# 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')
|
|
|
|
query = """
|
|
INSERT INTO
|
|
task (project_id, username, name, status, priority, date, time, created_at)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?);
|
|
"""
|
|
|
|
status = 'open'
|
|
priority = 'normal'
|
|
# TODO Date & time
|
|
date = ''
|
|
time = ''
|
|
|
|
cursor = conn.cursor()
|
|
cursor.execute(query, (active_project_id, getpass.getuser(), args.create_name, status, priority, date, time,
|
|
datetime.datetime.now(),))
|
|
|
|
task_id = cursor.lastrowid
|
|
|
|
logging.debug('CREATE ACTION ' + str(active_project_id) + str(task_id) )
|
|
action.create_action(cursor, project_id=active_project_id, task_id=task_id)
|
|
|
|
print('created task {}: {} (status: {})'.format(task_id, args.create_name, status))
|
|
|
|
def edit_task(args, task_id, conn):
|
|
logging.info('>> Edit task')
|
|
|
|
update_args = {}
|
|
if args.status:
|
|
update_args['status'] = args.status
|
|
if args.edit_name:
|
|
update_args['name'] = args.edit_name
|
|
|
|
query = 'UPDATE task SET {} WHERE id = ?'
|
|
query = query.format(', '.join("%s = '%s'" % (k, v) for k, v in update_args.items()))
|
|
logging.debug('update task query: ' + query)
|
|
|
|
cursor = conn.cursor()
|
|
if update_args:
|
|
logging.debug('Do a task update')
|
|
cursor.execute(query, (task_id,))
|
|
|
|
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')
|
|
|
|
action.create_action(cursor, task_id, message = 'update ' + str(update_args))
|
|
|
|
def moving_task(args):
|
|
print('moving')
|
|
|
|
def delete_task(task_id, conn):
|
|
logging.info('>> Remove task')
|
|
|
|
query = """
|
|
DELETE FROM task
|
|
WHERE id = ?;
|
|
"""
|
|
|
|
cursor = conn.cursor()
|
|
cursor.execute(query, (task_id,))
|
|
if cursor.rowcount != 1:
|
|
logging.error('DELETE FAILED')
|
|
print('deleted task {}: {}'.format(task_id, 'task_name'))
|
|
|
|
def list_task(active_project_id, active_task_id, conn):
|
|
logging.info('>> No arguments')
|
|
query = "SELECT id, username, status, priority, name FROM task WHERE project_id = ?;" #TODO Date & time
|
|
|
|
cursor = conn.cursor()
|
|
cursor.execute(query, (active_project_id,))
|
|
for row in cursor.fetchall():
|
|
nb_note = 0
|
|
task_id, username, status, priority, name = row
|
|
print('{:1} {:2d}: ({:8}) | {} | {} | {} ({} tasks )'.format('*' if active_task_id == task_id else '', task_id,
|
|
username, status, priority, name, nb_note))
|
|
|
|
# 2: (budd) |open| |normal| test (0 notes)
|
|
# * 3: (budd) |open| |normal| Dec 31, 2018 tet (0 notes) |