templates/add_server.html edit_server.html search_server.html
{% extends 'base.html' %}
{% block title %}
机房名字
主机名字
ip地址
操作系统
状态
{% if serverlist.has_previous %}
上一页
{% endif %}
Page {{ serverlist.number }} of {{ serverlist.paginator.num_pages }}.
{% if serverlist.has_next %}
下一页
{% endif %}
{% endblock %}
{% block content %}
主机名字 | 制造商/型号 | IP | 操作系统 | CPU | 内存 | 硬盘 | 状态 | 机房 | 操作 | |
---|---|---|---|---|---|---|---|---|---|---|
{{ s.hostname }} | {{ s.model }} | {{ s.ip }} | {{ s.os }} | {{ s.cpu }} | {{ s.memory }} | {{ s.disk }} | {% if s.status == 0 %} 在线 {% elif s.status == 1 %} 已下线 {% elif s.status == 2 %} 未知 {% elif s.status == 3 %} 故障 {% elif s.status == 4 %} 备用 {% endif %} | {{ s.machinaroom.name }} | 编辑 删除 |
{% endblock %}
cmdbapp/serverviews.py
-- coding: utf-8 --
from future import unicode_literals
from django.shortcuts import render,render_to_response
from django.core.paginator import Paginator,InvalidPage,EmptyPage
from cmdbapp.models import *
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from xlwt import *
import StringIO
def search_server(request):
error = False
each_page = 8
mname=request.GET.get('mname','').strip()
sname=request.GET.get('sname','').strip()
ip=request.GET.get('ip','').strip()
os=request.GET.get('os','').strip()
status=int(request.GET.get('status',100))
if mname=='' and sname=='' and ip=='' and os=='' and status==100:
record_list = Server.objects.all()
paginator = Paginator(record_list,each_page)
try:
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1
try:
contacts = paginator.page(page)
except (EmptyPage, InvalidPage):
contacts = paginator.page(paginator.num_pages)
return render_to_response('search_server.html',{'serverlist':contacts,"mname":mname,"sname":sname,"ip":ip,"os":os,"status":status})
else:
q={}
if mname!='' and sname=='' and ip=='' and os=='' and status==100:
a=Machinaroom.objects.filter(name__icontains=mname).values("id")
q['machinaroom__in']=a
if sname!='':
q['hostname__icontains']=sname
if ip!='':
q['ip__icontains']=ip
if os!='':
q['os__icontains']=os
if status!=100:
q['status']=status
record_list = Server.objects.filter(**q)
paginator = Paginator(record_list,each_page)
try:
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1
try:
contacts = paginator.page(page)
except (EmptyPage, InvalidPage):
contacts = paginator.page(paginator.num_pages)
return render_to_response('search_server.html',{'serverlist':contacts,"mname":mname,"sname":sname,"ip":ip,"os":os,"status":status})
def add_server(request):
if request.method=='GET':
objs=Machinaroom.objects.all()
return render_to_response("add_server.html",{"machinaroomlist":objs})
else:
hostname=request.POST.get("serverhostname","")
model=request.POST.get("servermodel","")
ip=request.POST.get("serverip","")
os=request.POST.get("serveros","")
cpu=request.POST.get("servercpu","")
memory=request.POST.get("servermemory","")
disk=request.POST.get("serverdisk","")
status=request.POST.get("serverstatus",0)
machinaroomid=request.POST.get("servermachinaroom")
machinaroom=Machinaroom.objects.get(id=int(machinaroomid))
uu=Server(hostname=hostname,model=model,ip=ip,os=os,cpu=cpu,memory=memory,disk=disk,status=status,machinaroom=machinaroom)
uu.save()
return HttpResponseRedirect("add_server.html")
def edit_server(request):
if request.method=='GET':
obj=Server.objects.get(id=int(request.GET.get('id')))
objs=Machinaroom.objects.all()
return render_to_response('edit_server.html',{'server':obj,'machinaroomlist':objs})
else:
id=request.POST.get("id")
hostname=request.POST.get("serverhostname","")
model=request.POST.get("servermodel","")
ip=request.POST.get("serverip","")
os=request.POST.get("serveros","")
cpu=request.POST.get("servercpu","")
memory=request.POST.get("servermemory","")
disk=request.POST.get("serverdisk","")
status=request.POST.get("serverstatus",0)
machinaroomid=request.POST.get("servermachinaroom")
machinaroom=Machinaroom.objects.get(id=int(machinaroomid))
uu=Server.objects.filter(id=id).update(hostname=hostname,model=model,ip=ip,os=os,cpu=cpu,memory=memory,disk=disk,status=status,machinaroom=machinaroom)
return HttpResponseRedirect("edit_server.html?id="+id)
def del_server(request):
id=request.GET.get('id')
iid=int(id)
Server.objects.filter(id=iid).delete()
mname=request.GET.get('mname','').strip()
sname=request.GET.get('sname','').strip()
ip=request.GET.get('ip','').strip()
os=request.GET.get('os','').strip()
status=request.GET.get('status',100)
page=request.GET.get('page','1')
return HttpResponseRedirect('search_server.html?mname='+mname+"&&page="+page+"&&sname="+sname+"&&ip="+ip+"&&os="+os+"&&status="+status)
def batdel_server(request):
ids=request.GET.get('ids')
b=ids.split(',')
arr = map(int,b)
for aaa in arr:
Server.objects.filter(id=aaa).delete()
mname=request.GET.get('mname')
sname=request.GET.get('sname','').strip()
ip=request.GET.get('ip','').strip()
os=request.GET.get('os','').strip()
status=request.GET.get('status',100)
return HttpResponseRedirect('search_server.html?mname='+mname+"&&sname="+sname+"&&ip="+ip+"&&os="+os+"&&status="+status)
def excel_server(request):
mname=request.GET.get('mname','').strip()
sname=request.GET.get('sname','').strip()
ip=request.GET.get('ip','').strip()
os=request.GET.get('os','').strip()
status=int(request.GET.get('status',100))
if mname=='' and sname=='' and ip=='' and os=='' and status==100:
list_obj = Server.objects.all()
else:
q={}
if mname!='' and sname=='' and ip=='' and os=='' and status==100:
a=Machinaroom.objects.filter(name__icontains=mname).values("id")
q['machinaroom__in']=a
if sname!='':
q['hostname__icontains']=sname
if ip!='':
q['ip__icontains']=ip
if os!='':
q['os__icontains']=os
if status!=100:
q['status']=status
list_obj = Server.objects.filter(**q)
if list_obj:
# 创建工作薄
ws = Workbook(encoding='utf-8')
w = ws.add_sheet(u"服务器清单")
w.write(0, 0, "id")
w.write(0, 1, u"主机名字")
w.write(0, 2, u"制造商/型号")
w.write(0, 3, u"IP")
w.write(0, 4, u"操作系统")
w.write(0,5,u"CPU")
w.write(0,6,u"内存")
w.write(0,7,u"硬盘")
w.write(0,8,u"状态")
w.write(0,9,u"机房")
# 写入数据
excel_row = 1
for obj in list_obj:
data_id = obj.id
data_hostname = obj.hostname
data_model = obj.model
data_ip = obj.ip
data_os = obj.os
data_cpu=obj.cpu
data_memory=obj.memory
data_disk=obj.disk
if obj.status == 0:
data_status='在线'
elif obj.status == 1:
data_status='已下线'
elif obj.status == 2:
data_status='未知'
elif obj.status == 3:
data_status='故障'
else:
data_status='备用'
data_machinaroom=obj.machinaroom.name
w.write(excel_row, 0, data_id)
w.write(excel_row, 1, data_hostname)
w.write(excel_row, 2, data_model)
w.write(excel_row, 3, data_ip)
w.write(excel_row, 4, data_os)
w.write(excel_row, 5, data_cpu)
w.write(excel_row, 6, data_memory)
w.write(excel_row, 7, data_disk)
w.write(excel_row, 8, data_status)
w.write(excel_row, 9, data_machinaroom)
excel_row += 1
sio = StringIO.StringIO()
ws.save(sio)
sio.seek(0)
response = HttpResponse(sio.getvalue(), content_type='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename=服务器清单-'+mname+sname+ip+os+'.xls'
response.write(sio.getvalue())
return response