From 98731ee5cf2b2a3efb01524c6ae348746df2b294 Mon Sep 17 00:00:00 2001 From: Luca Kato Date: Tue, 21 Jan 2025 16:47:27 -0500 Subject: [PATCH 1/4] zip send file --- app/routes.py | 53 ++++++++++++++++----------- app/templates/site/upload.html | 2 +- app/utils/helper_functions.py | 65 ++++++++++++++++------------------ 3 files changed, 63 insertions(+), 57 deletions(-) diff --git a/app/routes.py b/app/routes.py index 29917621..a196b52c 100644 --- a/app/routes.py +++ b/app/routes.py @@ -6,7 +6,7 @@ from app.utils import * # flask dependencies -from flask import Blueprint, request, render_template, jsonify, Response, stream_with_context, session, redirect, url_for, send_from_directory, current_app +from flask import Blueprint, request, render_template, Response, stream_with_context, session, redirect, url_for, send_from_directory, current_app, send_file import uuid, time @@ -27,18 +27,16 @@ def home(): @routes_bp.route('/viewer') def view(): session_id = session.get('session_id') - print('pulled', session_id) + if not session_id: + return "Session not found", 404 - session_template_path = os.path.join('static', 'sessions_data', session_id, 'output') - full_path = os.path.join(current_app.root_path, session_template_path) - - print('session_template_path:', session_template_path) - print('full_path:', full_path) + output_path = os.path.join(current_app.root_path, 'static', 'sessions_data', + session_id, 'output') - if not os.path.isdir(full_path): + if not os.path.isdir(output_path): return "Directory not found", 404 - return send_from_directory(full_path, 'viewer.html') + return send_from_directory(output_path, 'output.html') @routes_bp.route('/upload', methods=['POST']) def upload_file(): @@ -127,30 +125,43 @@ def generate(): @routes_bp.route('/upload/complete', methods=['GET']) def successful_upload(): print("Entering successful_upload function") - # Get the session ID from query parameter session_id = request.args.get('session_id') or session.get('session_id') - print('in successful upload func, id is: ', session_id) - if not session_id: print("No session ID found") return redirect(url_for('routes_bp.home')) try: - # make sure output.html can generate images - modify_img_paths(session_id) - # Generate the ZIP file and save it + # Generate the ZIP file with HTML and images generate_zip_file(session_id) - - print('before download url set') - download_url = url_for('static', filename=f'sessions_data/{session_id}/output/converted_xbrl.zip', _external=True) - print('download url is', download_url) - - return render_template("site/upload.html", session_id=session_id, download_url=download_url) + return render_template("site/upload.html", session_id=session_id) except Exception as e: print(f"Error in successful_upload: {str(e)}") return redirect(url_for('routes_bp.home')) +@routes_bp.route('/download_zip/') +def download_zip(session_id): + try: + # Use current_app.root_path to get absolute path + zip_path = os.path.join(current_app.root_path, 'static', 'sessions_data', + session_id, 'output', 'converted_xbrl.zip') + + if not os.path.exists(zip_path): + print(f"ZIP file not found at: {zip_path}") + return redirect(url_for('routes_bp.home')) + + print(f"Sending file from: {zip_path}") + + return send_file( + zip_path, + mimetype='application/zip', + as_attachment=True, + download_name='converted_xbrl.zip' + ) + except Exception as e: + print(f"Error in download_zip: {str(e)}") + return redirect(url_for('routes_bp.home')) + @routes_bp.route('/serve_image//') def serve_image(session_id, filename): # Specify the directory to send from. diff --git a/app/templates/site/upload.html b/app/templates/site/upload.html index 17a50744..155524ad 100644 --- a/app/templates/site/upload.html +++ b/app/templates/site/upload.html @@ -12,7 +12,7 @@ {% block card1 %}
Download your machine-readable financial statement
diff --git a/app/utils/helper_functions.py b/app/utils/helper_functions.py index 441ca1c1..145cd8a4 100644 --- a/app/utils/helper_functions.py +++ b/app/utils/helper_functions.py @@ -97,12 +97,16 @@ def update_session_timestamp(session): session['session_timestamp'] = datetime.now(timezone.utc) def modify_img_paths(session_id): - # Define the path to the HTML file - html_file_path = os.path.join('app', 'static', 'sessions_data', session_id, 'output', 'viewer.html') + # Define the paths to both HTML files + output_html_path = os.path.join('app', 'static', 'sessions_data', session_id, 'output', 'output.html') + viewer_html_path = os.path.join('app', 'static', 'sessions_data', session_id, 'output', 'viewer.html') - # Read the HTML file try: - with open(html_file_path, 'r', encoding='utf-8') as file: + # First, copy output.html to viewer.html + shutil.copy2(output_html_path, viewer_html_path) + + # Then modify the viewer.html file + with open(viewer_html_path, 'r', encoding='utf-8') as file: soup = BeautifulSoup(file, 'html.parser') # Find all tags and modify their src attribute @@ -114,13 +118,14 @@ def modify_img_paths(session_id): # Set the new src to just the filename img_tag['src'] = filename - # Save the modified HTML back to the file - with open(html_file_path, 'w', encoding='utf-8') as file: + # Save the modified HTML back to viewer.html + with open(viewer_html_path, 'w', encoding='utf-8') as file: file.write(str(soup)) - print(f"Successfully modified {html_file_path}") + print(f"Successfully created and modified {viewer_html_path}") except Exception as e: print(f"Error in modify_img_paths: {str(e)}") + raise # Re-raise the exception to handle it in the calling function # Generates a zip file to download once conversion is complete def generate_zip_file(session_id): @@ -129,32 +134,22 @@ def generate_zip_file(session_id): img_directory_path = os.path.join('app/static', 'sessions_data', session_id, 'input', 'img') zip_file_path = os.path.join(output_dir, 'converted_xbrl.zip') - # Ensure the output directory exists - os.makedirs(output_dir, exist_ok=True) - - # Debug prints to check paths - print(f"HTML file path: {html_file_path}") - print(f"Image directory path: {img_directory_path}") - print(f"ZIP file path: {zip_file_path}") - - # Create ZIP file - with zipfile.ZipFile(zip_file_path, 'w', zipfile.ZIP_DEFLATED) as zf: - # Add HTML file to zip - if os.path.exists(html_file_path): - print(f"Adding HTML file to zip: {html_file_path}") - zf.write(html_file_path, os.path.basename(html_file_path)) - else: - print(f"HTML file not found: {html_file_path}") + try: + with zipfile.ZipFile(zip_file_path, 'w', zipfile.ZIP_DEFLATED) as zf: + # Add HTML file + if os.path.exists(html_file_path): + zf.write(html_file_path, 'output.html') + + # Add images if they exist + if os.path.exists(img_directory_path): + for root, dirs, files in os.walk(img_directory_path): + for file in files: + file_path = os.path.join(root, file) + archive_name = os.path.join('img', file) + zf.write(file_path, archive_name) - # Add images to zip - if os.path.exists(img_directory_path): - for root, dirs, files in os.walk(img_directory_path): - for file in files: - file_path = os.path.join(root, file) - archive_name = os.path.relpath(file_path, img_directory_path) - print(f"Adding image to zip: {file_path} as {archive_name}") - zf.write(file_path, archive_name) - else: - print(f"Image directory not found: {img_directory_path}") - - zf.close() \ No newline at end of file + return zip_file_path + + except Exception as e: + print(f"Error generating zip file: {str(e)}") + raise \ No newline at end of file From f8ae11499cb572f66ab79264699adfc2b0ced17b Mon Sep 17 00:00:00 2001 From: Luca Kato Date: Wed, 22 Jan 2025 00:31:48 -0500 Subject: [PATCH 2/4] Reverted --- app/utils/helper_functions.py | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/app/utils/helper_functions.py b/app/utils/helper_functions.py index 145cd8a4..8e3dcbea 100644 --- a/app/utils/helper_functions.py +++ b/app/utils/helper_functions.py @@ -97,16 +97,12 @@ def update_session_timestamp(session): session['session_timestamp'] = datetime.now(timezone.utc) def modify_img_paths(session_id): - # Define the paths to both HTML files - output_html_path = os.path.join('app', 'static', 'sessions_data', session_id, 'output', 'output.html') - viewer_html_path = os.path.join('app', 'static', 'sessions_data', session_id, 'output', 'viewer.html') + # Define the path to the HTML file + html_file_path = os.path.join('app', 'static', 'sessions_data', session_id, 'output', 'viewer.html') + # Read the HTML file try: - # First, copy output.html to viewer.html - shutil.copy2(output_html_path, viewer_html_path) - - # Then modify the viewer.html file - with open(viewer_html_path, 'r', encoding='utf-8') as file: + with open(html_file_path, 'r', encoding='utf-8') as file: soup = BeautifulSoup(file, 'html.parser') # Find all tags and modify their src attribute @@ -118,14 +114,13 @@ def modify_img_paths(session_id): # Set the new src to just the filename img_tag['src'] = filename - # Save the modified HTML back to viewer.html - with open(viewer_html_path, 'w', encoding='utf-8') as file: + # Save the modified HTML back to the file + with open(html_file_path, 'w', encoding='utf-8') as file: file.write(str(soup)) - print(f"Successfully created and modified {viewer_html_path}") + print(f"Successfully modified {html_file_path}") except Exception as e: print(f"Error in modify_img_paths: {str(e)}") - raise # Re-raise the exception to handle it in the calling function # Generates a zip file to download once conversion is complete def generate_zip_file(session_id): From e5c2c7bda7b83e343703191f6a6d867765ec67bb Mon Sep 17 00:00:00 2001 From: Luca Kato Date: Wed, 22 Jan 2025 00:34:58 -0500 Subject: [PATCH 3/4] corrected (reverted) to display viewer.html --- app/routes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/routes.py b/app/routes.py index a196b52c..425f63cd 100644 --- a/app/routes.py +++ b/app/routes.py @@ -36,7 +36,7 @@ def view(): if not os.path.isdir(output_path): return "Directory not found", 404 - return send_from_directory(output_path, 'output.html') + return send_from_directory(output_path, 'viewer.html') @routes_bp.route('/upload', methods=['POST']) def upload_file(): From a2b2e68632e215e771d5e5a08dd8f140428658ef Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 22 Jan 2025 21:02:53 +0000 Subject: [PATCH 4/4] docs: update changelog for PR #182 --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e44dfdf0..4c044bb7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,8 +12,6 @@ - PR: #35 - 25 improve loading wheel - PR: #40 -- 7 sessions - - PR: #51 - 28 image handling - PR: #38 - #54 implemented custom line items @@ -148,4 +146,6 @@ - PR: #178 - Revert bracket change - PR: #180 +- 139 download blocked + - PR: #182