asset alert history

pull/26/head
root 9 years ago
parent 6de253df58
commit 3d6d714495

@ -1,7 +1,7 @@
# coding: utf-8
import ast
import xlsxwriter
from django.db.models import AutoField
from jumpserver.api import *
from jasset.models import ASSET_STATUS, ASSET_TYPE, ASSET_ENV, IDC, AssetRecord
@ -99,6 +99,7 @@ def db_asset_update(**kwargs):
asset_id = kwargs.pop('id')
Asset.objects.filter(id=asset_id).update(**kwargs)
#
#
# def batch_host_edit(host_alter_dic, j_user='', j_password=''):
@ -223,6 +224,13 @@ def asset_diff(before, after):
return alter_dic
def asset_diff_one(before, after):
print before.__dict__, after.__dict__
fields = Asset._meta.get_all_field_names()
for field in fields:
print before.field, after.field
def db_asset_alert(asset, username, alert_dic):
"""
asset alert info to db
@ -233,8 +241,8 @@ def db_asset_alert(asset, username, alert_dic):
print field
field_name = Asset._meta.get_field_by_name(field)[0].verbose_name
if field == 'idc':
old = IDC.objects.filter(id=value[0])
new = IDC.objects.filter(id=value[1])
old = IDC.objects.filter(id=value[0]) if value[0] else u''
new = IDC.objects.filter(id=value[1]) if value[1] else u''
old_name = old[0].name if old else u''
new_name = new[0].name if new else u''
alert_info = [field_name, old_name, new_name]
@ -250,12 +258,30 @@ def db_asset_alert(asset, username, alert_dic):
for group_id in value[1]:
group_name = AssetGroup.objects.get(id=int(group_id)).name
new.append(group_name)
if old == new:
continue
else:
alert_info = [field_name, ','.join(old), ','.join(new)]
elif field == 'use_default_auth':
pass
if unicode(value[0]) == 'True' and unicode(value[1]) == 'on' or \
unicode(value[0]) == 'False' and unicode(value[1]) == '':
continue
else:
name = asset.username
alert_info = [field_name, u'默认', name] if unicode(value[0]) == 'True' else \
[field_name, name, u'默认']
elif field in ['username', 'password']:
continue
elif field == 'is_active':
pass
if unicode(value[0]) == 'True' and unicode(value[1]) == '1' or \
unicode(value[0]) == 'False' and unicode(value[1]) == '0':
continue
else:
alert_info = [u'是否激活', u'激活', u'禁用'] if unicode(value[0]) == 'True' else \
[u'是否激活', u'禁用', u'激活']
else:
alert_info = [field_name, unicode(value[0]), unicode(value[1])]
@ -311,3 +337,11 @@ def write_excel(asset_all):
workbook.close()
ret = (True, file_name)
return ret
def copy_model_instance(obj):
initial = dict([(f.name, getattr(obj, f.name))
for f in obj._meta.fields
if not isinstance(f, AutoField) and\
not f in obj._meta.parents.values()])
return obj.__class__(**initial)

@ -211,29 +211,12 @@ def asset_edit(request):
# if not asset_id:
# return HttpResponse('没有该主机')
asset = get_object(Asset, id=asset_id)
asset_old = copy_model_instance(asset)
af = AssetForm(instance=asset)
if request.method == 'POST':
af_post = AssetForm(request.POST, instance=asset)
ip = request.POST.get('ip', '')
# ip = request.POST.get('ip')
# port = request.POST.get('port')
# groups = request.POST.getlist('groups')
# use_default_auth = True if request.POST.getlist('use_default_auth', []) else False
# is_active = True if request.POST.get('is_active') else False
# comment = request.POST.get('comment')
# if not use_default_auth:
# username = request.POST.get('username')
# password = request.POST.get('password')
# if password == asset.password:
# password_encode = password
# else:
# password_encode = CRYPTOR.encrypt(password)
# else:
# username = None
# password_encode = None
use_default_auth = request.POST.get('use_default_auth')
try:
asset_test = get_object(Asset, ip=ip)
if asset_test and asset_id != unicode(asset_test.id):
@ -244,10 +227,14 @@ def asset_edit(request):
else:
if af_post.is_valid():
af_save = af_post.save(commit=False)
if use_default_auth:
af_save.username = ''
af_save.password = ''
af_save.save()
af_post.save_m2m()
asset_new = get_object(Asset, id=asset_id)
asset_diff_one(asset_old, asset_new)
info = asset_diff(af_post.__dict__.get('initial'), request.POST)
print info
db_asset_alert(asset, username, info)
msg = u'主机 %s 修改成功' % ip

Loading…
Cancel
Save