From 3d6d714495385d27b6b739d2fd9ef1c4aba9622d Mon Sep 17 00:00:00 2001 From: root Date: Tue, 10 Nov 2015 00:11:54 +0800 Subject: [PATCH] asset alert history --- jasset/asset_api.py | 50 +++++++++++++++++++++++++++++++++++++-------- jasset/views.py | 27 +++++++----------------- 2 files changed, 49 insertions(+), 28 deletions(-) diff --git a/jasset/asset_api.py b/jasset/asset_api.py index 94f941b8f..b0f3c9b8c 100644 --- a/jasset/asset_api.py +++ b/jasset/asset_api.py @@ -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 @@ -46,7 +46,7 @@ def db_update_group(**kwargs): group = get_object(AssetGroup, id=group_id) 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) @@ -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) - 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': - 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])] @@ -285,7 +311,7 @@ def write_excel(asset_all): group_all = '/'.join(group_list) status = asset.get_status_display() 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) format = workbook.add_format() format.set_border(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) \ No newline at end of file diff --git a/jasset/views.py b/jasset/views.py index bde27c68e..232f9cda7 100644 --- a/jasset/views.py +++ b/jasset/views.py @@ -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