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 os
import subprocess import subprocess
from dataclasses import dataclass from dataclasses import dataclass
from flask import send_from_directory 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 @dataclass
class CLData(): class CLData():
username: str username: str
@ -53,8 +48,8 @@ def generate(data: CLData, unique):
base_tex = output_dir + unique + ".tex" base_tex = output_dir + unique + ".tex"
com = "pdflatex -halt-on-error -jobname=outputs/" + unique + " " + base_tex com = "pdflatex -halt-on-error -jobname=outputs/" + unique + " " + base_tex
print("COM: '" + com + "'") print("COM: '" + com + "'")
#TODO: If error, don't try to return the pdf try:
subprocess.run(['bash', '-c', com]) subprocess.check_call(['bash', '-c', com])
root_dir = os.path.dirname(os.getcwd()) root_dir = os.path.dirname(os.getcwd())
download = root_dir + '/undercover/outputs/' download = root_dir + '/undercover/outputs/'
@ -66,6 +61,8 @@ def generate(data: CLData, unique):
attachment_filename=data.username.replace(" ", "") + "_CoverLetter.pdf", attachment_filename=data.username.replace(" ", "") + "_CoverLetter.pdf",
as_attachment=True as_attachment=True
) )
except subprocess.CalledProcessError:
return None
def cleanup(unique): def cleanup(unique):
print(unique) print(unique)

View File

@ -1,6 +1,6 @@
# Copyright Sage Vaillancourt 2021 # 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 wtforms import Form, BooleanField, StringField, TextAreaField, validators
from latty import (CLData, generate) from latty import (CLData, generate)
@ -69,6 +69,11 @@ def index():
resp = generate(data, get_unique()) resp = generate(data, get_unique())
# Save entered data as cookies on user's machine # 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(): for pair in data.get_pairs():
resp.set_cookie(pair[0], urllib.parse.quote(pair[1])) resp.set_cookie(pair[0], urllib.parse.quote(pair[1]))
return resp return resp