Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 405 issue #30

Merged
merged 3 commits into from
Apr 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions app/views/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from flask import render_template, Blueprint, redirect, url_for, flash, g
from flask import abort, Blueprint, flash, g, render_template, redirect, request, url_for
from flask_login import LoginManager, UserMixin, current_user, login_user, login_required, logout_user
from urllib.parse import urlparse, urljoin
from app.forms.forms import LoginForm
from app.models.models import User

Expand All @@ -19,13 +20,21 @@ def login():
return redirect(url_for('main.capture'))
form = LoginForm()
if form.validate_on_submit():

user = User.query.filter_by(username=form.username.data).first()

if user is None or not user.check_password(form.password.data):
flash('Invalid username or password')
return redirect(url_for('main.login'))

login_user(user)
g.user = user
return redirect(url_for('main.capture'))

next_ = request.args.get('next')
if not is_safe_url(next_):
return abort(400)

return redirect(next_ or url_for('main.capture'), code=301)
return render_template('login.html', title='Sign In', form=form)

@main.route('/logout', methods=['GET'])
Expand All @@ -48,7 +57,9 @@ def prediction():
def dashboard():
return render_template('my-dashboard.html')

@main.errorhandler(401)
def page_not_found(e):
form = LoginForm()
return render_template('login.html', form=form, title='Sign In')
# http://flask.pocoo.org/snippets/62/
def is_safe_url(target):
ref_url = urlparse(request.host_url)
test_url = urlparse(urljoin(request.host_url, target))
return test_url.scheme in ('http', 'https') and \
ref_url.netloc == test_url.netloc
4 changes: 1 addition & 3 deletions application.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@
sslify = SSLify(application)

application.register_blueprint(views_blueprints)

login_manager = LoginManager(application)
login_manager.init_app(application)

login_manager.login_view = 'main.login'

@application.before_request
def inject_globals():
Expand Down