def download_statement_execution_result(statement_execution_id): with DBSession() as session: statement_execution = logic.get_statement_execution_by_id( statement_execution_id, session=session) api_assert(statement_execution is not None, message="Invalid statement execution") verify_query_execution_permission( statement_execution.query_execution_id, session=session) download_file_name = f"result_{statement_execution.query_execution_id}_{statement_execution_id}.csv" reader = GenericReader(statement_execution.result_path) response = None if reader.has_download_url: # If the Reader can generate a download, # we let user download the file by redirection download_url = reader.get_download_url( custom_name=download_file_name) response = redirect(download_url) else: # We read the raw file and download it for the user reader.start() raw = reader.read_raw() response = Response(raw) response.headers["Content-Type"] = "text/csv" response.headers[ "Content-Disposition"] = f'attachment; filename="{download_file_name}"' return response
def download_statement_execution_result(statement_execution_id): with DBSession() as session: statement_execution = logic.get_statement_execution_by_id( statement_execution_id, session=session) api_assert(statement_execution is not None, message="Invalid statement execution") verify_query_execution_permission( statement_execution.query_execution_id, session=session) reader = GenericReader(statement_execution.result_path) response = None if reader.has_download_url: # If the Reader can generate a download, # we proxy download the file download_url = reader.get_download_url() req = requests.get(download_url, stream=True) # 10 KB size response = Response(req.iter_content(chunk_size=10 * 1024)) else: # We read the raw file and download it for the user reader.start() raw = reader.read_raw() response = Response(raw) response.headers["Content-Type"] = "text/plain" response.headers[ "Content-Disposition"] = 'attachment; filename="result_{}_{}.csv"'.format( statement_execution.query_execution_id, statement_execution_id) return response