Ejemplo n.º 1
0
def index(request, user_dict):
    '''
    index页面函数
    '''
    form = CreatAssetForm()
    search_str = request.GET.get("search_string", "")  #得到按主机名过滤条件,默认为""
    print(search_str)
    if request.method == "GET" and user_dict[
            'user'] == 'admin':  #如果登录用户是admin,则显示所有主机条目信息
        all_asset_list = Asset.objects.all().filter(
            hostname__icontains=search_str)
    elif request.method == "GET" and user_dict[
            'user'] != 'admin':  #如果不是,则显示当前登录用户所属业务线的主机条目信息
        all_asset_list = Asset.objects.filter(
            bussiness__userinfo__username=user_dict['user'],
            hostname__icontains=search_str)
        print(all_asset_list.count())
    # all_users = UserInfo.objects.values_list("username") #所有用户名称
    # all_env = Env.objects.values_list("name") #所有主机环境名称
    username = user_dict['user']  #当前登录用户
    # page_num=request.GET.get("page_num") #获取前端点击页码

    try:
        current_page = int(request.GET.get('page_num'))
    except TypeError:
        current_page = 1
    all_count = all_asset_list.count()
    # page_obj = Page(current_page,all_count,'/hosts.html')
    page_obj = Page(current_page, all_count, request.path_info, search_str)
    all_asset_list = all_asset_list[page_obj.start:page_obj.end]
    page_str = page_obj.page_html()

    # p=Paginator(all_asset_list,10) #分页器对象
    #
    # try:
    #     all_asset_list=p.page(page_num) #当前页码的主机条目对象
    #     now_page_range = p.page_range[int(page_num)-1:int(page_num)+8] #最多显示9条记录
    # except PageNotAnInteger:
    #     all_asset_list=p.page(1) #访问/bms/index/页面时page_num=""传入p.page()会报错,所以让显示的主机条目为第一页
    #     now_page_range=p.page_range[0:9] #最多显示9条记录
    # except EmptyPage:
    #     all_asset_list=p.page(p.num_pages) #超过页码总数,则显示最后一页

    # print(p.page_range)
    #
    # print(all_asset_list)
    # print(request.GET, type(request.GET))
    # print(request.GET.urlencode())

    from django.http.request import QueryDict

    url_obj = QueryDict(mutable=True)
    url_obj['_getarg'] = request.GET.urlencode()

    # print(url_obj.urlencode())
    url_param = url_obj.urlencode()

    return render(request, 'index.html', locals())
Ejemplo n.º 2
0
def hosts(request):
    current_page = int(request.GET.get('page'))
    all_count = models.Host.objects.all().count()
    # page_obj=Page(current_page,all_count,'/hosts.html')
    page_obj = Page(current_page, all_count,
                    request.path_info)  #path_info包含当前url
    # host_list=models.Host.objects.all()[page_obj.start(),page_obj.end()] 添加property装饰器后不用再加()调用
    host_list = models.Host.objects.all()[page_obj.start:page_obj.end]
    page_str = page_obj.page_html()
    return render(request, 'hosts.html', {
        'host_list': host_list,
        'page_str': page_str
    })
Ejemplo n.º 3
0
def users(request, user_dict, id):
    search_str = request.GET.get("search_string", "")
    username = user_dict['user']
    if request.method == "GET" and user_dict[
            'user'] == 'admin':  #admin用户登录(get)时
        user_form = UserForm()
        all_user_list = UserInfo.objects.all().filter(
            username__icontains=search_str)
        # 分页功能:
        try:
            current_page = int(request.GET.get('page_num'))
        except TypeError:
            current_page = 1
        all_count = all_user_list.count()
        # page_obj = Page(current_page,all_count,'/hosts.html')
        page_obj = Page(current_page, all_count, request.path_info, search_str)
        all_user_list = all_user_list[page_obj.start:page_obj.end]
        page_str = page_obj.page_html()

        return render(request, 'users.html', locals())

    elif request.method == "POST":
        response = {'status': True, 'data': None, 'msg': None}
        user_form = UserForm(data=request.POST)
        if user_form.is_valid():  #如果POST过来的数据有效
            print("得到有效字段:", user_form.cleaned_data)
            username = user_form.cleaned_data.pop("username")  # 去掉 用户名字段
            bid_list = user_form.cleaned_data.pop(
                "bussinesslines")  #得到用户对应的业务线id
            UserInfo.objects.filter(
                id=id).update(**user_form.cleaned_data)  # 更改除了用户名,业务线其他字段

            if user_dict['user'] == 'admin':
                print("要更新的业务线id:", bid_list)
                obj = UserInfo.objects.filter(id=id).first()  # 要修改的用户obj
                obj.bussinesslines.set(bid_list)  # 只有admin能修改用户对应的业务线
        else:  #无效则返回False和错误的msg
            response['status'] = False
            response['msg'] = user_form.errors
        return HttpResponse(json.dumps(response))
    else:  #其他用户get时
        user_form = UserForm()
        all_user_list = UserInfo.objects.filter(username=username).filter(
            username__icontains=search_str)
        return render(request, 'users.html', locals())
Ejemplo n.º 4
0
def authorization(request, user_dict, id):
    search_str = request.GET.get("search_string", "")
    username = user_dict['user']
    if request.method == "GET" and user_dict[
            'user'] == 'admin':  # admin用户登录(get)时
        b_form = BussinessForm()
        all_bussiness_list = BussinessLine.objects.all().filter(
            name__icontains=search_str)
        # 分页功能:
        print(all_bussiness_list)
        try:
            current_page = int(request.GET.get('page_num'))
        except TypeError:
            current_page = 1
        all_count = all_bussiness_list.count()
        # page_obj = Page(current_page,all_count,'/hosts.html')
        page_obj = Page(current_page, all_count, request.path_info, search_str)
        all_user_list = all_bussiness_list[page_obj.start:page_obj.end]
        page_str = page_obj.page_html()

        return render(request, 'authorization.html', locals())

    elif request.method == "POST":
        response = {'status': True, 'data': None, 'msg': None}
        b_form = BussinessForm(data=request.POST)
        if b_form.is_valid():
            print("得到有效字段:", b_form.cleaned_data)
            name = b_form.cleaned_data.pop("name")  # 去掉业务线名字段
            BussinessLine.objects.filter(
                id=id).update(**b_form.cleaned_data)  # 更改除了业务线名,其他字段

            if user_dict['user'] == 'admin':
                print("要更新的业务线名:", name)
                BussinessLine.objects.filter(id=id).update(
                    name=name)  # 只有admin能修改用户对应的业务线

        else:  # 无效则返回False和错误的msg
            response['status'] = False
            response['msg'] = b_form.errors
        return HttpResponse(json.dumps(response))
    else:
        return render(request, 'users.html', locals())
Ejemplo n.º 5
0
def index(request,*args,**kwargs):
    '''
    主页面函数
    :param request:
    :param args:
    :param kwargs: {new_type_id:}
    :return:
    '''
    if request.method=="GET":
        register_form = RegisterForm()
        login_form = LoginForm()
        publish_form = PublishForm()
        search_str = request.GET.get("search_string", "")
        current_type_id = kwargs.get('new_type_id')
        if current_type_id:current_type_id=int(current_type_id)
        news_type_list = NewsType.objects.all()

        news_list = News.objects.filter(**kwargs).order_by('-ctime') # **kwargs <==> new_type_id = kwargs.get('new_type_id')


        try:
            current_page = int(request.GET.get('page_num'))
        except TypeError:
            current_page = 1
        all_count = news_list.count()
        # page_obj = Page(current_page,all_count,'/hosts.html')
        page_obj = Page(current_page, all_count, request.path_info, search_str)
        news_list = news_list[page_obj.start:page_obj.end]
        page_str = page_obj.page_html()

        print(news_list)

        user_dict = request.session.get('is_login', None)
        print(user_dict)
        if user_dict:
            username = user_dict['user']
            user_id = user_dict['user_id']
        return render(request,"index.html",locals())