python学习笔记_week26

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

python学习笔记_week26python学习笔记_week26

models
 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)
views
 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__))
urls
 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()),
]
nb-list
 (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);
business
 <!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>
server
 <!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