2021-07-23 17:30:15 -04:00
|
|
|
# Copyright Sage Vaillancourt 2021
|
|
|
|
|
2022-09-24 17:39:10 -04:00
|
|
|
import json
|
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-24 17:20:38 -04:00
|
|
|
from flask import (Blueprint, render_template, request, make_response, session, redirect, jsonify)
|
2022-09-25 21:33:09 -04:00
|
|
|
from wtforms import Form, SelectField, StringField, TextAreaField, validators
|
2022-09-24 17:20:38 -04:00
|
|
|
from email_validator import validate_email, EmailNotValidError
|
2021-07-23 23:09:08 -04:00
|
|
|
|
2022-09-23 11:35:56 -04:00
|
|
|
import undercover.db as db
|
2022-09-25 21:33:09 -04:00
|
|
|
import undercover.email as email
|
2022-09-23 13:37:37 -04:00
|
|
|
from undercover.pdf_builder import CLData
|
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-25 21:33:09 -04:00
|
|
|
letterName = SelectField(
|
|
|
|
'Letter Name:',
|
|
|
|
[validators.optional()],
|
|
|
|
choices=[(1, 'LETTER TITLE')]
|
|
|
|
)
|
|
|
|
|
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 15:02:15 -04:00
|
|
|
def login():
|
2022-09-23 14:13:43 -04:00
|
|
|
if request.method == 'POST':
|
2022-09-23 15:14:24 -04:00
|
|
|
username = request.form['login']
|
2022-09-23 14:13:43 -04:00
|
|
|
if db.login(username, request.form['password']):
|
|
|
|
session['username'] = username
|
|
|
|
return redirect('/')
|
2022-09-25 21:33:09 -04:00
|
|
|
return make_response(
|
|
|
|
render_template(
|
|
|
|
'writing.jinja2',
|
|
|
|
form=CLForm(),
|
|
|
|
error="Invalid username or password"
|
|
|
|
), 401)
|
2022-09-23 14:13:43 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2022-09-23 15:12:07 -04:00
|
|
|
@writing_blueprint.route('/logout', methods=['POST', 'GET'])
|
2022-09-23 15:02:15 -04:00
|
|
|
def logout():
|
|
|
|
session.pop('username', None)
|
|
|
|
return redirect('/')
|
|
|
|
|
|
|
|
|
2021-07-31 23:14:59 -04:00
|
|
|
@writing_blueprint.route('/', methods=['GET'])
|
|
|
|
def index_get():
|
2022-09-25 21:33:09 -04:00
|
|
|
email_address = session.get('username')
|
2022-09-24 17:39:10 -04:00
|
|
|
form = CLForm()
|
2022-09-25 21:33:09 -04:00
|
|
|
if email_address:
|
|
|
|
user = db.get_user(email_address)
|
2022-09-24 17:39:10 -04:00
|
|
|
letters = db.get_user_letters(user.id)
|
|
|
|
if len(letters) > 0:
|
2022-09-25 21:33:09 -04:00
|
|
|
letter_names = [(i + 1, letter.title) for i, letter in enumerate(letters)]
|
|
|
|
form.letterName.choices = letter_names
|
2022-09-24 17:39:10 -04:00
|
|
|
# TODO: Load this data more dynamically
|
2022-09-25 21:33:09 -04:00
|
|
|
# I.e. use a dictionary instead of explicitly-defined fields
|
2022-09-24 17:59:38 -04:00
|
|
|
data = json.loads(letters[0].contents)
|
|
|
|
|
2022-09-25 21:33:09 -04:00
|
|
|
form.letterName.data = 1
|
2022-09-24 17:39:10 -04:00
|
|
|
form.company.data = data['company']
|
|
|
|
form.body.data = data['body']
|
|
|
|
form.closingText.data = data['closingText']
|
|
|
|
form.jobAndPronoun.data = data['jobAndPronoun']
|
|
|
|
form.mySkills.data = data['mySkills']
|
|
|
|
form.skillTypes.data = data['skillTypes']
|
2022-09-24 17:59:38 -04:00
|
|
|
form.username.data = data['username']
|
2022-09-24 17:39:10 -04:00
|
|
|
|
2022-09-23 15:02:15 -04:00
|
|
|
return render_template(
|
|
|
|
'writing.jinja2',
|
2022-09-24 17:59:38 -04:00
|
|
|
form=form,
|
2022-09-23 15:12:07 -04:00
|
|
|
username=session.get('username')
|
2022-09-23 15:02:15 -04:00
|
|
|
)
|
2021-07-31 23:14:59 -04:00
|
|
|
|
2022-09-21 23:14:50 -04:00
|
|
|
|
2022-09-23 18:59:42 -04:00
|
|
|
@writing_blueprint.route('/create_account', methods=['GET'])
|
2022-09-23 19:40:07 -04:00
|
|
|
def create_account_page():
|
2022-09-23 18:59:42 -04:00
|
|
|
return render_template('create_account.jinja2')
|
|
|
|
|
|
|
|
|
2022-09-23 19:40:07 -04:00
|
|
|
@writing_blueprint.route('/create_account', methods=['POST'])
|
|
|
|
def create_account():
|
2022-09-25 21:33:09 -04:00
|
|
|
email_address = request.form['login']
|
2022-09-24 17:20:38 -04:00
|
|
|
try:
|
2022-09-25 21:33:09 -04:00
|
|
|
validate_email(email_address, check_deliverability=True)
|
2022-09-24 17:20:38 -04:00
|
|
|
except EmailNotValidError as e:
|
2022-09-25 21:33:09 -04:00
|
|
|
return make_response(render_template('writing.jinja2', form=CLForm(), error=str(e)), 401)
|
2022-09-24 17:20:38 -04:00
|
|
|
|
2022-09-25 21:33:09 -04:00
|
|
|
if db.get_user(email_address):
|
|
|
|
return make_response(render_template('writing.jinja2', form=CLForm(), error="A user with that email already exists!"), 401)
|
2022-09-24 17:20:38 -04:00
|
|
|
|
2022-09-25 21:33:09 -04:00
|
|
|
db.add_user(email_address, request.form['password'])
|
|
|
|
session['username'] = email_address
|
2022-09-23 19:40:07 -04:00
|
|
|
return redirect('/')
|
|
|
|
|
|
|
|
|
2022-09-25 21:33:09 -04:00
|
|
|
@writing_blueprint.route('/reset', methods=['POST', 'GET'])
|
|
|
|
def reset_password():
|
|
|
|
if request.method == 'POST':
|
|
|
|
email_address = request.form.get('login')
|
|
|
|
existing_reset_id = request.form.get('reset_id')
|
|
|
|
if email_address:
|
|
|
|
reset_id = db.initiate_password_reset(email_address)
|
|
|
|
if reset_id:
|
|
|
|
email.send_password_reset(email_address, 'https://undercover.cafe/reset?id=' + str(reset_id))
|
|
|
|
# TODO: Eventually remove db entry whether or not link is clicked
|
|
|
|
elif existing_reset_id:
|
|
|
|
new_password = request.form['password']
|
|
|
|
db.complete_reset(existing_reset_id, new_password)
|
|
|
|
# TODO: Log in?
|
|
|
|
|
|
|
|
return redirect('/')
|
|
|
|
|
|
|
|
query_reset_id = request.args.get('id')
|
|
|
|
# TODO: Add password validation
|
|
|
|
return f'''
|
|
|
|
<form formaction="/reset" method="post">
|
|
|
|
<p><input type=hidden name=reset_id value="{query_reset_id}"></p>
|
|
|
|
<label>New Password:</label>
|
|
|
|
<p><input type=password name=password></p>
|
|
|
|
<p><input type=submit value="Save Password"></p>
|
|
|
|
</form>
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
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():
|
2022-09-24 17:20:38 -04:00
|
|
|
expected_token = os.environ['GITLAB_HOOK_TOKEN']
|
|
|
|
given_token = request.headers['X-Gitlab-Token']
|
|
|
|
event_type = request.headers['X-Gitlab-Event']
|
|
|
|
|
|
|
|
if expected_token == given_token and event_type == "Push Hook":
|
2022-09-22 11:05:17 -04:00
|
|
|
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():
|
2022-09-25 21:33:09 -04:00
|
|
|
selected_letter = form.letterName.data or '1'
|
|
|
|
|
2021-07-23 23:09:08 -04:00
|
|
|
data = CLData(
|
2022-09-25 21:33:09 -04:00
|
|
|
selectedLetter=int(selected_letter) - 1,
|
2021-07-23 23:09:08 -04:00
|
|
|
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
|
|
|
|
2022-09-25 21:33:09 -04:00
|
|
|
email_address = session.get('username')
|
|
|
|
if email_address:
|
|
|
|
user = db.get_user(email_address)
|
2022-09-24 17:20:38 -04:00
|
|
|
letters = db.get_user_letters(user.id)
|
2022-09-24 23:02:52 -04:00
|
|
|
letter_json = jsonify(data).get_data(True)
|
|
|
|
if len(letters) == 0:
|
2022-09-25 21:33:09 -04:00
|
|
|
db.add_letter(user.id, 'My Cover Letter', letter_json)
|
2022-09-24 17:20:38 -04:00
|
|
|
else:
|
2022-09-25 21:33:09 -04:00
|
|
|
letter = letters[data.selectedLetter]
|
|
|
|
# TODO: Support title editing
|
2022-09-24 23:02:52 -04:00
|
|
|
db.edit_letter(letter.id, letter.title, letter_json)
|
2022-09-24 17:20:38 -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,
|
2022-09-24 17:20:38 -04:00
|
|
|
letter_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,
|
|
|
|
)
|