-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
103 lines (77 loc) · 2.99 KB
/
main.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
95
96
97
98
99
100
101
102
103
from flask import Flask, render_template, request, url_for, redirect, flash, send_from_directory
from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin, login_user, LoginManager, login_required, current_user, logout_user
from werkzeug.security import generate_password_hash, check_password_hash
app = Flask(__name__)
app.config['SECRET_KEY'] = 'any-secret-key-you-choose'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
login_manager = LoginManager()
login_manager.init_app(app)
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
##CREATE TABLE IN DB
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(100), unique=True)
password = db.Column(db.String(100))
name = db.Column(db.String(1000))
#Line below only required once, when creating DB.
# db.create_all()
@app.route('/')
def home():
return render_template("index.html", logged_in=current_user.is_authenticated)
@app.route('/register', methods=["GET", "POST"])
def register():
if request.method == "POST":
if User.query.filter_by(email=request.form.get('email')).first():
#User already exists
flash("You've already signed up with that email, log in instead.")
return redirect(url_for('login'))
hash_and_salt_password = generate_password_hash(
request.form.get("password"),
method="pbkdf2:sha256",
salt_length=8
)
new_user = User(
email=request.form.get("email"),
password=hash_and_salt_password,
name=request.form.get("name")
)
db.session.add(new_user)
db.session.commit()
login_user(new_user)
return redirect(url_for("secrets"))
return render_template("register.html")
@app.route('/login', methods=["GET", "POST"])
def login():
if request.method == "POST":
email = request.form.get('email')
password = request.form.get('password')
#Filter user by email entered.
user = User.query.filter_by(email=email).first()
#Email does not exist or password incorrect.
if not user or not check_password_hash(user.password, password):
flash('Invalid credentials, please try again.')
return redirect(url_for('login'))
else:
login_user(user)
return redirect(url_for('secrets'))
return render_template("login.html")
@app.route('/secrets')
@login_required
def secrets():
print(current_user.name)
return render_template("secrets.html", name=current_user.name, logged_in=True)
@app.route('/logout')
def logout():
logout_user()
return redirect(url_for('home'))
@app.route('/download')
def download():
return send_from_directory(
directory="static", path="files/cheat_sheet.pdf", as_attachment=True)
if __name__ == "__main__":
app.run(debug=True)