app.py
Python script, ASCII text executable
1import flask 2from flask_sqlalchemy import SQLAlchemy 3from flask_bcrypt import Bcrypt 4from flask_migrate import Migrate 5 6app = flask.Flask(__name__) 7app.config["SQLALCHEMY_DATABASE_URI"] = \ 8"postgresql://echo:1234@localhost:5432/echo" 9db = SQLAlchemy(app) 10bcrypt = Bcrypt(app) 11migrate = Migrate(app, db) 12app.config["SESSION_TYPE"] = "filesystem" 13app.config["SECRET_KEY"] = "super secret" 14 15with app.app_context(): 16class User(db.Model): 17username = db.Column(db.String(64), unique=True, nullable=False, primary_key=True) 18password = db.Column(db.String(72), nullable=False) 19admin = db.Column(db.Boolean, nullable=False, default=False) 20 21def __init__(self, username, password, admin=False): 22self.username = username 23self.password = bcrypt.generate_password_hash(password).decode("utf-8") 24self.admin = admin 25 26 27@app.context_processor 28def default(): 29return { 30"session": flask.session, 31} 32 33 34@app.route("/") 35def dashboard(): 36return flask.render_template("dashboard.html") 37 38 39@app.route("/login", methods=["GET"]) 40def login(): 41return flask.render_template("login.html") 42 43 44@app.route("/signup", methods=["GET"]) 45def signup(): 46return flask.render_template("signup.html") 47 48 49@app.route("/signup", methods=["POST"]) 50def signup_post(): 51if flask.request.form["password"] != flask.request.form["password2"]: 52flask.flash("Passwords do not match") 53return flask.redirect("/signup", code=303) 54if db.session.get(User, flask.request.form["username"]): 55flask.flash("Username already exists") 56return flask.redirect("/signup", code=303) 57 58new_user = User( 59flask.request.form["username"], 60flask.request.form["password"], 61) 62db.session.add(new_user) 63db.session.commit() 64flask.session["username"] = new_user.username 65return flask.redirect("/", code=303) 66 67 68@app.route("/timeline/<endpoint_id>") 69def info(endpoint_id): 70return flask.render_template("timeline.html", endpoint=endpoint_id) 71