Files
jikimo_sf/sf_wxwork_approval/models/wxwork_settings.py
2023-06-01 16:31:11 +08:00

260 lines
14 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- 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 = {}
# 创建一个字典包含wxwork.approval.template.controls和wxwork.approval.vacation.config记录的数据
template_data = {
'process_code': template,
'name': template_detail['template_names'][0]['text'],
'content_ids': [(0, 0, {
'property_id': [(0, 0, {
'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),
})],
'config_id': [(0, 0, {
'date_type': [(0, 0, {'date_type': 'day'})],
'selector_type': [(0, 0, {
'selector_type': 'single',
'options': [(0, 0, {'key': 'opt1', 'text': '选项1'})],
})],
'contact_type': [(0, 0, {'contact_type': 'single', 'contact_mode': 'user'})],
'table_type': [(0, 0, {
'table_children': [
(0, 0, {'control_type': 'Text', 'unique_control_id': 'text_2', 'title': '文本框2'})],
})],
'attendance_type': [(0, 0, {'attendance_type': '1', 'attendance_date_range_type': 'hour'})],
})],
})],
'vacation_config_id': [(0, 0, {
# 在这里添加wxwork.approval.vacation.config模型的字段数据
'vacation_id': 1,
'name': 'value2',
'lang': 'value2',
# ...
})],
}
# 调用create方法创建记录
template_record = self.env['wxwork.approval.template'].create(template_data)
# # 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)],
# }))
# # 准备用于创建记录的字段值
# 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),
# }
#
# if 'config' in control_data:
# config_data = control_data['config']
#
# # 根据config_data为config.model的One2many字段如options和table_children构建命令列表
# option_vals = [
# (0, 0, {'key': 'option_key_1', 'text': 'option_text_1', 'lang': 'zh_CN'}),
# (0, 0, {'key': 'option_key_2', 'text': 'option_text_2', 'lang': 'zh_CN'})
# ]
# vacation_vals = [{'key': 'vacation_key_1', 'text': 'vacation_text_1', 'lang': 'zh_CN'},
# {'key': 'vacation_key_2', 'text': 'vacation_text_2', 'lang': 'zh_CN'}]
# table_children_vals = [
# (0, 0, {
# 'control_type': 'Text',
# 'unique_control_id': 'control_id_1',
# 'title': '控件名称1',
# 'placeholder': '控件说明1',
# 'require': True
# }),
# (0, 0, {
# 'control_type': 'Number',
# 'unique_control_id': 'control_id_2',
# 'title': '控件名称2',
# 'placeholder': '控件说明2',
# 'require': False
# })
# ]
#
# # property_vals = [
# # (0, 0, {
# # 'control_type': 'Text',
# # 'unique_control_id': 'control_id_1',
# # 'title': '控件名称1',
# # 'placeholder': '控件说明1',
# # 'require': True
# # }),
# # (0, 0, {
# # 'control_type': 'Number',
# # 'unique_control_id': 'control_id_2',
# # 'title': '控件名称2',
# # 'placeholder': '控件说明2',
# # 'require': False
# # })
# # ]
#
# config_values = {
# 'date_type': config_data.get('type', '') if '"date":' in config_data else '',
# 'selector_type': config_data.get('type', '') if '"selector":' in config_data else '',
# 'contact_type': config_data.get('type', '') if '"contact":' in config_data else '',
# 'contact_mode': config_data.get('mode', '') if '"contact":' in config_data else '',
# 'options': option_vals if '"selector":' in config_data else [(6, 0, [])], # 使用示例中的option_vals
# 'table_children': table_children_vals if '"table":' in config_data else [(6, 0, [])],
# # 使用示例中的property_vals
# 'vacation_list': vacation_vals if '"vacation_list":' in config_data else [] # 添加vacation_vals
# }
#
# content_data_list.append((0, 0, {
# 'property_id': [(0, 0, property_values)],
# 'config_id': [(0, 0, config_values)] if 'config' in control_data else [(6, 0, [])],
# }))
#
# # 准备用于创建记录的字段值包括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='企业微信')