Share same connection
This commit is contained in:
83
project.py
83
project.py
@@ -1,16 +1,14 @@
|
||||
import sqlite3
|
||||
# import sqlite3
|
||||
import os
|
||||
import argparse
|
||||
import json
|
||||
import getpass
|
||||
import datetime
|
||||
import logging
|
||||
|
||||
import action
|
||||
|
||||
db_filename = 'pits.db'
|
||||
schema_filename = 'pits_schema.sql'
|
||||
|
||||
def handle_project(args, last_action):
|
||||
def handle_project(args, last_action, conn):
|
||||
logging.info('> handle project')
|
||||
logging.debug('args: ' + str(args))
|
||||
|
||||
@@ -18,23 +16,23 @@ def handle_project(args, last_action):
|
||||
|
||||
if args.create_name:
|
||||
do_something = True
|
||||
create_project(args)
|
||||
create_project(args, conn)
|
||||
|
||||
if args.delete_id:
|
||||
do_something = True
|
||||
delete_project(args.delete_id)
|
||||
delete_project(args.delete_id, conn)
|
||||
|
||||
if args.edit_id:
|
||||
do_something = True
|
||||
edit_project(args, args.edit_id)
|
||||
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)
|
||||
list_project(active_project_id, conn)
|
||||
|
||||
def create_project(args):
|
||||
def create_project(args, conn):
|
||||
# TODO Project is same name is forbidden
|
||||
logging.info('>> Create project')
|
||||
|
||||
@@ -46,17 +44,16 @@ def create_project(args):
|
||||
status = 'active'
|
||||
if args.status: status = args.status
|
||||
|
||||
with sqlite3.connect(db_filename) as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(query, (getpass.getuser(), args.create_name, status, datetime.datetime.now(),))
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(query, (getpass.getuser(), args.create_name, status, datetime.datetime.now(),))
|
||||
|
||||
project_id = cursor.lastrowid
|
||||
project_id = cursor.lastrowid
|
||||
|
||||
action.create_action(cursor, project_id=project_id)
|
||||
action.create_action(cursor, project_id=project_id)
|
||||
|
||||
print('created project {}: {} (status: {})'.format(project_id, args.create_name, status))
|
||||
print('created project {}: {} (status: {})'.format(project_id, args.create_name, status))
|
||||
|
||||
def edit_project(args, project_id):
|
||||
def edit_project(args, project_id, conn):
|
||||
logging.info('>> Edit project')
|
||||
|
||||
update_args = {}
|
||||
@@ -69,20 +66,19 @@ def edit_project(args, project_id):
|
||||
query = query.format(', '.join("%s = '%s'" % (k, v) for k, v in update_args.items()))
|
||||
logging.debug('update project query: ' + query)
|
||||
|
||||
with sqlite3.connect(db_filename) as conn:
|
||||
cursor = conn.cursor()
|
||||
if update_args:
|
||||
logging.debug('Do a project update')
|
||||
cursor.execute(query, (project_id,))
|
||||
cursor = conn.cursor()
|
||||
if update_args:
|
||||
logging.debug('Do a project update')
|
||||
cursor.execute(query, (project_id,))
|
||||
|
||||
log_args = ', '.join("%s: '%s'" % (k, v) for k, v in update_args.items())
|
||||
print('updated project {}: ({})'.format(project_id, log_args))
|
||||
else:
|
||||
print('updated project {}: set active project')
|
||||
log_args = ', '.join("%s: '%s'" % (k, v) for k, v in update_args.items())
|
||||
print('updated project {}: ({})'.format(project_id, log_args))
|
||||
else:
|
||||
print('updated project {}: set active project')
|
||||
|
||||
action.create_action(cursor, project_id, message = 'update ' + str(update_args))
|
||||
action.create_action(cursor, project_id, message = 'update ' + str(update_args))
|
||||
|
||||
def delete_project(project_id):
|
||||
def delete_project(project_id, conn):
|
||||
logging.info('>> Remove project')
|
||||
|
||||
query = """
|
||||
@@ -90,22 +86,21 @@ def delete_project(project_id):
|
||||
WHERE id = ?;
|
||||
"""
|
||||
|
||||
with sqlite3.connect(db_filename) as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(query, (project_id,))
|
||||
if cursor.rowcount != 1:
|
||||
logging.error('DELETE FAILED')
|
||||
print('deleted project {}: {}'.format(project_id, 'project_name'))
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(query, (project_id,))
|
||||
if cursor.rowcount != 1:
|
||||
logging.error('DELETE FAILED')
|
||||
print('deleted project {}: {}'.format(project_id, 'project_name'))
|
||||
|
||||
def list_project(active_project_id):
|
||||
def list_project(active_project_id, conn):
|
||||
logging.info('>> No arguments')
|
||||
query = "SELECT id, username, name, status, created_at 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, date = row
|
||||
nb_task = 0
|
||||
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))
|
||||
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(query)
|
||||
for row in cursor.fetchall():
|
||||
project_id, username, name, status, date = row
|
||||
nb_task = 0
|
||||
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))
|
||||
Reference in New Issue
Block a user