From 76356cbd8430d461562f9ebb08deb4095d3d533f Mon Sep 17 00:00:00 2001 From: "Maxence G. de Montauzan" Date: Wed, 4 Jul 2018 21:17:52 +0200 Subject: [PATCH] Improve how to retrieve & handle actions --- action.py | 18 ++++++++++++++---- project.py | 10 +++------- task.py | 28 +++++++++++----------------- 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/action.py b/action.py index 2938496..e7344e2 100644 --- a/action.py +++ b/action.py @@ -7,6 +7,18 @@ import datetime import logging import action +class Action: + def __init__(self, row): + if not row: + logging.debug('Action - empty row') + raise Exception('No action found - database need to be initialized.') + else: + logging.debug('Action - fill fields') + self.project_id, self.task_id, self.note_id = row + + def __str__(self): + return str(self.__dict__) + def create_action(cursor, project_id = '', task_id = '', note_id = '', message = ''): query = """ INSERT INTO action (project_id, task_id, note_id, username, message, created_at) @@ -21,11 +33,9 @@ def read_last_action(conn): logging.info('> last_action') # query = 'SELECT * FROM action WHERE id = (SELECT MAX(id) FROM action' # query = 'SELECT project_id, task_id, note_id FROM action' - query = 'SELECT project_id, task_id, note_id FROM action ORDER BY created_at DESC LIMIT 1;' + query = 'SELECT project_id, task_id, note_id FROM action ORDER BY id DESC LIMIT 1;' cursor = conn.cursor() cursor.execute(query) - logging.debug('DEEEEEEEEBU') - last_action = cursor.fetchone() - logging.debug(last_action) + last_action = Action(cursor.fetchone()) return last_action \ No newline at end of file diff --git a/project.py b/project.py index 4773398..305fe6f 100644 --- a/project.py +++ b/project.py @@ -27,10 +27,7 @@ def handle_project(args, last_action, conn): edit_project(args, args.edit_id, conn) if not do_something: - active_project_id = None - if last_action: - active_project_id = last_action[0] - list_project(active_project_id, conn) + list_project(last_action.project_id, conn) def create_project(args, conn): # TODO Project is same name is forbidden @@ -104,6 +101,5 @@ def list_project(active_project_id, conn): cursor.execute(query) for row in cursor.fetchall(): project_id, username, name, status, date, nb_task = row - current_project = '' - if active_project_id and active_project_id == project_id: current_project = '*' - print('{:1} {:2d}: ({:8}) | {} | {} ({} tasks)'.format(current_project, project_id, username, status, name, nb_task)) \ No newline at end of file + print('{:1} {:2d}: ({:8}) | {} | {} ({} tasks)'.format('*' if active_project_id == project_id else '', + project_id, username, status, name, nb_task)) \ No newline at end of file diff --git a/task.py b/task.py index a25b25b..dad82d4 100644 --- a/task.py +++ b/task.py @@ -12,21 +12,13 @@ def handle_task(args, last_action, conn): logging.info('> handle task') logging.debug('args: ' + str(args)) - active_project_id = None - if last_action: - active_project_id = last_action[0] - - active_task_id = None - if last_action: - active_task_id = last_action[1] - - logging.debug('Last action, project id: {} | task id: {}'.format(active_project_id, active_task_id)) + logging.debug('Last action: {}'.format(last_action)) do_something = False if args.create_name: do_something = True - create_task(args, active_project_id, conn) + create_task(args, last_action.project_id, conn) if args.delete_id: do_something = True @@ -35,19 +27,21 @@ def handle_task(args, last_action, 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(active_project_id, active_task_id, conn) + 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) + INSERT INTO + task (project_id, username, name, status, priority, date, time, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?); """ @@ -58,7 +52,8 @@ def create_task(args, active_project_id, conn): time = '' cursor = conn.cursor() - cursor.execute(query, (active_project_id, getpass.getuser(), args.create_name, status, priority, date, time, datetime.datetime.now(),)) + cursor.execute(query, (active_project_id, getpass.getuser(), args.create_name, status, priority, date, time, + datetime.datetime.now(),)) task_id = cursor.lastrowid @@ -118,9 +113,8 @@ def list_task(active_project_id, active_task_id, conn): for row in cursor.fetchall(): nb_note = 0 task_id, username, status, priority, name = row - current_task = '' - if active_task_id and active_task_id == task_id: current_task = '*' - print('{:1} {:2d}: ({:8}) | {} | {} | {} ({} tasks )'.format(current_task, task_id, username, status, priority, name, nb_note)) + 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) \ No newline at end of file