Skip to content

Commit

Permalink
默认资源组、权限组支持多选,优化系统配置下拉选项 fix #961
Browse files Browse the repository at this point in the history
  • Loading branch information
hhyo committed Nov 29, 2020
1 parent dd143e4 commit 1b84227
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 37 deletions.
15 changes: 6 additions & 9 deletions common/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,15 @@ def init_user(user):
# 添加到默认权限组
default_auth_group = SysConfig().get('default_auth_group', '')
if default_auth_group:
try:
group = Group.objects.get(name=default_auth_group)
user.groups.add(group)
except Group.DoesNotExist:
logger.info(f'无name为[{default_auth_group}]的权限组,无法默认关联,请到系统设置进行配置')
default_auth_group = default_auth_group.split(',')
[user.groups.add(group) for group in Group.objects.filter(name__in=default_auth_group)]

# 添加到默认资源组
default_resource_group = SysConfig().get('default_resource_group', '')
if default_resource_group:
try:
user.resource_group.add(ResourceGroup.objects.get(group_name=default_resource_group))
except ResourceGroup.DoesNotExist:
logger.info(f'无name为[{default_resource_group}]的资源组,无法默认关联,请到系统设置进行配置')
default_resource_group = default_resource_group.split(',')
[user.resource_group.add(group) for group in
ResourceGroup.objects.filter(group_name__in=default_resource_group)]


class ArcheryAuth(object):
Expand Down
82 changes: 62 additions & 20 deletions common/templates/config.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% extends "base.html" %}
{% load format_tags %}

{% block content %}
<div class="row clearfix">
Expand Down Expand Up @@ -192,11 +193,19 @@ <h5 style="color: darkgrey"><b>SQL上线</b></h5>
<label for="auto_review_tag"
class="col-sm-4 control-label">AUTO_REVIEW_TAG</label>
<div class="col-sm-5">
<input type="text" class="form-control"
id="auto_review_tag"
key="auto_review_tag"
value="{{ config.auto_review_tag }}"
placeholder="自动审批的实例标签CODE,多个使用逗号分隔">
<select id="auto_review_tag" key="auto_review_tag"
class="form-control selectpicker" data-live-search="true"
multiple data-selected-text-format="count > 5"
data-none-selected-text="自动审批的实例标签">
{% for tag in instance_tags %}
{% if tag.tag_code|is_in:config.auto_review_tag %}
<option value="{{ tag.tag_code }}"
selected>{{ tag.tag_name }}</option>
{% else %}
<option value="{{ tag.tag_code }}">{{ tag.tag_name }}</option>
{% endif %}
{% endfor %}
</select>
</div>
</div>
<div class="form-group">
Expand Down Expand Up @@ -387,11 +396,21 @@ <h6 style="color:red">注3:企业微信群机器人需要配置资源组webhoo
<label for="ddl_notify_auth_group"
class="col-sm-4 control-label">DDL_NOTIFY_AUTH_GROUP</label>
<div class="col-sm-5">
<input type="text" class="form-control"
id="ddl_notify_auth_group"
key="ddl_notify_auth_group"
value="{{ config.ddl_notify_auth_group }}"
placeholder="DDL工单通知权限组名,为空则不通知">
<select id="ddl_notify_auth_group" key="ddl_notify_auth_group"
class="selectpicker show-tick form-control bs-select-hidden"
data-live-search="true"
data-none-selected-text="DDL工单通知权限组"
multiple data-selected-text-format="count > 3"
required>
{% for auth_group in auth_group_list %}
{% if auth_group.name|is_in:config.ddl_notify_auth_group %}
<option value="{{ auth_group.name }}"
selected>{{ auth_group.name }}</option>
{% else %}
<option value="{{ auth_group.name }}">{{ auth_group.name }}</option>
{% endif %}
{% endfor %}
</select>
</div>
</div>
<div class="form-group">
Expand Down Expand Up @@ -711,22 +730,42 @@ <h4 style="color: darkgrey"><b>其他配置</b></h4>
<label for="default_auth_group"
class="col-sm-4 control-label">DEFAULT_AUTH_GROUP</label>
<div class="col-sm-5">
<input type="text" class="form-control"
id="default_auth_group"
key="default_auth_group"
value="{{ config.default_auth_group }}"
placeholder="默认权限组名,新用户首次登录自动关联, 老用户请手动配置">
<select id="default_auth_group" key="default_auth_group"
class="selectpicker show-tick form-control bs-select-hidden"
data-live-search="true"
data-none-selected-text="新用户首次登录自动关联权限组"
multiple data-selected-text-format="count > 3"
required>
{% for auth_group in auth_group_list %}
{% if auth_group.name|is_in:config.default_auth_group %}
<option value="{{ auth_group.name }}"
selected>{{ auth_group.name }}</option>
{% else %}
<option value="{{ auth_group.name }}">{{ auth_group.name }}</option>
{% endif %}
{% endfor %}
</select>
</div>
</div>
<div class="form-group">
<label for="default_resource_group"
class="col-sm-4 control-label">DEFAULT_RESOURCE_GROUP</label>
<div class="col-sm-5">
<input type="text" class="form-control"
id="default_resource_group"
key="default_resource_group"
value="{{ config.default_resource_group }}"
placeholder="默认资源组名,新用户首次登录自动关联, 老用户请手动配置">
<select id="default_resource_group" key="default_resource_group"
class="selectpicker show-tick form-control bs-select-hidden"
data-live-search="true"
data-none-selected-text="新用户首次登录自动关联资源组"
multiple data-selected-text-format="count > 3"
required>
{% for resource_group in group_list %}
{% if resource_group.group_name|is_in:config.default_resource_group %}
<option value="{{ resource_group.group_name }}"
selected>{{ resource_group.group_name }}</option>
{% else %}
<option value="{{ resource_group.group_name }}">{{ resource_group.group_name }}</option>
{% endif %}
{% endfor %}
</select>
</div>
</div>
<div class="form-group">
Expand Down Expand Up @@ -889,6 +928,9 @@ <h5 class="control-label text-bold">当前审批流程:<b id="workflow_auditor
function () {
var config_item = $(this).attr("key");
var config_value = $(this).val();
if (Array.isArray(config_value)) {
config_value = config_value.join(',')
}
configs.push({
key: config_item,
value: config_value
Expand Down
12 changes: 8 additions & 4 deletions sql/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def __notify_cnf_status():
qywx_webhook_status = sys_config.get("qywx_webhook")
feishu_webhook_status = sys_config.get("feishu_webhook")
feishu_status = sys_config.get("feishu")
if not any([mail_status, ding_status, ding_webhook_status, wx_status, feishu_status, feishu_webhook_status ,qywx_webhook_status]):
if not any([mail_status, ding_status, ding_webhook_status, wx_status, feishu_status, feishu_webhook_status,
qywx_webhook_status]):
logger.info('未开启任何消息通知,可在系统设置中开启')
return False
else:
Expand Down Expand Up @@ -209,7 +210,8 @@ def notify_for_audit(audit_id, **kwargs):
raise Exception('工单状态不正确')
logger.info(f"通知Debug{msg_to}{msg_cc}")
# 发送通知
__send(msg_title, msg_content, msg_to, msg_cc, feishu_webhook=feishu_webhook, dingding_webhook=dingding_webhook, qywx_webhook=qywx_webhook)
__send(msg_title, msg_content, msg_to, msg_cc, feishu_webhook=feishu_webhook, dingding_webhook=dingding_webhook,
qywx_webhook=qywx_webhook)


def notify_for_execute(workflow):
Expand Down Expand Up @@ -249,7 +251,8 @@ def notify_for_execute(workflow):
feishu_webhook = ResourceGroup.objects.get(group_id=workflow.group_id).feishu_webhook
qywx_webhook = ResourceGroup.objects.get(group_id=workflow.group_id).qywx_webhook
# 发送通知
__send(msg_title, msg_content, msg_to, msg_cc, dingding_webhook=dingding_webhook, feishu_webhook=feishu_webhook, qywx_webhook=qywx_webhook)
__send(msg_title, msg_content, msg_to, msg_cc, dingding_webhook=dingding_webhook, feishu_webhook=feishu_webhook,
qywx_webhook=qywx_webhook)

# DDL通知
if sys_config.get('ddl_notify_auth_group') and workflow.status == 'workflow_finish':
Expand All @@ -266,7 +269,8 @@ def notify_for_execute(workflow):
url,
workflow.sqlworkflowcontent.sql_content[0:500])
# 获取通知成员ddl_notify_auth_group
msg_to = Users.objects.filter(groups__name=sys_config.get('ddl_notify_auth_group'))
ddl_notify_auth_group = sys_config.get('ddl_notify_auth_group', '').split(',')
msg_to = Users.objects.filter(groups__name__in=ddl_notify_auth_group)
# 发送通知
__send(msg_title, msg_content, msg_to, msg_cc)

Expand Down
21 changes: 19 additions & 2 deletions sql/templatetags/format_tags.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
# -*- coding: UTF-8 -*-
from django import template
from django.template.defaultfilters import stringfilter
from django.utils.safestring import mark_safe

register = template.Library()


# 替换换行符
@register.simple_tag
def format_str(str):
def format_str(string):
# 换行
return mark_safe(str.replace(',', '<br/>').replace('\n', '<br/>'))
return mark_safe(string.replace(',', '<br/>').replace('\n', '<br/>'))


# split
@register.filter
@stringfilter
def split(string, sep):
"""Return the string split by sep.
Example usage: {{ value|split:"/" }}
"""
return string.split(sep)


# in
@register.filter
def is_in(var, args):
return True if str(var) in args.split(',') else False
5 changes: 3 additions & 2 deletions sql/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,16 +373,17 @@ def config(request):
"""配置管理页面"""
# 获取所有资源组名称
group_list = ResourceGroup.objects.all()

# 获取所有权限组
auth_group_list = Group.objects.all()
# 获取所有实例标签
instance_tags = InstanceTag.objects.all()
# 获取所有配置项
all_config = Config.objects.all().values('item', 'value')
sys_config = {}
for items in all_config:
sys_config[items['item']] = items['value']

context = {'group_list': group_list, 'auth_group_list': auth_group_list,
context = {'group_list': group_list, 'auth_group_list': auth_group_list, 'instance_tags': instance_tags,
'config': sys_config, 'WorkflowDict': WorkflowDict}
return render(request, 'config.html', context)

Expand Down

0 comments on commit 1b84227

Please sign in to comment.