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

search_machinaroom.html

{% extends 'base.html' %}

{% block title %}











搜索

  机房名字  

新增/编辑



  机房名字  
  机房位置  
  机房VPN  
  备注  













{% endblock %}

{% block content %}












{% for mr in machinaroomlist.object_list %}








{% endfor %}

机房名字 机房位置 VPN链接 备注 操作
{{ mr.name }} {{ mr.location }} {{ mr.vpnurl }} {{ mr.memo }}
编辑
删除





{% 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.*)',views.main_page,name='main_page'),
url(r'^base',views.base,name='base'),
url(r'^search_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),
]

你可能感兴趣的:(Django 搭建CMDB系统完整[8](机房增删改查))