Commit e9887775 authored by Christophe Benz's avatar Christophe Benz
Browse files

Use tempfile and read_bytes

parent 0f20ef37
...@@ -5,7 +5,9 @@ ...@@ -5,7 +5,9 @@
import copy import copy
import itertools import itertools
import json import json
import logging
import subprocess import subprocess
import tempfile
import time import time
from datetime import datetime from datetime import datetime
from io import BytesIO from io import BytesIO
...@@ -27,6 +29,8 @@ from validata_ui.validate_helper import ValidatorHelper ...@@ -27,6 +29,8 @@ from validata_ui.validate_helper import ValidatorHelper
MonkeyPatch.patch_fromisoformat() MonkeyPatch.patch_fromisoformat()
log = logging.getLogger(__name__)
def extract_source_data(source: ValidataSource, preview_rows_nb=5): def extract_source_data(source: ValidataSource, preview_rows_nb=5):
""" Computes table preview """ """ Computes table preview """
...@@ -279,8 +283,8 @@ def pdf_report(val_code): ...@@ -279,8 +283,8 @@ def pdf_report(val_code):
validation_url = '{}?input=url&print=true&url={}'.format(url_for('scdl_validator', val_code=val_code, _external=True), validation_url = '{}?input=url&print=true&url={}'.format(url_for('scdl_validator', val_code=val_code, _external=True),
quote_plus(url_param)) quote_plus(url_param))
# temporary pdf_report.pdf filename with tempfile.NamedTemporaryFile(prefix='validata_{}_report_'.format(val_code), suffix='.pdf') as tmpfile:
tmp_pdf_report = Path('/tmp') / 'validata_{}_{}_pdf_report.pdf'.format(val_code, str(time.time())) tmp_pdf_report = Path(tmpfile.name)
cmd = ['chromium', '--headless', '--disable-gpu', cmd = ['chromium', '--headless', '--disable-gpu',
'--print-to-pdf={}'.format(str(tmp_pdf_report)), validation_url] '--print-to-pdf={}'.format(str(tmp_pdf_report)), validation_url]
...@@ -292,7 +296,7 @@ def pdf_report(val_code): ...@@ -292,7 +296,7 @@ def pdf_report(val_code):
tmp_pdf_report.unlink() tmp_pdf_report.unlink()
return redirect(url_for('scdl_validator', val_code=val_code)) return redirect(url_for('scdl_validator', val_code=val_code))
response = make_response(tmp_pdf_report.open('rb').read()) response = make_response(tmp_pdf_report.read_bytes())
response.headers.set('Content-disposition', 'attachment', filename='report.pdf') response.headers.set('Content-disposition', 'attachment', filename='report.pdf')
response.headers.set('Content-type', 'application/pdf') response.headers.set('Content-type', 'application/pdf')
response.headers.set('Content-length', tmp_pdf_report.stat().st_size) response.headers.set('Content-length', tmp_pdf_report.stat().st_size)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment