odoo导入功能二开

原来有的导入功能相信很多小伙伴对其功能不是很满意,不过没关系,我们可以二开啊,把它的功能改造成你想要的样子,接下来让我们看看怎么办吧

  • 例如我想把员工导入功能中添加上用户同步注册功能
  • 首先,我要找到原模块中导入时调用的模型——importUser.py文件
  • 找到这个文件之后打开 HtkjtImport 类,该类继承import基础模块

    class HtkjtImport(models.TransientModel):
        _inherit = "base_import.import"
    
  • 在该类下面找到 do 方法,将其重写
  • 当然咯,重写该方法或者其他方法都可以,不一定就选择这个
  • 代码风格规范一点的也可以自己新建一个方法,然后调用就可以了

    @api.multi
    def do(self, fields, columns, options, dryrun=False):

    self.ensure_one()
    self._cr.execute('SAVEPOINT import')
    
    try:
        data, import_fields = self._convert_import_data(fields, options)
        # Parse date and float field
        data = self._parse_import_data(data, import_fields, options)
    except ValueError as error:
        return {
            'messages': [{
                'type': 'error',
                'message': pycompat.text_type(error),
                'record': False,
            }]
        }
    
    _logger.info('importing %d rows...', len(data))
    
    name_create_enabled_fields = options.pop('name_create_enabled_fields', {})
    model = self.env[self.res_model].with_context(import_file=True,
                                                  name_create_enabled_fields=name_create_enabled_fields)
    import_result = model.load(import_fields, data)
    _logger.info('done')
    
    # If transaction aborted, RELEASE SAVEPOINT is going to raise
    # an InternalError (ROLLBACK should work, maybe). Ignore that.
    try:
        if dryrun:
            self._cr.execute('ROLLBACK TO SAVEPOINT import')
            # cancel all changes done to the registry/ormcache
            self.pool.reset_changes()
        else:
            self._cr.execute('RELEASE SAVEPOINT import')
    except psycopg2.InternalError:
        pass
    
    # 导入成功后更新插入映射列
    if import_result['ids'] and options.get('headers'):
        BaseImportMapping = self.env['base_import.mapping']
        for index, column_name in enumerate(columns):
            if column_name:
                # Update to latest selected field
                exist_records = BaseImportMapping.search(
                    [('res_model', '=', self.res_model), ('column_name', '=', column_name)])
                if exist_records:
                    exist_records.write({'field_name': fields[index]})
                else:
                    BaseImportMapping.create({
                        'res_model': self.res_model,
                        'column_name': column_name,
                        'field_name': fields[index]
                    })
    
    for da in data:
        print(da[1])
        print(da[3])
    
        data = {
            'name': da[1],
            'display_name': da[1],
            'email': da[3],
        }
        partner = self.env['res.partner'].create(data)
        print(partner.id)
    
        data = {
            'login': da[3],
            'password': 123456,
            'partner_id': partner.id,
        }
        user = self.env['res.users'].create(data)
    
    return import_result

你可能感兴趣的:(导入,odoo)