Skip to content

Commit

Permalink
Merge pull request #183 from closup/dev
Browse files Browse the repository at this point in the history
Merge download fix into main
kwheelan authored Jan 23, 2025
2 parents cc575a7 + a2b2e68 commit 14d2b0a
Showing 4 changed files with 53 additions and 52 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

53 changes: 32 additions & 21 deletions app/routes.py
Original file line number Diff line number Diff line change
@@ -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, 'viewer.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/<session_id>')
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/<session_id>/<filename>')
def serve_image(session_id, filename):
# Specify the directory to send from.
2 changes: 1 addition & 1 deletion app/templates/site/upload.html
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@
{% block card1 %}
<div class="card-header card-header-lg">Download your machine-readable financial statement</div>
<div class="card-body text-center">
<a href="{{ download_url }}" download>
<a href="{{ url_for('routes_bp.download_zip', session_id=session_id) }}">
<button type="button" class="btn btn-michigan btn-lg">Download file</button>
</a>
</div>
46 changes: 18 additions & 28 deletions app/utils/helper_functions.py
Original file line number Diff line number Diff line change
@@ -129,32 +129,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()
return zip_file_path

except Exception as e:
print(f"Error generating zip file: {str(e)}")
raise

0 comments on commit 14d2b0a

Please sign in to comment.