Use correct mixin

This commit is contained in:
rubenwardy 2020-12-04 22:29:10 +00:00
parent 9620ceb842
commit a8537659e2
4 changed files with 27 additions and 27 deletions

@ -23,7 +23,7 @@ from flask_github import GitHub
from flask_wtf.csrf import CSRFProtect from flask_wtf.csrf import CSRFProtect
from flask_flatpages import FlatPages from flask_flatpages import FlatPages
from flask_babel import Babel from flask_babel import Babel
from flask_login import logout_user, current_user from flask_login import logout_user, current_user, LoginManager
import os, redis import os, redis
app = Flask(__name__, static_folder="public/static") app = Flask(__name__, static_folder="public/static")
@ -48,6 +48,10 @@ gravatar = Gravatar(app,
use_ssl=True, use_ssl=True,
base_url=None) base_url=None)
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "users.login"
from .sass import sass from .sass import sass
sass(app) sass(app)
@ -64,7 +68,13 @@ init_app(app)
# def get_locale(): # def get_locale():
# return request.accept_languages.best_match(app.config['LANGUAGES'].keys()) # return request.accept_languages.best_match(app.config['LANGUAGES'].keys())
from . import models, tasks, template_filters, usermgr from . import models, tasks, template_filters
@login_manager.user_loader
def load_user(user_id):
return models.User.query.filter_by(username=user_id).first()
from .blueprints import create_blueprints from .blueprints import create_blueprints
create_blueprints(app) create_blueprints(app)

@ -36,16 +36,15 @@ class LoginForm(FlaskForm):
def handle_login(form): def handle_login(form):
username = form.username.data.strip()
user = User.query.filter(or_(User.username == username, User.email == username)).first()
def show_safe_err(err): def show_safe_err(err):
if "@" in username: if "@" in username:
flash("Incorrect email or password", "danger") flash("Incorrect email or password", "danger")
else: else:
flash(err, "error") flash(err, "danger")
username = form.username.data.strip()
user = User.query.filter(or_(User.username == username, User.email == username)).first()
if user is None: if user is None:
return show_safe_err("User {} does not exist".format(username)) return show_safe_err("User {} does not exist".format(username))
@ -67,7 +66,6 @@ def handle_login(form):
return redirect(next or url_for("homepage.home")) return redirect(next or url_for("homepage.home"))
@bp.route("/user/login/", methods=["GET", "POST"]) @bp.route("/user/login/", methods=["GET", "POST"])
def login(): def login():
form = LoginForm(request.form) form = LoginForm(request.form)
@ -117,8 +115,16 @@ def register():
return render_template("users/register.html", form=form) return render_template("users/register.html", form=form)
class ForgotPassword(FlaskForm):
email = StringField("Email", [InputRequired(), Email()])
submit = SubmitField("Reset Password")
@bp.route("/user/forgot-password/", methods=["GET", "POST"]) @bp.route("/user/forgot-password/", methods=["GET", "POST"])
def forgot_password(): def forgot_password():
form = ForgotPassword(request.form)
if form.validate_on_submit():
pass
return "Forgot password page" return "Forgot password page"
@ -128,7 +134,6 @@ class SetPasswordForm(FlaskForm):
password2 = PasswordField("Verify password", [InputRequired(), Length(8, 100)]) password2 = PasswordField("Verify password", [InputRequired(), Length(8, 100)])
submit = SubmitField("Save") submit = SubmitField("Save")
@bp.route("/user/change-password/", methods=["GET", "POST"]) @bp.route("/user/change-password/", methods=["GET", "POST"])
@login_required @login_required
def change_password(): def change_password():
@ -180,7 +185,7 @@ def set_password():
return render_template("users/set_password.html", form=form, optional=request.args.get("optional")) return render_template("users/set_password.html", form=form, optional=request.args.get("optional"))
@bp.route("/users/verify/") @bp.route("/user/verify/")
def verify_email(): def verify_email():
token = request.args.get("token") token = request.args.get("token")
ver = UserEmailVerification.query.filter_by(token=token).first() ver = UserEmailVerification.query.filter_by(token=token).first()

@ -20,13 +20,12 @@ import enum
from urllib.parse import urlparse from urllib.parse import urlparse
from flask import url_for from flask import url_for
from flask_login import UserMixin
from flask_migrate import Migrate from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy, BaseQuery from flask_sqlalchemy import SQLAlchemy, BaseQuery
from sqlalchemy_searchable import SearchQueryMixin, make_searchable from sqlalchemy_searchable import SearchQueryMixin, make_searchable
from sqlalchemy_utils.types import TSVectorType from sqlalchemy_utils.types import TSVectorType
from . import app, gravatar, login_manager
from .usermgr import UserMixin, login_manager
from . import app, gravatar
# Initialise database # Initialise database
db = SQLAlchemy(app) db = SQLAlchemy(app)
@ -1481,7 +1480,3 @@ if app.config.get("LOG_SQL"):
import logging import logging
logging.basicConfig() logging.basicConfig()
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO) logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
@login_manager.user_loader
def load_user(user_id):
return User.query.filter_by(username=user_id).first()

@ -1,10 +0,0 @@
from . import app
from flask_login import LoginManager
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "users.login"
class UserMixin:
is_authenticated = True
is_anonymous = False