Files
math-website/app.py
T

88 lines
2.5 KiB
Python

from flask import Flask, render_template, redirect, request, url_for
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import generate_password_hash, check_password_hash
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///accounts.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.secret_key = 'super_secret_session_encryption_key'
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
password_hash = db.Column(db.String(128), nullable=False)
with app.app_context():
db.create_all()
@app.route('/submit-form', methods=['POST'])
def handle_submission():
form_username = request.form.get('username')
form_email = request.form.get('email')
form_password = request.form.get('password')
if User.query.filter_by(username=form_username).first():
return "<h3>Username taken!</h3>", 400
if User.query.filter_by(email=form_email).first():
return "<h3>Email already registered!</h3>", 400
hashed_password = generate_password_hash(form_password)
new_user = User(username=form_username, email=form_email, password_hash=hashed_password)
db.session.add(new_user)
db.session.commit()
return redirect(url_for('signup'))
return print('account made')
@app.route('/users')
def view_users():
all_users = User.query.all()
html_output = "<h2>Registered Accounts</h2><table border='1' cellpadding='10'>"
html_output += "<tr><th>ID</th><th>Username</th><th>Email</th></tr>"
for user in all_users:
html_output += f"<tr><td>{user.id}</td><td>{user.username}</td><td>{user.email}</td><td>{user.password_hash}</td></tr>"
html_output += "</table>"
if not all_users:
return "<h3>No accounts found in the database yet.</h3>"
return html_output
@app.route('/')
def home():
return render_template('index.html')
@app.route('/bivariate')
def bivariate():
return render_template('bivariate.html')
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/signin')
def signin():
return render_template('signin.html')
@app.route('/signup')
def signup():
return render_template('signup.html')
@app.route('/all_users')
def all_users():
return render_template('all_users.html')
if __name__ == '__main__':
app.run(debug=True)