Files
ppit/action.py
Maxence G. de Montauzan c0ece7a11c WIP Work on action
Very restrictive - use a 'current' table
2018-07-11 01:14:17 +02:00

88 lines
3.1 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'
DELETE = 'deleted'
ACTIVE = 'activated'
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():
_, 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))
elif taction == TypeAction.ACTIVE.value:
print('{} ({}): {}'.format(formated_date, username, message))
else:
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 record_action(cursor, action_type, message, project_id = '', task_id = None, note_id = None):
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_type.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