74 lines
2.0 KiB
Python
74 lines
2.0 KiB
Python
# Copyright Sage Vaillancourt 2021
|
|
|
|
import os
|
|
import subprocess
|
|
from dataclasses import dataclass
|
|
from flask import send_from_directory
|
|
|
|
|
|
@dataclass
|
|
class CLData():
|
|
username: str
|
|
company: str
|
|
jobandpronoun: str
|
|
skilltypes: str
|
|
myskills: str
|
|
closingtext: str
|
|
body: str
|
|
|
|
def get_pairs(self):
|
|
return [
|
|
("username", self.username),
|
|
("company", self.company),
|
|
("jobandpronoun", self.jobandpronoun),
|
|
("skilltypes", self.skilltypes),
|
|
("myskills", self.myskills),
|
|
("closingtext", self.closingtext),
|
|
("body", self.body),
|
|
]
|
|
|
|
def define(file, name, data, whitespace=""):
|
|
file.write("\\def \\" + name + "{" + data + whitespace + "}\n")
|
|
|
|
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():
|
|
file.write("\\def \\" + pair[0] + "{" + pair[0] + "}\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())
|
|
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
|
|
)
|
|
except subprocess.CalledProcessError:
|
|
return None
|
|
|
|
def cleanup(unique):
|
|
print(unique)
|
|
subprocess.run(['bash', '-c', "rm " + unique + ".*"])
|
|
|