Begin stripping out code from flask template
parent
b0f2514fd8
commit
3aafd03531
|
@ -8,11 +8,6 @@
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from flask import Flask, render_template
|
from flask import Flask, render_template
|
||||||
from flask_login import LoginManager
|
|
||||||
from flask_bcrypt import Bcrypt
|
|
||||||
from flask_debugtoolbar import DebugToolbarExtension
|
|
||||||
from flask_bootstrap import Bootstrap
|
|
||||||
from flask_sqlalchemy import SQLAlchemy
|
|
||||||
|
|
||||||
|
|
||||||
################
|
################
|
||||||
|
@ -30,43 +25,13 @@ app_settings = os.getenv('APP_SETTINGS', 'project.server.config.DevelopmentConfi
|
||||||
app.config.from_object(app_settings)
|
app.config.from_object(app_settings)
|
||||||
|
|
||||||
|
|
||||||
####################
|
|
||||||
#### extensions ####
|
|
||||||
####################
|
|
||||||
|
|
||||||
login_manager = LoginManager()
|
|
||||||
login_manager.init_app(app)
|
|
||||||
bcrypt = Bcrypt(app)
|
|
||||||
toolbar = DebugToolbarExtension(app)
|
|
||||||
bootstrap = Bootstrap(app)
|
|
||||||
db = SQLAlchemy(app)
|
|
||||||
|
|
||||||
|
|
||||||
###################
|
###################
|
||||||
### blueprints ####
|
### blueprints ####
|
||||||
###################
|
###################
|
||||||
|
|
||||||
from project.server.user.views import user_blueprint
|
|
||||||
from project.server.main.views import main_blueprint
|
from project.server.main.views import main_blueprint
|
||||||
app.register_blueprint(user_blueprint)
|
|
||||||
app.register_blueprint(main_blueprint)
|
app.register_blueprint(main_blueprint)
|
||||||
|
|
||||||
|
|
||||||
###################
|
|
||||||
### flask-login ####
|
|
||||||
###################
|
|
||||||
|
|
||||||
from project.server.models import User
|
|
||||||
|
|
||||||
login_manager.login_view = "user.login"
|
|
||||||
login_manager.login_message_category = 'danger'
|
|
||||||
|
|
||||||
|
|
||||||
@login_manager.user_loader
|
|
||||||
def load_user(user_id):
|
|
||||||
return User.query.filter(User.id == int(user_id)).first()
|
|
||||||
|
|
||||||
|
|
||||||
########################
|
########################
|
||||||
#### error handlers ####
|
#### error handlers ####
|
||||||
########################
|
########################
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
#################
|
#################
|
||||||
|
|
||||||
from flask import render_template, Blueprint
|
from flask import render_template, Blueprint
|
||||||
|
from aaaaaaa import pack_ints, unpack_ints
|
||||||
|
|
||||||
|
|
||||||
################
|
################
|
||||||
|
@ -22,9 +23,15 @@ main_blueprint = Blueprint('main', __name__,)
|
||||||
|
|
||||||
@main_blueprint.route('/')
|
@main_blueprint.route('/')
|
||||||
def home():
|
def home():
|
||||||
|
unpacked_set = set()
|
||||||
return render_template('main/home.html')
|
return render_template('main/home.html')
|
||||||
|
|
||||||
|
|
||||||
@main_blueprint.route("/about/")
|
@main_blueprint.route("/about/")
|
||||||
def about():
|
def about():
|
||||||
return render_template("main/about.html")
|
return render_template("main/about.html")
|
||||||
|
|
||||||
|
@main_blueprint.route("/<preferences>")
|
||||||
|
def returning_user(preferences):
|
||||||
|
unpacked_set = unpack_ints(preferences)
|
||||||
|
return render_template("main/home.html")
|
||||||
|
|
|
@ -1,40 +0,0 @@
|
||||||
# project/server/models.py
|
|
||||||
|
|
||||||
|
|
||||||
import datetime
|
|
||||||
|
|
||||||
from project.server import app, db, bcrypt
|
|
||||||
|
|
||||||
|
|
||||||
class User(db.Model):
|
|
||||||
|
|
||||||
__tablename__ = "users"
|
|
||||||
|
|
||||||
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
|
||||||
email = db.Column(db.String(255), unique=True, nullable=False)
|
|
||||||
password = db.Column(db.String(255), nullable=False)
|
|
||||||
registered_on = db.Column(db.DateTime, nullable=False)
|
|
||||||
admin = db.Column(db.Boolean, nullable=False, default=False)
|
|
||||||
|
|
||||||
def __init__(self, email, password, admin=False):
|
|
||||||
self.email = email
|
|
||||||
self.password = bcrypt.generate_password_hash(
|
|
||||||
password, app.config.get('BCRYPT_LOG_ROUNDS')
|
|
||||||
).decode('utf-8')
|
|
||||||
self.registered_on = datetime.datetime.now()
|
|
||||||
self.admin = admin
|
|
||||||
|
|
||||||
def is_authenticated(self):
|
|
||||||
return True
|
|
||||||
|
|
||||||
def is_active(self):
|
|
||||||
return True
|
|
||||||
|
|
||||||
def is_anonymous(self):
|
|
||||||
return False
|
|
||||||
|
|
||||||
def get_id(self):
|
|
||||||
return self.id
|
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
return '<User {0}>'.format(self.email)
|
|
|
@ -1 +0,0 @@
|
||||||
# project/server/user/__init__.py
|
|
|
@ -1,33 +0,0 @@
|
||||||
# project/server/user/forms.py
|
|
||||||
|
|
||||||
|
|
||||||
from flask_wtf import FlaskForm
|
|
||||||
from wtforms import StringField, PasswordField
|
|
||||||
from wtforms.validators import DataRequired, Email, Length, EqualTo
|
|
||||||
|
|
||||||
|
|
||||||
class LoginForm(FlaskForm):
|
|
||||||
email = StringField('Email Address', [DataRequired(), Email()])
|
|
||||||
password = PasswordField('Password', [DataRequired()])
|
|
||||||
|
|
||||||
|
|
||||||
class RegisterForm(FlaskForm):
|
|
||||||
email = StringField(
|
|
||||||
'Email Address',
|
|
||||||
validators=[
|
|
||||||
DataRequired(),
|
|
||||||
Email(message=None),
|
|
||||||
Length(min=6, max=40)
|
|
||||||
]
|
|
||||||
)
|
|
||||||
password = PasswordField(
|
|
||||||
'Password',
|
|
||||||
validators=[DataRequired(), Length(min=6, max=25)]
|
|
||||||
)
|
|
||||||
confirm = PasswordField(
|
|
||||||
'Confirm password',
|
|
||||||
validators=[
|
|
||||||
DataRequired(),
|
|
||||||
EqualTo('password', message='Passwords must match.')
|
|
||||||
]
|
|
||||||
)
|
|
|
@ -1,73 +0,0 @@
|
||||||
# project/server/user/views.py
|
|
||||||
|
|
||||||
|
|
||||||
#################
|
|
||||||
#### imports ####
|
|
||||||
#################
|
|
||||||
|
|
||||||
from flask import render_template, Blueprint, url_for, \
|
|
||||||
redirect, flash, request
|
|
||||||
from flask_login import login_user, logout_user, login_required
|
|
||||||
|
|
||||||
from project.server import bcrypt, db
|
|
||||||
from project.server.models import User
|
|
||||||
from project.server.user.forms import LoginForm, RegisterForm
|
|
||||||
|
|
||||||
################
|
|
||||||
#### config ####
|
|
||||||
################
|
|
||||||
|
|
||||||
user_blueprint = Blueprint('user', __name__,)
|
|
||||||
|
|
||||||
|
|
||||||
################
|
|
||||||
#### routes ####
|
|
||||||
################
|
|
||||||
|
|
||||||
@user_blueprint.route('/register', methods=['GET', 'POST'])
|
|
||||||
def register():
|
|
||||||
form = RegisterForm(request.form)
|
|
||||||
if form.validate_on_submit():
|
|
||||||
user = User(
|
|
||||||
email=form.email.data,
|
|
||||||
password=form.password.data
|
|
||||||
)
|
|
||||||
db.session.add(user)
|
|
||||||
db.session.commit()
|
|
||||||
|
|
||||||
login_user(user)
|
|
||||||
|
|
||||||
flash('Thank you for registering.', 'success')
|
|
||||||
return redirect(url_for("user.members"))
|
|
||||||
|
|
||||||
return render_template('user/register.html', form=form)
|
|
||||||
|
|
||||||
|
|
||||||
@user_blueprint.route('/login', methods=['GET', 'POST'])
|
|
||||||
def login():
|
|
||||||
form = LoginForm(request.form)
|
|
||||||
if form.validate_on_submit():
|
|
||||||
user = User.query.filter_by(email=form.email.data).first()
|
|
||||||
if user and bcrypt.check_password_hash(
|
|
||||||
user.password, request.form['password']):
|
|
||||||
login_user(user)
|
|
||||||
flash('You are logged in. Welcome!', 'success')
|
|
||||||
return redirect(url_for('user.members'))
|
|
||||||
else:
|
|
||||||
flash('Invalid email and/or password.', 'danger')
|
|
||||||
return render_template('user/login.html', form=form)
|
|
||||||
return render_template('user/login.html', title='Please Login', form=form)
|
|
||||||
|
|
||||||
|
|
||||||
@user_blueprint.route('/logout')
|
|
||||||
@login_required
|
|
||||||
def logout():
|
|
||||||
logout_user()
|
|
||||||
flash('You were logged out. Bye!', 'success')
|
|
||||||
return redirect(url_for('main.home'))
|
|
||||||
|
|
||||||
|
|
||||||
@user_blueprint.route('/members')
|
|
||||||
@login_required
|
|
||||||
def members():
|
|
||||||
return render_template('user/members.html')
|
|
Loading…
Reference in New Issue