From 0bab67459eed1227085521f71276813629125a00 Mon Sep 17 00:00:00 2001 From: MarvinDo Date: Wed, 21 Aug 2024 13:18:34 +0200 Subject: [PATCH] download tests --- .../webapp/download/download_functions.py | 21 +++++++++++++++ .../webapp/download/download_routes.py | 27 +++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/src/frontend_celery/webapp/download/download_functions.py b/src/frontend_celery/webapp/download/download_functions.py index 7233a0a9..99896258 100644 --- a/src/frontend_celery/webapp/download/download_functions.py +++ b/src/frontend_celery/webapp/download/download_functions.py @@ -121,9 +121,30 @@ def get_vcf(variants_oi, conn, worker=get_variant_vcf_line, check_vcf=True): return buffer, status, "", "" +import time +def test_large_download(): + for i in range(50): + yield str(i).encode() + print(i) + time.sleep(1) +def get_vcf_stream(variants_oi, conn, worker=get_variant_vcf_line): + for id in variants_oi: + info_headers, variant_vcf = worker(id, conn) + yield variant_vcf + #all_variant_vcf_lines.append(variant_vcf) + #final_info_headers = merge_info_headers(final_info_headers, info_headers) + + #printable_info_headers = list(final_info_headers.values()) + #printable_info_headers.sort() + #functions.write_vcf_header(printable_info_headers, lambda l: yield_something(l.encode()), tail='\n') + + +def yield_something(val): + yield val + ############################################################ diff --git a/src/frontend_celery/webapp/download/download_routes.py b/src/frontend_celery/webapp/download/download_routes.py index b616de50..a5817df9 100644 --- a/src/frontend_celery/webapp/download/download_routes.py +++ b/src/frontend_celery/webapp/download/download_routes.py @@ -76,6 +76,33 @@ def variant_list(): return send_file(vcf_file_buffer, as_attachment=True, download_name=download_file_name, mimetype="text/vcf") +from flask import Response, stream_with_context +@download_blueprint.route('/download/test') +@require_permission(["admin_resources"]) +def download_test(): + return Response( + stream_with_context(download_functions.test_large_download()), + headers={'Content-Disposition': 'attachment; filename=test.txt'} + ) + +@download_blueprint.route('/download/test_vcf') +@require_permission(["admin_resources"]) +def download_test_vcf(): + conn = get_connection() + + list_id = request.args.get('list_id') + require_valid(list_id, "user_variant_lists", conn) + + # check that the logged in user is the owner of this list + require_list_permission(list_id, ['read'], conn) + variant_ids_oi = conn.get_variant_ids_from_list(list_id) + + return Response( + stream_with_context(download_functions.get_vcf_stream(variant_ids_oi, conn)), + headers={'Content-Disposition': 'attachment; filename=test.txt'} + ) + + # listens on get parameter: raw @download_blueprint.route('/download/vcf/classified') @require_permission(['read_resources'])