def JobListAPI(request): ''' job list api ''' if request.GET.get('dt_till', ''): dt_till = request.GET.get('dt_till', '') dt_from = request.GET.get('dt_from', '') else: dt_till = int(time.time()) interval = int(request.GET.get('interval', '')) if request.GET.get( 'interval', '') else 60 * 60 * 24 dt_from = dt_till - interval dt_till = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(dt_till)) dt_from = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(dt_from)) if request.user.is_superuser: jobs = mongo.dataPlus.joblist.find({ 'time': { '$gte': dt_from, '$lte': dt_till } }).sort([('_id', -1)]) else: jobs = mongo.dataPlus.joblist.find({ 'user': request.user.username, 'time': { '$gte': dt_from, '$lte': dt_till } }).sort([('_id', -1)]) data = [{'user': job['user'], 'time': job['time'], 'client': job['client'], 'target': job['target'], 'fun': job['fun'], \ 'arg': job['arg'], 'progress': job['progress'], 'cjid': job['cjid'], 'status': job['status'] } for job in jobs ] page = request.GET.get('page', 1) limit = request.GET.get('limit', 10) count, data = my_paginator(data, page, limit) res = {'code': 0, 'msg': '', 'count': count, 'data': data} return JsonResponse(res)
def UserListAPI(request): # 查询处理 search_filter = request.GET.get('search_filter', '') role = request.GET.get('role', '') q = Q(username__icontains=search_filter) | Q( email__icontains=search_filter) if role: user_set = User.objects.select_related().filter( role__name__icontains=role).filter(q).distinct() else: user_set = User.objects.select_related().filter(q).distinct() # 构造返回数据结构 user_list = [] for user in user_set: roles = [] for role in user.role_set.all(): roles.append(role.name) user_instance = {'id': user.id, 'username': user.username, 'email': user.email, 'roles': roles, \ 'is_active': '已激活' if user.is_active else '未激活', 'phone': user.profile.phone, \ 'wechat': user.profile.wechat, 'comment': user.profile.comment, \ 'last_login': (user.last_login + datetime.timedelta(hours=8)).strftime( '%Y-%m-%d %H:%M:%S' ) \ if user.last_login else user.last_login} user_list.append(user_instance) # 分页处理 page = request.GET.get('page', 1) limit = request.GET.get('limit', 10) count, data = my_paginator(user_list, page, limit) res = {'code': 0, 'msg': '', 'count': count, 'data': data} return JsonResponse(res)
def HostSearch(request): data = { key: value for key, value in request.GET.items() if value and (key not in ['csrfmiddlewaretoken', 'page', 'limit']) } if data: query_filter = {} for key, value in data.items(): query_filter[key] = {'$regex': value, '$options': 'i'} hosts = mongo.dataPlus.host.find(query_filter) else: hosts = mongo.dataPlus.host.find({}) # 构造返回数据结构 host_list = [] for host in hosts: host_instance = {'hostname': host['hostname'], 'ip': host['ip'], 'os': host['os'], 'cpu': host['cpu'], \ 'mem': host['mem'], 'is_virtual': host['is_virtual'], 'minion_status': host['minion_status'], \ 'minion_id': host['minion_id'], 'salt_key': host['salt_key']} host_list.append(host_instance) # 分页处理 page = request.GET.get('page', 1) limit = request.GET.get('limit', 10) count, data = my_paginator(host_list, page, limit) res = {'code': 0, 'msg': '', 'count': count, 'data': data} return JsonResponse(res)
def CronListAPI(request): job_instances = scheduler.get_jobs() data = [{'id':job.id, 'cron_string':job.meta['cron_string'], 'targets':','.join(job.args[2]), \ 'arg':job.args[3], 'created_at':job.created_at.strftime("%Y-%m-%d %H:%M:%S"), \ 'last_exacuted':job.ended_at.strftime("%Y-%m-%d %H:%M:%S") if job.ended_at else ''} for job in job_instances] page = request.GET.get('page', 1) limit = request.GET.get('limit', 10) count, data = my_paginator(data, page, limit) res = {'code': 0, 'msg': '', 'count': count, 'data': data} return JsonResponse(res)
def ScriptListAPI(request): ''' script list API''' keyword = request.GET.get('keyword', '') if keyword: q = Q(code__icontains=keyword) | Q(name__icontains=keyword) scripts = File.objects.filter(file_type='script').filter(q) else: scripts = File.objects.filter(file_type='script') data = [{'id': script.id, 'code': script.code, 'name': script.name, 'file_type': script.file_type, 'created_by': script.created_by, \ 'comment': script.comment, 'create_time': script.create_time.strftime("%Y:%m:%d %H:%M:%S"), 'update_time': script.update_time.strftime("%Y:%m:%d %H:%M:%S")} for script in scripts ] page = request.GET.get('page', 1) limit = request.GET.get('limit', 10) count, data = my_paginator(data, page, limit) res = {'code': 0, 'msg': '', 'count': count, 'data': data} return JsonResponse(res)
def MenuListAPI(request): menus = Menu.objects.all() # 构造返回数据 data = [{ 'id': menu.id, 'name': menu.name, 'code': menu.code, 'url': menu.url, 'comment': menu.comment } for menu in menus] # 分页处理 page = request.GET.get('page', 1) limit = request.GET.get('limit', 10) count, data = my_paginator(data, page, limit) res = {'code': 0, 'msg': '', 'count': count, 'data': data} return JsonResponse(res)
def HostListAPI(request): # 查询处理 search_filter = request.GET.get('search_filter', '') q = Q(ip__icontains=search_filter) | Q( hostname__icontains=search_filter) | Q( admins__username__icontains=search_filter) hosts = Host.objects.select_related().filter(q).distinct() # 构造返回数据结构 host_list = [] for host in hosts: host_instance = {'hostname': host.hostname, 'ip': host.ip, 'os': host.os, 'cpu': host.cpu, \ 'mem': host.mem, 'is_virtual': host.is_virtual, 'minion_status': host.minion_status, \ 'minion_id': host.minion_id, 'salt_key': host.salt_key} host_list.append(host_instance) # 分页处理 page = request.GET.get('page', 1) limit = request.GET.get('limit', 10) count, data = my_paginator(host_list, page, limit) res = {'code': 0, 'msg': '', 'count': count, 'data': data} return JsonResponse(res)