说明
Django用的比较多,最近自己的taskmonitor项目添加了采集爬虫相关的内容。总结一下,其中的代码片段。
代码片段
改变状态颜色
@admin.display(description='调度状态', ordering='status')
def status_colored(self, obj):
colors = { 0: 'red', 1: 'green' }
return format_html(
'<p style="color:{};">{}</p>',
colors[obj.status],
STATUS_CHOICE[obj.status],
)
重写保存函数
#重写保存函数
def save_model(self, request, obj, form, change):
#新增默认不启动计划任务,测试后调整调度状态
if change:
if len(obj.crond.split(' ')) != 5:
obj.status = 0
else:
obj.status = 0
#保存
super(SpiderTaskAdmin, self).save_model(request, obj, form, change)
任务调度开关
def schedule_switch(self, request, queryset):
"""
任务调度开关
"""
for obj in queryset:
c_len= len(obj.crond.strip().split())
if c_len == 5:
if obj.status == 1:
obj.status = 0
res = remove_celery_task(obj.id)
else:
print(obj.status)
obj.status = 1
res = add_celery_task(obj.id)
if res:
obj.save()
自定义操作
操作列设置
# 定义一些操作示例
@admin.display(description='手动操作', ordering='sname')
def operate(self, obj):
#容器名称
con_name = 'spider_{}_1'.format(obj.sname)
#获取容器
containers = get_containers({"name":con_name})
#操作容器
if containers:
uparas = '{}-正在运行...'.format(con_name)
up_btn = "<button onclick='self.parent.app.$msgbox(\"{u_d}\")' class='el-button el-button--info el-button--small'>启动</button>"
dparas = "'{}','{}'".format(obj.sname, '0')
down_btn = "<button onclick='javascript:spidermanager({d_d})' class='el-button el-button--danger el-button--small'>停止</button>"
else:
uparas = "'{}','{}'".format(obj.sname, '1')
up_btn = "<button onclick='javascript:spidermanager({u_d})' class='el-button el-button--success el-button--small'>启动</button>"
dparas = '{}-不存在'.format(con_name)
down_btn = "<button onclick='self.parent.app.$msgbox(\"{d_d}\")' class='el-button el-button--info el-button--small'>停止</button>"
# 查看日志
if obj.logpath:
log_data = {'name':'{}|日志'.format(con_name), 'icon': 'fas fa-user-tie', 'url':'/spider/getlog/?path={}'.format(obj.logpath)}
log_btn = "<button onclick='self.parent.app.openTab({l_d})' class='el-button el-button--primary el-button--small'>最新日志</button>"
else:
log_data = '{}-最新日志无'.format(con_name)
log_btn = "<button onclick='self.parent.app.$msgbox(\"{l_d}\")' class='el-button el-button--info el-button--small'>最新日志</button>"
#拼接并返回
html_str = "<div>" + up_btn + down_btn + log_btn + "</div>"
return format_html(html_str, u_d=uparas, d_d=dparas, l_d=log_data)
自定义js
class Media:
js = ('js/common.js',)
js代码
//spidermanager函数传入cname和opera两个参数
//cname 容器名
//opera 启动、关闭
function spidermanager(sname, opera){
var url = "/spider/manager/?sname="+sname+"&opera="+String(opera);
var Http = new XMLHttpRequest();
Http.open("GET", url, false);
Http.send(null);
console.log(Http.responseText);
}
接口代码
def manager(request):
'''
容器操作
'''
if request.method == 'GET':
opera = request.GET.get('opera', default='2')
sname = request.GET.get('sname', default='')
if not sname or opera == '2':
messages.error(request, '参数非法')
else:
con_name = 'spider_{}_1'.format(sname)
containers = get_containers({"name":con_name})
if containers:
if opera == '1':
messages.info(request, '容器:{} |已运行...'.format(con_name))
else:
for container in containers:
container.stop()
messages.success(request, '容器:{} |成功停止...'.format(con_name))
else:
if opera == '1':
sync_start_process.delay(sname)
messages.success(request, '容器:{} |成功启动...'.format(con_name))
else:
messages.info(request, '容器:{} |未运行...'.format(con_name))
return HttpResponse('ok')
评论 (0)