class Client(AppModel): def __init__(self): log.info('Client::__init__') self.dbconn = DboMySQL() self.table = 'clients' self._fields = self._getFields() def findByHash(self, hash): log.info('Client::findByHash') log.debug(hash) data = { 'client_hash': hash } data = self._quote(data) log.debug(data) rows = self.dbconn.query(""" SELECT * FROM %s WHERE client_hash = %s LIMIT 1 """ % ( self.table, data.get('client_hash') ) ) if not rows is None and len(rows) > 0: return rows[0] return None def addClient(self, data): log.info('Client::addClient') log.debug(data) data = self._quote(data) return self.dbconn.insert(""" INSERT INTO %s (user_id, client_hash, client, created, modified) VALUES (%s, %s, %s, NOW(), NOW()) """ % ( self.table, data.get('user_id'), data.get('client_hash'), data.get('client')) )
class Transaction(AppModel): def __init__(self): log.info('Transaction::__init__') self.dbconn = DboMySQL() self.table = 'transactions' self._fields = self._getFields() def create(self, data): log.info('Transaction::create') log.debug(data) data = self._quote(data) return self.dbconn.insert(""" INSERT INTO %s (`user_id`, `entity_id`, `client_id`, `ticket`, `started`, `action`, `created`, `modified`) VALUES (%s, %s, %s, %s, NOW(), %s, NOW(), NOW()) """ % ( self.table, data.get('user_id'), data.get('entity_id'), data.get('client_id'), data.get('ticket'), data.get('action') ) ) def removeUncompleted(self, user_id, client_id): log.info('Transaction::removeUncompleted') data = { 'user_id': user_id, 'client_id': client_id } data = self._quote(data) log.debug(data) return self.dbconn.query(""" DELETE Transaction, Entity FROM transactions as Transaction LEFT JOIN entities as Entity ON Entity.`transaction_id` = Transaction.id WHERE Entity.user_id = %s AND Transaction.user_id = %s AND Transaction.client_id = %s AND NOW() > DATE_ADD(Transaction.started, INTERVAL 1 DAY) AND Transaction.finished IS NULL """ % ( data.get('user_id'), data.get('user_id'), data.get('client_id') ) ) def getUnCompleted(self, user_id, client_id): log.info('Transaction::getUnCompleted') data = { 'user_id': user_id, 'client_id': client_id } data = self._quote(data) log.debug(data) rows = self.dbconn.query(""" SELECT Entity.id as 'Entity.id', Entity.user_id as 'Entity.user_id', Entity.transaction_id as 'Entity.transaction_id', Entity.path as 'Entity.path', Entity.size as 'Entity.size', Entity.filedate as 'Entity.filedate', Entity.hash as 'Entity.hash', Entity.stored as 'Entity.stored', Entity.created as 'Entity.created', Entity.modified as 'Entity.modified', Entity.deleted as 'Entity.deleted', Transaction.user_id as 'Transaction.user_id', Transaction.client_id as 'Transaction.client_id', Transaction.ticket as 'Transaction.ticket', Transaction.started as 'Transaction.started', Transaction.finished as 'Transaction.finished', Transaction.action as 'Transaction.action', Transaction.created as 'Transaction.created', Transaction.modified as 'Transaction.modified', Transaction.deleted as 'Transaction.deleted' FROM %s as Transaction LEFT JOIN entities as Entity ON Entity.`transaction_id` = Transaction.id WHERE Entity.user_id = %s AND Transaction.user_id = %s AND Transaction.client_id = %s AND #NOW() < DATE_ADD(Transaction.started, INTERVAL 1 DAY) AND Transaction.finished IS NULL #GROUP BY Transaction.entity_id #GROUP BY Entity.path ORDER BY Transaction.started ASC """ % ( self.table, data.get('user_id'), data.get('user_id'), data.get('client_id') ) ) return rows def prepareToClient(self, rows): log.info('Transaction::prepareToClient') log.debug(rows) entities = [] for row in rows: try: row['Entity.size'] = int(row['Entity.size']) except Exception, e: row['Entity.size'] = 0 entity = { 'path': row['Entity.path'], 'modified': Datetime.utcformat_datetime(row['Entity.filedate']), 'size':row['Entity.size'], 'hash': row['Entity.hash'], 'action': self.__actionToStr(row['Transaction.action']), 'ticket': row['Transaction.ticket'] } entities.append(entity) return entities