def user_perm_asset_num(user_id): user = get_object(User, id=user_id) if user: user_perm_info = get_group_user_perm(user) return len(user_perm_info.get('asset').keys()) else: return 0
def print_asset_group(self): """ 打印用户授权的资产组 """ user_asset_group_all = get_group_user_perm(self.user).get('asset_group', []) color_print('[%-3s] %-20s %s' % ('ID', '组名', '备注'), 'title') for asset_group in user_asset_group_all: print '[%-3s] %-15s %s' % (asset_group.id, asset_group.name, asset_group.comment) print
def print_asset_group(self): """ 打印用户授权的资产组 """ user_asset_group_all = get_group_user_perm(self.user).get( 'asset_group', []) color_print('[%-3s] %-20s %s' % ('ID', '组名', '备注'), 'title') for asset_group in user_asset_group_all: print '[%-3s] %-15s %s' % (asset_group.id, asset_group.name, asset_group.comment) print
def __init__(self, user): self.user = user self.user_perm = get_group_user_perm(self.user) if NAV_SORT_BY == 'ip': self.perm_assets = sorted(self.user_perm.get('asset', []).keys(), key=lambda x: [int(num) for num in x.ip.split('.') if num.isdigit()]) elif NAV_SORT_BY == 'hostname': self.perm_assets = self.natural_sort_hostname(self.user_perm.get('asset', []).keys()) else: self.perm_assets = tuple(self.user_perm.get('asset', [])) self.search_result = self.perm_assets self.perm_asset_groups = self.user_perm.get('asset_group', [])
def download(request): user = request.user assets = get_group_user_perm(user).get('asset').keys() asset_select = [] if request.method == 'POST': remote_ip = request.META.get('REMOTE_ADDR') asset_ids = request.POST.getlist('asset_ids', '') file_path = request.POST.get('file_path') date_now = datetime.datetime.now().strftime("%Y%m%d%H%M%S") upload_dir = get_tmp_dir() for asset_id in asset_ids: asset_select.append(get_object(Asset, id=asset_id)) if not set(asset_select).issubset(set(assets)): illegal_asset = set(asset_select).issubset(set(assets)) return HttpResponse( u'没有权限的服务器 %s' % ','.join([asset.hostname for asset in illegal_asset])) res = gen_resource({'user': user, 'asset': asset_select}) runner = MyRunner(res) runner.run('fetch', module_args='src=%s dest=%s' % (file_path, upload_dir), pattern='*') FileLog(user=request.user.username, host=' '.join([asset.hostname for asset in asset_select]), filename=file_path, type='download', remote_ip=remote_ip, result=runner.results).save() logger.debug(runner.results) tmp_dir_name = os.path.basename(upload_dir) file_zip = '/tmp/' + tmp_dir_name + '.zip' zf = zipfile.ZipFile(file_zip, "w", zipfile.ZIP_DEFLATED) for dirname, subdirs, files in os.walk(upload_dir): zf.write(dirname) for filename in files: zf.write(os.path.join(dirname, filename)) zf.close() f = open(file_zip) data = f.read() f.close() response = HttpResponse(data, content_type='application/octet-stream') response[ 'Content-Disposition'] = 'attachment; filename=%s.zip' % tmp_dir_name return response return render_to_response('download.html', locals(), context_instance=RequestContext(request))
def upload(request): user = request.user assets = get_group_user_perm(user).get('asset').keys() asset_select = [] if request.method == 'POST': remote_ip = request.META.get('REMOTE_ADDR') asset_ids = request.POST.getlist('asset_ids', '') upload_files = request.FILES.getlist('file[]', None) date_now = datetime.datetime.now().strftime("%Y%m%d%H%M%S") upload_dir = get_tmp_dir() # file_dict = {} for asset_id in asset_ids: asset_select.append(get_object(Asset, id=asset_id)) if not set(asset_select).issubset(set(assets)): illegal_asset = set(asset_select).issubset(set(assets)) return HttpResponse( '没有权限的服务器 %s' % ','.join([asset.hostname for asset in illegal_asset])) for upload_file in upload_files: file_path = '%s/%s' % (upload_dir, upload_file.name) with open(file_path, 'w') as f: for chunk in upload_file.chunks(): f.write(chunk) res = gen_resource({'user': user, 'asset': asset_select}) runner = MyRunner(res) runner.run('copy', module_args='src=%s dest=%s directory_mode' % (upload_dir, '/tmp'), pattern='*') ret = runner.results logger.debug(ret) FileLog(user=request.user.username, host=' '.join([asset.hostname for asset in asset_select]), filename=' '.join([f.name for f in upload_files]), type='upload', remote_ip=remote_ip, result=ret).save() if ret.get('failed'): error = u'上传目录: %s <br> 上传失败: [ %s ] <br>上传成功 [ %s ]' % ( upload_dir, ', '.join(ret.get('failed').keys()), ', '.join( ret.get('ok').keys())) return HttpResponse(error, status=500) msg = u'上传目录: %s <br> 传送成功 [ %s ]' % (upload_dir, ', '.join( ret.get('ok').keys())) return HttpResponse(msg) return render_to_response('upload.html', locals(), RequestContext(request))
def user_detail(request): header_title, path1, path2 = '用户详情', '用户管理', '用户详情' user_id = request.GET.get('id', '') if not user_id: return HttpResponseRedirect(reverse('user_list')) user = get_object(User, id=user_id) if not user: return HttpResponseRedirect(reverse('user_list')) user_perm_info = get_group_user_perm(user) role_assets = user_perm_info.get('role') user_log_ten = Log.objects.filter(user=user.username).order_by('id')[0:10] user_log_last = Log.objects.filter(user=user.username).order_by('id')[0:50] user_log_last_num = len(user_log_last) return render_to_response('account/user_detail.html', locals(), RequestContext(request))
def __init__(self, user): self.user = user self.user_perm = get_group_user_perm(self.user) if NAV_SORT_BY == 'ip': self.perm_assets = sorted( self.user_perm.get('asset', []).keys(), key=lambda x: [int(num) for num in x.ip.split('.') if num.isdigit()]) elif NAV_SORT_BY == 'hostname': self.perm_assets = self.natural_sort_hostname( self.user_perm.get('asset', []).keys()) else: self.perm_assets = tuple(self.user_perm.get('asset', [])) self.search_result = self.perm_assets self.perm_asset_groups = self.user_perm.get('asset_group', [])
def upload(request): user = request.user assets = get_group_user_perm(user).get('asset').keys() asset_select = [] if request.method == 'POST': remote_ip = request.META.get('REMOTE_ADDR') asset_ids = request.POST.getlist('asset_ids', '') upload_files = request.FILES.getlist('file[]', None) date_now = datetime.datetime.now().strftime("%Y%m%d%H%M%S") upload_dir = get_tmp_dir() # file_dict = {} for asset_id in asset_ids: asset_select.append(get_object(Asset, id=asset_id)) if not set(asset_select).issubset(set(assets)): illegal_asset = set(asset_select).issubset(set(assets)) return HttpResponse('没有权限的服务器 %s' % ','.join([asset.hostname for asset in illegal_asset])) for upload_file in upload_files: file_path = '%s/%s' % (upload_dir, upload_file.name) with open(file_path, 'w') as f: for chunk in upload_file.chunks(): f.write(chunk) res = gen_resource({'user': user, 'asset': asset_select}) runner = MyRunner(res) runner.run('copy', module_args='src=%s dest=%s directory_mode' % (upload_dir, '/tmp'), pattern='*') ret = runner.results logger.debug(ret) FileLog(user=request.user.username, host=' '.join([asset.hostname for asset in asset_select]), filename=' '.join([f.name for f in upload_files]), type='upload', remote_ip=remote_ip, result=ret).save() if ret.get('failed'): error = u'上传目录: %s <br> 上传失败: [ %s ] <br>上传成功 [ %s ]' % (upload_dir, ', '.join(ret.get('failed').keys()), ', '.join(ret.get('ok').keys())) return HttpResponse(error, status=500) msg = u'上传目录: %s <br> 传送成功 [ %s ]' % (upload_dir, ', '.join(ret.get('ok').keys())) return HttpResponse(msg) return render_to_response('upload.html', locals(), RequestContext(request))
def download(request): user = request.user assets = get_group_user_perm(user).get('asset').keys() asset_select = [] if request.method == 'POST': remote_ip = request.META.get('REMOTE_ADDR') asset_ids = request.POST.getlist('asset_ids', '') file_path = request.POST.get('file_path') date_now = datetime.datetime.now().strftime("%Y%m%d%H%M%S") upload_dir = get_tmp_dir() for asset_id in asset_ids: asset_select.append(get_object(Asset, id=asset_id)) if not set(asset_select).issubset(set(assets)): illegal_asset = set(asset_select).issubset(set(assets)) return HttpResponse(u'没有权限的服务器 %s' % ','.join([asset.hostname for asset in illegal_asset])) res = gen_resource({'user': user, 'asset': asset_select}) runner = MyRunner(res) runner.run('fetch', module_args='src=%s dest=%s' % (file_path, upload_dir), pattern='*') FileLog(user=request.user.username, host=' '.join([asset.hostname for asset in asset_select]), filename=file_path, type='download', remote_ip=remote_ip, result=runner.results).save() logger.debug(runner.results) tmp_dir_name = os.path.basename(upload_dir) file_zip = '/tmp/'+tmp_dir_name+'.zip' zf = zipfile.ZipFile(file_zip, "w", zipfile.ZIP_DEFLATED) for dirname, subdirs, files in os.walk(upload_dir): zf.write(dirname) for filename in files: zf.write(os.path.join(dirname, filename)) zf.close() f = open(file_zip) data = f.read() f.close() response = HttpResponse(data, content_type='application/octet-stream') response['Content-Disposition'] = 'attachment; filename=%s.zip' % tmp_dir_name return response return render_to_response('download.html', locals(), context_instance=RequestContext(request))
def asset_list(request): """ asset list view """ header_title, path1, path2 = u'查看资产', u'资产管理', u'查看资产' username = request.user.username user_perm = request.user.is_superuser idc_all = IDC.objects.filter() asset_group_all = AssetGroup.objects.all() asset_types = ASSET_TYPE asset_status = ASSET_STATUS idc_name = request.GET.get('idc', '') group_name = request.GET.get('group', '') asset_type = request.GET.get('asset_type', '') status = request.GET.get('status', '') keyword = request.GET.get('keyword', '') export = request.GET.get("export", False) group_id = request.GET.get("group_id", '') idc_id = request.GET.get("idc_id", '') asset_id_all = request.GET.getlist("id", '') if group_id: group = get_object(AssetGroup, id=group_id) if group: asset_find = Asset.objects.filter(group=group) elif idc_id: idc = get_object(IDC, id=idc_id) if idc: asset_find = Asset.objects.filter(idc=idc) else: if user_perm: asset_find = Asset.objects.all() else: asset_id_all = [] user = get_object(User, username=username) asset_perm = get_group_user_perm(user) if user else {'asset': ''} user_asset_perm = asset_perm['asset'].keys() for asset in user_asset_perm: asset_id_all.append(asset.id) asset_find = Asset.objects.filter(pk__in=asset_id_all) asset_group_all = list(asset_perm['asset_group']) if idc_name: asset_find = asset_find.filter(idc__name__contains=idc_name) if group_name: asset_find = asset_find.filter(group__name__contains=group_name) if asset_type: asset_find = asset_find.filter(asset_type__contains=asset_type) if status: asset_find = asset_find.filter(status__contains=status) if keyword: asset_find = asset_find.filter( Q(hostname__contains=keyword) | Q(other_ip__contains=keyword) | Q(ip__contains=keyword) | Q(remote_ip__contains=keyword) | Q(comment__contains=keyword) | Q(username__contains=keyword) | Q(group__name__contains=keyword) | Q(cpu__contains=keyword) | Q(memory__contains=keyword) | Q(disk__contains=keyword) | Q(brand__contains=keyword) | Q(cabinet__contains=keyword) | Q(sn__contains=keyword) | Q(system_type__contains=keyword) | Q(system_version__contains=keyword)) if export: if asset_id_all: asset_find = [] for asset_id in asset_id_all: asset = get_object(Asset, id=asset_id) if asset: asset_find.append(asset) s = write_excel(asset_find) if s[0]: file_name = s[1] smg = u'excel文件已生成,请点击下载!' return render_to_response('asset/asset_excel_download.html', locals(), RequestContext(request)) assets_list, p, assets, page_range, current_page, show_first, show_end = pages(asset_find, request) return render_to_response('asset/asset_list.html', locals(), RequestContext(request))
def asset_list(request): """ asset list view """ header_title, path1, path2 = u'查看资产', u'资产管理', u'查看资产' username = request.user.username user_perm = request.user.is_superuser idc_all = IDC.objects.filter() asset_group_all = AssetGroup.objects.all() asset_types = ASSET_TYPE asset_status = ASSET_STATUS idc_name = request.GET.get('idc', '') group_name = request.GET.get('group', '') asset_type = request.GET.get('asset_type', '') status = request.GET.get('status', '') keyword = request.GET.get('keyword', '') export = request.GET.get("export", False) group_id = request.GET.get("group_id", '') idc_id = request.GET.get("idc_id", '') asset_id_all = request.GET.getlist("id", '') if group_id: group = get_object(AssetGroup, id=group_id) if group: asset_find = Asset.objects.filter(group=group) elif idc_id: idc = get_object(IDC, id=idc_id) if idc: asset_find = Asset.objects.filter(idc=idc) else: if user_perm: asset_find = Asset.objects.all() else: asset_id_all = [] user = get_object(User, username=username) asset_perm = get_group_user_perm(user) if user else {'asset': ''} user_asset_perm = asset_perm['asset'].keys() for asset in user_asset_perm: asset_id_all.append(asset.id) asset_find = Asset.objects.filter(pk__in=asset_id_all) asset_group_all = list(asset_perm['asset_group']) if idc_name: asset_find = asset_find.filter(idc__name__contains=idc_name) if group_name: asset_find = asset_find.filter(group__name__contains=group_name) if asset_type: asset_find = asset_find.filter(asset_type__contains=asset_type) if status: asset_find = asset_find.filter(status__contains=status) if keyword: asset_find = asset_find.filter( Q(hostname__contains=keyword) | Q(other_ip__contains=keyword) | Q(ip__contains=keyword) | Q(remote_ip__contains=keyword) | Q(comment__contains=keyword) | Q(username__contains=keyword) | Q(group__name__contains=keyword) | Q(cpu__contains=keyword) | Q(memory__contains=keyword) | Q(disk__contains=keyword) | Q(brand__contains=keyword) | Q(cabinet__contains=keyword) | Q(sn__contains=keyword) | Q(system_type__contains=keyword) | Q(system_version__contains=keyword)) if export: if asset_id_all: asset_find = [] for asset_id in asset_id_all: asset = get_object(Asset, id=asset_id) if asset: asset_find.append(asset) s = write_excel(asset_find) if s[0]: file_name = s[1] smg = u'excel文件已生成,请点击下载!' return render_to_response('asset/asset_excel_download.html', locals(), RequestContext(request)) assets_list, p, assets, page_range, current_page, show_first, show_end = pages( asset_find, request) return render_to_response('asset/asset_list.html', locals(), RequestContext(request))