Files
ppit/action.py
Maxence G. de Montauzan 09036fe283 Logging + DB connection + add init action
I hope script could be used anywhere in filesystem
2018-07-09 01:35:05 +02:00

85 lines
2.9 KiB
Python

import sqlite3
import os
import argparse
import json
import getpass
import datetime
import logging
from datetime import datetime
from enum import Enum
import sys
class TypeAction(Enum):
CREATE = 'created'
UPDATE = 'updated'
MOVE = 'moved'
class Action:
def __init__(self, row):
if not row:
logging.debug('Action - empty row')
# TODO
# 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 handle_action(args, last_action, conn):
logging.info('>> handle action')
query = 'SELECT * FROM action;'
cursor = conn.cursor()
cursor.execute(query)
for row in cursor.fetchall():
log_id, project_id, task_id, note_id, username, taction, message, created_at = row
logging.debug(row)
formated_date = datetime.strptime(created_at[:26], '%Y-%m-%d %H:%M:%S.%f').strftime('%b %d, %Y %H:%M')
if taction == 'init':
print('{} ({}): {}'.format(formated_date, username, message))
sys.exit(0)
object_type = ''
id_object = ''
object_type = '' # TODO Enum ?
if (project_id):
object_type = 'project'
id_object = project_id
if (task_id):
object_type = 'task'
id_object = task_id
if (note_id):
object_type = 'note'
id_object = note_id
print('{} ({}): {} {} {}: {}'.format(formated_date, username, taction, object_type, id_object, message))
# print('Sep 02, 2017 02:33 (dakota ): updated task 1: Passage à Angular 4 (status: in progress)')
# Sep 02, 2017 02:33 (dakota ): updated task 1: Passage à Angular 4 (status: in progress)
# Jun 13, 2018 01:55 (edelweiss ): updated note 67: (message: Fix anonymize when get a replica with ID [task 30, status:in progress]
# Jul 05, 2018 00:40 (budd): moved task 2: from project 1 to project 2
def create_action(cursor, project_id = '', task_id = '', note_id = '', message = '', action = TypeAction.CREATE):
query = """
INSERT INTO action (project_id, task_id, note_id, username, action, message, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?);
"""
cursor.execute(query, (project_id, task_id, note_id, getpass.getuser(), action.value, message, datetime.now(),))
logging.debug('created action')
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 id DESC LIMIT 1;'
cursor = conn.cursor()
cursor.execute(query)
last_action = Action(cursor.fetchone())
return last_action