# -*- coding: utf-8 -*- import json from odoo import api, fields, models from odoo.addons.sf_wxwork_approval.models import wx_work_api class WxSettings(models.Model): _name = 'wxwork.settings' _description = '企业微信设置' wx_work_app = fields.Char(string='企业微信应用名称') wx_work_corp_id = fields.Char(string='企业微信CorpID') wx_work_secret = fields.Char(string='企业微信Secret') wx_work_template_ids = fields.Many2many('wxwork.template.settings', string='审批模板') def update_template(self): template_dict = {} template_list = [] print(self.wx_work_template_ids) for template in self.wx_work_template_ids: # template_dict[template.sp_name] = template.template_id template_list.append(template.template_id) # template_dict = {} # print(template_list) wx = wx_work_api.WxWorkAPI(self.wx_work_corp_id, self.wx_work_secret) # print(wx) # 获取模型中的所有记录 all_records = self.env['wxwork.approval.template'].search([]) all_records1 = self.env['wxwork.approval.template.controls'].search([]) # 删除所有记录 all_records.unlink() all_records1.unlink() for template in template_list: print('====================') temp_dict = {'template_id': template} # print(json.dumps(temp_dict)) # aa = json.dumps(temp_dict) template_detail = wx.get_template_detail(temp_dict) # print('template_detail%s' % template_detail) # # # 准备用于创建记录的字段值 # values = { # 'process_code': template, # 'name': template_detail['template_names'][0]['text'], # # 如果需要设置content_ids,请按照正确格式添加关联数据 # } # # 使用create方法创建记录 # new_record = self.env['wxwork.approval.template'].create(values) # 解析template_detail中的内容数据 content_data_list = [] # print('jjjjjjjj', template_detail['template_content']) # print('jjjjjjjjjj', template_detail['template_content']['controls']) for control_data in template_detail['template_content']['controls']: print('control_data', control_data) property_data = control_data['property'] config_values = {} # print(property_data) # 准备用于创建记录的字段值 property_values = { 'control_type': property_data.get('control', ''), 'unique_control_id': property_data.get('id', ''), 'title': property_data['title'][0]['text'] if 'title' in property_data else '', 'placeholder': property_data['placeholder'][0]['text'] if 'placeholder' in property_data else '', 'require': property_data.get('require', None), 'un_print': property_data.get('un_print', None), 'un_replace': property_data.get('un_replace', None), 'display': property_data.get('display', None), # 'un_replace': template_detail['template_names'][0]['text'], # 如果需要设置content_ids,请按照正确格式添加关联数据 } # print('property_values', property_values) # config_data = None if 'config' in control_data: config_data = control_data['config'] # print('config_data===', config_data) if '"date":' in config_data: # config_values = { # 'date_type': config_data.get('type', ''), # } config_values['date_type'] = config_data.get('type', '') if '"selector":' in config_data: # config_values = { # 'selector_type': config_data.get('type', ''), # # 'options': config_data['title'][0]['text'] if 'title' in property_data else '', # } config_values['selector_type'] = config_data.get('type', '') if '"contact":' in config_data: # config_values = { # 'contact_type': config_data.get('type', ''), # 'contact_mode': config_data.get('mode', ''), # # } config_values['contact_type'] = config_data.get('type', '') config_values['contact_mode'] = config_data.get('mode', '') if '"table":' in config_data: pass # config_values = { # 'table_children': config_data.get('un_print', None), # } if '"attendance":' in config_data: pass # config_values = { # 'attendance_type': config_data.get('un_replace', None), # 'attendance_date_range_type': config_data.get('display', None), # # 如果需要设置content_ids,请按照正确格式添加关联数据 # } if '"vacation_list":' in config_data: pass # config_values = { # 'vacation_list': config_data['template_names'][0]['text'], # # 如果需要设置content_ids,请按照正确格式添加关联数据 # } content_data_list.append((0, 0, { 'property_id': [(0, 0, property_values)], 'config_id': [(0, 0, config_values)], })) else: content_data_list.append((0, 0, { 'property_id': [(0, 0, property_values)], # 'config_id': [(0, 0, config_values)], })) # 准备用于创建记录的字段值,包括content_ids数据 values = { 'process_code': template, 'name': template_detail['template_names'][0]['text'], 'content_ids': content_data_list, } # 使用create方法创建记录 new_record = self.env['wxwork.approval.template'].create(values) class WxTemplateSettings(models.Model): _name = 'wxwork.template.settings' _description = '企业微信模板设置' sp_name = fields.Char(string='审批模板名称') template_id = fields.Char(string='审批模板ID') # wxwork_id = fields.Many2one('wxwork.settings', string='企业微信')