2021-07-23 17:30:15 -04:00
|
|
|
# Copyright Sage Vaillancourt 2021
|
|
|
|
|
2021-07-24 15:44:24 -04:00
|
|
|
from flask import (Blueprint, render_template, request, make_response)
|
2022-09-21 23:14:50 -04:00
|
|
|
from wtforms import Form, StringField, TextAreaField, validators
|
2021-07-25 14:59:01 -04:00
|
|
|
import urllib.parse
|
2021-07-23 17:30:15 -04:00
|
|
|
|
2021-07-27 23:03:03 -04:00
|
|
|
from latty import CLData
|
2021-07-31 23:14:59 -04:00
|
|
|
import flaskr
|
2022-09-21 23:14:50 -04:00
|
|
|
import flaskr.db as db
|
2022-09-22 11:05:17 -04:00
|
|
|
import os
|
2022-09-22 15:43:36 -04:00
|
|
|
import subprocess
|
|
|
|
import threading
|
2021-07-23 23:09:08 -04:00
|
|
|
|
2021-07-23 17:30:15 -04:00
|
|
|
|
|
|
|
writing_blueprint = Blueprint('writing', __name__,)
|
|
|
|
|
2022-09-21 23:14:50 -04:00
|
|
|
|
2021-07-23 17:30:15 -04:00
|
|
|
class CLForm(Form):
|
2022-09-21 23:14:50 -04:00
|
|
|
username = StringField(
|
|
|
|
'Username:',
|
2021-07-23 17:30:15 -04:00
|
|
|
[validators.Length(min=4, max=99)],
|
2022-09-22 15:12:54 -04:00
|
|
|
default="Sage"
|
2021-07-23 17:30:15 -04:00
|
|
|
)
|
2022-09-21 23:14:50 -04:00
|
|
|
company = StringField(
|
|
|
|
'Company:',
|
2021-07-31 23:29:56 -04:00
|
|
|
[validators.Length(min=2, max=99)],
|
2021-07-23 17:30:15 -04:00
|
|
|
default="BananaCorp"
|
|
|
|
)
|
2022-09-22 16:51:53 -04:00
|
|
|
job_and_pronoun = StringField(
|
2022-09-21 23:14:50 -04:00
|
|
|
'Job and Pronoun (a/an):',
|
2021-07-23 17:30:15 -04:00
|
|
|
[validators.Length(min=4, max=99)],
|
2021-07-24 01:08:18 -04:00
|
|
|
default="a banana stocker"
|
2021-07-23 17:30:15 -04:00
|
|
|
)
|
2022-09-22 16:51:53 -04:00
|
|
|
skill_types = StringField(
|
2022-09-21 23:14:50 -04:00
|
|
|
'Skill Type:',
|
2021-07-31 23:29:56 -04:00
|
|
|
[validators.Length(min=2, max=99)],
|
2021-07-23 17:30:15 -04:00
|
|
|
default="practical"
|
|
|
|
)
|
2022-09-22 16:51:53 -04:00
|
|
|
my_skills = StringField(
|
2022-09-21 23:14:50 -04:00
|
|
|
'My Skills:',
|
2021-07-31 23:29:56 -04:00
|
|
|
[validators.Length(min=2, max=99)],
|
2021-07-24 01:08:18 -04:00
|
|
|
default="stocking bananas"
|
2021-07-23 17:30:15 -04:00
|
|
|
)
|
2022-09-22 16:51:53 -04:00
|
|
|
|
|
|
|
closing_text = TextAreaField(
|
2022-09-21 23:14:50 -04:00
|
|
|
'Closing Text:',
|
2021-07-31 23:29:56 -04:00
|
|
|
[validators.Length(min=2, max=99)],
|
2021-07-24 14:51:13 -04:00
|
|
|
default="I look forward to hearing from you"
|
2021-07-23 17:30:15 -04:00
|
|
|
)
|
|
|
|
|
2022-09-21 23:14:50 -04:00
|
|
|
body = TextAreaField(
|
|
|
|
'Body:',
|
2021-07-24 14:51:13 -04:00
|
|
|
[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 "
|
2022-09-22 16:51:53 -04:00
|
|
|
"{\\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."
|
|
|
|
)
|
2021-07-23 17:30:15 -04:00
|
|
|
)
|
|
|
|
|
2022-09-21 23:14:50 -04:00
|
|
|
|
2021-07-31 23:14:59 -04:00
|
|
|
@writing_blueprint.route('/', methods=['GET'])
|
|
|
|
def index_get():
|
2022-09-21 23:14:50 -04:00
|
|
|
if flaskr.INDEX is None:
|
2021-07-31 23:14:59 -04:00
|
|
|
flaskr.INDEX = render_template(
|
2022-09-22 16:11:26 -04:00
|
|
|
'writing.jinja2',
|
2021-07-31 23:14:59 -04:00
|
|
|
form=CLForm()
|
|
|
|
)
|
|
|
|
return flaskr.INDEX
|
|
|
|
|
2022-09-21 23:14:50 -04:00
|
|
|
|
|
|
|
@writing_blueprint.route('/dbtest', methods=['GET'])
|
2022-09-22 16:51:53 -04:00
|
|
|
def db_test_get():
|
2022-09-22 11:05:17 -04:00
|
|
|
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()
|
2022-09-22 11:05:17 -04:00
|
|
|
return response
|
|
|
|
else:
|
|
|
|
return make_response("", 404)
|
2022-09-21 23:14:50 -04:00
|
|
|
|
|
|
|
|
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])
|
|
|
|
|
|
|
|
|
2021-07-31 23:14:59 -04:00
|
|
|
@writing_blueprint.route('/', methods=['POST'])
|
|
|
|
def index_post():
|
2021-07-23 17:30:15 -04:00
|
|
|
form = CLForm(request.form)
|
2021-07-31 23:14:59 -04:00
|
|
|
if form.validate():
|
2021-07-23 23:09:08 -04:00
|
|
|
data = CLData(
|
|
|
|
username=form.username.data,
|
|
|
|
company=form.company.data,
|
2022-09-22 16:51:53 -04:00
|
|
|
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,
|
|
|
|
)
|
2021-07-23 17:30:15 -04:00
|
|
|
|
2021-07-28 18:43:19 -04:00
|
|
|
(resp, errors) = data.generate_pdf()
|
|
|
|
if errors:
|
2022-09-21 23:14:50 -04:00
|
|
|
resp = make_response(render_template(
|
2022-09-22 16:11:26 -04:00
|
|
|
'writing.jinja2',
|
2021-07-24 15:44:24 -04:00
|
|
|
form=form,
|
2021-07-28 18:43:19 -04:00
|
|
|
errors=errors,
|
2021-07-24 15:44:24 -04:00
|
|
|
))
|
2021-07-28 19:21:02 -04:00
|
|
|
# Save entered data as cookies on user's machine
|
2021-07-24 00:26:44 -04:00
|
|
|
for pair in data.get_pairs():
|
2021-07-24 14:51:13 -04:00
|
|
|
resp.set_cookie(pair[0], urllib.parse.quote(pair[1]))
|
2021-07-24 00:26:44 -04:00
|
|
|
return resp
|
|
|
|
|
2022-09-21 23:14:50 -04:00
|
|
|
return render_template(
|
2022-09-22 16:11:26 -04:00
|
|
|
'writing.jinja2',
|
2021-07-23 17:30:15 -04:00
|
|
|
form=form,
|
|
|
|
)
|