首页 > 其他分享 >odoo 文件导入流程

odoo 文件导入流程

时间:2023-01-09 00:22:44浏览次数:29  
标签:matches 流程 self headers 导入 file odoo preview options

文件上传到表中base_import.import

@http.route('/base_import/set_file', methods=['POST'])
    def set_file(self, file, import_id, jsonp='callback'):
        import_id = int(import_id)

        written = request.env['base_import.import'].browse(import_id).write({
            'file': file.read(),
            'file_name': file.filename,
            'file_type': file.content_type,
        })

        return 'window.top.%s(%s)' % (misc.html_escape(jsonp), json.dumps({'result': written}))

执行文件解析,前端调用触发

    @api.multi
    def parse_preview(self, options, count=10):
        """ Generates a preview of the uploaded files, and performs
            fields-matching between the import's file data and the model's
            columns.

            If the headers are not requested (not options.headers),
            ``matches`` and ``headers`` are both ``False``.

            :param int count: number of preview lines to generate
            :param options: format-specific options.
                            CSV: {quoting, separator, headers}
            :type options: {str, str, str, bool}
            :returns: {fields, matches, headers, preview} | {error, preview}
            :rtype: {dict(str: dict(...)), dict(int, list(str)), list(str), list(list(str))} | {str, str}
        """
        self.ensure_one()
        fields = self.get_fields(self.res_model)
        try:
            rows = self._read_file(options)
            headers, matches = self._match_headers(rows, fields, options)
            # Match should have consumed the first row (iif headers), get
            # the ``count`` next rows for preview
            preview = list(itertools.islice(rows, count))
            assert preview, "file seems to have no content"
            header_types = self._find_type_from_preview(options, preview)
            if options.get('keep_matches') and len(options.get('fields', [])):
                matches = {}
                for index, match in enumerate(options.get('fields')):
                    if match:
                        matches[index] = match.split('/')

            if options.get('keep_matches'):
                advanced_mode = options.get('advanced')
            else:
                # Check is label contain relational field
                has_relational_header = any(len(models.fix_import_export_id_paths(col)) > 1 for col in headers)
                # Check is matches fields have relational field
                has_relational_match = any(len(match) > 1 for field, match in matches.items() if match)
                advanced_mode = has_relational_header or has_relational_match

            return {
                'fields': fields,
                'matches': matches or False,
                'headers': headers or False,
                'headers_type': header_types or False,
                'preview': preview,
                'options': options,
                'advanced_mode': advanced_mode,
                'debug': self.user_has_groups('base.group_no_one'),
            }
        except Exception as error:
            # Due to lazy generators, UnicodeDecodeError (for
            # instance) may only be raised when serializing the
            # preview to a list in the return.
            _logger.debug("Error during parsing preview", exc_info=True)
            preview = None
            if self.file_type == 'text/csv' and self.file:
                preview = self.file[:ERROR_PREVIEW_BYTES].decode('iso-8859-1')
            return {
                'error': str(error),
                # iso-8859-1 ensures decoding will always succeed,
                # even if it yields non-printable characters. This is
                # in case of UnicodeDecodeError (or csv.Error
                # compounded with UnicodeDecodeError)
                'preview': preview,
            }

标签:matches,流程,self,headers,导入,file,odoo,preview,options
From: https://www.cnblogs.com/qianxunman/p/17035811.html

相关文章

  • Odoo 美化登录界面
    实践环境Odoo14.0-20221212(CommunityEdition)OdooWebLoginScreen14.0https://apps.odoo.com/apps/modules/14.0/odoo_web_login/#操作步骤1、把下载的odoowe......
  • 2.3JS中的流程控制
    ​  基本和JAVA中的一致顺序结构略 分支结构if switch  <!DOCTYPEhtml><html><head><metacharset="UTF-8">......
  • 2.3JS中的流程控制
    ​  基本和JAVA中的一致顺序结构略 分支结构if switch  <!DOCTYPEhtml><html><head><metacharset="UTF-8">......
  • 3.流程控制语句
    1.已知点的坐标(x,y),判断其所在的象限。x=int(input("请输入x的坐标:"))y=int(input("请输入y的坐标:"))ifx>0andy>0:print("第一象限")elifx<0<......
  • 接入jira OAuth权限流程
    如果要在自己的系统中操作jira的api完成这些单据的创建、审批等操作,就不得不要先完成jira的第三方授权,才能在第三方系统去做这些jira的操作。首先必须在jira系统配置客......
  • lsposed运行流程分析
    xposed适用的最高版本为android8.0,针对高版本的ARTHOOK框架可以使用比较有名的lsposed。它使用了lsplantARTHOOK框架(早期使用YAHFA)并提供了和xposed一样的接口API与其......
  • 解决scikit-learn中导入数据的问题
    在学习sklearn时,使用boston房价数据时,使用如下代码获取数据:1、无法导入数据,我这里版本是1.2,具体:importpandasaspdimportnumpyasnpimportsklearnfromsk......
  • Java 流程控制
    Java流程控制用户交互Scannerjava.util.Scanner是Java5的特征Scanner类是用于获取用户的输入通过Scanner类的next()和nextLine()方法获取输入的字符串读取前需要使......
  • 04-流程控制+循环
    (一):流程控制介绍流程控制主要有三种结构,分别是顺序结构、分支结构和循环结构,这三种结构代表三种代码执行的顺序顺序流程控制顺序结构是程序中最简单的、最基本的流......
  • Golang入门到实战核心编程-第五章-程序流程控制
    目录1.程序流程控制1.1程序流程个控制介绍1.2三大流程控制介绍及案例1.2.1顺序流程控制1.2.1.1顺利流程控制介绍1.2.1.2顺序流程控制流程图1.2.1.3顺序流程控制案例......