def index_data(request): """ 切换首页展示数据 :param request: :return: """ user_id = request.session.get('user_id', '') # 从session中获取user_id if not user_id: return render(request, 'user/login_action.html') elif request.method == 'GET': off = request.GET.get('off', '') plan_list = [] prj_list = is_superuser(user_id, type='list', off=off) plan = Plan.objects.filter(project_id__in=prj_list) for plan_ in plan: plan_list.append(plan_.plan_id) if off == '1': project_num = Project.objects.aggregate( Count('prj_id'))['prj_id__count'] else: project_num = Project.objects.filter(user_id=user_id).aggregate( Count('prj_id'))['prj_id__count'] env_num = Environment.objects.filter( project_id__in=prj_list).aggregate( Count('env_id'))['env_id__count'] interface_num = Interface.objects.filter( project_id__in=prj_list).aggregate(Count('if_id'))['if_id__count'] case_num = Case.objects.filter(project_id__in=prj_list).aggregate( Count('case_id'))['case_id__count'] plan_num = Plan.objects.filter(project_id__in=prj_list).aggregate( Count('plan_id'))['plan_id__count'] sign_num = Sign.objects.aggregate(Count('sign_id'))['sign_id__count'] report_num = Report.objects.filter(plan_id__in=plan_list).aggregate( Count('report_id'))['report_id__count'] periodic_num = PeriodicTask.objects.aggregate(Count('id'))['id__count'] crontabSchedule_num = CrontabSchedule.objects.aggregate( Count('id'))['id__count'] user_num = User.objects.aggregate(Count('id'))['id__count'] info = { 'project_num': project_num, 'env_num': env_num, 'interface_num': interface_num, 'case_num': case_num, 'plan_num': plan_num, 'sign_num': sign_num, 'report_num': report_num, 'task_num': periodic_num + crontabSchedule_num, 'user_num': user_num } return JsonResponse(info)
def mock_api(request): user_id = request.session.get('user_id', '') if not get_user(user_id): request.session['login_from'] = '/base/plan/' return render(request, 'user/login_action.html') else: patt = re.compile('\d+') if request.method == 'POST': data = eval(request.body.decode('utf-8')) msg = insert_mock_data(**data) msg = json.dumps(msg) return HttpResponse(msg) elif request.method == "DELETE": id = patt.findall(request.get_full_path())[0] Api.objects.filter(id=int(id)).delete() msg = json.dumps(VALID) return HttpResponse(msg) elif request.method == 'PUT': id = patt.findall(request.get_full_path())[0] data = eval(request.body.decode('utf-8')) msg = update_mock_data(id, **json.loads(data)) msg = json.dumps(msg) return HttpResponse(msg) else: prj_list = is_superuser(user_id) api_name = request.GET.get('api_name', '') if api_name: api_info = Api.objects.filter(name__contains=api_name) else: api_info = Api.objects.all() api_list = [] for api in api_info: if api in api_list: pass else: api_list.append(api.project_id) api_prj_list = Project.objects.filter(prj_id__in=api_list) info = { 'prj_list': prj_list, 'api_info': api_info, 'api_prj_list': api_prj_list } return render(request, 'mocks/index.html', info)
def index(request): """ 首页展示数据 :param request: :return: """ user_id = request.session.get('user_id', '') # 从session中获取user_id if not user_id: return render(request, 'user/login_action.html') if request.method == 'POST': if get_user(user_id): url = request.POST.get('url', '') path = request.POST.get('path', '') appid = request.POST.get('appid', '') appsecret = request.POST.get('appsecret', '') if appid != '' and appsecret != '': if path == '': path = '?from=1' url = {'path': path} qr_code_name = getACodeImage(appid, appsecret, url) if not qr_code_name: log.info('用户 {} 生成小程序码 失败!appid、appsecret错误,未返回正确的token!') return HttpResponse('2') else: log.info('用户 {} 生成小程序码 {}-->{}'.format( user_id, qr_code_name, url)) return JsonResponse(str(qr_code_name), safe=False) elif url != '': qr_code_name = gr_code(url) log.info('用户 {} 生成二维码 {}-->{}'.format(user_id, qr_code_name, url)) return JsonResponse(str(qr_code_name), safe=False) else: return HttpResponse('1') else: return HttpResponse('0') elif request.method == 'GET': plan_list = [] prj_list = is_superuser(user_id, type='list') plan = Plan.objects.filter(project_id__in=prj_list) for plan_ in plan: plan_list.append(plan_.plan_id) superuser = User.objects.get(id=user_id).is_superuser if superuser: project_num = Project.objects.aggregate( Count('prj_id'))['prj_id__count'] else: project_num = Project.objects.filter(user_id=user_id).aggregate( Count('prj_id'))['prj_id__count'] env_num = Environment.objects.filter( project_id__in=prj_list).aggregate( Count('env_id'))['env_id__count'] interface_num = Interface.objects.filter( project_id__in=prj_list).aggregate(Count('if_id'))['if_id__count'] case_num = Case.objects.filter(project_id__in=prj_list).aggregate( Count('case_id'))['case_id__count'] plan_num = Plan.objects.filter(project_id__in=prj_list).aggregate( Count('plan_id'))['plan_id__count'] sign_num = Sign.objects.aggregate(Count('sign_id'))['sign_id__count'] report_num = Report.objects.filter(plan_id__in=plan_list).aggregate( Count('report_id'))['report_id__count'] periodic_num = PeriodicTask.objects.aggregate(Count('id'))['id__count'] crontabSchedule_num = CrontabSchedule.objects.aggregate( Count('id'))['id__count'] user_num = User.objects.aggregate(Count('id'))['id__count'] total = get_total_values(user_id) if 'HTTP_X_FORWARDED_FOR' in request.META: # 在线人数 ip = request.META['HTTP_X_FORWARDED_FOR'] else: ip = request.META['REMOTE_ADDR'] online_ips = cache.get("online_ips", []) if online_ips: online_ips = list(cache.get_many(online_ips).keys()) cache.set(ip, 0, 5 * 60) if ip not in online_ips: online_ips.append(ip) cache.set("online_ips", online_ips) log.info('同时在线人员:{}'.format(cache.get("online_ips", []))) info = { 'project_num': project_num, 'env_num': env_num, 'interface_num': interface_num, 'case_num': case_num, 'plan_num': plan_num, 'total': total, 'sign_num': sign_num, 'report_num': report_num, "online_ips": len(cache.get("online_ips", [])), 'task_num': periodic_num + crontabSchedule_num, 'user_num': user_num } return render(request, "index.html", info)