This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
devicehub-teal/ereuse_devicehub/views.py
Cayo Puigdefabregas 8aa997a2f6 comment decorators
2021-12-28 11:46:17 +01:00

48 lines
1.6 KiB
Python

import flask
from flask import Blueprint
from flask.views import View
from flask_login import login_required, login_user
from ereuse_devicehub.forms import LoginForm
from ereuse_devicehub.resources.user.models import User
from ereuse_devicehub.utils import is_safe_url
core = Blueprint('core', __name__)
class LoginView(View):
methods = ['GET', 'POST']
template_name = 'ereuse_devicehub/user_login.html'
def dispatch_request(self):
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)
from flask import g, current_app as app
class UserProfileView(View):
# decorators = [login_required]
template_name = 'ereuse_devicehub/user_profile.html'
def dispatch_request(self):
context = {}
return flask.render_template(self.template_name, **context)
core.add_url_rule('/login/', view_func=LoginView.as_view('login'))
core.add_url_rule('/profile/', view_func=UserProfileView.as_view('user-profile'))