From 7a694eeed519bccb716c8fde9f89c0a12a21eb06 Mon Sep 17 00:00:00 2001 From: hanshuaikang <1758504262@qq.com> Date: Fri, 1 Sep 2023 17:20:47 +0800 Subject: [PATCH] =?UTF-8?q?minor:=20=E7=A4=BE=E5=8C=BA=E7=89=88=E4=BC=81?= =?UTF-8?q?=E4=B8=9A=E7=89=88=E5=BF=AB=E6=8D=B7=E5=AE=A1=E6=89=B9=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E5=90=88=E5=B9=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../open => itsm/component}/bkchat/config.py | 1 + .../open => itsm/component}/bkchat/utils.py | 51 +++++++------------ itsm/openapi/ticket/views.py | 5 +- itsm/ticket/tasks.py | 6 +-- platform_config/__init__.py | 7 +-- 5 files changed, 24 insertions(+), 46 deletions(-) rename {platform_config/open => itsm/component}/bkchat/config.py (99%) rename {platform_config/open => itsm/component}/bkchat/utils.py (86%) diff --git a/platform_config/open/bkchat/config.py b/itsm/component/bkchat/config.py similarity index 99% rename from platform_config/open/bkchat/config.py rename to itsm/component/bkchat/config.py index 04eb2ff6c..1acd1c1f1 100644 --- a/platform_config/open/bkchat/config.py +++ b/itsm/component/bkchat/config.py @@ -17,6 +17,7 @@ def notify_fast_approval( """ if not settings.USE_BKCHAT: return + # 判断当前节点是否存在 try: current_state = State.objects.get(id=state_id) diff --git a/platform_config/open/bkchat/utils.py b/itsm/component/bkchat/utils.py similarity index 86% rename from platform_config/open/bkchat/utils.py rename to itsm/component/bkchat/utils.py index 121985500..43119e35c 100644 --- a/platform_config/open/bkchat/utils.py +++ b/itsm/component/bkchat/utils.py @@ -1,6 +1,5 @@ # -*- coding: utf-8 -*- import json -import re import requests from django.conf import settings @@ -13,8 +12,6 @@ from itsm.component.utils.conversion import show_conditions_validate, format_exp_value from itsm.ticket.models import Ticket, Status, TicketField, SignTask -from itsm.ticket.utils import build_message - # 当前运行环境 RUNNING_ENV = { "STAGING": "(预发布)", @@ -31,7 +28,6 @@ def notify_fast_approval_message( """ 构建快速审批通知参数 """ - task_id = kwargs.get("task_id") # 如果关闭通知服务则不通知 # CLOSE_NOTIFY 从环境变量中得到 @@ -67,38 +63,30 @@ def notify_fast_approval_message( ) ) return - - # 从右向左删除 - # 用来处理「当前环节」后的审批人列表 - def rreplace(self, old, new, *max_length): - count = len(self) - if max_length and str(max_length[0]).isdigit(): - count = max_length[0] - return new.join(self.rsplit(old, count)) - - # 构造内容,标题 - content, title = build_message( - _notify, task_id, ticket, message, action, **kwargs + title = "## 『ITSM』{}: {}".format( + RUNNING_ENV.get(settings.RUN_MODE, ""), ticket.title ) + content = title + "\n" + content += "**单号**: {}\n".format(ticket.sn) + content += "**服务目录**: {}\n".format(ticket.catalog_service_name) - # 修改「标题」,添加环境标识 - title = title.replace( - "『ITSM』", "『ITSM{}』".format(RUNNING_ENV.get(settings.RUN_MODE, "")) - ) + current_steps = ",".join([step.get("name") for step in ticket.current_steps]) - # 修改「单号」,只显示单号,去除超链接 - content = content.replace(content.split("\n")[2], " 单号:{}".format(ticket.sn)) + content += "**当前环节**: {}\n".format(current_steps) + content += "**提单人**: {}\n".format(ticket.creator) - # 去除审批人列表 - # 例:当前环节:admin审批(admin) -> 当前环节:admin审批 - all_approver = (re.findall(r"[(](.*?)[)]", content)[-1]).split("(")[-1] - content = rreplace(content, "({})".format(all_approver), "", 1) + processor_list = [ + processor for processor in ticket.current_processors.split(",") if processor + ] + if len(processor_list) > 3: + processor_content = ",".join(processor_list[0:3]) + "..." + else: + processor_content = ",".join(processor_list) - # 添加「提单人」信息 - content = "{}提单人:{}".format(content, ticket.creator) + content += "**审批人**: {}".format(processor_content) # 添加「提单信息」 - content = "{}\n --- 单据基本信息 ---".format(content) + content = "{}\n **--- 单据基本信息 ---**".format(content) state_fields = ticket.get_state_fields( ticket.first_state_id, need_serialize=False ) @@ -113,11 +101,10 @@ def rreplace(self, old, new, *max_length): if show_conditions_validate(f.show_conditions, key_value): continue - detail = "{}:{}".format( + detail = "**{}**:{}".format( f.name, ticket.display_content(f.type, f.display_value) ) content = "{}\n {}".format(content, detail) - content = "{}\n --------------------".format(content) # 发送微信通知 send_fast_approval_message(title, content, receivers, ticket, state_id) @@ -147,10 +134,10 @@ def send_fast_approval_message(title, content, receivers, ticket, state_id): "action": [{"name": "同意", "value": "true"}, {"name": "拒绝", "value": "false"}], "context": {"ticket_id": ticket_id, "state_id": state_id}, } + logger.info( "[fast_approval({})]send fast approval message data:{}".format(ticket_sn, data) ) - # 构造请求参数 headers = { "Content-Type": "application/json", diff --git a/itsm/openapi/ticket/views.py b/itsm/openapi/ticket/views.py index 5c8127a09..fe0fe7457 100644 --- a/itsm/openapi/ticket/views.py +++ b/itsm/openapi/ticket/views.py @@ -35,6 +35,7 @@ from common.log import logger from common.cipher import AESVerification from common.redis import Cache +from itsm.component.bkchat.utils import proceed_fast_approval from itsm.component.constants import ( API, QUEUEING, @@ -635,10 +636,6 @@ def proceed_fast_approval(self, request): """ 处理快速审批请求 """ - if settings.RUN_VER == "ieod": - from platform_config.ieod.bkchat.utils import proceed_fast_approval - else: - from platform_config.open.bkchat.utils import proceed_fast_approval return proceed_fast_approval(request) @action(detail=False, methods=["get"]) diff --git a/itsm/ticket/tasks.py b/itsm/ticket/tasks.py index bc363e15d..da62cbe22 100644 --- a/itsm/ticket/tasks.py +++ b/itsm/ticket/tasks.py @@ -41,8 +41,8 @@ from common.log import logger from common.mymako import render_mako_tostring from common.redis import Cache -from config import RUN_VER from config.default import AUTO_COMMENT_DAYS, CLOSE_NOTIFY +from itsm.component.bkchat.utils import notify_fast_approval_message from itsm.component.constants import ( PROCESS_RUNNING, @@ -294,10 +294,6 @@ def notify_task(ticket, receivers, message, action, **kwargs): @task def notify_fast_approval_task(ticket, state_id, receivers, message, action, **kwargs): """发送快速审批通知""" - if RUN_VER == "ieod": - from platform_config.ieod.bkchat.utils import notify_fast_approval_message - else: - from platform_config.open.bkchat.utils import notify_fast_approval_message notify_fast_approval_message(ticket, state_id, receivers, message, action, **kwargs) diff --git a/platform_config/__init__.py b/platform_config/__init__.py index 727a06911..a4c5d545c 100644 --- a/platform_config/__init__.py +++ b/platform_config/__init__.py @@ -8,15 +8,12 @@ from django.conf import settings -bkchat_config = importlib.import_module( - "platform_config.{}.bkchat.config".format(settings.RUN_VER) -) +from itsm.component.bkchat.config import BaseBkchatConfig + moa_config = importlib.import_module( "platform_config.{}.moa.config".format(settings.RUN_VER) ) - -BaseBkchatConfig = bkchat_config.BaseBkchatConfig BaseMoaConfig = moa_config.BaseMoaConfig