def run_task(self): # TODO: add log / pk source = {} #data = {} fields = [] # group fields by table for i in self.task_action_ids: if not source.has_key(i.source): source[i.source] = [] source[i.source].append(i) for source, actions in source.items(): if source.source_type == 'csv': db = coredb.DB(source.source_type, data=source.data) conn = db.connect() rows = db.get_rows() for row in rows.dicts(): # d = {model: {data fields}} d = {} for action in actions: if not d.has_key(action.odoo_model.model): d[action.odoo_model.model] = {} d[action.odoo_model.model][action.odoo_field_id.name] = row[action.field_id.name] for model, data in d.items(): self.env[model].create(data) elif source.source_type in ['pymssql', 'psycopg2']: # get tables and fields tables = {} for action in actions: if not tables.has_key(action.table): tables[action.table.name] = [] tables[action.table.name].append(action.field_id.name) # connect with database db = coredb.DB(source.source_type) conn = db.connect(source.host, source.port, source.database, source.username, source.password) if not conn: print 'ERROR' # get rows from tables for table, fields in tables.items(): rows = db.get_rows(table=table, fields=fields) for row in rows: d = {} for action in actions: if not d.has_key(action.odoo_model.model): d[action.odoo_model.model] = {} d[action.odoo_model.model][action.odoo_field_id.name] = row[action.field_id.name] for model, data in d.items(): print model, data self.env[model].create(data)
def refresh(self): db = coredb.DB(self.source_type, data=self.data) conn = db.connect(self.host, self.port, self.database, self.username, self.password) if not conn: raise Exception("I can't connect with this source: %s" % self.name) for table in self.table_ids: print 'Refreshing %s...' % table.name data = { 'rows': db.refresh(table.name) } print data table.write(data)
def _get_rows_related(self): if self.env['etileno.field'].search_count([('table_id', '=', self.id)]) <= 10: fields = [i.name for i in self.env['etileno.field'].search([('table_id', '=', self.id)])] else: fields = [i.name for i in self.env['etileno.field'].search([('table_id', '=', self.id), ('visible', '=', True)])] if fields: # TODO: to keep simple connections db = coredb.DB(self.source_id.source_type) conn = db.connect(self.source_id.host, self.source_id.port, self.source_id.database, self.source_id.username, self.source_id.password) rows = db.get_data(self.name, fields) # add column name #data = dict([(F[i], fields[i]) for i in xrange(len(fields))]) #self.row_ids |= self.env['etileno.row'].create(data) # add data rows for row in rows: data = {} for i in xrange(len(fields)): data[F[i]] = row[fields[i]] self.row_ids |= self.env['etileno.row'].create(data)
def search_match(self): rows = {} # get codes from source db = coredb.DB(self.source.source_type, data=self.source.data) conn = db.connect(self.source.host, self.source.port, self.source.database, self.source.username, self.source.password) if not conn: raise Exception("I can't connect with this source: %s" % self.name) rows_source = db.get_rows(table=self.table_id.name, fields=self.table_id.pks.split(',') + [self.field_match_id.name]) for row in rows_source: #print '>>', row if rows.has_key(row[-1]) or row[-1] == None: if self.test: raise Exception("Code repeated at source: %s with id %s" % (row[-1], ','.join(map(str, row[0:-1])))) else: continue rows[row[-1]] = { 'id_source': ','.join(map(str, row[0:-1])) } # get codes from odoo rows_odoo = self.env[self.odoo_match_id.model_id.model].search([]) for row in rows_odoo: #if not rows.has_key(getattr(row, self.odoo_match_id.name)): # if self.test: # raise Exception("Code repeated at odoo: %s" % getattr(row, self.odoo_match_id.name)) # else: # print 'continue' # continue rows[getattr(row, self.odoo_match_id.name)]['id_odoo'] = str(row.id) codes = dict([i.code, i] for i in self.code_ids) for k, v in rows.items(): if not codes.has_key(k): data = { 'translate_id': self.id, 'code': k, 'id_source': v['id_source'], 'id_odoo': v['id_odoo'] } codes['k'] = self.env['etileno.table.translate.code'].create(data)
def introspection(self): db = coredb.DB(self.source_type, data=self.data) conn = db.connect(self.host, self.port, self.database, self.username, self.password) tables = db.show_tables() table = self.env['etileno.table'] for k,v in tables.items(): print v # get fields fields = [] for i in v['fields']: fields.append((0,0, { 'name': i[0], 'field_type': i[1], 'pk': i[0] in v['pk'] and (v['pk'].index(i[0]) + 1) })) # create table and fields data = { 'source_id': self.id, 'name': k, 'rows': v['count'], 'field_ids': fields } table.create(data)
def verify_connection(self): db = coredb.DB(self.source_type, data=self.data) conn = db.connect(self.host, self.port, self.database, self.username, self.password) if conn: self.state = 'verified'
def run_task(self): # TODO: add log / pk source = {} #data = {} fields = [] # group fields by table for i in self.task_action_ids: if not source.has_key(i.source): source[i.source] = [] source[i.source].append(i) for source, actions in source.items(): if source.source_type == 'csv': db = coredb.DB(source.source_type, data=source.data) conn = db.connect() rows = db.get_rows() for row in rows.dicts(): # d = {model: {data fields}} d = {} for action in actions: if not d.has_key(action.odoo_model.model): d[action.odoo_model.model] = {} d[action.odoo_model.model][action.odoo_field_id.name] = row[action.field_id.name] for model, data in d.items(): self.env[model].create(data) elif source.source_type in ['pymssql', 'psycopg2']: # get tables and fields tables = {} for action in actions: if not tables.has_key(action.table.name): tables[action.table.name] = [] tables[action.table.name].append(action.field_id.name) # connect with database db = coredb.DB(source.source_type) conn = db.connect(source.host, source.port, source.database, source.username, source.password) if not conn: print 'ERROR' # get rows from tables for table, fields in tables.items(): #print table, fields rows = db.get_rows(table=table, fields=fields) for row in rows: #print row d = {} create = True for action in actions: # prepare data for create if action.action in ['c', 'cr']: if not d.has_key(action.odoo_model.model): d[action.odoo_model.model] = {} # checks if action.action == 'pk': # primary key field_pk = row[action.field_id.name] elif action.action == 'C': # check value print '>', row[action.field_id.name], action.action_value # TODO: cast action_value right if not row[action.field_id.name] == int(action.action_value): create = False elif action.action == 'c': # create d[action.odoo_model.model][action.odoo_field_id.name] = row[action.field_id.name] print '+++', create, d # create register if create: for model, data in d.items(): #print model, data pk_id = self.env[model].create(data) log_data = { 'task_id': self.id, 'table_row_pk': field_pk, 'model_row_pk': pk_id } self.env['etileno.task.log'].create(log_data)