def start_export(request): if request.method == 'POST': #We retrieve all selected exporters listExporter = request.POST.getlist('my_exporters') instances = request.session['instancesExplore'] listId = request.session['listIdToExport'] #Creation of ZIP file in_memory = StringIO() zip = zipfile.ZipFile(in_memory, "a") is_many_inst = len(instances) > 1 for instance in instances: xmlResults = [] #Retrieve data sessionName = "resultsExplore" + json.loads(instance)['name'] results = request.session[sessionName] if (len(results) > 0): for result in results: if result['id'] in listId: xmlResults.append(result) #For each data, we convert if len(xmlResults) > 0: #Init the folder name folder_name = None if is_many_inst: folder_name = json.loads(instance)['name'] #Check if the XSLT converter is asked. If yes, we start with this one because there is a specific treatment listXslt = request.POST.getlist('my_xslts') #Get the content of the file if len(listXslt) > 0: exporter = XSLTExporter() for xslt in listXslt: xslt = ExporterXslt.objects.get(pk=xslt) exporter._setXslt(xslt.content) if folder_name == None: exporter._transformAndZip(xslt.name, xmlResults, zip) else: exporter._transformAndZip(folder_name+"/"+xslt.name, xmlResults, zip) #We export for others exporters for exporter in listExporter: exporter = get_exporter(exporter) exporter._transformAndZip(folder_name, xmlResults, zip) zip.close() #ZIP file to be downloaded in_memory.seek(0) response = HttpResponse(in_memory.read()) response["Content-Disposition"] = "attachment; filename=Query_Results.zip" response['Content-Type'] = 'application/x-zip' request.session['listIdToExport'] = '' return response
def start_export(request): if request.method == 'POST': #We retrieve all selected exporters listExporter = request.POST.getlist('my_exporters') instances = request.session['instancesExplore'] listId = request.session['listIdToExport'] xmlResults = [] #Creation of ZIP file in_memory = StringIO() zip = zipfile.ZipFile(in_memory, "a") is_many_inst = len(instances) > 1 for instance in instances: #Retrieve data sessionName = "resultsExplore" + eval(instance)['name'] results = request.session[sessionName] if (len(results) > 0): for result in results: if result['id'] in listId: xmlResults.append(result) #For each data, we convert if len(xmlResults) > 0: #Init the folder name folder_name = None if is_many_inst: folder_name = eval(instance)['name'] #Check if the XSLT converter is asked. If yes, we start with this one because there is a specific treatment listXslt = request.POST.getlist('my_xslts') #Get the content of the file if len(listXslt) > 0: exporter = XSLTExporter() for xslt in listXslt: xslt = ExporterXslt.objects.get(pk=xslt) exporter._setXslt(xslt.content) if folder_name == None: exporter._transformAndZip(xslt.name, xmlResults, zip) else: exporter._transformAndZip( folder_name + "/" + xslt.name, xmlResults, zip) #We export for others exporters for exporter in listExporter: exporter = get_exporter(exporter) exporter._transformAndZip(folder_name, xmlResults, zip) zip.close() #ZIP file to be downloaded in_memory.seek(0) response = HttpResponse(in_memory.read()) response["Content-Disposition"] = "attachment; filename=Results.zip" response['Content-Type'] = 'application/x-zip' request.session['listIdToExport'] = '' return response else: # We retrieve the result_id for each file the user wants to export listId = request.GET.getlist('listId[]') request.session['listIdToExport'] = listId # Get all schemaId from the listId listSchemas = XMLdata.getByIDsAndDistinctBy(listId, "schema") # XMLdata.objects(pk__in=listId).distinct(field="schema") export_form = ExportForm(listSchemas) upload_xslt_Form = UploadXSLTForm(listSchemas) template = loader.get_template('explore/export_start.html') context = Context({ 'export_form': export_form, 'upload_xslt_Form': upload_xslt_Form, 'nb_elts_exp': len(export_form.EXPORT_OPTIONS), 'nb_elts_xslt': len(upload_xslt_Form.EXPORT_OPTIONS) }) return HttpResponse(json.dumps({'template': template.render(context)}), content_type='application/javascript')
def start_export(request): if request.method == 'POST': #We retrieve all selected exporters listExporter = request.POST.getlist('my_exporters') instances = request.session['instancesExplore'] listId = request.session['listIdToExport'] xmlResults = [] #Creation of ZIP file in_memory = StringIO() zip = zipfile.ZipFile(in_memory, "a") is_many_inst = len(instances) > 1 for instance in instances: #Retrieve data sessionName = "resultsExplore" + eval(instance)['name'] results = request.session[sessionName] if (len(results) > 0): for result in results: if result['id'] in listId: xmlResults.append(result) #For each data, we convert if len(xmlResults) > 0: #Init the folder name folder_name = None if is_many_inst: folder_name = eval(instance)['name'] #Check if the XSLT converter is asked. If yes, we start with this one because there is a specific treatment listXslt = request.POST.getlist('my_xslts') #Get the content of the file if len(listXslt) > 0: exporter = XSLTExporter() for xslt in listXslt: xslt = ExporterXslt.objects.get(pk=xslt) exporter._setXslt(xslt.content) if folder_name == None: exporter._transformAndZip(xslt.name, xmlResults, zip) else: exporter._transformAndZip(folder_name+"/"+xslt.name, xmlResults, zip) #We export for others exporters for exporter in listExporter: exporter = get_exporter(exporter) exporter._transformAndZip(folder_name, xmlResults, zip) zip.close() #ZIP file to be downloaded in_memory.seek(0) response = HttpResponse(in_memory.read()) response["Content-Disposition"] = "attachment; filename=Results.zip" response['Content-Type'] = 'application/x-zip' request.session['listIdToExport'] = '' return response else: # We retrieve the result_id for each file the user wants to export listId = request.GET.getlist('listId[]') request.session['listIdToExport'] = listId # Get all schemaId from the listId listSchemas = XMLdata.getByIDsAndDistinctBy(listId, "schema") # XMLdata.objects(pk__in=listId).distinct(field="schema") export_form = ExportForm(listSchemas) upload_xslt_Form = UploadXSLTForm(listSchemas) template = loader.get_template('explore/export_start.html') context = Context({'export_form':export_form, 'upload_xslt_Form':upload_xslt_Form, 'nb_elts_exp': len(export_form.EXPORT_OPTIONS), 'nb_elts_xslt' : len(upload_xslt_Form.EXPORT_OPTIONS)}) return HttpResponse(json.dumps({'template': template.render(context)}), content_type='application/javascript')