pull/26/head
halcyon 9 years ago
commit ec7b543bf2

1
.gitignore vendored

@ -36,6 +36,7 @@ nosetests.xml
.mr.developer.cfg .mr.developer.cfg
.project .project
.pydevproject .pydevproject
*.xlsx
node_modules node_modules
logs logs
keys keys

@ -1,7 +1,7 @@
# coding: utf-8 # coding: utf-8
import ast import ast
import xlsxwriter import xlsxwriter
from django.db.models import AutoField
from jumpserver.api import * from jumpserver.api import *
from jasset.models import ASSET_STATUS, ASSET_TYPE, ASSET_ENV, IDC, AssetRecord from jasset.models import ASSET_STATUS, ASSET_TYPE, ASSET_ENV, IDC, AssetRecord
@ -46,7 +46,7 @@ def db_update_group(**kwargs):
group = get_object(AssetGroup, id=group_id) group = get_object(AssetGroup, id=group_id)
for asset_id in asset_id_list: for asset_id in asset_id_list:
group_add_asset(group, asset_id) group_add_asset(group, asset_id)
AssetGroup.objects.filter(id=group_id).update(**kwargs) AssetGroup.objects.filter(id=group_id).update(**kwargs)
@ -99,6 +99,7 @@ def db_asset_update(**kwargs):
asset_id = kwargs.pop('id') asset_id = kwargs.pop('id')
Asset.objects.filter(id=asset_id).update(**kwargs) Asset.objects.filter(id=asset_id).update(**kwargs)
# #
# #
# def batch_host_edit(host_alter_dic, j_user='', j_password=''): # def batch_host_edit(host_alter_dic, j_user='', j_password=''):
@ -223,6 +224,13 @@ def asset_diff(before, after):
return alter_dic 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): def db_asset_alert(asset, username, alert_dic):
""" """
asset alert info to db asset alert info to db
@ -233,8 +241,8 @@ def db_asset_alert(asset, username, alert_dic):
print field print field
field_name = Asset._meta.get_field_by_name(field)[0].verbose_name field_name = Asset._meta.get_field_by_name(field)[0].verbose_name
if field == 'idc': if field == 'idc':
old = IDC.objects.filter(id=value[0]) old = IDC.objects.filter(id=value[0]) if value[0] else u''
new = IDC.objects.filter(id=value[1]) new = IDC.objects.filter(id=value[1]) if value[1] else u''
old_name = old[0].name if old else u'' old_name = old[0].name if old else u''
new_name = new[0].name if new else u'' new_name = new[0].name if new else u''
alert_info = [field_name, old_name, new_name] 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]: for group_id in value[1]:
group_name = AssetGroup.objects.get(id=int(group_id)).name group_name = AssetGroup.objects.get(id=int(group_id)).name
new.append(group_name) new.append(group_name)
alert_info = [field_name, ','.join(old), ','.join(new)] if old == new:
continue
else:
alert_info = [field_name, ','.join(old), ','.join(new)]
elif field == 'use_default_auth': 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': 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: else:
alert_info = [field_name, unicode(value[0]), unicode(value[1])] alert_info = [field_name, unicode(value[0]), unicode(value[1])]
@ -285,7 +311,7 @@ def write_excel(asset_all):
group_all = '/'.join(group_list) group_all = '/'.join(group_list)
status = asset.get_status_display() status = asset.get_status_display()
alter_dic = [asset.hostname, asset.ip, asset.idc.name, asset.mac, asset.remote_ip, asset.cpu, asset.memory, alter_dic = [asset.hostname, asset.ip, asset.idc.name, asset.mac, asset.remote_ip, asset.cpu, asset.memory,
asset.disk, asset.system_type, asset.cabinet, group_all, status, asset.comment] asset.disk, asset.system_type, asset.cabinet, group_all, status, asset.comment]
data.append(alter_dic) data.append(alter_dic)
format = workbook.add_format() format = workbook.add_format()
format.set_border(1) format.set_border(1)
@ -311,3 +337,11 @@ def write_excel(asset_all):
workbook.close() workbook.close()
ret = (True, file_name) ret = (True, file_name)
return ret 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)

@ -210,29 +210,12 @@ def asset_edit(request):
# if not asset_id: # if not asset_id:
# return HttpResponse('没有该主机') # return HttpResponse('没有该主机')
asset = get_object(Asset, id=asset_id) asset = get_object(Asset, id=asset_id)
asset_old = copy_model_instance(asset)
af = AssetForm(instance=asset) af = AssetForm(instance=asset)
if request.method == 'POST': if request.method == 'POST':
af_post = AssetForm(request.POST, instance=asset) af_post = AssetForm(request.POST, instance=asset)
ip = request.POST.get('ip', '') ip = request.POST.get('ip', '')
use_default_auth = request.POST.get('use_default_auth')
# 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
try: try:
asset_test = get_object(Asset, ip=ip) asset_test = get_object(Asset, ip=ip)
if asset_test and asset_id != unicode(asset_test.id): if asset_test and asset_id != unicode(asset_test.id):
@ -243,10 +226,14 @@ def asset_edit(request):
else: else:
if af_post.is_valid(): if af_post.is_valid():
af_save = af_post.save(commit=False) af_save = af_post.save(commit=False)
if use_default_auth:
af_save.username = ''
af_save.password = ''
af_save.save() af_save.save()
af_post.save_m2m() 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) info = asset_diff(af_post.__dict__.get('initial'), request.POST)
print info
db_asset_alert(asset, username, info) db_asset_alert(asset, username, info)
msg = u'主机 %s 修改成功' % ip msg = u'主机 %s 修改成功' % ip

Loading…
Cancel
Save