WWW service status tracker

By using this site, you agree to have cookies stored on your device, strictly for functional purposes, such as storing your session and preferences.

Dismiss

 app.py

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