2021-07-23 17:30:15 -04:00
|
|
|
# Copyright Sage Vaillancourt 2021
|
|
|
|
|
2022-09-22 11:05:17 -04:00
|
|
|
import os
|
2022-09-22 15:43:36 -04:00
|
|
|
import subprocess
|
|
|
|
import threading
|
2022-09-23 11:35:56 -04:00
|
|
|
import urllib.parse
|
|
|
|
|
2022-09-23 13:59:43 -04:00
|
|
|
from flask import (Blueprint, render_template, request, make_response, session, redirect)
|
2022-09-23 11:35:56 -04:00
|
|
|
from wtforms import Form, StringField, TextAreaField, validators
|
2021-07-23 23:09:08 -04:00
|
|
|
|
2022-09-23 11:35:56 -04:00
|
|
|
import undercover.db as db
|
2022-09-23 13:37:37 -04:00
|
|
|
from undercover.pdf_builder import CLData
|
2021-07-23 17:30:15 -04:00
|
|
|
|
2022-09-23 11:35:56 -04:00
|
|
|
index_cache = None
|
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:55:49 -04:00
|
|
|
jobAndPronoun = 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:55:49 -04:00
|
|
|
skillTypes = 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:55:49 -04:00
|
|
|
mySkills = 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
|
|
|
|
2022-09-22 16:55:49 -04:00
|
|
|
closingText = 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:55:49 -04:00
|
|
|
"{\\jobAndPronoun} with your company. I think my {\\skillTypes} knowledge "
|
2022-09-22 16:58:12 -04:00
|
|
|
"of {\\mySkills} could contribute well 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
|
|
|
|
2022-09-23 14:13:43 -04:00
|
|
|
@writing_blueprint.route('/login', methods=['POST', 'GET'])
|
2022-09-23 13:59:43 -04:00
|
|
|
def login_post():
|
2022-09-23 14:13:43 -04:00
|
|
|
if request.method == 'POST':
|
|
|
|
username = request.form['username']
|
|
|
|
if db.login(username, request.form['password']):
|
|
|
|
session['username'] = username
|
|
|
|
return redirect('/')
|
|
|
|
return make_response("", 401)
|
|
|
|
|
|
|
|
return '''
|
|
|
|
<form method="post">
|
|
|
|
<p><input type=text name=username></p>
|
|
|
|
<p><input type=password name=password></p>
|
|
|
|
<p><input type=submit value=Login></p>
|
|
|
|
</form>
|
|
|
|
'''
|
2022-09-23 13:59:43 -04:00
|
|
|
|
|
|
|
|
2021-07-31 23:14:59 -04:00
|
|
|
@writing_blueprint.route('/', methods=['GET'])
|
|
|
|
def index_get():
|
2022-09-23 11:35:56 -04:00
|
|
|
global index_cache
|
2022-09-23 14:14:06 -04:00
|
|
|
if not index_cache or 'username' in session:
|
2022-09-23 11:35:56 -04:00
|
|
|
index_cache = render_template(
|
2022-09-22 16:11:26 -04:00
|
|
|
'writing.jinja2',
|
2022-09-23 14:14:06 -04:00
|
|
|
form=CLForm(),
|
|
|
|
username=session['username']
|
2021-07-31 23:14:59 -04:00
|
|
|
)
|
2022-09-23 11:35:56 -04:00
|
|
|
return index_cache
|
2021-07-31 23:14:59 -04:00
|
|
|
|
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:55:49 -04:00
|
|
|
jobAndPronoun=form.jobAndPronoun.data,
|
|
|
|
skillTypes=form.skillTypes.data,
|
|
|
|
mySkills=form.mySkills.data,
|
|
|
|
closingText=form.closingText.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,
|
|
|
|
)
|