Skip to content

Commit

Permalink
Merge pull request #1191 from hanshuaikang/feature/develop_by_han
Browse files Browse the repository at this point in the history
minor: 社区版企业版快捷审批功能合并
  • Loading branch information
hanshuaikang authored Sep 1, 2023
2 parents 36134a8 + 7a694ee commit 5fc8971
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def notify_fast_approval(
"""
if not settings.USE_BKCHAT:
return

# 判断当前节点是否存在
try:
current_state = State.objects.get(id=state_id)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
import json
import re

import requests
from django.conf import settings
Expand All @@ -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": "(预发布)",
Expand All @@ -31,7 +28,6 @@ def notify_fast_approval_message(
"""
构建快速审批通知参数
"""
task_id = kwargs.get("task_id")

# 如果关闭通知服务则不通知
# CLOSE_NOTIFY 从环境变量中得到
Expand Down Expand Up @@ -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
)
Expand All @@ -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)
Expand Down Expand Up @@ -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",
Expand Down
5 changes: 1 addition & 4 deletions itsm/openapi/ticket/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"])
Expand Down
6 changes: 1 addition & 5 deletions itsm/ticket/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)

Expand Down
7 changes: 2 additions & 5 deletions platform_config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit 5fc8971

Please sign in to comment.