-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
94 lines (71 loc) · 2.38 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from flask import Flask, redirect, url_for, render_template, request, session, flash
from datetime import timedelta
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.secret_key = "hello"
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///users.sqlite3"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.permanent_session_lifetime = timedelta(minutes=5)
db = SQLAlchemy(app)
class users(db.Model):
_id = db.Column("id", db.Integer, primary_key=True)
name = db.Column(db.String(100))
email = db.Column(db.String(100))
def __init__(self, name, email):
self.name = name
self.email = email
@app.route("/")
def home():
return render_template("index.html")
@app.route("/view")
def view():
return render_template("view.html", values=users.query.all())
@app.route("/login", methods=["POST", "GET"])
def login():
if request.method == "POST":
session.permanent = True
user = request.form["nm"]
session["user"] = user
found_user = users.query.filter_by(name=user).first()
if found_user:
session["email"] = found_user.email
else:
usr = users(user, "")
db.session.add(usr)
db.session.commit()
flash("Login gelukt!")
return redirect(url_for("user"))
else:
if "user" in session:
flash("Al ingelogd!")
return redirect(url_for("user"))
return render_template("login.html")
@app.route("/user", methods=["POST", "GET"])
def user():
email = None
if "user" in session:
user = session["user"]
if request.method == "POST":
email = request.form["email"]
session["email"] = email
found_user = users.query.filter_by(name=user).first()
found_user.email = email
db.session.commit()
flash("Email opgeslagen")
else:
if "email" in session:
email = session["email"]
return render_template("user.html", email=email)
else:
flash("Nog niet ingelogd!")
return redirect(url_for("login"))
@app.route("/logout")
def logout():
flash("Uitgelogd", "info")
session.pop("user", None)
session.pop("email", None)
return redirect(url_for("login"))
if __name__ == '__main__':
with app.app_context():
db.create_all()
app.run(debug=True)