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 '''