From 197b2e38e51d3225feb1b01f262d055ea7d9dc79 Mon Sep 17 00:00:00 2001 From: "Maxence G. de Montauzan" Date: Thu, 14 Jun 2018 19:53:07 +0200 Subject: [PATCH] Start to handle project Beginning of configuration --- pit_schema.sql | 47 ++++++++++++++++++ pits.py | 126 +++++++++++++++++++++++++++++++++---------------- 2 files changed, 133 insertions(+), 40 deletions(-) create mode 100644 pit_schema.sql diff --git a/pit_schema.sql b/pit_schema.sql new file mode 100644 index 0000000..d34fbcd --- /dev/null +++ b/pit_schema.sql @@ -0,0 +1,47 @@ +-- schema.sql + +-- Schema for pit in python / SQLite mode +-- v0.1 + +CREATE TABLE project ( + id integer primary key, + username text, -- User the project belongs to. + name text, -- Project name. + status text, -- Project status. + --number_of_tasks integer, -- Number of tasks for the project. + created_at date, -- When the project was created? + updatet_at date -- When the project was last updated? +); + +CREATE TABLE task ( + id integer primary key, + project_id integer, -- Which project the task belongs to? + username text, -- User the task belongs to. + name text, -- Task name. + status text, -- Task status. + priority text, -- Task priority. + date date, -- Generic date/time, ex: task deadline. + time date, -- Generic time, ex: time spent on the task. + -- number_of_notes integer, -- Number of notes for the task. + created_at date, -- When the task was created? + updated_at date -- When the task was last updated? +); + +CREATE TABLE note ( + id integer primary key, + project_id integer, -- Project the note belongs to (0 if belongs to task). + task_id integer, -- Task the note belongs to (0 if belongs to project). + username text, -- User who created the note. + message text, -- The body of the note. + created_at date, -- When the note was created? + updated_at date -- When the note was last updated? +); + +CREATE TABLE action ( + project_id integer, -- Project id (always set). + task_id integer, -- Task id (set for task or note related actions). + note_id integer, -- Note id (set for note related actions only). + username text, -- Who added the log message? + message text, -- Log message. + created_at date -- When log message was added? +); diff --git a/pits.py b/pits.py index 0cdc439..2291196 100644 --- a/pits.py +++ b/pits.py @@ -1,50 +1,96 @@ import sqlite3 import os +import argparse +import json +import getpass -db_filename = 'pits.sql' +def info(): + print('info') + +def handle_project(args): + print('> handle project') + print(args) + if args.create_name: + # TODO Project is same name is forbidden + print('>> Create project') + + query = """ + INSERT INTO project (username, name, status) + VALUES (?, ?, 'active'); + """ + + with sqlite3.connect(db_filename) as conn: + cursor = conn.cursor() + cursor.execute(query, (getpass.getuser(), args.c,)) + + project_id = cursor.lastrowid + + print('created project {}: {} (status: {})'.format(project_id, args.c, 'active')) + if args.delete_id: + print('>> 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: + print('DELETE FAILED') + print('deleted project {}: {}'.format(args.delete_id, 'project_name')) + else: + print('>> No arguments') + query = "SELECT id, username, name, status 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 = row + nb_task = 0 + print('{:2d}: ({:16}) | {} | {} ({} tasks)'.format(project_id, username, status, name, nb_task)) + + # desired output + #created project 3: toto (status: active) + +def handle_note(args): + print('>> handle note') + +db_filename = 'pits.db' +schema_filename = 'pit_schema.sql' db_is_new = not os.path.exists(db_filename) -conn = sqlite3.connect(db_filename) +with sqlite3.connect(db_filename) as conn: + if db_is_new: + print('Creating schema') + with open(schema_filename, 'rt') as f: + schema = f.read() + conn.executescript(schema) -if db_is_new: - conn.execute('''CREATE TABLE project ( - id int, - username text, - name text, - status text, - created_at timestring, - updatet_at timestring - )''') + print('Inserting initial data') + conn.executescript(""" + insert into project values(1, 'me', 'test', 'in progress', 'now', 'now')""") - conn.execute('''CREATE TABLE task ( - id int, - project_id int, - username text, - name text, - status text, - priority text, - date timestring, - time timestring, - created_at timestring, - updated_at timestring - )''') +parser = argparse.ArgumentParser() +subparsers = parser.add_subparsers() - conn.execute('''CREATE TABLE note ( - id int, - project_id int, - task_id int, - username text, - message text, - created_at timestring, - updated_at timestring - )''') +parser_note = subparsers.add_parser('project')#, usage='toto') +parser_note.add_argument('-c', type=str, dest='create_name', metavar='name', help='name of project') +parser_note.add_argument('-e', type=int, dest='edit_id', metavar='number', help='Edit a project') +parser_note.add_argument('-d', type=int, dest='delete_id', nargs='?', const=-1, metavar='number', help='Delete a project') +parser_note.add_argument('-s', type=str, dest='status', metavar='status') +parser_note.add_argument('-q', type=int) +parser_note.set_defaults(func=handle_project) - conn.execute('''CREATE TABLE action ( - project_id int, - task_id int, - note_id int, - username text, - message text, - created_at timestring - )''') +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) + +parser.add_argument('--version', action='version', version='%(prog)s 1.0') + +args = parser.parse_args() +args.func(args)