DTCLOUD之树形列表批量更改

# -*- coding: utf-8 -*-
# Part of dtcloud. See LICENSE file for full copyright and licensing details.

from dtcloud import models, tools, _, api
import xmltodict
import json


class BaseModel(models.AbstractModel):
    _inherit = 'base'

    @api.model
    def load_views(self, views, options=None):
        """ Returns the fields_views of given views, along with the fields of
            the current model, and optionally its filters for the given action.

        :param views: list of [view_id, view_type]
        :param options['toolbar']: True to include contextual actions when loading fields_views
        :param options['load_filters']: True to return the model's filters
        :param options['action_id']: id of the action to get the filters
        :return: dictionary with fields_views, fields and optionally filters
        """
        options = options or {}
        result = {}
        toolbar = options.get('toolbar')
        result['fields_views'] = {
            v_type: self.fields_view_get(v_id, v_type if v_type != 'list' else 'tree',
                                         toolbar=toolbar if v_type != 'search' else False)
            for [v_id, v_type] in views
        }
        # TEL TODO 树形列表视图批量更改
        xml_str = result.get('fields_views').get('list').get('arch')
        new_xml_str = self.update_arch_result(xml_str)
        result.get('fields_views').get('list').update({'arch': new_xml_str})

        result['fields'] = self.fields_get()
        if options.get('load_filters'):
            result['filters'] = self.env['ir.filters'].get_filters(self._name, options.get('action_id'))
        return result

    def update_arch_result(self, xml_str):
        """
        :param xml_str: str树形视图
        :return: 新的树形视图结构
        """
        # 返回一个OrderedDict类型的对象
        new_dict_obj = xmltodict.parse(xml_str, encoding='UTF-8')
        # 使用内置的json模块转换成json字符串
        json_str = json.dumps(new_dict_obj, ensure_ascii=False)
        # 转换Json格式
        jsons = json.loads(json_str)
        # Json格式
        tree_json = jsons.get('tree')
        export_xlsx_attr = tree_json.get('@export_xlsx', False)
        # 不存在export_xlsx属性则更新
        if not export_xlsx_attr:
            tree_json.update({'@export_xlsx': 0})
        # 将以更改好Json数据重新解析为字符串
        new_xml_str = xmltodict.unparse(jsons)
        # 替换固定表头
        new_xml_str = new_xml_str.replace('<?xml version="1.0" encoding="utf-8"?>', '')
        return new_xml_str
posted @   中亿丰数字科技  阅读(14)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
点击右上角即可分享
微信分享提示