2021-12-28 08:39:12 +00:00
|
|
|
import flask
|
|
|
|
from flask import Blueprint
|
2021-12-27 07:54:55 +00:00
|
|
|
from flask.views import View
|
2021-12-28 08:39:12 +00:00
|
|
|
from flask_login import login_user
|
2021-12-27 07:54:55 +00:00
|
|
|
|
2021-12-28 08:39:12 +00:00
|
|
|
from ereuse_devicehub.forms import LoginForm
|
|
|
|
from ereuse_devicehub.resources.user.models import User
|
|
|
|
from ereuse_devicehub.utils import is_safe_url
|
2021-12-22 23:29:44 +00:00
|
|
|
|
|
|
|
core = Blueprint('core', __name__)
|
|
|
|
|
|
|
|
|
2021-12-27 08:15:06 +00:00
|
|
|
class LoginView(View):
|
2021-12-28 08:39:12 +00:00
|
|
|
methods = ['GET', 'POST']
|
2021-12-27 08:15:06 +00:00
|
|
|
template_name = 'ereuse_devicehub/user_login.html'
|
|
|
|
|
|
|
|
def dispatch_request(self):
|
2021-12-28 08:39:12 +00:00
|
|
|
form = LoginForm()
|
|
|
|
if form.validate_on_submit():
|
|
|
|
# Login and validate the user.
|
|
|
|
# user should be an instance of your `User` class
|
|
|
|
user = User.query.filter_by(email=form.email.data).first()
|
|
|
|
login_user(user)
|
|
|
|
|
|
|
|
next_url = flask.request.args.get('next')
|
|
|
|
# is_safe_url should check if the url is safe for redirects.
|
|
|
|
# See http://flask.pocoo.org/snippets/62/ for an example.
|
|
|
|
if not is_safe_url(flask.request, next_url):
|
|
|
|
return flask.abort(400)
|
|
|
|
|
|
|
|
return flask.redirect(next_url or flask.url_for('core.user-profile'))
|
|
|
|
return flask.render_template('ereuse_devicehub/user_login.html', form=form)
|
2021-12-27 08:15:06 +00:00
|
|
|
|
|
|
|
|
2021-12-27 07:54:55 +00:00
|
|
|
class UserProfileView(View):
|
|
|
|
template_name = 'ereuse_devicehub/user_profile.html'
|
|
|
|
|
|
|
|
def dispatch_request(self):
|
2021-12-28 08:39:12 +00:00
|
|
|
context = {}
|
|
|
|
return flask.render_template(self.template_name, **context)
|
2021-12-27 07:54:55 +00:00
|
|
|
|
|
|
|
|
2021-12-27 08:15:06 +00:00
|
|
|
core.add_url_rule('/login/', view_func=LoginView.as_view('login'))
|
2021-12-27 07:54:55 +00:00
|
|
|
core.add_url_rule('/profile/', view_func=UserProfileView.as_view('user-profile'))
|