· 4 years ago · Jun 08, 2021, 08:46 PM
1# makes render template work using jinja2
2import os
3from flask import Flask, flash, session, render_template, redirect, request, url_for,request
4from flask_wtf.csrf import CSRFProtect
5from .forms import RegistrationForm, LoginForm
6from flask_sqlalchemy import SQLAlchemy
7from flask_bcrypt import bcrypt
8from flask_login import user_loaded_from_header, LoginManager
9
10
11
12
13app = Flask(__name__)
14csrf = CSRFProtect(app)
15db = SQLAlchemy(app)
16# Setup CSRF secret key
17SECRET_KEY = os.urandom(32)
18app.config['SECRET_KEY'] = SECRET_KEY
19csrf = CSRFProtect(app)
20csrf.init_app(app)
21# setup databases
22app.config['SQLALCHEMY_DATABASE_URI'] ='User'
23SQLAlchemy(app)
24# Make Login user work
25login_manager = LoginManager()
26login_manager.init_app(app)
27# confused what this does.
28@login_manager.user_loader
29def load_user(user_id):
30 return User.get(user_id)
31
32
33class User(db.Model):
34 id = db.Column(db.Integer, primary_key=True)
35 username = db.Column(db.String(80), unique=True, nullable=False)
36 hashed_password = db.Column(db.String(128), nullable=False)
37 email = db.Column(db.String(120), unique=True, nullable=False)
38
39
40
41#todo turn into a database why is there no post number like 1st post ever posted in general etc?
42posts = {
43 "username": "author",
44 "author": "Bobby Bobson",
45 "Title": "Hello World",
46 "Content": "This is a post content 1",
47 "date_posted": "March 17 2021"
48}
49
50
51
52
53@app.route("/about")
54def about():
55 return render_template('about.html')
56
57
58
59
60@app.route("/register", methods = ['POST', 'GET'])
61def register():
62 form = RegistrationForm()
63 # if form.validate_on_submit():?
64 if request.method == 'POST' and form.validate():
65 # get data from wtf forms
66 username = form.username.data
67 email = form.email.data
68 password = form.password.data
69
70 hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
71 user_db = user_db(username=username, email=email, hashed_password=hashed_password)
72 db.session.add(user_db)
73 # session commit what does this do
74 db.session.commit()
75
76 login_user(user_db)
77 flash('You have registered successfully')
78 return redirect(url_for('login'))
79 return render_template('register.html',title='register', form=form)
80
81@app.route("/login",methods = ['POST', 'GET'])
82def login():
83 form = LoginForm()
84 if request.method == 'POST' and form.validate():
85 # Querying Records
86 # check if username or password inputted in login forms matches the database
87 username = form.username.data
88 # do I need .first()?
89 db_username= User.query.filter_by(username=username).first()
90
91 password = form.password.data
92 hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
93 # login user
94 db_hashed_password = User.query.filter_by(hashed_password=hashed_password).first()
95 user_db = user_db(db_username=db_username,db_hashed_password=db_hashed_password)
96 login_user(user_db)
97 flash('You have logged in successfully')
98
99 return render_template('login.html',title='login', form=form)
100
101
102# read the post
103@app.route("/")
104@app.route("/home")
105def home():
106 return render_template('home.html', posts=posts)
107@app.route("/logoff")
108def logoff():
109 return render_template('home.html')
110# create the posts
111@app.route("/post")
112def post():
113 return render_template('home.html')
114
115if __name__ == '__main__':
116 app.run(debug=True)
117