UnderCover/writing.py

108 lines
2.9 KiB
Python

# 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
writing_blueprint = Blueprint('writing', __name__,)
class CLForm(Form):
username = StringField(
'Username:',
[validators.Length(min=4, max=99)],
default="Sage Bernerner"
)
company = StringField(
'Company:',
[validators.Length(min=2, max=99)],
default="BananaCorp"
)
jobandpronoun = StringField(
'Job and Pronoun (a/an):',
[validators.Length(min=4, max=99)],
default="a banana stocker"
)
skilltypes = StringField(
'Skill Type:',
[validators.Length(min=2, max=99)],
default="practical"
)
myskills = StringField(
'My Skills:',
[validators.Length(min=2, max=99)],
default="stocking bananas"
)
closingtext = TextAreaField(
'Closing Text:',
[validators.Length(min=2, max=99)],
default="I look forward to hearing from you"
)
body_string = (
"My name is {\\username}. I would like to work as "
"{\\jobandpronoun} with your company. I think my {\\skilltypes} knowledge "
"of {\\myskills} can contribute a lot to your team.\n\n"
"I am passionate about what I do, and I think we would work well together.\n\n"
"Thank you for your consideration."
)
body = TextAreaField(
'Body:',
[validators.Length(min=4, max=9999)],
default=body_string
)
@writing_blueprint.route('/', methods=['GET'])
def index_get():
if flaskr.INDEX is None:
flaskr.INDEX = render_template(
'writing.html',
form=CLForm()
)
return flaskr.INDEX
@writing_blueprint.route('/dbtest', methods=['GET'])
def index_get():
return db.get_user_letters(1)[0].contents
@writing_blueprint.route('/', methods=['POST'])
def index_post():
form = CLForm(request.form)
if form.validate():
data = CLData(
username=form.username.data,
company=form.company.data,
jobandpronoun=form.jobandpronoun.data,
skilltypes=form.skilltypes.data,
myskills=form.myskills.data,
closingtext=form.closingtext.data,
body=form.body.data,
)
(resp, errors) = data.generate_pdf()
if errors:
resp = make_response(render_template(
'writing.html',
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.html',
form=form,
)