python学习笔记_week26
一、CMDB
-采集资产
-API
-后台管理
-资产列表(CURD)
-业务线列表(CURD)
-用户列表(CURD)
-组列表(CURD)
...
===>简单<===
公共组件:删改查
查:
资产列表(CURD)
config=[
{
"q":"id",
},{
"q":"name",
}]
values_list=["id","name"]
result=model.TB.objects.filter(条件).values(*values_list)
#[{},{},{}] 具体列
===>标配:配置+数据操作
二、算法


from django.db import models
# Create your models here.
class UserInfo(models.Model):
name=models.CharField(max_length=32)
age=models.IntegerField()
class BusinessUnit(models.Model):
name = models.CharField(max_length=32)
class Server(models.Model):
server_type_choices=(
(1,"WEB"),
(2,"存储"),
(3,"缓存"),
)
server_type=models.IntegerField(choices=server_type_choices)
hostname=models.CharField(max_length=32)
port=models.IntegerField()
business_unit=models.ForeignKey('BusinessUnit',on_delete=models.CASCADE)
user=models.ForeignKey('UserInfo',on_delete=models.CASCADE)
from django.shortcuts import render
from django.views import View
from django.shortcuts import HttpResponse
from django.views import View
from app01 import models
import json
# Create your views here.
class BaseResponse(object):
def __init__(self):
self.status=True
self.data=None
self.message=None
class ServerView(View):
def get(self,request,*args,**kwargs):
return render(request,'server.html')
class ServerJsonView(View):
def get(self,request,*args,**kwargs):
response=BaseResponse()
try:
# 获取要显示的列
# 获取数据
table_config=[{
'q':'id',
'title': '主机名',
'display':0,
'text':{},
'attr':{}
},{
'q':'hostname',
'title':'主机名',
'display':1,
'text':{'content':'{m}','kwargs':{'m':'@hostname'}},
'attr': {'k1':'@hostname','k2':'v2'}
},# '{n}-{m}'.format({'n':'@hostname','m':'@hostname'}) =>hostname-c1.com
{
'q':'port',
'title': '端口',
'display': 1,
'text': {'content':'{m}', 'kwargs': { 'm': '@port'}},
'attr': {'k1': '@port', 'k2': 'v2'}
},{
'q':'business_unit_id',
'title': '业务线ID',
'display': 1,
#去全局变量business_unit_list=[
# {id:1,name:'WEB'},
# {id:2,name:'存储'},
# {id:1,name:'商城'},]
'text': {'content': '{m}', 'kwargs': { 'm': '@@business_unit_list'}},
'attr': {'k1': '@business_unit_id', 'k2': 'v2'}
},{
'q':'business_unit__name',
'title': '业务线名称',
'display': 1,
'text': {'content': '{key}-{m}', 'kwargs': { 'key':'@business_unit_id','m': '@business_unit__name'}},
'attr': {'k1': '@business_unit__name', 'k2': 'v2'}
},{
'q':None,
'title': '操作',
'display': 1,
'text': {'content': '<a herf="server-detail-{n}.html">查看详细</a>', 'm': '@id'},
'attr': {'k1': '@id', 'k2': 'v2'}
},]
values_list=[]
for item in table_config:
if item['q']:
values_list.append(item['q'])
data_list=models.Server.objects.values(*values_list)
#[{},{}]
data_list=list(data_list)
print(data_list)
response.data={
'table_config': table_config,
'data_list':data_list,
}
except Exception as e:
response.status=False
response.message=str(e)
return HttpResponse(json.dumps(response.__dict__))
class BusinessView(View):
def get(self,request,*args,**kwargs):
return render(request,'business.html')
class BusinessJsonView(View):
def get(self,request,*args,**kwargs):
response=BaseResponse()
try:
# 获取要显示的列
# 获取数据
table_config=[{
'q':'id',
'title':'ID',
'display':1,
},{
'q':'name',
'title': '业务线名称',
'display': 1,
},{
'q':None,
'title': '操作',
'display': 1,
},]
values_list=[]
for item in table_config:
if item['q']:
values_list.append(item['q'])
data_list=models.BusinessUnit.objects.values(*values_list)
#[{},{}]
data_list=list(data_list)
print(data_list)
response.data={
'table_config': table_config,
'data_list':data_list,
}
except Exception as e:
response.status=False
response.message=str(e)
return HttpResponse(json.dumps(response.__dict__))
from django.contrib import admin
#from django.urls import path
from django.conf.urls import url
from app01 import views
urlpatterns = [
url(r'^admin/',admin.site.urls),
url(r'^server.html',views.ServerView.as_view()),
url(r'^server-json.html',views.ServerJsonView.as_view()),
url(r'^business.html', views.BusinessView.as_view()),
url(r'^business-json.html', views.BusinessJsonView.as_view()),
]
(function (jq) {
var requestURL;
//为字符串创建format方法,用于字符串格式化
String.prototype.format=function(args){
return this.replace(/\{(\w+)\}/g,function(s,i){
return args[i];
});
};
function init() {
//获取要显示的列
//获取数据
$.ajax({
url:requestURL,
type:'GET',
dataType:'JSON',
success:function (arg) {
if(arg.status){
//创建表格标题
createTablehead(arg.data.table_config);
/*
[{'hostname': 'c1.com', 'port': 11}, {'hostname': 'c2.com', 'port': 23}]
*/
createTablebody(arg.data.table_config,arg.data.data_list);
}else{
alert(arg.message)
}
}
})
}
function createTablehead(config){
/*
tr
td
td
tr
[{
'title':'主机名',
'display':0,
},{
'title': '端口',
'display': 1,
}] */
var tr = document.createElement('tr')
$.each(config,function(k,v){
if(v.display){
var th = document.createElement('th')
th.innerHTML=v.title;
$(tr).append(th);
}
});
$('#thead').append(tr);
}
function createTablebody(tableConfig,dataList){
/*
,dataList=[{'hostname': 'c1.com', 'port': 11}, {'hostname': 'c2.com', 'port': 23}]
tableConfig=[{
'q':'hostname',
'title':'主机名',
'display':1,
},{
'q':'port',
'title': '端口',
'display': 1,
},{
'q':None,
'title': '操作',
'display': 1,
}]*/
$.each(dataList,function (k1,row) {
//row={'port':11,'hostname':'c1.com'}
//row={'port':22,'hostname':'c2.com'}
var tr=document.createElement('tr');
$.each(tableConfig,function (k2,configItem) {
if(configItem.display){
/*configItem={
'q':'hostname',
'title':'主机名',
'display':1,},
'text': {'content': '<a herf="server-detail-{n}.html">查看详细</a>', 'm': '@id'}
'attr':{}, */
var td =document.createElement('td');
//td.innerHTML=row[configItem.q];
//configItem.text.content
var kwargs={};
$.each(configItem.text.kwargs,function (key,value) {
if(value.startsWith("@")){
var temp=value.substring(1,value.length);
kwargs[key]=row[temp]
}else{
kwargs[key]=value;
}
});
td.innerHTML=configItem.text.content.format(kwargs);
$.each(configItem.attr,function (key,value) {
if(value.startsWith("@")){
var temp=value.substring(1,value.length);
td.setAttribute(key,row[temp]);
}else{
td.setAttribute(key,value);
}
});
$(tr).append(td);
}});
$('#tbody').append(tr);
})}
jq.extend({
'linan': function (url) {
requestURL=url
init();
}
})
})(jQuery);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<table border="1">
<thead id="thead"></thead>
<tr>
</tr>
<tbody id="tbody"></tbody>
</table>
<script src="/static/jquery-1.12.4.js"></script>
<script src="/static/nb-list.js"></script>
<script>
$(function () {
$.linan('/business-json.html');
});
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<table border="1">
<thead id="thead"></thead>
<tr>
</tr>
<tbody id="tbody"></tbody>
</table>
<script src="/static/jquery-1.12.4.js"></script>
<script src="/static/nb-list.js"></script>
<script>
$(function () {
$.linan('/server-json.html');
});
</script>
</body>
</html>
CMDB
python学习笔记_week26的更多相关文章
- python学习笔记整理——字典
python学习笔记整理 数据结构--字典 无序的 {键:值} 对集合 用于查询的方法 len(d) Return the number of items in the dictionary d. 返 ...
- VS2013中Python学习笔记[Django Web的第一个网页]
前言 前面我简单介绍了Python的Hello World.看到有人问我搞搞Python的Web,一时兴起,就来试试看. 第一篇 VS2013中Python学习笔记[环境搭建] 简单介绍Python环 ...
- python学习笔记之module && package
个人总结: import module,module就是文件名,导入那个python文件 import package,package就是一个文件夹,导入的文件夹下有一个__init__.py的文件, ...
- python学习笔记(六)文件夹遍历,异常处理
python学习笔记(六) 文件夹遍历 1.递归遍历 import os allfile = [] def dirList(path): filelist = os.listdir(path) for ...
- python学习笔记--Django入门四 管理站点--二
接上一节 python学习笔记--Django入门四 管理站点 设置字段可选 编辑Book模块在email字段上加上blank=True,指定email字段为可选,代码如下: class Autho ...
- python学习笔记--Django入门0 安装dangjo
经过这几天的折腾,经历了Django的各种报错,翻译的内容虽然不错,但是与实际的版本有差别,会出现各种奇葩的错误.现在终于找到了解决方法:查看英文原版内容:http://djangobook.com/ ...
- python学习笔记(一)元组,序列,字典
python学习笔记(一)元组,序列,字典
- Pythoner | 你像从前一样的Python学习笔记
Pythoner | 你像从前一样的Python学习笔记 Pythoner
- OpenCV之Python学习笔记
OpenCV之Python学习笔记 直都在用Python+OpenCV做一些算法的原型.本来想留下发布一些文章的,可是整理一下就有点无奈了,都是写零散不成系统的小片段.现在看 到一本国外的新书< ...
随机推荐
- tar 命令 简易使用方法
创建压缩文件方法 tar zcf 压缩包存放位置 压缩那个目录/内容 (压缩) 例:在/data目录下压缩/etc/目录,并创建名称为etc.tar.gz [root@web01 /]# tar zc ...
- oracle 修改进程
SQL> show parameter session NAME TYPE VALUE------------------------------------ ----------- ----- ...
- B-trees
B-trees are balanced search trees designed to work well on disks or other direct accesssecondary sto ...
- 【oracle入门】数据模型
数据模式也是一这种模型,它是数据库中用于提供信息表示的操作手段的形式架构,是数据库中用来对现实世界惊喜抽象的工具.数据模型按不同的应用层次分为3种类型,分别为概念数据模型.逻辑数据模型.物理数据模型. ...
- 获取China大陆IP段的范围
这里有几个网站提供了大陆的IP段范围.别问我要这个列表干什么,我也不知道. http://www.ip2location.com/blockvisitorsbycountry.aspx老牌网站,国内很 ...
- CentOS6.5上安装MySQL
1.查看操作系统的相关信息 2.查看系统上所有MySQL的rpm包并删除 [root@master ~]# rpm -qa | grep -i mysql [root@masterc ~]# yum ...
- 【Java】字符串工具类
import android.annotation.SuppressLint; import java.io.UnsupportedEncodingException; import java.uti ...
- Go语言判断if else语句
基本格式: if 条件语句{ 执行语句 }else{ 执行语句 } package main import "fmt" func main(){ fmt.Println(Add(- ...
- 20155208徐子涵 《网络对抗》Exp1 PC平台逆向破解
20155208徐子涵 <网络对抗>Exp1 PC平台逆向破解 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数 ...
- pycharm 在线激活
1.在pycharm过期页面选择 enter License 2.激活界面的License server输入:http://idea.liyang.io 然后点击激活 (确保电脑能上网,亲测有效)