Start to handle project
Beginning of configuration
This commit is contained in:
47
pit_schema.sql
Normal file
47
pit_schema.sql
Normal file
@@ -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?
|
||||||
|
);
|
||||||
126
pits.py
126
pits.py
@@ -1,50 +1,96 @@
|
|||||||
import sqlite3
|
import sqlite3
|
||||||
import os
|
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)
|
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:
|
if db_is_new:
|
||||||
conn.execute('''CREATE TABLE project (
|
print('Creating schema')
|
||||||
id int,
|
with open(schema_filename, 'rt') as f:
|
||||||
username text,
|
schema = f.read()
|
||||||
name text,
|
conn.executescript(schema)
|
||||||
status text,
|
|
||||||
created_at timestring,
|
|
||||||
updatet_at timestring
|
|
||||||
)''')
|
|
||||||
|
|
||||||
conn.execute('''CREATE TABLE task (
|
print('Inserting initial data')
|
||||||
id int,
|
conn.executescript("""
|
||||||
project_id int,
|
insert into project values(1, 'me', 'test', 'in progress', 'now', 'now')""")
|
||||||
username text,
|
|
||||||
name text,
|
|
||||||
status text,
|
|
||||||
priority text,
|
|
||||||
date timestring,
|
|
||||||
time timestring,
|
|
||||||
created_at timestring,
|
|
||||||
updated_at timestring
|
|
||||||
)''')
|
|
||||||
|
|
||||||
conn.execute('''CREATE TABLE note (
|
parser = argparse.ArgumentParser()
|
||||||
id int,
|
subparsers = parser.add_subparsers()
|
||||||
project_id int,
|
|
||||||
task_id int,
|
|
||||||
username text,
|
|
||||||
message text,
|
|
||||||
created_at timestring,
|
|
||||||
updated_at timestring
|
|
||||||
)''')
|
|
||||||
|
|
||||||
conn.execute('''CREATE TABLE action (
|
parser_note = subparsers.add_parser('project')#, usage='toto')
|
||||||
project_id int,
|
parser_note.add_argument('-c', type=str, dest='create_name', metavar='name', help='name of project')
|
||||||
task_id int,
|
parser_note.add_argument('-e', type=int, dest='edit_id', metavar='number', help='Edit a project')
|
||||||
note_id int,
|
parser_note.add_argument('-d', type=int, dest='delete_id', nargs='?', const=-1, metavar='number', help='Delete a project')
|
||||||
username text,
|
parser_note.add_argument('-s', type=str, dest='status', metavar='status')
|
||||||
message text,
|
parser_note.add_argument('-q', type=int)
|
||||||
created_at timestring
|
parser_note.set_defaults(func=handle_project)
|
||||||
)''')
|
|
||||||
|
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user