def execute_import(filename, connection, separator=',', transaction=False, error_stop=False): """ Read the file, and launched import_data """ model = filename.split('/').pop().replace('.csv', '') if model.find('_') > 4: model = model[model.find('-') + 1:model.find('_')] else: model = model[model.find('-') + 1:] logger.debug('model: %s' % model) obj = Object(connection, model) logger.info('Read and analyse the file content') fp = open(filename, 'r') header = False lines = [] count = 0 reader = csv.reader(fp, delimiter=separator) for line in reader: count += 1 if not header: header = line logger.debug('header: %s' % str(header)) continue lines.append(line) logger.debug('line: %s' % str(line)) logger.info('Read the file content is finished') logger.info('Start import the content in OpenERP (%d datas)' % len(lines)) count = 0 ctx = { 'defer_parent_store_computation': True, 'lang': opts.lang, 'import': True, } if opts.inactive: ctx['active_test'] = False print ctx if transaction: try: logger.info('Import %s lines in one transaction' % len(lines)) res = obj.import_data(header, lines, 'init', '', False, ctx) if res[0] == -1: logger.error('%s' % res[2]) logger.error('%s' % str(res[1])) logger.info('End transaction import') except Exception, e: logger.error(str(e)) if error_stop: raise StopError(str(e))
def execute_import(filename, connection, separator=',', transaction=False, error_stop=False): """ Read the file, and launched import_data """ model = filename.split('/').pop().replace('.csv', '') if model.find('_') > 4: model = model[model.find('-') + 1:model.find('_')] else: model = model[model.find('-') + 1:] logger.debug('model: %s' % model) obj = Object(connection, model) logger.info('Read and analyse the file content') fp = open(filename, 'r') header = False lines = [] count = 0 reader = csv.reader(fp, delimiter=separator) for line in reader: count += 1 if not header: header = line logger.debug('header: %s' % str(header)) continue lines.append(line) logger.debug('line: %s' %str(line)) logger.info('Read the file content is finished') logger.info('Start import the content in OpenERP (%d datas)' % len(lines)) count = 0 ctx = { 'defer_parent_store_computation': True, 'lang': opts.lang, 'import': True, } if opts.inactive: ctx['active_test'] = False print ctx if transaction: try: logger.info('Import %s lines in one transaction' % len(lines)) res = obj.import_data(header, lines, 'init', '', False, ctx) if res[0] == -1: logger.error('%s' % res[2]) logger.error('%s' % str(res[1])) logger.info('End transaction import') except Exception, e: logger.error(str(e)) if error_stop: raise StopError(str(e))
def release(self, path, fh): """ Writing of the file is finished, import the contents into OpenERP """ # FIXME : Don't know why it doesn't work without rebuilding the StringIO object... value = StringIO(self.files[path].getvalue()) # Parse the CSV file contents csvFile = csv.reader(value) lines = list(csvFile) # Import data into OpenERP model = path.replace('.csv', '')[1:] oerpObject = Object(self.oerp_connection, model) oerpObject.import_data(lines[0], lines[1:], 'init', '', False, {'import': True}) # Close StringIO and free memory self.files[path].close() del self.files[path] value.close() del value return True