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
}
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: 新的树形视图结构
"""
new_dict_obj = xmltodict.parse(xml_str, encoding='UTF-8')
json_str = json.dumps(new_dict_obj, ensure_ascii=False)
jsons = json.loads(json_str)
tree_json = jsons.get('tree')
export_xlsx_attr = tree_json.get('@export_xlsx', False)
if not export_xlsx_attr:
tree_json.update({'@export_xlsx': 0})
new_xml_str = xmltodict.unparse(jsons)
new_xml_str = new_xml_str.replace('<?xml version="1.0" encoding="utf-8"?>', '')
return new_xml_str