同步企业微信审批模板~除”控件配置“外基本完成

This commit is contained in:
mgw
2023-05-31 18:13:12 +08:00
parent ce532a6b63
commit 48e0809bd7
6 changed files with 392 additions and 181 deletions

View File

@@ -5,5 +5,6 @@ from . import wxwork_approval_template_controls
from . import wxwork_settings
from . import wxwork_approval_template_summary
from . import we_approval_record
from . import wx_work_api
# from . import res_config_setting

View File

@@ -19,6 +19,17 @@ class WxWorkAPI:
else:
raise Exception(f"获取AccessToken失败: {result}")
def get_template_detail(self, template_data):
url = f"https://qyapi.weixin.qq.com/cgi-bin/oa/gettemplatedetail?access_token={self.access_token}"
headers = {'Content-Type': 'application/json'}
response = requests.post(url, json=template_data, headers=headers)
result = response.json()
if result['errcode'] == 0:
# print(result)
return result
else:
raise Exception(f"获取模板详情请求失败: {result}")
def create_approval_request(self, approval_data):
url = f"https://qyapi.weixin.qq.com/cgi-bin/oa/applyevent?access_token={self.access_token}"
headers = {'Content-Type': 'application/json'}
@@ -60,89 +71,89 @@ class WxWorkAPI:
return result['media_id']
template_id_dict = {"test": "ZvmJBAt7U2iUju9JLePxfkfuN22meQMVTAHymA"}
creator_userid = "MaGuangWei"
corid = 'wweaf7f1caab27a136'
secret = 'L6z-kjrUBOWfhBiLOmX_fNSY3jydDkLUNNWnemafn00'
wx = WxWorkAPI(corid, secret)
media_id = wx.upload_temp_file()
print(media_id)
# access = wx.get_access_token()
approval_datas = {
"creator_userid": creator_userid,
"template_id": template_id_dict['test'],
"use_template_approver": 0,
# "choose_department": 2,
"approver": [
{
"attr": 2,
"userid": ["MaGuangWei", "YangJinLing"]
},
{
"attr": 1,
"userid": ["MaGuangWei"]
}
],
"notifyer": ["MaGuangWei", "PengYuBo"],
# "notifyer": ["MaGuangWei"],
"notify_type": 1,
"apply_data": {
"contents": [
# 申请事项
{
"control": "Text",
"id": "Text-1640339319582",
"value": {
"text": "测试测试测试测试测试"
}
},
# 申请内容
{
"control": "Textarea",
"id": "Textarea-1640339335659",
"value": {
"text": "odoo>>>企业微信审批"
}
},
# 附件
{
"control": "File",
"id": "File-1640339381728",
"value": {
"files": [
{
"file_id": media_id
}
]
}
},
]
},
"summary_list": [
{
"summary_info": [{
"text": "测试附件上传",
"lang": "zh_CN"
}]
},
# {
# "summary_info": [{
# "text": "摘要第2行",
# "lang": "zh_CN"
# }]
# },
# {
# "summary_info": [{
# "text": "摘要第3行",
# "lang": "zh_CN"
# }]
# }
]
}
wx.create_approval_request(approval_datas)
print('创建成功')
# template_id_dict = {"test": "ZvmJBAt7U2iUju9JLePxfkfuN22meQMVTAHymA"}
# creator_userid = "MaGuangWei"
# corid = 'wweaf7f1caab27a136'
# secret = 'L6z-kjrUBOWfhBiLOmX_fNSY3jydDkLUNNWnemafn00'
# wx = WxWorkAPI(corid, secret)
# media_id = wx.upload_temp_file()
# print(media_id)
# # access = wx.get_access_token()
# approval_datas = {
# "creator_userid": creator_userid,
# "template_id": template_id_dict['test'],
# "use_template_approver": 0,
# # "choose_department": 2,
# "approver": [
# {
# "attr": 2,
# "userid": ["MaGuangWei", "YangJinLing"]
# },
# {
# "attr": 1,
# "userid": ["MaGuangWei"]
# }
# ],
# "notifyer": ["MaGuangWei", "PengYuBo"],
# # "notifyer": ["MaGuangWei"],
# "notify_type": 1,
# "apply_data": {
# "contents": [
# # 申请事项
# {
# "control": "Text",
# "id": "Text-1640339319582",
# "value": {
# "text": "测试测试测试测试测试"
# }
# },
# # 申请内容
# {
# "control": "Textarea",
# "id": "Textarea-1640339335659",
# "value": {
# "text": "odoo>>>企业微信审批"
# }
# },
# # 附件
# {
# "control": "File",
# "id": "File-1640339381728",
# "value": {
# "files": [
# {
# "file_id": media_id
# }
# ]
# }
#
# },
#
# ]
# },
# "summary_list": [
# {
# "summary_info": [{
# "text": "测试附件上传",
# "lang": "zh_CN"
# }]
# },
# # {
# # "summary_info": [{
# # "text": "摘要第2行",
# # "lang": "zh_CN"
# # }]
# # },
# # {
# # "summary_info": [{
# # "text": "摘要第3行",
# # "lang": "zh_CN"
# # }]
# # }
# ]
# }
# wx.create_approval_request(approval_datas)
# print('创建成功')
aa = {"errcode": 0, "errmsg": "ok", "template_names": [{"text": "对接开发测试", "lang": "zh_CN"}], "template_content": {
"controls": [{"property": {"control": "Text", "id": "Text-1640339319582",

View File

@@ -6,97 +6,103 @@ from odoo.exceptions import UserError
_logger = logging.getLogger(__name__)
# 控件模型
class WxWorkApprovalTemplate(models.Model):
_name = 'wxwork.approval.template.controls'
_description = "企业微信审批模板控件"
template_id = fields.Many2one('wxwork.approval.template', string='模板')
property_id = fields.One2many('property.model', 'control_id', string='控件属性')
config_id = fields.One2many('config.model', 'control_id', string='控件配置')
# 控件属性模型
class PropertyModel(models.Model):
_name = 'property.model'
_rec_name = "title"
SELECTION_RELATION = {
'申请事项': {'id': 'Text-1640339319582', 'control': 'Text', 'value': '{"text": "文本填写的内容"}'},
'申请内容': {'id': 'Textarea-1640339335659', 'control': 'Textarea', 'value': '{"text": "文本填写的内容"}'},
'多行文本': {'id': 'Textarea-1684823453770', 'control': 'Textarea', 'value': '{"text": "文本填写的内容"}'},
'金额': {'id': 'Money-1684823460232', 'control': 'Money', 'value': '{"new_money": "0"}'},
'部门': {'id': 'Contact-1684823477216', 'control': 'Contact',
'value': '{"departments":[{"openapi_id":"2","name":"销售部"},{"openapi_id":"3","name":"生产部"}]}'},
'成员': {'id': 'Contact-1684823465317', 'control': 'Contact',
'value': '{"members":[{"userid":"WuJunJie","name":"Jackie"},{"userid":"WangXiaoMing","name":"Tom"}]}'},
'明细': {'id': 'Table-1684823492261', 'control': 'Table',
'value': '{"children":[{"list":['
'{"control":"Text","id":"Text-15111111111","title":'
'[{"text":"明细内文本控件","lang":"zh_CN"}],"value":{"text":"明细文本1"}},'
'{"control":"Money","id":"Text-15111111112","title":[{"text":"明细内金额控件","lang":"zh_CN"}],'
'"value":{"new_money":"700"}}]},'
'{"list":[{"control":"Text","id":"Text-15111111111",'
'"title":[{"text":"明细内文本控件","lang":"zh_CN"}],"value":{"text":"明细文本2"}},'
'{"control":"Money","id":"Text-15111111112","title":[{"text":"明细内金额控件","lang":"zh_CN"}],'
'"value":{"new_money":"900"}}]}]}'},
'关联申请单': {'id': 'RelatedApproval-1684823485883', 'control': 'RelatedApproval',
'value': '{"related_approval":[{"sp_no":"202011180001"}]}'},
'附件': {'id': 'File-1640339381728', 'control': 'File',
'value': '{"files":[{"file_id":"1G6nrLmr5EC3MMb_-zK1dDdzmd0p7cNliYu9V5w7o8K1aaa"}]}'},
'数字': {'id': 'File-1640339381728', 'control': 'Number', 'value': '{"new_number": "700"}'},
'日期/日期+时间': {'id': 'File-1640339381728', 'control': 'Date',
'value': '{"date": {"type": "day","s_timestamp": "1569859200"}}'},
'单选/多选': {'id': 'File-1640339381728', 'control': 'Selector',
'value': '{"selector": '
'{"type": "multi","options": [{"key": "option-15111111111"},{"key": "option-15222222222"}]}}'
},
'说明文字': {'id': 'File-1640339381728', 'control': 'Tips'},
'位置': {'id': 'File-1640339381728', 'control': 'Location',
'value': '{"location":{"latitude":"30.547239","longitude":"104.063291",'
'"title":"腾讯科技(成都)有限公司(腾讯成都大厦)",'
'"address":"四川省成都市武侯区天府三街198号腾讯成都大厦A座","time":1605690460}}'},
'公式': {'id': 'File-1640339381728', 'control': 'Formula', 'value': '{"formula":{"value":"5.0"}}'},
'时长': {'id': 'File-1640339381728', 'control': 'DateRange',
'value': '{"date_range":{"new_begin":1570550400,"new_end":1570593600,"new_duration":86400}}'},
'请假组件': {'id': 'File-1640339381728', 'control': 'Vacation',
'value': '{"vacation":{"selector":{"type":"single","options":[{"key":"3",'
'"value":[{"text":"病假","lang":"zh_CN"}]}],"exp_type":0},'
'"attendance":{"date_range":{"type":"hour","new_begin":1568077200,'
'"new_end":1568368800,"new_duration":291600},"type":1}}}'},
'出差/外出/加班组件': {'id': 'File-1640339381728', 'control': 'Attendance',
'value': '{"attendance":{"date_range":{"type":"halfday","new_begin":1570550400,'
'"new_end":1570593600,"new_duration":86400},"type":4}}'},
}
title = fields.Selection(
selection=[(key, key) for key in SELECTION_RELATION.keys()],
string='控件名称',
)
template_id = fields.Many2one('wxwork.approval.template', string='模板名称')
control_id = fields.Char(string='控件ID')
control = fields.Char(string='控件类型')
value = fields.Char(string='内容')
placeholder = fields.Char(string='占位符')
require = fields.Boolean(string='必填', compute='_compute_require')
un_print = fields.Boolean(string='不打印')
CONTROL_TYPE_SELECTION = [
('Text', '文本'),
('Textarea', '多行文本'),
('Number', '数字'),
('Money', '金额'),
('Date', '日期/日期+时间'),
('Selector', '单选/多选'),
('Contact', '成员/部门'),
('Tips', '说明文字'),
('File', '附件'),
('Table', '明细'),
('Attendance', '假勤控件'),
('Vacation', '请假控件'),
('Location', '位置'),
('RelatedApproval', '关联审批单'),
('Formula', '公式'),
('DateRange', '时长')
]
control_type = fields.Selection(CONTROL_TYPE_SELECTION, string='控件类型')
control_id = fields.Many2one('wxwork.approval.template.controls', string='控件')
unique_control_id = fields.Char(string='控件ID')
title = fields.Char(string='控件名称', translate=True)
placeholder = fields.Char(string='控件说明', translate=True)
require = fields.Boolean(string='是否必填')
un_print = fields.Boolean(string='是否参与打印')
un_replace = fields.Boolean(string='不替换')
display = fields.Boolean(string='显示', compute='_compute_display')
display = fields.Boolean(string='显示')
child_control_id = fields.Many2one('config.model', string='明细子控件')
@api.onchange('title')
def _onchange_title(self):
if self.title:
related_values = WxWorkApprovalTemplate.SELECTION_RELATION[self.title]
self.control_id = related_values['id']
self.control = related_values['control']
# 特殊处理:对于"说明文字",将"value"设置为空字符串
if self.control == 'Tips':
self.value = ''
else:
self.value = related_values['value']
# 控件配置模型
class ConfigModel(models.Model):
_name = 'config.model'
_rec_name = "title"
@api.depends('title')
def _compute_require(self):
for record in self:
record.require = record.title in ['申请事项', '申请内容']
control_id = fields.Many2one('wxwork.approval.template.controls', string='控件')
# 在此添加其他字段以存储不同控件类型的附加类型和属性
date_type = fields.Selection([('day', '日期'), ('hour', '日期+时间')], string='日期类型')
@api.depends('title')
def _compute_display(self):
for record in self:
record.display = record.title in ['申请事项', '申请内容']
selector_type = fields.Selection([('single', '单选'), ('multi', '多选')], string='选择器类型')
options = fields.One2many('config.option', 'config_id', string='选项')
contact_type = fields.Selection(
[('single', '单选'), ('multi', '多选')],
string='联系人类型')
contact_mode = fields.Selection(
[('user', '成员'), ('department', '部门')],
string='联系人模式')
# table_children = fields.One2many('config.child', 'config_id', string='明细子控件')
table_children = fields.One2many('property.model', 'child_control_id', string='明细子控件')
attendance_type = fields.Selection([('1', '请假'), ('3', '出差'), ('4', '外出'), ('5', '加班')], string='假勤组件类型')
attendance_date_range_type = fields.Selection([('hour', '分钟'), ('halfday', '上午/下午')], string='假勤时间刻度')
vacation_list = fields.One2many('config.option', 'config_id', string='假期类型列表')
class ConfigOption(models.Model):
_name = 'config.option'
key = fields.Char(string='选项Key')
text = fields.Char(string='选项Text')
lang = fields.Char(string='语言')
config_id = fields.Many2one('config.model', string='配置')
class ConfigChild(models.Model):
_name = 'config.child'
control = fields.Char(string='控件类型')
control_id = fields.Char(string='控件ID')
# 其他属性...
class VacationType(models.Model):
_name = 'vacation.type'
config_id = fields.Many2one('config.model', string='配置')
vacation_id = fields.Integer(string='假期ID')
name = fields.Char(string='假期名称')
lang = fields.Char(string='语言')
class ApproverLine(models.Model):

View File

@@ -1,6 +1,8 @@
# -*- 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):
@@ -10,7 +12,130 @@ class WxSettings(models.Model):
wx_work_app = fields.Char(string='企业微信应用名称')
wx_work_corp_id = fields.Char(string='企业微信CorpID')
wx_work_secret = fields.Char(string='企业微信Secret')
wx_work_templates = fields.One2many('wxwork.template.settings', 'wxwork_id', string='审批模板')
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):
@@ -19,4 +144,4 @@ class WxTemplateSettings(models.Model):
sp_name = fields.Char(string='审批模板名称')
template_id = fields.Char(string='审批模板ID')
wxwork_id = fields.Many2one('wxwork.settings', string='企业微信')
# wxwork_id = fields.Many2one('wxwork.settings', string='企业微信')

View File

@@ -7,6 +7,10 @@ access_wxwork_approval_approver_line,wxwork.approval.approver_line,model_wxwork_
access_wxwork_settings,wxwork.settings,model_wxwork_settings,base.group_user,1,1,1,1
access_wxwork_approval_template_summary,wxwork.approval.template.summary,model_wxwork_approval_template_summary,base.group_user,1,1,1,1
access_wxwork_template_settings,wxwork.template.settings,model_wxwork_template_settings,base.group_user,1,1,1,1
access_property_model,property.model,model_property_model,base.group_user,1,1,1,1
access_config_model,config.model,model_config_model,base.group_user,1,1,1,1
access_config_option,config.option,model_config_option,base.group_user,1,1,1,1
access_vacation_type,vacation.type,model_vacation_type,base.group_user,1,1,1,1
1 id name model_id:id group_id:id perm_read perm_write perm_create perm_unlink
7 access_wxwork_settings wxwork.settings model_wxwork_settings base.group_user 1 1 1 1
8 access_wxwork_approval_template_summary wxwork.approval.template.summary model_wxwork_approval_template_summary base.group_user 1 1 1 1
9 access_wxwork_template_settings wxwork.template.settings model_wxwork_template_settings base.group_user 1 1 1 1
10 access_property_model property.model model_property_model base.group_user 1 1 1 1
11 access_config_model config.model model_config_model base.group_user 1 1 1 1
12 access_config_option config.option model_config_option base.group_user 1 1 1 1
13 access_vacation_type vacation.type model_vacation_type base.group_user 1 1 1 1
14
15
16

View File

@@ -10,8 +10,8 @@
<field name="arch" type="xml">
<!-- <tree default_order="id desc" js_class="we_approval_tem_tree_class"> -->
<tree js_class="we_approval_tem_tree_class">
<field name="process_code"/>
<field name="name"/>
<field name="process_code"/>
<field name="company_id"/>
</tree>
</field>
@@ -23,8 +23,8 @@
<field name="arch" type="xml">
<form>
<group>
<field name="process_code"/>
<field name="name"/>
<field name="process_code"/>
<field name="content_ids"/>
<field name="company_id"/>
<field name="choose_department"/>
@@ -33,7 +33,7 @@
<field name="notifyer" attrs="{'invisible': [('use_template_approver', '=', '1')]}"/>
<field name="notify_type" attrs="{'invisible': [('use_template_approver', '=', '1')]}"/>
<field name="summary_info_ids"/>
<!-- <field name="summary_info_ids"/> -->
<button name="submit" type="object" string="提交" class="btn-success"/>
</group>
</form>
</field>
@@ -72,15 +72,8 @@
<field name="arch" type="xml">
<tree default_order="id desc" js_class="we_approval_tem_tree_class">
<field name="template_id"/>
<field name="title"/>
<field name="control_id"/>
<field name="control"/>
<field name="value" widget="text"/>
<field name="placeholder"/>
<field name="require"/>
<field name="un_print"/>
<field name="un_replace"/>
<field name="display"/>
<field name="property_id" widget="many2many_tags"/>
<field name="config_id"/>
</tree>
</field>
</record>
@@ -91,10 +84,40 @@
<form>
<group>
<field name="template_id"/>
<field name="property_id"/>
<field name="config_id"/>
</group>
</form>
</field>
</record>
<!-- 审批模板控件属性 -->
<record id="property_model_tree_view" model="ir.ui.view">
<field name="name">审批模板控件属性</field>
<field name="model">property.model</field>
<field name="arch" type="xml">
<tree>
<field name="title"/>
<field name="control_id"/>
<field name="control"/>
<field name="value" widget="text"/>
<!-- <field name="control_id"/> -->
<field name="unique_control_id"/>
<field name="control_type"/>
<field name="placeholder"/>
<field name="require"/>
<field name="un_print"/>
<field name="un_replace"/>
<field name="display"/>
</tree>
</field>
</record>
<record id="property_model_form" model="ir.ui.view">
<field name="name">审批模板控件属性</field>
<field name="model">property.model</field>
<field name="arch" type="xml">
<form>
<group>
<field name="title"/>
<!-- <field name="control_id"/> -->
<field name="unique_control_id"/>
<field name="control_type"/>
<field name="placeholder"/>
<field name="require"/>
<field name="un_print"/>
@@ -105,6 +128,47 @@
</field>
</record>
<!-- 审批模板控件配置 -->
<record id="config_model_tree_view" model="ir.ui.view">
<field name="name">审批模板控件配置</field>
<field name="model">config.model</field>
<field name="arch" type="xml">
<tree>
<field name="control_id"/>
<field name="date_type"/>
<field name="selector_type"/>
<field name="options"/>
<field name="contact_type"/>
<field name="contact_mode"/>
<field name="table_children"/>
<field name="attendance_type"/>
<field name="attendance_date_range_type"/>
<field name="vacation_list"/>
</tree>
</field>
</record>
<record id="config_model_form" model="ir.ui.view">
<field name="name">审批模板控件配置</field>
<field name="model">config.model</field>
<field name="arch" type="xml">
<form>
<group>
<field name="control_id"/>
<field name="date_type"/>
<field name="selector_type"/>
<field name="options"/>
<field name="contact_type"/>
<field name="contact_mode"/>
<field name="table_children"/>
<field name="attendance_type"/>
<field name="attendance_date_range_type"/>
<field name="vacation_list"/>
</group>
</form>
</field>
</record>
<record id="we_approval_template_action" model="ir.actions.act_window">
<field name="name">审批模板</field>
<field name="res_model">wxwork.approval.template</field>