UnderCover/writing.py

132 lines
3.6 KiB
Python
Raw Normal View History

# Copyright Sage Vaillancourt 2021
from flask import (Blueprint, render_template, request, make_response)
from wtforms import Form, StringField, TextAreaField, validators
import urllib.parse
from latty import CLData
import flaskr
import flaskr.db as db
import os
2022-09-22 15:43:36 -04:00
import subprocess
import threading
2021-07-23 23:09:08 -04:00
writing_blueprint = Blueprint('writing', __name__,)
class CLForm(Form):
username = StringField(
'Username:',
[validators.Length(min=4, max=99)],
default="Sage"
)
company = StringField(
'Company:',
[validators.Length(min=2, max=99)],
default="BananaCorp"
)
job_and_pronoun = StringField(
'Job and Pronoun (a/an):',
[validators.Length(min=4, max=99)],
2021-07-24 01:08:18 -04:00
default="a banana stocker"
)
skill_types = StringField(
'Skill Type:',
[validators.Length(min=2, max=99)],
default="practical"
)
my_skills = StringField(
'My Skills:',
[validators.Length(min=2, max=99)],
2021-07-24 01:08:18 -04:00
default="stocking bananas"
)
closing_text = TextAreaField(
'Closing Text:',
[validators.Length(min=2, max=99)],
default="I look forward to hearing from you"
)
body = TextAreaField(
'Body:',
[validators.Length(min=4, max=9999)],
2022-09-22 15:47:51 -04:00
default=(
"My name is {\\username}. I'm excited for the opportunity to work as "
"{\\job_and_pronoun} with your company. I think my {\\skill_types} knowledge "
"of {\\my_skills} can contribute a lot to your team.\n\n"
2022-09-22 15:47:51 -04:00
"I am passionate about what I do, and I think we would work well together.\n\n"
"Thank you for your consideration."
)
)
@writing_blueprint.route('/', methods=['GET'])
def index_get():
if flaskr.INDEX is None:
flaskr.INDEX = render_template(
'writing.jinja2',
form=CLForm()
)
return flaskr.INDEX
@writing_blueprint.route('/dbtest', methods=['GET'])
def db_test_get():
response = make_response(db.get_user_letters(1)[0].contents, 200)
response.mimetype = "text/plain"
return response
@writing_blueprint.route('/update', methods=['POST'])
def update_get():
if os.environ['GITLAB_HOOK_TOKEN'] == request.headers['X-Gitlab-Token'] and request.headers['X-Gitlab-Event'] == "Push Hook":
print("Update notification received.")
response = make_response("", 200)
response.mimetype = "text/plain"
2022-09-22 15:43:36 -04:00
threading.Timer(5, git_update, []).start()
return response
else:
return make_response("", 404)
2022-09-22 15:43:36 -04:00
def git_update():
script = os.environ['UPDATE_SCRIPT_PATH']
if not script:
return
subprocess.run(['bash', '-c', "test -f " + script + " && " + script])
@writing_blueprint.route('/', methods=['POST'])
def index_post():
form = CLForm(request.form)
if form.validate():
2021-07-23 23:09:08 -04:00
data = CLData(
username=form.username.data,
company=form.company.data,
job_and_pronoun=form.job_and_pronoun.data,
skill_types=form.skill_types.data,
my_skills=form.my_skills.data,
closing_text=form.closing_text.data,
2021-07-23 23:09:08 -04:00
body=form.body.data,
)
(resp, errors) = data.generate_pdf()
if errors:
resp = make_response(render_template(
'writing.jinja2',
form=form,
errors=errors,
))
# Save entered data as cookies on user's machine
for pair in data.get_pairs():
resp.set_cookie(pair[0], urllib.parse.quote(pair[1]))
return resp
return render_template(
'writing.jinja2',
form=form,
)