Django 搭建CMDB系统完整[8](机房增删改查)

search_machinaroom.html

{% extends 'base.html' %}

{% block title %}
<script type="text/javascript" src="/static/scripts/jquery/jquery-1.7.1.js"></script>
<link href="/static/style/authority/basic_layout.css" rel="stylesheet" type="text/css">
<link href="/static/style/authority/common_style.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="/static/scripts/authority/commonAll.js"></script>
<script type="text/javascript" src="/static/scripts/fancybox/jquery.fancybox-1.3.4.js"></script>
<script type="text/javascript" src="/static/scripts/fancybox/jquery.fancybox-1.3.4.pack.js"></script>
<link rel="stylesheet" type="text/css" href="/static/style/authority/jquery.fancybox-1.3.4.css" media="screen"></link>
<script type="text/javascript" src="/static/scripts/artDialog/artDialog.js?skin=default"></script>

<div id="container">
<div class="ui_content">
<div class="ui_text_indent">
<div id="box_border">
<div id="box_top">搜索</div>
  机房名字  <input type="text" id="mname" name="mname" value="{{ mname }}" class="ui_input_txt02" />
</div>
<div id="box_top">新增/编辑</div>
<form id="submitForm" name="submitForm" method="post" action="add_machinaroom.html">
<input type="hidden" id="machinaroomid" name="machinaroomid"/>
  机房名字  <input type="text" id="machinaroomname" name="machinaroomname" class="ui_input_txt02" />
  机房位置  <input type="text" id="machinaroomlocation" name="machinaroomlocation" class="ui_input_txt02" />
  机房VPN  <input type="text" id="machinaroomvpnurl" name="machinaroomvpnurl" class="ui_input_txt02" />
  备注  <input type="text" id="machinaroommemo" name="machinaroommemo" class="ui_input_txt02" />
</div>
<div id="box_bottom">
<div class="pagination">
<span class="current">
{% if machinaroomlist.has_previous %}
<a href="javascript:void(0)" onclick="search_machina({{ machinaroomlist.previous_page_number }});">上一页</a>
{% endif %}

    <span class="current">
        Page {{ machinaroomlist.number }} of {{ machinaroomlist.paginator.num_pages }}.
    </span>

    {% if machinaroomlist.has_next %}
        <a href="javascript:void(0)" onclick="search_machina({{ machinaroomlist.next_page_number }});">下一页</a>
    {% endif %}
</span>

</div>
<input type="button" value="查询" class="ui_input_btn01" onclick="search_machina(1);"/>
<input type="submit" value="新增" class="ui_input_btn01" />
<input type="button" value="编辑" class="ui_input_btn01" onclick="edit_machinaroom({{ machinaroomlist.number }});" />
<input type="button" value="删除" class="ui_input_btn01" onclick="batdel_machinaroom();" />
<input type="button" value="导入" class="ui_input_btn01" id="importBtn" />
<input type="button" value="导出" class="ui_input_btn01" onclick="exportExcel();" />
</div>
</form>
</div>
</div>
{% endblock %}

{% block content %}

<div class="ui_content">
<div class="ui_tb">
<table class="table" cellspacing="0" cellpadding="0" width="100%" align="center" border="0">
<tr>
<th width="30"><input type="checkbox" id="id" name="id" />
</th>
<th>机房名字</th>
<th>机房位置</th>
<th>VPN链接</th>
<th>备注</th>
<th>操作</th>
</tr>
{% for mr in machinaroomlist.object_list %}
<tr>
<td><input type="checkbox" name="idcheck" value={{ mr.id }} class="acb" /></td>
<td>{{ mr.name }}</td>
<td>{{ mr.location }}</td>
<td>{{ mr.vpnurl }}</td>
<td>{{ mr.memo }}</td>
<td>
<a href="javascript:void(0)" onclick="edit_mr('{{ mr.id }}','{{ mr.name }}','{{ mr.location }}','{{ mr.vpnurl }}','{{ mr.memo }}')" class="edit">编辑</a>
<a href="javascript:void(0)" onclick="del_machinaroom({{ mr.id }},{{ machinaroomlist.number }});">删除</a>
</td>
</tr>
{% endfor %}
</table>
</div>
</div>
</div>
<script type="text/javascript">
function search_machina(page){
var text1 = document.getElementById("mname").value;
var myurl="search_machinaroom.html"+"?"+"mname="+text1+"&&page="+page;
window.location.assign(encodeURI(myurl))
}
function edit_mr(id,name,ll,vpnurl,memo){
document.getElementById("machinaroomid").value=id;
document.getElementById("machinaroomname").value=name;
document.getElementById("machinaroomlocation").value=ll;
document.getElementById("machinaroomvpnurl").value=vpnurl;
document.getElementById("machinaroommemo").value=memo;
}
function edit_machinaroom(page){
var text1 = document.getElementById("mname").value;
id=document.getElementById("machinaroomid").value;
name=document.getElementById("machinaroomname").value;
ll=document.getElementById("machinaroomlocation").value;
vpnurl=document.getElementById("machinaroomvpnurl").value;
memo=document.getElementById("machinaroommemo").value;
var myurl="edit_machinaroom.html"+"?"+"id="+id+"&&name="+name+"&&location="+ll+"&&vpnurl="+vpnurl+"&&memo="+memo+"&&mname="+text1+"&&page="+page;
window.location.assign(encodeURI(myurl))
}
function del_machinaroom(id,page){
if(window.confirm('确定要删除该记录吗?')){
var text1= document.getElementById("mname").value;
var myurl="del_machinaroom.html"+"?"+"id="+id+"&&mname="+text1+"&&page="+page;
window.location.assign(encodeURI(myurl))
return true;
}else{
//alert("取消");
return false;
}
}
function batdel_machinaroom(){
if(window.confirm('确定要删除记录吗?')){
obj = document.getElementsByName("idcheck");
var text1= document.getElementById("mname").value;
check_val = [];
for(k in obj){
if(obj[k].checked)
check_val.push(obj[k].value);
}
var myurl="batdel_machinaroom.html"+"?"+"ids="+check_val+"&&mname="+text1;
window.location.assign(encodeURI(myurl))
return true;
}else{
return false;
}
}
</script>
{% endblock %}

views.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

Create your views here.

def main_page(request):
return render_to_response('main_page.html', {'user':request.user })
def base(request):
return render_to_response('base.html')

def search_machinaroom(request):
error = False
each_page = 5
mname=request.GET.get('mname','0')
if mname=='0' or mname.strip()=='':
record_list = Machinaroom.objects.all()
mname=''
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_machinaroom.html',{'machinaroomlist':contacts,"mname":mname})
else:

        record_list = Machinaroom.objects.filter(name=mname)
        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_machinaroom.html',{'machinaroomlist':contacts,"mname":mname})

def add_machinaroom(request):
n = request.POST.get('machinaroomname')
l = request.POST.get('machinaroomlocation')
v = request.POST.get('machinaroomvpnurl')
m = request.POST.get('machinaroommemo')
Machinaroom.objects.create(name=n,location=l,vpnurl=v,memo=m)
return HttpResponseRedirect('search_machinaroom.html')
def edit_machinaroom(request):
id=request.GET.get('id')
iid=int(id)
name=request.GET.get('name')
location=request.GET.get('location')
vpnurl=request.GET.get('vpnurl')
memo=request.GET.get('memo')
Machinaroom.objects.filter(id=iid).update(name=name,location=location,vpnurl=vpnurl,memo=memo)
page=request.GET.get('page')
mname=request.GET.get('mname')
return HttpResponseRedirect('search_machinaroom.html?mname='+mname+"&&page="+page)
def del_machinaroom(request):
id=request.GET.get('id')
iid=int(id)
Machinaroom.objects.filter(id=iid).delete()
page=request.GET.get('page')
mname=request.GET.get('mname')
return HttpResponseRedirect('search_machinaroom.html?mname='+mname+"&&page="+page)
def batdel_machinaroom(request):
ids=request.GET.get('ids')
b=ids.split(',')
arr = map(int,b)
for aaa in arr:
Machinaroom.objects.filter(id=aaa).delete()
mname=request.GET.get('mname')
return HttpResponseRedirect('search_machinaroom.html?mname='+mname)

models.py

-- coding: utf-8 --

from future import unicode_literals

from django.db import models

class Machinaroom(models.Model):
id=models.AutoField(primary_key=True)
name=models.CharField(max_length=200,default=u'机房')
location=models.CharField(max_length=200,default=u'厦门')
vpnurl=models.URLField(default="")
memo = models.CharField(u'备注', max_length=200,default="")
def unicode(self):
return self.name
class Server(models.Model):
"""
服务器信息
"""
machinaroom= models.ForeignKey(Machinaroom)

    hostname = models.CharField(max_length=128, unique=True)
    sn = models.CharField('SN号', max_length=64,default="")
    manufacturer = models.CharField(verbose_name='制造商', max_length=64, default="")
    model = models.CharField('型号', max_length=64, default="")

    manage_ip = models.GenericIPAddressField('管理IP', default="")
    other_ip = models.GenericIPAddressField('其他IP', default="")

    os_platform = models.CharField('系统', max_length=16, default="")
    os_version = models.CharField('系统版本', max_length=16, default="")

    cpu_count = models.IntegerField('CPU个数', default=0)
    cpu_physical_count = models.IntegerField('CPU物理个数', default=0)
    cpu_model = models.CharField('CPU型号', max_length=128, default="")

    memory_model=models.CharField('memory型号', max_length=128, default="")
    memory_size=models.CharField('memory大小',max_length=128, default="")

    disk_memo=models.CharField('硬盘信息',max_length=200, default="")
    status_choices = ((0, '在线'),
                  (1, '已下线'),
                  (2, '未知'),
                  (3, '故障'),
                  (4, '备用'),
                  )
    status = models.SmallIntegerField(choices=status_choices, default=0)

    id =models.AutoField(primary_key=True)
    class Meta:
            verbose_name_plural = "服务器表"

    def __str__(self):
            return self.hostname

class VirtualMachina(models.Model):
"""虚拟机信息"""
server= models.ForeignKey(Server)
hostname = models.CharField(max_length=128, unique=True)
manage_ip = models.GenericIPAddressField('管理IP', default="")
other_ip = models.GenericIPAddressField('其他IP', default="")
os_platform = models.CharField('系统', max_length=16, default="")
os_version = models.CharField('系统版本', max_length=16, default="")
cpu_memo = models.CharField('CPU信息',max_length=128, default="")
memory_size=models.CharField('memory大小',max_length=128, default="")

    disk_memo=models.CharField('硬盘信息',max_length=200, default="")
    status_choices = ((0, '在线'),
                  (1, '已下线'),
                  (2, '未知'),
                  (3, '故障'),
                  (4, '备用'),
                  )
    status = models.SmallIntegerField(choices=status_choices, default=0)

    id =models.AutoField(primary_key=True)

    def __str__(self):
            return self.hostname

class NetworkDevice(models.Model):
status_choices = ((0, '在线'),
(1, '已下线'),
(2, '未知'),
(3, '故障'),
(4, '备用'),
)
status = models.SmallIntegerField(choices=status_choices, default=0)

    id =models.AutoField(primary_key=True)
    machinaroom= models.ForeignKey(Machinaroom)

    hostname = models.CharField(max_length=128, unique=True)
    port_num = models.SmallIntegerField(u'端口个数', default=0)
    device_detail = models.TextField(u'设置详细配置', default="")
    model = models.CharField(u'型号', max_length=128, default="")
    manage_ip = models.GenericIPAddressField('管理IP', default="")
    other_ip = models.GenericIPAddressField('其他IP', default="")

    class Meta:
            verbose_name = '网络设备'
            verbose_name_plural = "网络设备"
    def __str__(self):
            return self.hostname

class SecurityDevice(models.Model):
status_choices = ((0, '在线'),
(1, '已下线'),
(2, '未知'),
(3, '故障'),
(4, '备用'),
)
status = models.SmallIntegerField(choices=status_choices, default=0)

    id =models.AutoField(primary_key=True)
    machinaroom= models.ForeignKey(Machinaroom)
    type=models.CharField(u'类型', max_length=128, default="")

    hostname = models.CharField(max_length=128, unique=True)
    port_num = models.SmallIntegerField(u'端口个数', default=0)
    device_detail = models.TextField(u'设置详细配置', default="")
    model = models.CharField(u'型号', max_length=128, default="")
    manage_ip = models.GenericIPAddressField('管理IP', default="")
    other_ip = models.GenericIPAddressField('其他IP', default="")
    
    class Meta:
            verbose_name = '安全设备'
            verbose_name_plural = "安全设备"
    def __str__(self):
            return self.hostname

class StorageDevice(models.Model):
"""存储设备"""
machinaroom= models.ForeignKey(Machinaroom)
model = models.CharField(u'型号', max_length=128, default="")
id =models.AutoField(primary_key=True)
manage_ip = models.GenericIPAddressField('管理IP', default="")
status_choices = ((0, '在线'),
(1, '已下线'),
(2, '未知'),
(3, '故障'),
(4, '备用'),
)
status = models.SmallIntegerField(choices=status_choices, default=0)
disk_memo=models.CharField('容量信息',max_length=200, default="")
memo = models.TextField(u'备注', default="")
class Meta:
verbose_name = '存储设备'
verbose_name_plural = "存储设备"

    def __str__(self):
            return self.model

class Software(models.Model):
sub_assset_type_choices = (
(0, 'OS'),
(1, '办公\开发软件'),
(2, '业务软件'),

    )
    sub_asset_type = models.SmallIntegerField(choices=sub_assset_type_choices, verbose_name="服务器类型", default=0)
    license_num = models.IntegerField(verbose_name="授权数",default=0)
    version = models.CharField(u'软件/系统版本', max_length=64, help_text=u'eg. CentOS release 6.5 (Final)', unique=True)

    id =models.AutoField(primary_key=True)
    def __str__(self):
            return self.version

    class Meta:
            verbose_name = '软件/系统'
            verbose_name_plural = "软件/系统"

class Manufactory(models.Model):
"""厂商"""

    id =models.AutoField(primary_key=True)
    manufactory = models.CharField(u'厂商名称', max_length=64, unique=True)
    support_num = models.CharField(u'支持电话', max_length=30, default="")
    memo = models.CharField(u'备注', max_length=128,  default="")

    def __str__(self):
            return self.manufactory

    class Meta:
            verbose_name = '厂商'
            verbose_name_plural = "厂商"

urls.py

urlpatterns = [
url(r'^static/(?P<path>.*)',serve,{'document_root': settings.STATIC_ROOT}), url(r'^',views.main_page,name='main_page'),
url(r'^base',views.base,name='base'),
url(r'^search_machinaroom.html',views.search_machinaroom,name='search_machinaroom'), url(r'^add_machinaroom.html',views.add_machinaroom,name='add_machinaroom'),
url(r'^edit_machinaroom.html',views.edit_machinaroom,name='edit_machinaroom'),
url(r'^del_machinaroom.html',views.del_machinaroom,name='del_machinaroom'),
url(r'^batdel_machinaroom.html',views.batdel_machinaroom,name='batdel_machinaroom'),
url(r'^login/$', login),
]

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,214评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,307评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,543评论 0 341
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,221评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,224评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,007评论 1 284
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,313评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,956评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,441评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,925评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,018评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,685评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,234评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,240评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,464评论 1 261
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,467评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,762评论 2 345

推荐阅读更多精彩内容