def test_get_exported_compressed_file_if_owner_is_none_raises_access_control_error_with_setting_false( self, ): # Act # Assert with self.assertRaises(AccessControlError): exported_compressed_file_api.get_by_id( self.fixture.exported_compressed_file_3.id, None )
def test_get_exported_compressed_file_not_owner_raises_access_control_error(self): # Arrange user = create_mock_user("2") # Act # Assert with self.assertRaises(AccessControlError): exported_compressed_file_api.get_by_id( self.fixture.exported_compressed_file_1.id, user )
def test_get_exported_compressed_file_user_as_none_raises_access_control_error_if_user_is_not_none( self, ): # Act # Assert with self.assertRaises(AccessControlError): exported_compressed_file_api.get_by_id( self.fixture.exported_compressed_file_1.id, None )
def test_get_exported_compressed_file_as_anonymous_raises_access_control_error_with_setting_false( self, ): # Arrange user = create_mock_user(None, is_anonymous=True) # Act # Assert with self.assertRaises(AccessControlError): exported_compressed_file_api.get_by_id( self.fixture.exported_compressed_file_3.id, user )
def check_download_status(request): """Checks if a file is ready for download, Id is expected on the request Args: request: Returns: """ file_id = request.GET.get("file_id", None) if file_id is not None: try: # Get the exported file with the given id exported_file = exported_file_api.get_by_id(file_id, request.user) except exceptions.DoesNotExist as e: return HttpResponseBadRequest( "The file with the given id does not exist.") except Exception as e: logger.error("Something went wrong while downloading: {0}".format( str(e))) return HttpResponseBadRequest( "Something went wrong while downloading. Please contact an administrator." ) return HttpResponse( json.dumps({ "is_ready": exported_file.is_ready, "message": "The file is now ready for download", }), content_type="application/javascript", ) else: return HttpResponseBadRequest("File id is missing in parameters")
def export_data(transform_result_list, user, title): """Get the transformed Data file Args: transform_result_list: user: title: Returns: HttpResponse: """ # get the list of the transformed content(first element since we have only one data) transform_result_content_list = transform_result_list[ 0].transform_result_content file_content = "" file_name = title extension = "" content_type = "" if len(transform_result_content_list) > 1: # export as a zip file (more than file) exported_file = ExportedCompressedFile( file_name="Query_Results.zip", is_ready=False, mime_type="application/zip", user_id=str(user.id), ) # Save in database to generate an Id and be accessible via url exported_file = exported_compressed_file_api.upsert(exported_file) # Export in Zip AbstractExporter.export(exported_file.id, transform_result_list, user) # Serialize object return_value = ExporterExportedCompressedFileSerializer(exported_file) compressed_file_object = exported_compressed_file_api.get_by_id( return_value.data["id"], user) file_content = compressed_file_object.file.read() file_name = compressed_file_object.file_name elif len(transform_result_content_list) == 1: # export as a ordinary file (first element since we have only one transformed content) file_content = transform_result_content_list[0].content_converted # get the extension extension = transform_result_content_list[0].content_extension # get the type content by removing '.' from the extension if extension: content_type = "text/" + extension.split(".")[1] return get_file_http_response( file_content, file_name, content_type, extension, )
def download_exported_compressed_file(request): """Download view, ID file expected in parameters Args: request: Returns: """ exporter_file_id = request.GET["id"] exported_file = None # Generate a default context context = { "message": "Please wait, the download will start automatically", "is_ready": False, "id_file": exporter_file_id, } try: # Get the exported file with the given id exported_file = exported_file_api.get_by_id(exporter_file_id, request.user) except exceptions.DoesNotExist as e: context["message"] = "The file with the given id does not exist." except Exception as e: logger.error("Something went wrong while downloading: {0}".format(str(e))) context[ "message" ] = "Something went wrong while downloading. Please contact administrator" if exported_file and exported_file.is_ready: # the file is ready to be downloaded response = HttpResponse(exported_file.file.read()) response["Content-Disposition"] = ( "attachment; filename=" + exported_file.file_name ) response["Content-Type"] = exported_file.file.content_type return response else: # Add assets assets = { "js": [ { "path": "core_exporters_app/user/js/exporter_compressed_file/download.js", "is_raw": False, } ], "css": [], } # Render the page return render( request, "core_exporters_app/user/exported_compressed_file/download.html", context=context, assets=assets, )
def test_get_exported_compressed_file_as_superuser_returns_object(self): # Arrange user = create_mock_user("1", is_superuser=True) # Act result = exported_compressed_file_api.get_by_id( self.fixture.exported_compressed_file_2.id, user ) # Assert self.assertEqual(result, self.fixture.exported_compressed_file_2)
def get_object(self, pk): """ Retrieve an exported compressed file Args: pk: ObjectId Returns: ZipFile """ try: return exported_compressed_file_api.get_by_id(pk) except exceptions.DoesNotExist: raise Http404
def download_exported_compressed_file(request): """ Download view, ID file expected in parameters Args: request: Returns: """ exporter_file_id = request.GET['id'] try: # Get the exported file with the given id exported_file = exported_file_api.get_by_id(exporter_file_id) except: # TODO: catch good exception, redirect to error page pass # The file is not ready yet if exported_file.is_ready is False: # Generate the context context = { 'message': 'Please wait, the download will start automatically', 'is_ready': False, 'id_file': exporter_file_id } # Add assets assets = { "js": [{ "path": 'core_exporters_app/user/js/exporter_compressed_file/download.js', "is_raw": False }], "css": [], } # Render the page return render( request, 'core_exporters_app/user/exported_compressed_file/download.html', context=context, assets=assets) # the file is ready to be downloaded else: response = HttpResponse(exported_file.file.read()) response[ 'Content-Disposition'] = "attachment; filename=" + exported_file.file_name response['Content-Type'] = exported_file.file.content_type return response
def generate_zip(exported_compressed_file_id, transformed_result_list, user): """Generates the zip file Args: exported_compressed_file_id: transformed_result_list: user: Returns: """ # Needed otherwise the file in db is not updated exported_compressed_file = exported_compressed_file_api.get_by_id( exported_compressed_file_id, user) # ZIP fileCreation in_memory = BytesIO() zip = zipfile.ZipFile(in_memory, "a") # For each result for transformed_result in transformed_result_list: # Loops on contents converted for each file for content in transformed_result.transform_result_content: path = "{!s}/{!s}{!s}".format( transformed_result.source_document_name, content.file_name, content.content_extension, ) zip.writestr(path, content.content_converted) # fix for Linux zip files read in Windows for xmlFile in zip.filelist: xmlFile.create_system = 0 # Close zip file zip.close() # ZIP file to be downloaded in_memory.seek(0) # save the file and upset the object exported_compressed_file.file.put( in_memory, content_type=exported_compressed_file.mime_type) exported_compressed_file.is_ready = True return exported_compressed_file_api.upsert(exported_compressed_file)
def check_download_status(request): """ Checks if a file is ready for download, Id is expected on the request Args: request: Returns: """ file_id = request.GET.get('file_id', None) if file_id is not None: try: # Get the exported file with the given id exported_file = exported_file_api.get_by_id(file_id) except: return HttpResponseBadRequest("File with the given id does not exist") return HttpResponse(json.dumps({'is_ready': exported_file.is_ready, 'message': "The file is now ready for download"}), content_type='application/javascript') else: return HttpResponseBadRequest("File id is missing in parameters")