diff --git a/init b/init index 3c4c08b..b528307 100755 --- a/init +++ b/init @@ -38,7 +38,7 @@ commands=( "python3 -m venv $SCRIPT_DIR/.venv" "source .venv/bin/activate" "pip3 install -r requirements.txt" - "mkdir $SCRIPT_DIR/outputs" + "mkdir -p $SCRIPT_DIR/outputs" ) for com in "${commands[@]}"; do diff --git a/requirements.txt b/requirements.txt index d67bd53..c35fc13 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,6 @@ bcrypt==4.0.0 click==8.1.3 +email-validator==1.3.0 Flask==2.2.2 Flask-WTF==1.0.1 Flask-SQLAlchemy==2.5.1 diff --git a/undercover/routes.py b/undercover/routes.py index d645264..b6eb9c5 100644 --- a/undercover/routes.py +++ b/undercover/routes.py @@ -5,8 +5,9 @@ import subprocess import threading 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 email_validator import validate_email, EmailNotValidError import undercover.db as db from undercover.pdf_builder import CLData @@ -103,10 +104,15 @@ def create_account_page(): @writing_blueprint.route('/create_account', methods=['POST']) def create_account(): email = request.form['login'] - user = db.get_user(email) - if user: + try: + 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) - # TODO: Validate email address + db.add_user(email, request.form['password']) session['username'] = email return redirect('/') @@ -121,7 +127,11 @@ def db_test_get(): @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": + 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.") response = make_response("", 200) response.mimetype = "text/plain" @@ -152,12 +162,22 @@ def index_post(): 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() if errors: resp = make_response(render_template( 'writing.jinja2', form=form, - errors=errors, + letter_errors=errors, )) # Save entered data as cookies on user's machine for pair in data.get_pairs(): diff --git a/undercover/templates/base.jinja2 b/undercover/templates/base.jinja2 index 477efe4..20543dc 100644 --- a/undercover/templates/base.jinja2 +++ b/undercover/templates/base.jinja2 @@ -14,6 +14,12 @@