コード例 #1
0
def lable_edit(request, id):
    header_title = ["标签管理", "修改标签"]
    title = header_title[-1]

    label = Label.objects.get(uuid=id)

    form = LabelForm(instance=label)

    host_all = get_node_list(request)
    host_select = host_all.filter(label=label)
    host_no_select = [a for a in host_all if a not in host_select]

    if request.method == "POST":
        form = LabelForm(request.POST, instance=Label.objects.get(uuid=id))
        if form.is_valid():
            serverSelect = request.POST.getlist('hostSelect')
            serverNoSelect = request.POST.getlist('hostNoSelect')
            for h in serverSelect:
                label.server_set.add(Server.objects.get(uuid=h))
            for h in serverNoSelect:
                label.server_set.remove(Server.objects.get(uuid=h))

            form.save()
            return redirect('lable_list')
    return render(request, 'assets/lable_edit.html', locals())
コード例 #2
0
def project_edit(request, id):
    header_title = ["主机组管理", "修改主机组"]
    title = header_title[-1]
    form = ProjectFrom(instance=Project.objects.get(uuid=id))
    project = Project.objects.get(uuid=id)

    host_all = get_node_list(request)

    host_select = host_all.filter(project=project)
    host_no_select = [a for a in host_all if a not in host_select]

    if request.method == "POST":
        form = ProjectFrom(request.POST, instance=Project.objects.get(uuid=id))

        if form.is_valid():
            serverSelect = request.POST.getlist('hostSelect')
            serverNoSelect = request.POST.getlist('hostNoSelect')
            for h in serverSelect:
                project.server_set.add(Server.objects.get(uuid=h))
            for h in serverNoSelect:
                project.server_set.remove(Server.objects.get(uuid=h))
            form.save()
            return redirect('project_list')
        else:
            print(form)
    return render(request, 'assets/project_edit.html', context=locals())
コード例 #3
0
def displayServerList(request):
    '''
    selectpicker 任务管理模块获取主机列表
    :param request:
    :return:
    '''
    if request.is_ajax():
        try:
            data = json.loads(request.POST.get('data'))
            # print(data)
            server_select = get_node_list(request)

            query = {}
            for i, v in data.items():
                if not v: continue
                if i == 'idc_select':
                    query['idc_name__uuid__in'] = v
                elif i == 'project_select':
                    query['project__uuid__in'] = v
                elif i == 'label_select':
                    query['label__uuid__in'] = v

            server_select = server_select.filter(**query).values(
                'name', 'uuid')
            res_data = [x for x in server_select]
            # print(res_data)
            res = {'status': 'success', 'data': res_data}
        except:
            res = {'status': 'failed', 'data': ''}

        return HttpResponse(json.dumps(res, ensure_ascii=Files))
コード例 #4
0
def idc_edit(request, id, server_all=None):
    header_title = ["机房管理", "修改机房"]
    title = header_title[-1]
    server_all = get_node_list(request)

    form = IdcForm(instance=IDC.objects.get(uuid=id))

    host_select = server_all.filter(idc_name=IDC.objects.get(uuid=id))
    host_no_select = [a for a in server_all if a not in host_select]
    if request.method == "POST":
        form = IdcForm(request.POST, instance=IDC.objects.get(uuid=id))
        # print(form)
        if form.is_valid():
            host_select = request.POST.getlist('hostSelect')
            # host_no_select = request.POST.getlist('hostNoSelect')
            idcObj = IDC.objects.get(uuid=id)
            Server.objects.filter(uuid__in=host_select).update(idc_name=idcObj)
            form.save()
            return redirect('idc_list')
        else:
            print('form err')
    return render(request, 'assets/idc_edit.html', locals())
コード例 #5
0
def getServerList(request):
    '''
    datatable 获取主机列表
    :param request:
    :return:
    '''
    server_all = get_node_list(request)
    print(server_all)

    if request.method == "POST":

        postData = request.POST
        for i in postData:
            if i.startswith('order[0][column]'):
                orderColumnName = "columns[%s][name]" % postData.get(i, 1)
                orderColumnValue = postData.get(orderColumnName)
            if i == 'order[0][dir]':
                orderDirValue = postData.get(i)
            if i == 'search[value]':
                searchValue = postData.get(i)

        displayLength = request.POST.get('length', 10)
        displayStart = int(request.POST.get('start', 0))
        draw = postData.get('draw', 1)

        # last_time = Server.objects.order_by('-create_time').first().create_time

        # server_all = Server.objects.filter(create_time=last_time).order_by('create_time')
        try:
            if orderColumnValue:
                if orderDirValue == 'desc':
                    server_all = server_all.order_by('-%s' % orderColumnValue)
                else:
                    server_all = server_all.order_by('%s' % orderColumnValue)
        except:
            pass
        try:
            if searchValue:
                server_all = server_all.filter(
                    Q(name__icontains=searchValue)
                    | Q(idc_name__name__icontains=searchValue)
                    | Q(project__name__icontains=searchValue))
        except:
            pass

        resultLength = server_all.count()

        paginator = Paginator(server_all, displayLength)

        try:
            server_all = paginator.page(displayStart / 10 + 1)
        except PageNotAnInteger:
            server_all = paginator.page(1)
        except EmptyPage:
            server_all = paginator.page(paginator.num_pages)

        dataTable = {
            'draw': draw,
            'recordsTotal': resultLength,
            'recordsFiltered': resultLength
        }
        data = []
        for item in server_all:
            project = [x[2] for x in item.project.values_list()]
            lable = [x[2] for x in item.label.values_list()]
            hardware = [item.cpu_nums, item.mem, item.disk]

            row = {
                'id': str(item.uuid),
                'name': str(item.name),
                'ip': item.ip,
                'project': project,
                'label': lable,
                'is_active': item.is_active,
                'idc': item.idc_name.name,
                'hardware': hardware
            }

            data.append(row)
        dataTable['data'] = data

        return HttpResponse(json.dumps(dataTable, ensure_ascii=False))
コード例 #6
0
ファイル: views.py プロジェクト: larryzl/Fortress
 def post(self, request):
     server_all = get_node_list(request)