Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加sql上线可执行时间选择,且审核人可修改这个时间 #257

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions sql/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ class SqlWorkflow(models.Model):
engineer_display = models.CharField('发起人中文名', max_length=50, default='')
status = models.CharField(max_length=50, choices=SQL_WORKFLOW_CHOICES)
audit_auth_groups = models.CharField('审批权限组列表', max_length=255)
starttime = models.DateTimeField('可执行起始时间', null=True, blank=True)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这两个字段名称可以推敲一下,我也想不到好的😄

endtime = models.DateTimeField('可执行结束时间', null=True, blank=True)
create_time = models.DateTimeField('创建时间', auto_now_add=True)
finish_time = models.DateTimeField('结束时间', null=True, blank=True)
is_manual = models.IntegerField('是否原生执行', choices=((0, '否'), (1, '是')), default=0)
Expand Down
42 changes: 40 additions & 2 deletions sql/sql_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from sql.models import ResourceGroup, Users
from sql.utils.resource_group import user_groups, user_instances
from sql.utils.tasks import add_sql_schedule, del_schedule
from sql.utils.sql_review import can_timingtask, can_cancel, can_execute
from sql.utils.sql_review import can_timingtask, can_cancel, can_execute, on_Correct_time_period
from sql.utils.workflow_audit import Audit
from .models import SqlWorkflow, SqlWorkflowContent, Instance
from django_q.tasks import async_task
Expand Down Expand Up @@ -126,6 +126,8 @@ def submit(request):
is_backup = True if request.POST.get('is_backup') == 'True' else False
notify_users = request.POST.getlist('notify_users')
list_cc_addr = [email['email'] for email in Users.objects.filter(username__in=notify_users).values('email')]
starttime = request.POST['run_date_start']
endtime = request.POST['run_date_end']

# 服务器端参数验证
if None in [sql_content, db_name, instance_name, db_name, is_backup]:
Expand Down Expand Up @@ -178,7 +180,9 @@ def submit(request):
db_name=db_name,
is_manual=0,
syntax_type=check_result.syntax_type,
create_time=timezone.now()
create_time=timezone.now(),
starttime=starttime,
endtime=endtime
)
SqlWorkflowContent.objects.create(workflow=sql_workflow,
sql_content=sql_content,
Expand All @@ -204,6 +208,36 @@ def submit(request):

return HttpResponseRedirect(reverse('sql:detail', args=(workflow_id,)))

@permission_required('sql.sql_review', raise_exception=True)
def alter_time(request):
"""
审核人修改可执行时间
:param request:
:return:
"""
workflow_id = int(request.POST.get('workflow_id', 0))
starttime = request.POST['run_date_start']
endtime = request.POST['run_date_end']
if workflow_id == 0:
context = {'errMsg': 'workflow_id参数为空.'}
return render(request, 'error.html', context)

user = request.user
if Audit.can_review(user, workflow_id, 2) is False:
context = {'errMsg': '你无权操作当前工单!'}
return render(request, 'error.html', context)

try:
# 存进数据库里
alterworkflow = SqlWorkflow.objects.get(id=workflow_id)
alterworkflow.starttime = starttime
alterworkflow.endtime = endtime
alterworkflow.save()
except Exception as msg:
context = {'errMsg': msg}
return render(request, 'error.html', context)

return HttpResponseRedirect(reverse('sql:detail', args=(workflow_id,)))

@permission_required('sql.sql_review', raise_exception=True)
def passed(request):
Expand Down Expand Up @@ -264,6 +298,10 @@ def execute(request):
if can_execute(request.user, workflow_id) is False:
context = {'errMsg': '你无权操作当前工单!'}
return render(request, 'error.html', context)

if on_Correct_time_period(workflow_id) is False:
context = {'errMsg': '请在正确的时间执行上线!'}
return render(request, 'error.html', context)
# 根据执行模式进行对应修改
mode = request.POST.get('mode')
if mode == "auto":
Expand Down
91 changes: 91 additions & 0 deletions sql/templates/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ <h4 style="display: inline;"><span>{{ workflow_detail.workflow_name }}</span></h
<th>
发起时间
</th>
<th>
可执行起始时间
</th>
<th>
可执行最后时间
</th>
<th>
结束时间
</th>
Expand Down Expand Up @@ -72,6 +78,12 @@ <h4 style="display: inline;"><span>{{ workflow_detail.workflow_name }}</span></h
<td>
{{ workflow_detail.create_time }}
</td>
<td>
{{ workflow_detail.starttime }}
</td>
<td>
{{ workflow_detail.endtime }}
</td>
<td>
{{ workflow_detail.finish_time }}
</td>
Expand Down Expand Up @@ -154,6 +166,15 @@ <h4 style="display: inline;"><span>{{ workflow_detail.workflow_name }}</span></h
placeholder="请填写审核备注/终止原因" rows=3></textarea>
<br>
{% endif %}
<!--审核人修改可执行时间按钮-->
{% if is_can_review %}
<form id="from-passed" action="/passed/" method="post" style="display:inline-block;">
{% csrf_token %}
<input type="hidden" name="workflow_id" value="{{ workflow_detail.id }}">
<input type="hidden" id="audit_remark" name="audit_remark" value="">
<input type="button" class="btn btn-info" data-toggle="modal" data-target="#Executabletime" value="可执行时间变更"/>
</form>
{% endif %}
<!--审核通过按钮-->
{% if is_can_review %}
<form id="from-passed" action="/passed/" method="post" style="display:inline-block;">
Expand Down Expand Up @@ -281,6 +302,41 @@ <h4 class="modal-title text-danger">执行进度</h4>
</div>
</div>

<!-- 修改可执行时间弹出框 -->
<form id="from-alterexecutabletime" action="/altertime/" method="post" style="display:inline-block;">
<div class="modal fade" id="Executabletime" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header ">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
aria-hidden="true">×</span></button>
<h4 class="modal-title text-danger">修改可执行时间</h4>
</div>
{% csrf_token %}
<div class="modal-body">
<input type="hidden" name="workflow_id" value="{{ workflow_detail.id }}">
<div class='form-group input-group date' id='datetimepicker_run_date_start'>
<input id="run_date_start" name="run_date_start" type='text' autocomplete="off" class="form-control"
placeholder="请选择可执行起始时间" value="{{ workflow_detail.starttime }}"/>
<span class="input-group-addon"><span class="glyphicon glyphicon-time"></span></span>
</div>
<div class='input-group date' id='datetimepicker_run_date_end'>
<input id="run_date_end" name="run_date_end" type='text' autocomplete="off" class="form-control"
placeholder="请选择最后可执行时间" value="{{ workflow_detail.endtime }}"/>
<span class="input-group-addon"><span class="glyphicon glyphicon-time"></span></span>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-info" data-dismiss="modal">取消</button>

<input type="hidden" name="workflow_id" value="{{ workflow_detail.id }}">
<input type="button" id="btnalterExecutabletime" class="btn btn-danger" value="确认提交"/>
</div>

</div>
</div>
</div>
</form>

{% endblock content %}

Expand All @@ -305,8 +361,35 @@ <h4 class="modal-title text-danger">执行进度</h4>
inline: true,
sideBySide: true,
minuteStep: 5,
});
$("#datetimepicker_run_date_start").datetimepicker({
format: "yyyy-mm-dd hh:ii",
autoclose: true,
todayBtn: true,
pickerPosition: "bottom-left",
language: 'zh-CN',//中文,需要引用zh-CN.js包
startView: 1,//天视图
minView: 0,//分钟
startDate: date,
inline: true,
sideBySide: true,
minuteStep: 5,
});
$("#datetimepicker_run_date_end").datetimepicker({
format: "yyyy-mm-dd hh:ii",
autoclose: true,
todayBtn: true,
pickerPosition: "bottom-left",
language: 'zh-CN',//中文,需要引用zh-CN.js包
startView: 1,//天视图
minView: 0,//分钟
startDate: date,
inline: true,
sideBySide: true,
minuteStep: 5,
})
</script>

<!--按钮控制 -->
<script>
var editWorkflowNname = "{{ workflow_detail.workflow_name }}";
Expand Down Expand Up @@ -398,6 +481,14 @@ <h4 class="modal-title text-danger">执行进度</h4>
}
});

// 修改可执行时间
$("#btnalterExecutabletime").click(function () {
//获取form对象,判断输入,通过则提交
var formAlterexecutabletime = $("#from-alterexecutabletime");
loading(this);
formAlterexecutabletime.submit();
});

// 按钮禁用
function loading(obj) {
$(obj).button('loading').delay(2500).queue(function () {
Expand Down
50 changes: 50 additions & 0 deletions sql/templates/sqlsubmit.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@
{% endfor %}
</select>
</div>
<!--可执行时间-->
<div class="form-group">
<div class='form-group input-group date' id='datetimepicker_run_date_start'>
<input id="run_date_start" name="run_date_start" type='text' autocomplete="off" class="form-control"
placeholder="请选择可执行起始时间"/>
<span class="input-group-addon"><span class="glyphicon glyphicon-time"></span></span>
</div>
<div class='input-group date' id='datetimepicker_run_date_end'>
<input id="run_date_end" name="run_date_end" type='text' autocomplete="off" class="form-control"
placeholder="请选择最后可执行时间"/>
<span class="input-group-addon"><span class="glyphicon glyphicon-time"></span></span>
</div>
</div>
<!--审批流程-->
<input type="hidden" id="workflow_auditors" name="workflow_auditors" data-name="审批流程"
data-placeholder="请配置审批流程!" required>
Expand Down Expand Up @@ -157,6 +170,43 @@ <h4 class="modal-title text-danger">提交信息确认</h4>
<script src="{% static 'bootstrap-fileinput/js/fileinput.min.js' %}"></script>
<script src="{% static 'bootstrap-fileinput/js/locales/zh.js' %}"></script>

<link href="{% static 'datetimepicker/css/bootstrap-datetimepicker.css' %}" rel="stylesheet" type="text/css"/>
<script src="{% static 'daterangepicker/js/moment.min.js' %}"></script>
<script src="{% static 'datetimepicker/js/bootstrap-datetimepicker.js' %}"></script>
<script src="{% static 'datetimepicker/js/bootstrap-datetimepicker.zh-CN.js' %}"></script>

<!--初始化时间控件 -->
<script>
var date = new Date();
$("#datetimepicker_run_date_start").datetimepicker({
format: "yyyy-mm-dd hh:ii",
autoclose: true,
todayBtn: true,
pickerPosition: "bottom-left",
language: 'zh-CN',//中文,需要引用zh-CN.js包
startView: 1,//天视图
minView: 0,//分钟
startDate: date,
inline: true,
sideBySide: true,
minuteStep: 5,
{#initialDate: new Date(),#}
});
$("#datetimepicker_run_date_end").datetimepicker({
format: "yyyy-mm-dd hh:ii",
autoclose: true,
todayBtn: true,
pickerPosition: "bottom-left",
language: 'zh-CN',//中文,需要引用zh-CN.js包
startView: 1,//天视图
minView: 0,//分钟
startDate: date,
inline: true,
sideBySide: true,
minuteStep: 5,
{#initialDate: new Date(),#}
})
</script>
<!--upload -->
<script>
//初始化上传控件
Expand Down
1 change: 1 addition & 0 deletions sql/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
path('passed/', sql_workflow.passed),
path('execute/', sql_workflow.execute),
path('timingtask/', sql_workflow.timing_task),
path('altertime/', sql_workflow.alter_time),
path('cancel/', sql_workflow.cancel),
path('rollback/', views.rollback),
path('sqlanalyze/', views.sqlanalyze),
Expand Down
22 changes: 22 additions & 0 deletions sql/utils/sql_review.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import re
import sqlparse

Expand Down Expand Up @@ -58,6 +59,11 @@ def can_execute(user, workflow_id):
"""
workflow_detail = SqlWorkflow.objects.get(id=workflow_id)
result = False
ctime = datetime.datetime.now()
stime = workflow_detail.starttime
etime = workflow_detail.endtime
if (stime and stime>ctime) or (etime and etime<ctime):
return result
# 只有审核通过和定时执行的数据才可以立即执行
if workflow_detail.status in ['workflow_review_pass', 'workflow_timingtask']:
# 当前登录用户有资源组粒度执行权限,并且为组内用户
Expand All @@ -69,6 +75,22 @@ def can_execute(user, workflow_id):
result = True
return result

def on_Correct_time_period(workflow_id):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

函数名全部小写

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯 , 我修改下 重新提, 昨天 看好久没动静 就撤掉了

"""
判断是否在可执行时间段内
:param user:
:param workflow_id:
:return:
"""
workflow_detail = SqlWorkflow.objects.get(id=workflow_id)
result = True
ctime = datetime.datetime.now()
stime = workflow_detail.starttime
etime = workflow_detail.endtime
if (stime and stime>ctime) or (etime and etime<ctime):
result = False
return result


def can_timingtask(user, workflow_id):
"""
Expand Down