def new_load(self, cr, uid, fields, data, context=None): import_data_obj = self.pool.get('smile.import_data') import_context = context.copy() if context else {} model_ids = self.pool.get('ir.model').search(cr, uid, [('model', '=', self._name)], context=context) if not model_ids: raise ValueError('Unknown model: %s' % (self._name, )) with RegistryManager.get(cr.dbname).cursor() as cr2: import_id = import_data_obj.create(cr2, uid, { 'import_fields': str(fields), 'model_id': model_ids[0] }, context) cr2.commit() logger = SmileDBLogger(cr.dbname, 'smile.import_data', import_id, uid) import_context['import_logger'] = logger import_context['import_line_number'] = 0 result = origin_load(self, cr, uid, fields, data, import_context) if not result['messages']: logger.info('Import completed after %s line(s)' % (import_context['import_line_number'], )) return result
def generate(self, cr, uid, ids, context=None): """ context used to specify export_mode export_mode can be: - same_thread_raise_error (default) - same_thread_rollback_and_continue - new_thread """ if isinstance(ids, (int, long)): ids = [ids] context = context or {} export_mode = context.get('export_mode', 'same_thread_rollback_and_continue') for export_id in ids: logger = SmileDBLogger(cr.dbname, 'ir.model.export', export_id, uid) if export_mode == 'new_thread': t = threading.Thread(target=self._generate_with_new_cursor, args=(cr.dbname, uid, export_id, logger, context)) t.start() else: cr.execute('SAVEPOINT smile_export') try: self._generate(cr, uid, export_id, logger, context) except Exception, e: if export_mode == 'same_thread_rollback_and_continue': cr.execute("ROLLBACK TO SAVEPOINT smile_export") logger.info("Export rollbacking") else: # same_thread_raise_error raise e
def unlink_res_ids(self, cr, uid, ids, model, res_ids, context): unlink_line_ids = [] for template in self.browse(cr, uid, ids, context): if template.model != model: raise except_orm( _('Error'), _("unlink_res_ids: model(%s) does not match template model (%s, %s)" ) % (model, template.id, template.model)) export_line_ids = self.pool.get('ir.model.export.line').search( cr, uid, [ ('export_id.export_tmpl_id', '=', template.id), ('res_id', 'in', res_ids), ], context=context) if export_line_ids: real_res_ids = [ line['res_id'] for line in self.pool.get('ir.model.export.line').read( cr, uid, export_line_ids, ['res_id'], context) ] logger = SmileDBLogger(cr.dbname, 'ir.model.export.template', template.id, uid) logger.info( 'Unlinking model:%s, res_ids: %s - real_res_ids found: %s' % (model, res_ids, real_res_ids)) self.pool.get('ir.model.export.line').unlink( cr, uid, export_line_ids, context) unlink_line_ids.extend(export_line_ids) return unlink_line_ids
def run(self, cr, uid, ids, context=None): context = context or {} if isinstance(ids, (int, long)): ids = [ids] intervention_obj = self.pool.get('smile.script.intervention') for script in self.browse(cr, uid, ids, context): if not context.get('test_mode'): if script.state != 'validated': raise orm.except_orm(_('Error!'), _('You can only run validated scripts!')) if script.automatic_dump: self.dump_database(cr) intervention_id = intervention_obj.create(cr, uid, {'script_id': script.id, 'test_mode': context.get('test_mode')}, context) logger = SmileDBLogger(cr.dbname, 'smile.script.intervention', intervention_id, uid) if not context.get('do_not_use_new_cursor'): intervention_cr = sql_db.db_connect(cr.dbname).cursor() else: intervention_cr = cr intervention_vals = {} try: _logger.info('Running script: %s\nCode:\n%s' % (script.name.encode('utf-8'), script.code.encode('utf-8'))) result = self._run(intervention_cr, uid, script, intervention_id, logger, context) if not context.get('do_not_use_new_cursor') and context.get('test_mode'): logger.info('TEST MODE: Script rollbacking') intervention_cr.rollback() elif not context.get('do_not_use_new_cursor'): intervention_cr.commit() intervention_vals.update({'state': 'done', 'result': result}) _logger.info('Script execution SUCCEEDED: %s\n' % (script.name.encode('utf-8'),)) except Exception, e: intervention_vals.update({'state': 'exception', 'result': _get_exception_message(e)}) _logger.error('Script execution FAILED: %s\nError:\n%s' % (script.name.encode('utf-8'), _get_exception_message(e).encode('utf-8'))) finally:
def create_import(self, cr, uid, ids, context=None): """ context used to specify test_mode and import_mode import_mode can be: - same_thread_raise_error (default) - same_thread_rollback_and_continue - new_thread returns a dict: {'template_id1': import_id1, ...} """ if isinstance(ids, (int, long)): ids = [ids] context = context or {} import_obj = self.pool.get('ir.model.import') import_name = context.get('import_name', '') test_mode = context.get('test_mode', False) import_mode = context.get('import_mode', 'same_thread_full_rollback') result = {} for template in self.browse(cr, uid, ids, context): import_name = import_name or template.name logger = SmileDBLogger(cr.dbname, 'ir.model.import.template', template.id, uid) import_id = import_obj.create_new_cr(cr.dbname, uid, { 'name': import_name, 'import_tmpl_id': template.id, 'test_mode': test_mode, 'pid': logger.pid, 'state': 'running', 'from_date': time.strftime('%Y-%m-%d %H:%M:%S'), }, context) result[template.id] = import_id if import_mode == 'new_thread': t = threading.Thread(target=import_obj._process_with_new_cursor, args=(cr.dbname, uid, import_id, logger, context)) t.start() else: cr.execute('SAVEPOINT smile_import') try: import_obj._process_import(cr, uid, import_id, logger, context) except Exception, e: if import_mode == 'same_thread_rollback_and_continue': cr.execute("ROLLBACK TO SAVEPOINT smile_import") logger.info("Import rollbacking") else: # same_thread_raise_error raise e
def run(self, cr, uid, ids, context=None): context = context or {} if isinstance(ids, (int, long)): ids = [ids] intervention_obj = self.pool.get('smile.script.intervention') for script in self.browse(cr, uid, ids, context): if not context.get('test_mode'): if script.state != 'validated': raise orm.except_orm( _('Error!'), _('You can only run validated scripts!')) if script.automatic_dump: self.dump_database(cr) intervention_id = intervention_obj.create( cr, uid, { 'script_id': script.id, 'test_mode': context.get('test_mode') }, context) logger = SmileDBLogger(cr.dbname, 'smile.script.intervention', intervention_id, uid) if not context.get('do_not_use_new_cursor'): intervention_cr = sql_db.db_connect(cr.dbname).cursor() else: intervention_cr = cr intervention_vals = {} try: _logger.info( 'Running script: %s\nCode:\n%s' % (script.name.encode('utf-8'), script.code.encode('utf-8'))) result = self._run(intervention_cr, uid, script, intervention_id, logger, context) if not context.get('do_not_use_new_cursor') and context.get( 'test_mode'): logger.info('TEST MODE: Script rollbacking') intervention_cr.rollback() elif not context.get('do_not_use_new_cursor'): intervention_cr.commit() intervention_vals.update({'state': 'done', 'result': result}) _logger.info('Script execution SUCCEEDED: %s\n' % (script.name.encode('utf-8'), )) except Exception, e: intervention_vals.update({ 'state': 'exception', 'result': _get_exception_message(e) }) _logger.error('Script execution FAILED: %s\nError:\n%s' % (script.name.encode('utf-8'), _get_exception_message(e).encode('utf-8'))) finally:
def unlink_res_ids(self, cr, uid, ids, model, res_ids, context): unlink_line_ids = [] for template in self.browse(cr, uid, ids, context): if template.model != model: raise osv.except_osv(_('Error'), _("unlink_res_ids: model(%s) does not match template model (%s, %s)") % (model, template.id, template.model)) export_line_ids = self.pool.get('ir.model.export.line').search(cr, uid, [('export_id.export_tmpl_id', '=', template.id), ('res_id', 'in', res_ids), ], context=context) if export_line_ids: real_res_ids = [line['res_id'] for line in self.pool.get('ir.model.export.line').read(cr, uid, export_line_ids, ['res_id'], context)] logger = SmileDBLogger(cr.dbname, 'ir.model.export.template', template.id, uid) logger.info('Unlinking model: %s, res_ids: %s - real_res_ids found: %s' % (model, res_ids, real_res_ids)) self.pool.get('ir.model.export.line').unlink(cr, uid, export_line_ids, context) unlink_line_ids.extend(export_line_ids) return unlink_line_ids
def new_load(self, cr, uid, fields, data, context=None): import_data_obj = self.pool.get('smile.import_data') import_context = context.copy() if context else {} model_ids = self.pool.get('ir.model').search(cr, uid, [('model', '=', self._name)], context=context) if not model_ids: raise ValueError('Unknown model: %s' % (self._name,)) with RegistryManager.get(cr.dbname).cursor() as cr2: import_id = import_data_obj.create(cr2, uid, {'import_fields': str(fields), 'model_id': model_ids[0]}, context) cr2.commit() logger = SmileDBLogger(cr.dbname, 'smile.import_data', import_id, uid) import_context['import_logger'] = logger import_context['import_line_number'] = 0 result = origin_load(self, cr, uid, fields, data, import_context) if not result['messages']: logger.info('Import completed after %s line(s)' % (import_context['import_line_number'],)) return result