Add email validation

Add a simple error display to base.jinja2
Build a bit of skeleton for saving letters to the DB.
This commit is contained in:
Sage Vaillancourt 2022-09-24 17:20:38 -04:00
parent 86563761f2
commit b3f56872d7
5 changed files with 41 additions and 14 deletions

2
init
View File

@ -38,7 +38,7 @@ commands=(
"python3 -m venv $SCRIPT_DIR/.venv" "python3 -m venv $SCRIPT_DIR/.venv"
"source .venv/bin/activate" "source .venv/bin/activate"
"pip3 install -r requirements.txt" "pip3 install -r requirements.txt"
"mkdir $SCRIPT_DIR/outputs" "mkdir -p $SCRIPT_DIR/outputs"
) )
for com in "${commands[@]}"; do for com in "${commands[@]}"; do

View File

@ -1,5 +1,6 @@
bcrypt==4.0.0 bcrypt==4.0.0
click==8.1.3 click==8.1.3
email-validator==1.3.0
Flask==2.2.2 Flask==2.2.2
Flask-WTF==1.0.1 Flask-WTF==1.0.1
Flask-SQLAlchemy==2.5.1 Flask-SQLAlchemy==2.5.1

View File

@ -5,8 +5,9 @@ import subprocess
import threading import threading
import urllib.parse import urllib.parse
from flask import (Blueprint, render_template, request, make_response, session, redirect) from flask import (Blueprint, render_template, request, make_response, session, redirect, jsonify)
from wtforms import Form, StringField, TextAreaField, validators from wtforms import Form, StringField, TextAreaField, validators
from email_validator import validate_email, EmailNotValidError
import undercover.db as db import undercover.db as db
from undercover.pdf_builder import CLData from undercover.pdf_builder import CLData
@ -103,10 +104,15 @@ def create_account_page():
@writing_blueprint.route('/create_account', methods=['POST']) @writing_blueprint.route('/create_account', methods=['POST'])
def create_account(): def create_account():
email = request.form['login'] email = request.form['login']
user = db.get_user(email) try:
if user: validate_email(email, check_deliverability=True)
except EmailNotValidError as e:
form = CLForm(request.form)
return make_response(render_template('writing.jinja2', form=form, error=str(e)), 401)
if db.get_user(email):
return make_response("A user with that email already exists!", 401) return make_response("A user with that email already exists!", 401)
# TODO: Validate email address
db.add_user(email, request.form['password']) db.add_user(email, request.form['password'])
session['username'] = email session['username'] = email
return redirect('/') return redirect('/')
@ -121,7 +127,11 @@ def db_test_get():
@writing_blueprint.route('/update', methods=['POST']) @writing_blueprint.route('/update', methods=['POST'])
def update_get(): def update_get():
if os.environ['GITLAB_HOOK_TOKEN'] == request.headers['X-Gitlab-Token'] and request.headers['X-Gitlab-Event'] == "Push Hook": 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":
print("Update notification received.") print("Update notification received.")
response = make_response("", 200) response = make_response("", 200)
response.mimetype = "text/plain" response.mimetype = "text/plain"
@ -152,12 +162,22 @@ def index_post():
body=form.body.data, body=form.body.data,
) )
email = session.get('login')
if email:
user = db.get_user(email)
letters = db.get_user_letters(user.id)
if True: # len(letters) == 0:
db.add_letter(user.id, 'LETTER_TITLE', jsonify(data).get_data(True))
else:
print("TODO: Add letter editing!")
# db.edit_letter()
(resp, errors) = data.generate_pdf() (resp, errors) = data.generate_pdf()
if errors: if errors:
resp = make_response(render_template( resp = make_response(render_template(
'writing.jinja2', 'writing.jinja2',
form=form, form=form,
errors=errors, letter_errors=errors,
)) ))
# Save entered data as cookies on user's machine # Save entered data as cookies on user's machine
for pair in data.get_pairs(): for pair in data.get_pairs():

View File

@ -14,6 +14,12 @@
<h1><span class="logo left">Under</span><span class="logo right">Cover</span></h1> <h1><span class="logo left">Under</span><span class="logo right">Cover</span></h1>
</a> </a>
<h2>The secret cover letter generator</h2> <h2>The secret cover letter generator</h2>
{% if error %}
<div class="errors">
{{ error }}
</div>
{% endif %}
<div
{% block content %} {% block content %}
{{ content }} {{ content }}
{% endblock content %} {% endblock content %}

View File

@ -5,7 +5,7 @@
{% block head %} {% block head %}
<script type="text/javascript"> <script type="text/javascript">
window.onload = () => { window.onload = () => {
{% if errors %} {% if letter_errors %}
window.scrollTo(0, document.body.scrollHeight); window.scrollTo(0, document.body.scrollHeight);
{% endif %} {% endif %}
@ -26,7 +26,7 @@
} }
const clearErrors = () => { const clearErrors = () => {
const e = document.getElementById('errors') const e = document.getElementById('letter_errors')
e.parentNode.removeChild(e) e.parentNode.removeChild(e)
} }
</script> </script>
@ -69,15 +69,15 @@
{{ render_field(form.closingText) }} {{ render_field(form.closingText) }}
</dl> </dl>
{% if errors %} {% if letter_errors %}
<div id=errors> <div id=letter_errors>
<h3>Your letter has errors.</h3> <h3>Your letter has errors.</h3>
<p> <p>
There is likely a typo in one of your variables (the {\blabla} bits).<br> There is likely a typo in one of your variables (the {\blabla} bits).<br>
Make sure each is spelled correctly, and has the correct brackets on either side Make sure each is spelled correctly, and has the correct brackets on either side
</p> </p>
<p class=errors> <p class=letter_errors>
{% for error in errors %} {% for error in letter_errors %}
{{ error }}<br> {{ error }}<br>
{% endfor %} {% endfor %}
</p> </p>
@ -86,7 +86,7 @@
<a href="javascript:void(0)" <a href="javascript:void(0)"
class="wipe up-wipe" class="wipe up-wipe"
{% if errors %} {% if letter_errors %}
onclick="clearErrors();document.getElementById('letter-form').submit()" onclick="clearErrors();document.getElementById('letter-form').submit()"
{% else %} {% else %}
onclick="document.getElementById('letter-form').submit()" onclick="document.getElementById('letter-form').submit()"