Centralize index rendering.
Start building more flexibility about missing environment variables.
This commit is contained in:
parent
0f73db5e45
commit
611f4f10f6
|
@ -27,13 +27,20 @@ class UserWithHash:
|
||||||
password_hash: str
|
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():
|
def connect():
|
||||||
return psycopg.connect(
|
return psycopg.connect(
|
||||||
host=os.environ['UNDERCOVER_POSTGRES_HOST'],
|
host=host,
|
||||||
dbname=os.environ['UNDERCOVER_POSTGRES_DBNAME'],
|
dbname=db_name,
|
||||||
port=os.environ['UNDERCOVER_POSTGRES_PORT'],
|
port=port,
|
||||||
user=os.environ['UNDERCOVER_POSTGRES_USER'],
|
user=user,
|
||||||
password=os.environ['UNDERCOVER_POSTGRES_PASSWORD'])
|
password=os.environ.get('UNDERCOVER_POSTGRES_PASSWORD'))
|
||||||
|
|
||||||
|
|
||||||
def connected(action):
|
def connected(action):
|
||||||
|
|
|
@ -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'])
|
@writing_blueprint.route('/login', methods=['POST', 'GET'])
|
||||||
def login():
|
def login():
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
|
@ -78,12 +89,7 @@ def login():
|
||||||
if db.login(username, request.form['password']):
|
if db.login(username, request.form['password']):
|
||||||
session['username'] = username
|
session['username'] = username
|
||||||
return redirect('/')
|
return redirect('/')
|
||||||
return make_response(
|
return render_index(error="Invalid username or password", status=401)
|
||||||
render_template(
|
|
||||||
'writing.jinja2',
|
|
||||||
form=CLForm(),
|
|
||||||
error="Invalid username or password"
|
|
||||||
), 401)
|
|
||||||
|
|
||||||
return '''
|
return '''
|
||||||
<form method="post">
|
<form method="post">
|
||||||
|
@ -123,11 +129,7 @@ def index_get():
|
||||||
form.skillTypes.data = data['skillTypes']
|
form.skillTypes.data = data['skillTypes']
|
||||||
form.username.data = data['username']
|
form.username.data = data['username']
|
||||||
|
|
||||||
return render_template(
|
return render_index(form=form)
|
||||||
'writing.jinja2',
|
|
||||||
form=form,
|
|
||||||
username=session.get('username')
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@writing_blueprint.route('/create_account', methods=['GET'])
|
@writing_blueprint.route('/create_account', methods=['GET'])
|
||||||
|
@ -141,10 +143,10 @@ def create_account():
|
||||||
try:
|
try:
|
||||||
validate_email(email_address, check_deliverability=True)
|
validate_email(email_address, check_deliverability=True)
|
||||||
except EmailNotValidError as e:
|
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):
|
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'])
|
db.add_user(email_address, request.form['password'])
|
||||||
session['username'] = email_address
|
session['username'] = email_address
|
||||||
|
@ -241,17 +243,10 @@ def index_post():
|
||||||
|
|
||||||
(resp, errors) = data.generate_pdf()
|
(resp, errors) = data.generate_pdf()
|
||||||
if errors:
|
if errors:
|
||||||
resp = make_response(render_template(
|
resp = render_index(form=form, letter_errors=errors)
|
||||||
'writing.jinja2',
|
|
||||||
form=form,
|
|
||||||
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():
|
||||||
resp.set_cookie(pair[0], urllib.parse.quote(pair[1]))
|
resp.set_cookie(pair[0], urllib.parse.quote(pair[1]))
|
||||||
return resp
|
return resp
|
||||||
|
|
||||||
return render_template(
|
return render_index(form=form)
|
||||||
'writing.jinja2',
|
|
||||||
form=form,
|
|
||||||
)
|
|
||||||
|
|
Loading…
Reference in New Issue