同步模板功能基本完成

This commit is contained in:
mgw
2023-06-02 17:03:03 +08:00
parent 8b19e0be38
commit f973bb031d
6 changed files with 594 additions and 46 deletions

View File

@@ -32,6 +32,9 @@ class WxSettings(models.Model):
# 删除所有记录
all_records.unlink()
all_records1.unlink()
content_data_list = []
template_data = {}
controls_data = []
for template in template_list:
print('====================')
temp_dict = {'template_id': template}
@@ -50,54 +53,225 @@ class WxSettings(models.Model):
# 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']
# print('property_data', property_data)
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'})],
if 'config' in control_data:
config_data = control_data['config']
print('config_data', config_data)
# 先提取table_children数据
table_children_data = config_data.get('table', {}).get('children', [])
# 创建一个空列表用于存储table_children记录
table_children_records = []
# 遍历table_children_data并为每个子控件创建记录
for child_data in table_children_data:
print('child_data====================', child_data)
# 提取property相关数据
child_property_data = {
# 'control_type': child_data.get('property', {}).get('control', None),
# 'unique_control_id': child_data.get('property', {}).get('id', None),
# 'title': child_data.get('property', {}).get('title', [{}])[0].get('text', None),
# 'placeholder': child_data.get('property', {}).get('placeholder', [{}])[0].get('text', None),
# 'require': child_data.get('property', {}).get('require', None),
# 'un_print': child_data.get('property', {}).get('un_print', None),
# 'un_replace': child_data.get('property', {}).get('un_replace', None),
# 'display': child_data.get('property', {}).get('display', None),
'control_type': child_data.get('property', {}).get('control', None),
'unique_control_id': child_data.get('property', {}).get('id', None),
'title': child_data.get('property', {}).get('title', [{}])[0].get('text', None),
'placeholder': child_data.get('property', {}).get('placeholder', [{}])[0].get('text', None),
'require': child_data.get('property', {}).get('require', None),
'un_print': child_data.get('property', {}).get('un_print', None),
'un_replace': child_data.get('property', {}).get('un_replace', None),
'display': child_data.get('property', {}).get('display', None),
}
# 在这里根据控件类型为每个子控件创建相应的config_id数据
# 例如:
config_id_data = {
'date_type': [(0, 0, {'date_type': child_data.get('config', {}).get('date', {}).get('type',
None)})] if 'date' in child_data.get(
'config', {}) else [],
'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',
# ...
})],
}
})] if 'selector' in child_data.get('config', {}) else [],
'contact_type': [
(0, 0, {'contact_type': config_data.get('contact', {}).get('type', None),
'contact_mode': config_data.get('contact', {}).get('mode',
None)})] if 'contact' in child_data.get(
'config', {}).get('contact', {}) else [],
'file_type': [
(0, 0, {'is_only_photo': config_data.get('file', {}).get('is_only_photo',
None)})] if 'file' in child_data.get(
'config', {}).get('file', {}) else [],
# 'table_type': [(0, 0, {
# 'table_children': [
# (0, 0, {'control_type': config_data.get('table', {}).get('children', None)[0].get('property', {}).get('control', None),
# 'unique_control_id': config_data.get('table', {}).get('children', None)[0].get('property', {}).get('id', None),
# 'title': config_data.get('table', {}).get('children', None)[0].get('property', {}).get('title', None)[0].get('text', None),
# 'placeholder': config_data.get('table', {}).get('children', None)[0].get('property', {}).get('placeholder', None)[0].get('text', None),
# 'require': config_data.get('table', {}).get('children', None)[0].get('property', {}).get('require', None),
# 'un_print': config_data.get('table', {}).get('children', None)[0].get('property', {}).get('un_print', None),
# 'un_replace': config_data.get('table', {}).get('children', None)[0].get('property', {}).get('un_replace', None),
# 'display': config_data.get('table', {}).get('children', None)[0].get('property', {}).get('display', None),
# })],
# })] if 'table' in config_data else [],
# 'attendance_type': [(0, 0, {'attendance_type': '1',
# 'attendance_date_range_type': 'hour'})] if 'attendance' in config_data else [],
}
# 调用create方法创建记录
template_record = self.env['wxwork.approval.template'].create(template_data)
# 将config_id_data添加到property_data中如果有的话
if config_id_data:
child_property_data['child_control_id'] = config_id_data
# 将property_data添加到table_children_records中
table_children_records.append((0, 0, child_property_data))
# 创建一个包含多个wxwork.approval.template.controls的字典列表
controls_data = [
(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': config_data.get('date', {}).get('type',
None)})] if 'date' in config_data else [],
# 'selector_type': [(0, 0, {
# 'selector_type': config_data['table']['children'][0].get('config', {}).get(
# 'selector', {}).get('type', None),
# 'options': [
# (0, 0, {'key': option['key'], 'text': option['value'][0]['text']})
# for option in config_data['table']['children'][0].get('config', {}).get('selector', {}).get('options', [])
# ],
# })] if 'selector' in config_data else [],
'selector_type': [
(0, 0, {
'selector_type': child.get('config', {}).get('selector', {}).get('type', None),
'options': [
(0, 0, {'key': option['key'], 'text': option['value'][0]['text']})
for option in child.get('config', {}).get('selector', {}).get('options', [])
],
})
for child in config_data.get('table', {}).get('children', [])
if 'selector' in child.get('config', {})
],
'contact_type': [
(0, 0, {'contact_type': config_data.get('contact', {}).get('type', None),
'contact_mode': config_data.get('contact', {}).get('mode',
None)})] if 'contact' in config_data else [],
'file_type': [
(0, 0, {'is_only_photo': config_data.get('file', {}).get('is_only_photo',
None)})] if 'file' in config_data else [],
'table_type': [(0, 0, {
'table_children': table_children_records,
})] if 'table' in config_data else [],
# 'contact_type': [
# (0, 0, {'contact_type': config_data.get('contact', {}).get('type', None)})],
# 'contact_mode': [
# (0, 0, {'contact_mode': config_data.get('contact', {}).get('mode', None)})],
# 'table_type': [(0, 0, {
# 'table_children': [
# (0, 0,
# {'control_type': 'Text', 'unique_control_id': 'text_2', 'title': '文本框2'})],
# })],
# # [
# # (0, 0, {'control_type': config_data.get('table', {}).get('children', {})[0].
# # get('property', {}).get('control', None), 'unique_control_id': 'text_2',
# # 'title': '文本框2'})]
'attendance_type': [(0, 0, {'attendance_type': '1',
'attendance_date_range_type': 'hour'})] if 'attendance' in config_data else [],
})],
}),
# 在这里添加更多的控件数据
]
else:
# 创建一个包含多个wxwork.approval.template.controls的字典列表
controls_data = [
(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),
})],
}),
# 在这里添加更多的控件数据
]
# 从vacation_list中提取假期类型数据
vacation_items = []
vacation_items_data = template_detail.get('vacation_list', [])
# 将假期类型数据转换为适用于vacation_items字段的格式
if vacation_items_data and 'item' in vacation_items_data:
items = []
for item in vacation_items_data['item']:
id = item.get('id')
name = item.get('name')[0].get('text')
items.append({'id': id, 'name': name})
print(items)
# if vacation_items_data:
# print('vacation_items_data', vacation_items_data)
# print('vacation_items_data', type(vacation_items_data))
# print('vacation_items_data', vacation_items_data['items'])
# print('vacation_items_data', vacation_items_data[0])
vacation_items = [(0, 0, {
'vacation_id': item['id'],
'name': item['name'],
'lang': 'zh_CN',
}) for item in items]
# 创建一个包含WxWorkApprovalTemplate和关联记录数据的字典
template_data = {
'process_code': template,
'name': template_detail['template_names'][0]['text'],
'content_ids': controls_data,
# 'vacation_config_id': [(0, 0, {
# # 在这里添加wxwork.approval.vacation.config模型的字段数据
# 'vacation_id': template_detail.get('vacation_id', None),
# 'name': template_detail.get('vacation_name', None),
# 'lang': template_detail.get('vacation_lang', None),
# # ...
# })],
'vacation_config_id': vacation_items
}
# 检查数据库中是否已经存在具有相同process_code的模板
existing_template = self.env['wxwork.approval.template'].search(
[('process_code', '=', template)], limit=1)
if existing_template:
# 如果模板已经存在更新其content_ids字段即添加控件记录
existing_template.write({'content_ids': controls_data})
else:
# 如果模板不存在,创建一个新的模板及其关联记录
# 调用create方法创建记录
template_record = self.env['wxwork.approval.template'].create(template_data)
# # print(property_data)
# # 准备用于创建记录的字段值