Files
ppit/task.py
2018-07-10 22:36:44 +02:00

142 lines
4.3 KiB
Python

# import sqlite3
import os
import argparse
import json
import getpass
import datetime
import logging
import sys
import action
from args import get_string_arg
class Task:
def __init__(self):
self.name = ''
self.status = 'open'
self.priority = 'normal'
def handle_task(args, last_action, conn):
logging.info('> handle task')
logging.debug('args: ' + str(args))
if not args:
list_task(last_action.project_id, last_action.task_id, conn)
sys.exit(0)
if args[0].isdigit():
view_task()
sys.exit(0)
if args[0] == '-c':
if len(args) == 1:
print('missing task name')
sys.exit(1)
task = Task()
task.name = project.arg_string(args[1], 'task name')
create_task(task, last_action.project_id, conn)
def create_task(task, 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 (?, ?, ?, ?, ?, ?, ?, ?);
"""
# TODO Date & time
date = ''
time = ''
cursor = conn.cursor()
cursor.execute(query, (active_project_id, getpass.getuser(), task.name, task.status,
task.priority, date, time, datetime.datetime.now(),))
task_id = cursor.lastrowid
logging.debug('CREATE ACTION ' + str(active_project_id) + str(task_id) )
action_message = '{} (status: {}, priority: {}, project: {})'.format(task.name,
task.status, task.priority, active_project_id)
# TODO if date/time/other => add to message
action.create_action(cursor, project_id=active_project_id, task_id=task_id, message=action_message)
print('created task {}: {} (status: {})'.format(task_id, task.name, task.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')
# TODO If not edit name... => retrieve name ?
# TODO Retrieve project id ?
action_message = '{} ({})'.format(args.edit_name, ', '.join("%s: %s" % (k, v) for k, v in update_args.items()))
logging.debug('UPDATE TASK - ACTION MESSAGE : ' + action_message)
action.create_action(cursor, task_id=task_id, action=action.TypeAction.UPDATE, message = action_message)
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, name, status, created_at,
(SELECT count(*) FROM task WHERE task.project_id = project.id)
FROM project;
"""
query = """
SELECT id, username, status, priority, name,
(SELECT count(*) FROM note WHERE note.task_id = task.id)
FROM task WHERE project_id = ?;
""" #TODO Date & time
cursor = conn.cursor()
cursor.execute(query, (active_project_id,))
for row in cursor.fetchall():
task_id, username, status, priority, name, nb_note = row
print('{:1} {:2d}: ({:8}) | {} | {} | {} ({} notes)'.format('*' if active_task_id == task_id else '', task_id,
username, status, priority, name, nb_note))
# TODO Note
# 2: (budd) |open| |normal| test (0 notes)
# * 3: (budd) |open| |normal| Dec 31, 2018 tet (0 notes)
def view_task():
print('TODO View Task')