diff --git a/latty.py b/latty.py index c159c30..78c39d9 100644 --- a/latty.py +++ b/latty.py @@ -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) diff --git a/writing.py b/writing.py index 038d103..c570e30 100644 --- a/writing.py +++ b/writing.py @@ -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