一、资产自动回报数据及个更新流程图
二、表结构注释(NewAssetApprovalZone)
class NewAssetApprovalZone(models.Model):
"""新资产待审批区""" sn = models.CharField(u'资产SN号', max_length=128, unique=True)
'''
必填的:
1、sn号
如果管理员不审批。客户端就会不停的回报,难道每次汇报一次往表里写一次?所以就用sn来进行区分
如果sn变了资产就变了,只要不批准,就一直待存取,让待存取和和客户端保持一致,即便更新了 2、把数据存进去前要不要进行一个合法性验证
'''
asset_type_choices = (
('server', u'服务器'),
('switch', u'交换机'),
('router', u'路由器'),
('firewall', u'防火墙'),
('storage', u'存储设备'),
('NLB', u'NetScaler'),
('wireless', u'无线AP'),
('software', u'软件资产'),
('others', u'其它类'),
)
asset_type = models.CharField(choices=asset_type_choices, max_length=64, blank=True, null=True)
manufactory = models.CharField(max_length=64, blank=True, null=True)
model = models.CharField(max_length=128, blank=True, null=True)
ram_size = models.IntegerField(blank=True, null=True)
cpu_model = models.CharField(max_length=128, blank=True, null=True)
cpu_count = models.IntegerField(blank=True, null=True)
cpu_core_count = models.IntegerField(blank=True, null=True)
os_distribution = models.CharField(max_length=64, blank=True, null=True)
os_type = models.CharField(max_length=64, blank=True, null=True)
os_release = models.CharField(max_length=64, blank=True, null=True)
这些东西都没多少用,所以这几个字段是为了让用户看到
data = models.TextField(u'资产数据')
只有data对我来说用用,因为这个表里存的是临时数据,为了让管理员审批用的,要是审核通过
还是去data里面拿数据,
sn = models.CharField(u'资产SN号', max_length=128, unique=True)
三、把数据存进去前要不要进行一个合法性验证
def mandatory_check(self, data, only_check_sn=False):
for field in self.mandatory_fields:
if field not in data:
self.response_msg('error', 'MandatoryCheckFailed',
"The field [%s] is mandatory and not provided in your reporting data" % field)
else:
if self.response['error']: return False
try: if not only_check_sn:
self.asset_obj = models.Asset.objects.get(id=int(data['asset_id']), sn=data['sn'])
else:
self.asset_obj = models.Asset.objects.get(sn=data['sn'])
return True
except ObjectDoesNotExist as e:
self.response_msg('error', 'AssetDataInvalid',
"Cannot find asset object in DB by using asset id [%s] and SN [%s] " % (
data['asset_id'], data['sn']))
self.waiting_approval = True
return False
首先拿到数据
生成给客户端返回一个空字典
先判断能不能获取到,如果能获取到,检查only_check_sn mandtory_check到客户端回报的数据 ,在不在这个data,如果不在,
首先判断error在不在这个临安
self.response_msg('error', 'MandatoryCheckFailed',
四、一次性能返回多个错误
为什么要这样存?
1、就是把错误信息 放到error字典里面,但是没有返回给客户端
2、一次性返回多个信息,一次提交2个返回一个很麻烦
3、一个key 一个消息传给了函数,写前端需要10个字段你一刷新说那九个字段都需要比填
循环完字段,这几个必填字段,有报错,直接就结束,不用往下判断。
if not only_check_sn:
self.asset_obj = models.Asset.objects.get(id=int(data['asset_id']), sn=data['sn'])
else:
self.asset_obj = models.Asset.objects.get(sn=data['sn'])
return True
except ObjectDoesNotExist as e:
self.response_msg('error', 'AssetDataInvalid',
"Cannot find asset object in DB by using asset id [%s] and SN [%s] " % (
data['asset_id'], data['sn']))
self.waiting_approval = True
return False
1 、首先从mandatory_check这个地方检测,如果这一段如果通过了之后
去资产表里获取资产,因为没有所以怎么都获取不到,获取不到就报错,就认为是一个新资产,设为等待批准
2、然后就返回
谁调用的C:\MadKing-master\assets\core 下的mandatory_check?
答:是下面这段调用的
C:\MadKing-master\assets\core\views.py @csrf_exempt
def asset_with_no_asset_id(request): res = ass_handler.get_asset_id_by_sn()
五、C:\MadKing-master\assets\core.py部分代码注释
class Asset(object):
def __init__(self, request):
self.request = request
self.mandatory_fields = ['sn', 'asset_id', 'asset_type'] # must contains 'sn' , 'asset_id' and 'asset_type'
self.field_sets = {
'asset': ['manufactory'],
'server': ['model', 'cpu_count', 'cpu_core_count', 'cpu_model', 'raid_type', 'os_type', 'os_distribution',
'os_release'],
'networkdevice': []
}
self.response = {
'error': [],
'info': [],
'warning': []
} def response_msg(self, msg_type, key, msg):
if msg_type in self.response:
self.response[msg_type].append({key: msg})
else:
raise ValueError def mandatory_check(self, data, only_check_sn=False):
for field in self.mandatory_fields:
if field not in data:
self.response_msg('error', 'MandatoryCheckFailed',
"The field [%s] is mandatory and not provided in your reporting data" % field)
else:
if self.response['error']: return False
'''是有资产的时候,更新用的,因为数据库里没有这个资产,所以直接就报错'''
try: if not only_check_sn:
self.asset_obj = models.Asset.objects.get(id=int(data['asset_id']), sn=data['sn'])
else:
self.asset_obj = models.Asset.objects.get(sn=data['sn'])
return True
except ObjectDoesNotExist as e:
self.response_msg('error', 'AssetDataInvalid',
"Cannot find asset object in DB by using asset id [%s] and SN [%s] " % (
data['asset_id'], data['sn']))
'''数据库里没有这条资产,肯定会报这个错'''
self.waiting_approval = True '''
重点是这条:这条资产为待批准,这里就结束,
self.waiting_approval是什么意思?为什么要设置这个东西?
报错,没有退出,没有返回
''' return False def get_asset_id_by_sn(self):
'''When the client first time reports it's data to Server,it doesn't know it's asset id yet,so it will come to the server asks for the asset it first,then report the data again '''
data = self.request.POST.get("asset_data")
response = {}
if data:
try:
data = json.loads(data)
if self.mandatory_check(data,
only_check_sn=True): # the asset is already exist in DB,just return it's asset id to client
response = {'asset_id': self.asset_obj.id}
else:
if hasattr(self, 'waiting_approval'):
response = {
'needs_aproval': "this is a new asset,needs IT admin's approval to create the new asset id."}
self.clean_data = data
self.save_new_asset_to_approval_zone()
print(response)
else:
response = self.response
except ValueError as e:
self.response_msg('error', 'AssetDataInvalid', str(e))
response = self.response else:
self.response_msg('error', 'AssetDataInvalid', "The reported asset data is not valid or provided")
response = self.response
return response '''
若果这个表里没有,就执行if hasattr(self, 'waiting_approval'):
给用户返回的时候,同时存在待存区,
如何存呢?
''' def save_new_asset_to_approval_zone(self):
'''When find out it is a new asset, will save the data into approval zone to waiting for IT admin's approvals'''
asset_sn = self.clean_data.get('sn')
asset_already_in_approval_zone = models.NewAssetApprovalZone.objects.get_or_create(sn=asset_sn,
data=json.dumps(
self.clean_data),
manufactory=self.clean_data.get(
'manufactory'),
model=self.clean_data.get(
'model'),
asset_type=self.clean_data.get(
'asset_type'),
ram_size=self.clean_data.get(
'ram_size'),
cpu_model=self.clean_data.get(
'cpu_model'),
cpu_count=self.clean_data.get(
'cpu_count'),
cpu_core_count=self.clean_data.get(
'cpu_core_count'),
os_distribution=self.clean_data.get(
'os_distribution'),
os_release=self.clean_data.get(
'os_release'),
os_type=self.clean_data.get(
'os_type'), )
return True '''
如果存在就取出来,不存在就创建一条
这里面有一个waiting_approval标志位,这里面有一个waiting_approval标志位
'''