Centralize index rendering.

Start building more flexibility about missing environment variables.
This commit is contained in:
Sage Vaillancourt 2022-09-25 21:57:55 -04:00
parent 0f73db5e45
commit 611f4f10f6
2 changed files with 29 additions and 27 deletions

View File

@ -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):

View File

@ -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 '''
<form method="post">
@ -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)