From 611f4f10f62039ed815f43dcf75f42e3ba95396e Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Sun, 25 Sep 2022 21:57:55 -0400 Subject: [PATCH] Centralize index rendering. Start building more flexibility about missing environment variables. --- undercover/db.py | 17 ++++++++++++----- undercover/routes.py | 39 +++++++++++++++++---------------------- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/undercover/db.py b/undercover/db.py index e37c1b1..75331a9 100644 --- a/undercover/db.py +++ b/undercover/db.py @@ -27,13 +27,20 @@ class UserWithHash: password_hash: str +host = os.environ.get('UNDERCOVER_POSTGRES_HOST') +db_name = os.environ.get('UNDERCOVER_POSTGRES_DBNAME') +port = os.environ.get('UNDERCOVER_POSTGRES_PORT') +user = os.environ.get('UNDERCOVER_POSTGRES_USER') + +db_available = host and db_name and port and user and os.environ.get('UNDERCOVER_POSTGRES_PASSWORD') + def connect(): return psycopg.connect( - host=os.environ['UNDERCOVER_POSTGRES_HOST'], - dbname=os.environ['UNDERCOVER_POSTGRES_DBNAME'], - port=os.environ['UNDERCOVER_POSTGRES_PORT'], - user=os.environ['UNDERCOVER_POSTGRES_USER'], - password=os.environ['UNDERCOVER_POSTGRES_PASSWORD']) + host=host, + dbname=db_name, + port=port, + user=user, + password=os.environ.get('UNDERCOVER_POSTGRES_PASSWORD')) def connected(action): diff --git a/undercover/routes.py b/undercover/routes.py index afa69b8..72bd057 100644 --- a/undercover/routes.py +++ b/undercover/routes.py @@ -71,6 +71,17 @@ class CLForm(Form): ) +def render_index(form=CLForm(), error=None, status=200, letter_errors=None): + return make_response( + render_template( + 'writing.jinja2', + form=form, + username=session.get('username'), + error=error, + letter_errors=letter_errors, + ), status) + + @writing_blueprint.route('/login', methods=['POST', 'GET']) def login(): if request.method == 'POST': @@ -78,12 +89,7 @@ def login(): if db.login(username, request.form['password']): session['username'] = username return redirect('/') - return make_response( - render_template( - 'writing.jinja2', - form=CLForm(), - error="Invalid username or password" - ), 401) + return render_index(error="Invalid username or password", status=401) return '''
@@ -123,11 +129,7 @@ def index_get(): form.skillTypes.data = data['skillTypes'] form.username.data = data['username'] - return render_template( - 'writing.jinja2', - form=form, - username=session.get('username') - ) + return render_index(form=form) @writing_blueprint.route('/create_account', methods=['GET']) @@ -141,10 +143,10 @@ def create_account(): try: validate_email(email_address, check_deliverability=True) except EmailNotValidError as e: - return make_response(render_template('writing.jinja2', form=CLForm(), error=str(e)), 401) + return render_index(error=str(e), status=401) if db.get_user(email_address): - return make_response(render_template('writing.jinja2', form=CLForm(), error="A user with that email already exists!"), 401) + return render_index(error="A user with that email already exists!", status=401) db.add_user(email_address, request.form['password']) session['username'] = email_address @@ -241,17 +243,10 @@ def index_post(): (resp, errors) = data.generate_pdf() if errors: - resp = make_response(render_template( - 'writing.jinja2', - form=form, - letter_errors=errors, - )) + resp = render_index(form=form, letter_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.jinja2', - form=form, - ) + return render_index(form=form)