# -*- 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