Beispiel #1
0
def db_sql_dumps(request): 
    if request.method == "POST":#执行原生SQL  
        try:
            dbServer = DataBase_Server_Config.objects.get(id=int(request.POST.get('dbId')))
        except Exception, ex:
            dbServer = None 
        if dbServer:
            mysql = MySQLPool(host=dbServer.db_host,port=dbServer.db_port,user=dbServer.db_user,passwd=dbServer.db_passwd,dbName=dbServer.db_name)
            result = mysql.execute(sql=request.POST.get('sql'),num=1000)
            if isinstance(result,str):
                return JsonResponse({'msg':"不支持导出功能","code":500,'data':result,'tag':3}) 
            else:
                if result[0] == 0:return JsonResponse({'msg':"不支持导出功能","code":200,'data':"SQL执行成功",'tag':3})  
            file_name = "query_result.csv"    
            with open(file_name,"w") as csvfile: 
                writer = csv.writer(csvfile,dialect='excel')
                #先写入columns_name
                writer.writerow(result[2])
                #写入多行用writerows
                for ds in result[1]:
                    writer.writerows([list(ds)])               
            response = StreamingHttpResponse(base.file_iterator(file_name))
            response['Content-Type'] = 'application/octet-stream'
            response['Content-Disposition'] = 'attachment; filename="{file_name}'.format(file_name=file_name)
            return response                           
Beispiel #2
0
def db_sql_dumps(request): 
    if request.method == "POST":#执行原生SQL  
        try:
            dbServer = DataBase_Server_Config.objects.get(id=int(request.POST.get('dbId')))
        except Exception, ex:
            dbServer = None 
        if dbServer:
            mysql = MySQLPool(host=dbServer.db_host,port=dbServer.db_port,user=dbServer.db_user,passwd=dbServer.db_passwd,dbName=dbServer.db_name)
            result = mysql.execute(sql=request.POST.get('sql'),num=1000)
            if isinstance(result,str):
                return JsonResponse({'msg':"不支持导出功能","code":500,'data':result,'tag':3}) 
            else:
                if result[0] == 0:return JsonResponse({'msg':"不支持导出功能","code":200,'data':"SQL执行成功",'tag':3})  
            file_name = "query_result.csv"    
            with open(file_name,"w") as csvfile: 
                writer = csv.writer(csvfile,dialect='excel')
                #先写入columns_name
                writer.writerow(result[2])
                #写入多行用writerows
                for ds in result[1]:
                    writer.writerows([list(ds)])               
            response = StreamingHttpResponse(base.file_iterator(file_name))
            response['Content-Type'] = 'application/octet-stream'
            response['Content-Disposition'] = 'attachment; filename="{file_name}'.format(file_name=file_name)
            return response                           
Beispiel #3
0
def file_downloads(request):
    if request.method == "POST":
        try:
            order = Order_System.objects.get(id=request.POST.get('id',0),order_type=3)
        except Exception,ex:
            return JsonResponse({'msg':ex,"code":500,'data':[]})
        if request.user.is_superuser or request.user.id in [order.order_executor,order.order_user]:
            sList,resource = AssetsSource().queryAssetsByIp(ipList=request.POST.getlist('dest_server'))
            if len(sList) > 0 and order.order_status == 8:
                ANS = ANSRunner(resource)           
                dest = os.getcwd() + '/upload/file/download/' 
                module_args = "src={src} dest={dest}".format(src=request.POST.get('path'),dest=dest)
                ANS.run_model(host_list=sList,module_name='fetch',module_args=module_args)
                filesData = json.loads(ANS.get_model_result())
                filePath = filesData.get('success').get(request.POST.get('dest_server')).get('dest')
                if filePath:
                    response = StreamingHttpResponse(base.file_iterator(filePath))
                    response['Content-Type'] = 'application/octet-stream'
                    response['Content-Disposition'] = 'attachment; filename="{file_name}'.format(file_name=os.path.basename(filePath))
                    return response    
            else:
                return JsonResponse({'msg':"操作失败,未选择主机或者该分组没有成员","code":500,'data':[]})                                  
        else:
            return JsonResponse({'msg':"操作失败,您没有权限进行操作","code":500,'data':[]})