Move generate into CLData as generat_pdf()
This commit is contained in:
parent
178bdb5955
commit
b8ddd290a7
|
@ -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',
|
||||
|
|
67
latty.py
67
latty.py
|
@ -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,44 +33,35 @@ 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):
|
||||
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])
|
||||
|
||||
def generate_pdf(self):
|
||||
import threading
|
||||
unique_id = get_unique()
|
||||
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()
|
||||
return send_from_directory(
|
||||
download,
|
||||
unique + ".pdf",
|
||||
attachment_filename=data.username.replace(" ", "") + "_CoverLetter.pdf",
|
||||
as_attachment=True
|
||||
)
|
||||
except subprocess.CalledProcessError:
|
||||
return None
|
||||
unique_file = output_dir + unique_id + ".tex"
|
||||
f = open(unique_file, "w")
|
||||
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()
|
||||
|
||||
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()
|
||||
|
||||
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):
|
||||
print(unique)
|
||||
subprocess.run(['bash', '-c', "rm " + unique + ".*"])
|
||||
|
||||
|
|
|
@ -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',
|
||||
|
|
Loading…
Reference in New Issue