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