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 writing
from latty import generate
def create_app(test_config=None):
app = Flask(__name__, instance_relative_config=True)
@ -29,13 +28,6 @@ def create_app(test_config=None):
except OSError:
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(
writing.writing_blueprint,
#url_prefix='/writing',

View File

@ -6,6 +6,12 @@ from dataclasses import dataclass
from flask import send_from_directory
def get_unique():
import uuid
unique = str(uuid.uuid1().hex)
print("Unique ID: " + unique)
return unique
@dataclass
class CLData():
username: str
@ -27,38 +33,30 @@ class CLData():
("body", self.body),
]
def get_unique():
import uuid
unique = str(uuid.uuid1().hex)
print("Unique ID: " + unique)
return unique
def generate(data: CLData):
def generate_pdf(self):
import threading
unique = get_unique()
proj_dir = os.path.dirname(os.getcwd()) + '/undercover/'
unique_id = get_unique()
root_dir = os.path.dirname(os.getcwd())
proj_dir = root_dir + '/undercover/'
output_dir = proj_dir + 'outputs/'
unique_file = output_dir + unique + ".tex"
unique_file = output_dir + unique_id + ".tex"
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(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
com = "pdflatex -halt-on-error -jobname=outputs/" + unique_id + " " + unique_file
print("COM: '" + com + "'")
try:
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(
download,
unique + ".pdf",
attachment_filename=data.username.replace(" ", "") + "_CoverLetter.pdf",
output_dir,
unique_id + ".pdf",
attachment_filename=self.username.replace(" ", "") + "_CoverLetter.pdf",
as_attachment=True
)
except subprocess.CalledProcessError:
@ -67,4 +65,3 @@ def generate(data: CLData):
def cleanup(unique):
print(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
import urllib.parse
from latty import (CLData, generate)
from latty import CLData
writing_blueprint = Blueprint('writing', __name__,)
@ -61,7 +61,7 @@ def index():
body=form.body.data,
)
resp = generate(data)
resp = data.generate_pdf()
# Save entered data as cookies on user's machine
if not resp:
resp = make_response(render_template('writing.html',