diff --git a/note.py b/note.py new file mode 100644 index 0000000..4253601 --- /dev/null +++ b/note.py @@ -0,0 +1,102 @@ +# import sqlite3 +import os +import argparse +import json +import getpass +import datetime +import logging + +import action + +def handle_note(args, last_action, conn): + logging.info('> handle note') + logging.debug('args: ' + str(args)) + + logging.debug('Last action: {}'.format(last_action)) + + do_something = False + + if args.create_name: + do_something = True + create_note(args, last_action.project_id, last_action.task_id, conn) + + if args.delete_id: + do_something = True + delete_note(args.delete_id, conn) + + if args.edit_id: + do_something = True + edit_note(args, args.edit_id, conn) + + if not do_something: + list_note(last_action.project_id, last_action.task_id, last_action.note_id, conn) + +def create_note(args, active_project_id, active_task_id, conn): + # TODO Don't create note if no project (nothing selected?) ==> foreign key is not a solution: there is no project ID :) + logging.info('>> Create note') + + query = """ + INSERT INTO note (project_id, task_id, username, message) + VALUES (?, ?, ?, ?); + """ + + cursor = conn.cursor() + cursor.execute(query, (active_project_id, active_task_id, getpass.getuser(), args.create_name)) + + note_id = cursor.lastrowid + + action.create_action(cursor, project_id=active_project_id, task_id = active_task_id, note_id=note_id) + + print('created note {}: {} (task {})'.format(note_id, args.create_name, active_task_id)) + +def edit_note(args, note_id, conn): + logging.info('>> Edit note') + + update_args = {} + if args.status: + update_args['status'] = args.status + if args.edit_name: + update_args['name'] = args.edit_name + + query = 'UPDATE note SET {} WHERE id = ?' + query = query.format(', '.join("%s = '%s'" % (k, v) for k, v in update_args.items())) + logging.debug('update note query: ' + query) + + cursor = conn.cursor() + if update_args: + logging.debug('Do a note update') + cursor.execute(query, (note_id,)) + + log_args = ', '.join("%s: '%s'" % (k, v) for k, v in update_args.items()) + print('updated note {}: ({})'.format(note_id, log_args)) + else: + print('updated note {}: set active note') + + action.create_action(cursor, note_id, message = 'update ' + str(update_args)) + +def delete_note(note_id, conn): + logging.info('>> Remove note') + + query = """ + DELETE FROM note + WHERE id = ?; + """ + + cursor = conn.cursor() + cursor.execute(query, (note_id,)) + if cursor.rowcount != 1: + logging.error('DELETE FAILED') + print('deleted note {}: {}'.format(note_id, 'note_name')) + +def list_note(active_project_id, active_task_id, active_note_id, conn): + logging.info('>> No arguments') + query = "SELECT id, username, message FROM note WHERE project_id = ? AND task_id = ?;" #TODO Date & time + + cursor = conn.cursor() + cursor.execute(query, (active_project_id, active_task_id)) + for row in cursor.fetchall(): + note_id, username, message = row + print('{:1} {:2d}: ({:8}) {}'.format('*' if active_note_id == note_id else '', note_id, username, message)) + + # 2: (budd) |open| |normal| test (0 notes) + # * 3: (budd) |open| |normal| Dec 31, 2018 tet (0 notes) \ No newline at end of file diff --git a/pits.py b/pits.py index df8d6db..eb34980 100644 --- a/pits.py +++ b/pits.py @@ -9,6 +9,7 @@ import logging import project import action import task +import note # logging.basicConfig(level=logging.DEBUG, format='%(asctime)s :: %(levelname)s :: %(message)s') logging.basicConfig(level=logging.DEBUG, format='%(levelname)7s :: %(message)s') @@ -88,10 +89,11 @@ parser_task.add_argument('-i', type=int, dest='project_id', metavar='project id' parser_task.set_defaults(func=task.handle_task) parser_note = subparsers.add_parser('note') -parser_note.add_argument('-c', type=str) -parser_note.add_argument('-e', type=int) -parser_note.add_argument('-d', type=int) -parser_note.set_defaults(func=handle_note) +group_note = parser_note.add_mutually_exclusive_group() +group_note.add_argument('-c', type=str, dest='create_name', metavar='name', help='name of task') +group_note.add_argument('-e', type=int, dest='edit_id', metavar='number', help='Edit a task') +group_note.add_argument('-d', type=int, dest='delete_id', nargs='?', const=-1, metavar='number', help='Delete a task') +parser_note.set_defaults(func=note.handle_note) parser_action = subparsers.add_parser('action') parser_action.set_defaults(func=handle_action)