diff --git a/itsm/component/bkchat/config.py b/itsm/component/bkchat/config.py index 1acd1c1f1..5cb323a95 100644 --- a/itsm/component/bkchat/config.py +++ b/itsm/component/bkchat/config.py @@ -37,7 +37,5 @@ def notify_fast_approval( return # 异步执行发送快速审批通知操作 - notify_fast_approval_task.apply_async( - args=[self, state_id, receivers, message, action], kwargs=kwargs - ) + notify_fast_approval_task.apply_async(args=[self, state_id, receivers]) return diff --git a/itsm/component/bkchat/utils.py b/itsm/component/bkchat/utils.py index 43119e35c..300fe443f 100644 --- a/itsm/component/bkchat/utils.py +++ b/itsm/component/bkchat/utils.py @@ -22,9 +22,52 @@ APPROVE_MESSAGE = {"true": "同意", "false": "拒绝"} -def notify_fast_approval_message( - ticket, state_id, receivers, message, action, **kwargs -): +def build_bkchat_summary(ticket): + 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) + + current_steps = ",".join([step.get("name") for step in ticket.current_steps]) + + content += "**当前环节**: {}\n".format(current_steps) + content += "**提单人**: {}\n".format(ticket.creator) + + 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(processor_content) + + # 添加「提单信息」 + content = "{}\n **--- 单据基本信息 ---**".format(content) + state_fields = ticket.get_state_fields(ticket.first_state_id, need_serialize=False) + # 隐藏字段过滤 + for f in state_fields.exclude(type__in=["TABLE", "CUSTOMTABLE", "FILE"]): + if f.show_type == SHOW_BY_CONDITION: + key_value = { + "params_%s" + % item["key"]: format_exp_value(item["type"], item["_value"]) + for item in f.ticket.fields.values("key", "_value", "type") + } + if show_conditions_validate(f.show_conditions, key_value): + continue + + detail = "**{}**:{}".format( + f.name, ticket.display_content(f.type, f.display_value) + ) + content = "{}\n {}".format(content, detail) + + return content + + +def notify_fast_approval_message(ticket, state_id, receivers): """ 构建快速审批通知参数 """ @@ -63,51 +106,10 @@ def notify_fast_approval_message( ) ) return - 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) - - current_steps = ",".join([step.get("name") for step in ticket.current_steps]) - - content += "**当前环节**: {}\n".format(current_steps) - content += "**提单人**: {}\n".format(ticket.creator) - - 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(processor_content) - - # 添加「提单信息」 - content = "{}\n **--- 单据基本信息 ---**".format(content) - state_fields = ticket.get_state_fields( - ticket.first_state_id, need_serialize=False - ) - # 隐藏字段过滤 - for f in state_fields.exclude(type__in=["TABLE", "CUSTOMTABLE", "FILE"]): - if f.show_type == SHOW_BY_CONDITION: - key_value = { - "params_%s" - % item["key"]: format_exp_value(item["type"], item["_value"]) - for item in f.ticket.fields.values("key", "_value", "type") - } - if show_conditions_validate(f.show_conditions, key_value): - continue - - detail = "**{}**:{}".format( - f.name, ticket.display_content(f.type, f.display_value) - ) - content = "{}\n {}".format(content, detail) + content = build_bkchat_summary(ticket) # 发送微信通知 - send_fast_approval_message(title, content, receivers, ticket, state_id) + send_fast_approval_message(ticket.title, content, receivers, ticket, state_id) def send_fast_approval_message(title, content, receivers, ticket, state_id): diff --git a/itsm/openapi/ticket/views.py b/itsm/openapi/ticket/views.py index fe0fe7457..f462465c7 100644 --- a/itsm/openapi/ticket/views.py +++ b/itsm/openapi/ticket/views.py @@ -35,7 +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.bkchat.utils import proceed_fast_approval, build_bkchat_summary from itsm.component.constants import ( API, QUEUEING, @@ -638,6 +638,19 @@ def proceed_fast_approval(self, request): """ return proceed_fast_approval(request) + @action(detail=False, methods=["get"]) + @catch_openapi_exception + @custom_apigw_required + def get_fast_approval_summary(self, request): + sn = request.query_params.get("sn") + try: + ticket = Ticket.objects.get(sn=sn) + except Ticket.DoesNotExist: + raise ParamError("sn[{}]对应的单据不存在!".format(sn)) + + summary = build_bkchat_summary(ticket) + return Response({"summary": summary}) + @action(detail=False, methods=["get"]) @catch_openapi_exception @custom_apigw_required diff --git a/itsm/ticket/tasks.py b/itsm/ticket/tasks.py index da62cbe22..f7d44f7ad 100644 --- a/itsm/ticket/tasks.py +++ b/itsm/ticket/tasks.py @@ -292,10 +292,10 @@ def notify_task(ticket, receivers, message, action, **kwargs): @task -def notify_fast_approval_task(ticket, state_id, receivers, message, action, **kwargs): +def notify_fast_approval_task(ticket, state_id, receivers): """发送快速审批通知""" - notify_fast_approval_message(ticket, state_id, receivers, message, action, **kwargs) + notify_fast_approval_message(ticket, state_id, receivers) @periodic_task(run_every=(crontab(minute="*/1")), ignore_result=True)