Update app.py

This commit is contained in:
ralphi3
2026-06-18 10:36:26 +12:00
parent bcf3f659bb
commit 19fc81c7fb
+1 -7
View File
@@ -23,18 +23,15 @@ with app.app_context():
def handle_submission(): def handle_submission():
form_username = request.form.get('username') form_username = request.form.get('username')
form_email = request.form.get('email') form_email = request.form.get('email')
form_password = request.form.get('password') # 1. Grab password from HTML form_password = request.form.get('password')
# Check for duplicates
if User.query.filter_by(username=form_username).first(): if User.query.filter_by(username=form_username).first():
return "<h3>Username taken!</h3>", 400 return "<h3>Username taken!</h3>", 400
if User.query.filter_by(email=form_email).first(): if User.query.filter_by(email=form_email).first():
return "<h3>Email already registered!</h3>", 400 return "<h3>Email already registered!</h3>", 400
# 2. Hash the password into a secure scramble
hashed_password = generate_password_hash(form_password) hashed_password = generate_password_hash(form_password)
# 3. Save the user with the scrambled hash
new_user = User(username=form_username, email=form_email, password_hash=hashed_password) new_user = User(username=form_username, email=form_email, password_hash=hashed_password)
db.session.add(new_user) db.session.add(new_user)
db.session.commit() db.session.commit()
@@ -47,10 +44,8 @@ def success():
@app.route('/users') @app.route('/users')
def view_users(): def view_users():
# Fetch all user records from the database
all_users = User.query.all() all_users = User.query.all()
# Create a simple HTML table to display them nicely
html_output = "<h2>Registered Accounts</h2><table border='1' cellpadding='10'>" html_output = "<h2>Registered Accounts</h2><table border='1' cellpadding='10'>"
html_output += "<tr><th>ID</th><th>Username</th><th>Email</th></tr>" html_output += "<tr><th>ID</th><th>Username</th><th>Email</th></tr>"
@@ -59,7 +54,6 @@ def view_users():
html_output += "</table>" html_output += "</table>"
# If the database is completely empty
if not all_users: if not all_users:
return "<h3>No accounts found in the database yet.</h3>" return "<h3>No accounts found in the database yet.</h3>"