From 3c28678090e900c6c8ef9f20c704ce6b5b5f61ec Mon Sep 17 00:00:00 2001 From: Jonathan Wu Date: Thu, 7 Dec 2023 01:30:02 -0500 Subject: [PATCH] Code cleanup --- src/application.py | 25 ++++--------------------- src/helpers.py | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/src/application.py b/src/application.py index 9272c08..4d764f5 100644 --- a/src/application.py +++ b/src/application.py @@ -227,7 +227,7 @@ def login(): rows = db.execute("SELECT * FROM users WHERE username=:username", username=request.form.get("username")) code = login_chk(rows) - if code != 0: + if code: return render_template("auth/login.html", site_key=app.config['HCAPTCHA_SITE']), code @@ -282,27 +282,10 @@ def register(): confirmation = request.form.get("confirmation") email = request.form.get("email") - # Ensure username is valid - if not username or not verify_text(username): - flash('Invalid username', 'danger') + code = register_chk(username, password, confirmation, email) + if code: return render_template("auth/register.html", - site_key=app.config['HCAPTCHA_SITE']), 400 - - # Ensure password is not blank - if not password or len(password) < 8: - flash('Password must be at least 8 characters', 'danger') - return render_template("auth/register.html", - site_key=app.config['HCAPTCHA_SITE']), 400 - if not confirmation or password != confirmation: - flash('Passwords do not match', 'danger') - return render_template("auth/register.html", - site_key=app.config['HCAPTCHA_SITE']), 400 - - # Ensure email is valid - if "+" in email: - flash('Plus character not allowed in email', 'danger') - return render_template("auth/register.html", - site_key=app.config['HCAPTCHA_SITE']), 400 + site_key=app.config['HCAPTCHA_SITE']), code email = email.lower() # Ensure captcha is valid diff --git a/src/helpers.py b/src/helpers.py index eb1119f..04ef369 100644 --- a/src/helpers.py +++ b/src/helpers.py @@ -330,6 +330,30 @@ def login_chk(rows): return 0 +def register_chk(username, password, confirmation, email): + """ + Determines if the user is allowed to register + Used by register() in application.py + """ + if not username or not verify_text(username): + flash('Invalid username', 'danger') + return 400 + + if not password or len(password) < 8: + flash('Password must be at least 8 characters', 'danger') + return 400 + + if not confirmation or password != confirmation: + flash('Passwords do not match', 'danger') + return 400 + + if "+" in email: + flash('Plus character not allowed in email', 'danger') + return 400 + + return 0 + + def contest_ended(info): """ Determine if the contest from db query info has ended