Move generate into CLData as generat_pdf()

This commit is contained in:
Sage Vaillancourt 2021-07-27 23:03:03 -04:00
parent 178bdb5955
commit b8ddd290a7
3 changed files with 34 additions and 45 deletions

View File

@ -10,7 +10,6 @@ import subprocess
import time import time
import writing import writing
from latty import generate
def create_app(test_config=None): def create_app(test_config=None):
app = Flask(__name__, instance_relative_config=True) app = Flask(__name__, instance_relative_config=True)
@ -29,13 +28,6 @@ def create_app(test_config=None):
except OSError: except OSError:
pass pass
@app.route('/outputs/<unique>')
def output(unique):
root_dir = os.path.dirname(os.getcwd())
download = root_dir + '/undercover/outputs/'
generate(unique)
return send_from_directory(download, unique + ".pdf", as_attachment=True)
app.register_blueprint( app.register_blueprint(
writing.writing_blueprint, writing.writing_blueprint,
#url_prefix='/writing', #url_prefix='/writing',

View File

@ -6,6 +6,12 @@ from dataclasses import dataclass
from flask import send_from_directory from flask import send_from_directory
def get_unique():
import uuid
unique = str(uuid.uuid1().hex)
print("Unique ID: " + unique)
return unique
@dataclass @dataclass
class CLData(): class CLData():
username: str username: str
@ -27,38 +33,30 @@ class CLData():
("body", self.body), ("body", self.body),
] ]
def get_unique(): def generate_pdf(self):
import uuid
unique = str(uuid.uuid1().hex)
print("Unique ID: " + unique)
return unique
def generate(data: CLData):
import threading import threading
unique = get_unique() unique_id = get_unique()
proj_dir = os.path.dirname(os.getcwd()) + '/undercover/' root_dir = os.path.dirname(os.getcwd())
proj_dir = root_dir + '/undercover/'
output_dir = proj_dir + 'outputs/' output_dir = proj_dir + 'outputs/'
unique_file = output_dir + unique + ".tex"
unique_file = output_dir + unique_id + ".tex"
f = open(unique_file, "w") f = open(unique_file, "w")
for pair in data.get_pairs(): for pair in self.get_pairs():
f.write("\\def \\" + pair[0] + "{" + pair[1] + "}\n") f.write("\\def \\" + pair[0] + "{" + pair[1] + "}\n")
f.write(open(proj_dir + "/writing_templates/base.tex", "r").read()) f.write(open(proj_dir + "/writing_templates/base.tex", "r").read())
f.close() f.close()
base_tex = output_dir + unique + ".tex" com = "pdflatex -halt-on-error -jobname=outputs/" + unique_id + " " + unique_file
com = "pdflatex -halt-on-error -jobname=outputs/" + unique + " " + base_tex
print("COM: '" + com + "'") print("COM: '" + com + "'")
try: try:
subprocess.check_call(['bash', '-c', com]) subprocess.check_call(['bash', '-c', com])
threading.Timer(60 * 30, cleanup, [output_dir + unique_id]).start()
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( return send_from_directory(
download, output_dir,
unique + ".pdf", unique_id + ".pdf",
attachment_filename=data.username.replace(" ", "") + "_CoverLetter.pdf", attachment_filename=self.username.replace(" ", "") + "_CoverLetter.pdf",
as_attachment=True as_attachment=True
) )
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
@ -67,4 +65,3 @@ def generate(data: CLData):
def cleanup(unique): def cleanup(unique):
print(unique) print(unique)
subprocess.run(['bash', '-c', "rm " + unique + ".*"]) subprocess.run(['bash', '-c', "rm " + unique + ".*"])

View File

@ -4,7 +4,7 @@ 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
import urllib.parse import urllib.parse
from latty import (CLData, generate) from latty import CLData
writing_blueprint = Blueprint('writing', __name__,) writing_blueprint = Blueprint('writing', __name__,)
@ -61,7 +61,7 @@ def index():
body=form.body.data, body=form.body.data,
) )
resp = generate(data) resp = data.generate_pdf()
# Save entered data as cookies on user's machine # Save entered data as cookies on user's machine
if not resp: if not resp:
resp = make_response(render_template('writing.html', resp = make_response(render_template('writing.html',