Don't try to send file unless latex succeeded

TODO: Report latex errors to the user
This commit is contained in:
Sage Vaillancourt 2021-07-24 15:44:24 -04:00
parent 4f27f8a174
commit 53284df822
2 changed files with 19 additions and 17 deletions

View File

@ -3,14 +3,9 @@
import os
import subprocess
from dataclasses import dataclass
from flask import send_from_directory
# latex /home/sage/Documents/latex/coverLetter/temp.tex'
# dvipdf /home/sage/Documents/latex/coverLetter/temp.dvi ~/Documents/SageVaillancourt_CoverLetter.pdf'
# rm -r /home/sage/Documents/latex/coverLetter/temp*'
@dataclass
class CLData():
username: str
@ -53,19 +48,21 @@ def generate(data: CLData, unique):
base_tex = output_dir + unique + ".tex"
com = "pdflatex -halt-on-error -jobname=outputs/" + unique + " " + base_tex
print("COM: '" + com + "'")
#TODO: If error, don't try to return the pdf
subprocess.run(['bash', '-c', com])
try:
subprocess.check_call(['bash', '-c', com])
root_dir = os.path.dirname(os.getcwd())
download = root_dir + '/undercover/outputs/'
root_dir = os.path.dirname(os.getcwd())
download = root_dir + '/undercover/outputs/'
threading.Timer(60 * 30, cleanup, [output_dir + unique]).start()
return send_from_directory(
download,
unique + ".pdf",
attachment_filename=data.username.replace(" ", "") + "_CoverLetter.pdf",
as_attachment=True
)
threading.Timer(60 * 30, cleanup, [output_dir + unique]).start()
return send_from_directory(
download,
unique + ".pdf",
attachment_filename=data.username.replace(" ", "") + "_CoverLetter.pdf",
as_attachment=True
)
except subprocess.CalledProcessError:
return None
def cleanup(unique):
print(unique)

View File

@ -1,6 +1,6 @@
# Copyright Sage Vaillancourt 2021
from flask import (Blueprint, render_template, request)
from flask import (Blueprint, render_template, request, make_response)
from wtforms import Form, BooleanField, StringField, TextAreaField, validators
from latty import (CLData, generate)
@ -69,6 +69,11 @@ def index():
resp = generate(data, get_unique())
# Save entered data as cookies on user's machine
if not resp:
resp = make_response(render_template('writing.html',
form=form,
unique=get_unique(),
))
for pair in data.get_pairs():
resp.set_cookie(pair[0], urllib.parse.quote(pair[1]))
return resp