2021-12-28 08:39:12 +00:00
|
|
|
import flask
|
2022-04-11 15:16:20 +00:00
|
|
|
from flask import Blueprint, request
|
2021-12-27 07:54:55 +00:00
|
|
|
from flask.views import View
|
2021-12-28 12:26:56 +00:00
|
|
|
from flask_login import current_user, login_required, login_user, logout_user
|
2021-12-27 07:54:55 +00:00
|
|
|
|
2022-04-11 15:16:20 +00:00
|
|
|
from ereuse_devicehub import __version__, messages
|
|
|
|
from ereuse_devicehub.db import db
|
2022-04-11 17:48:59 +00:00
|
|
|
from ereuse_devicehub.forms import LoginForm, PasswordForm, ProfileForm
|
2021-12-28 08:39:12 +00:00
|
|
|
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__)
|
|
|
|
|
|
|
|
|
2022-04-05 08:49:27 +00:00
|
|
|
@core.route("/")
|
|
|
|
def index():
|
|
|
|
return flask.redirect(flask.url_for('core.login'))
|
|
|
|
|
|
|
|
|
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()
|
2021-12-29 07:10:26 +00:00
|
|
|
login_user(user, remember=form.remember.data)
|
2021-12-28 08:39:12 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2022-03-15 13:33:27 +00:00
|
|
|
return flask.redirect(next_url or flask.url_for('inventory.devicelist'))
|
2022-03-15 10:46:32 +00:00
|
|
|
context = {'form': form, 'version': __version__}
|
|
|
|
return flask.render_template('ereuse_devicehub/user_login.html', **context)
|
2021-12-27 08:15:06 +00:00
|
|
|
|
|
|
|
|
2021-12-28 12:05:41 +00:00
|
|
|
class LogoutView(View):
|
|
|
|
def dispatch_request(self):
|
|
|
|
logout_user()
|
|
|
|
return flask.redirect(flask.url_for('core.login'))
|
|
|
|
|
|
|
|
|
2021-12-27 07:54:55 +00:00
|
|
|
class UserProfileView(View):
|
2022-04-11 15:16:20 +00:00
|
|
|
methods = ['GET', 'POST']
|
2021-12-28 08:44:04 +00:00
|
|
|
decorators = [login_required]
|
2021-12-27 07:54:55 +00:00
|
|
|
template_name = 'ereuse_devicehub/user_profile.html'
|
|
|
|
|
|
|
|
def dispatch_request(self):
|
2022-04-06 11:50:08 +00:00
|
|
|
form = ProfileForm()
|
2022-04-11 15:16:20 +00:00
|
|
|
if request.method == 'GET':
|
|
|
|
form = ProfileForm(user=current_user.individual)
|
|
|
|
|
2022-04-06 10:35:08 +00:00
|
|
|
sessions = {s.created.strftime('%H:%M %d-%m-%Y') for s in current_user.sessions}
|
2021-12-28 12:26:56 +00:00
|
|
|
context = {
|
|
|
|
'current_user': current_user,
|
2022-04-06 10:35:08 +00:00
|
|
|
'sessions': sessions,
|
2022-03-15 10:46:32 +00:00
|
|
|
'version': __version__,
|
2022-04-06 11:50:08 +00:00
|
|
|
'profile_form': form,
|
2022-04-11 17:48:59 +00:00
|
|
|
'password_form': PasswordForm(),
|
2021-12-28 12:26:56 +00:00
|
|
|
}
|
2022-04-11 15:16:20 +00:00
|
|
|
|
|
|
|
if form.validate_on_submit():
|
|
|
|
form.save(commit=False)
|
|
|
|
messages.success('Modify user Profile datas successfully!')
|
2022-04-11 17:48:59 +00:00
|
|
|
db.session.commit()
|
2022-04-11 15:16:20 +00:00
|
|
|
elif form.errors:
|
2022-04-11 17:48:59 +00:00
|
|
|
messages.error('Error modifying user Profile data!')
|
2022-04-11 15:16:20 +00:00
|
|
|
|
2021-12-28 08:39:12 +00:00
|
|
|
return flask.render_template(self.template_name, **context)
|
2021-12-27 07:54:55 +00:00
|
|
|
|
|
|
|
|
2022-04-11 17:48:59 +00:00
|
|
|
class UserPasswordView(View):
|
|
|
|
methods = ['POST']
|
|
|
|
decorators = [login_required]
|
|
|
|
|
|
|
|
def dispatch_request(self):
|
|
|
|
form = PasswordForm()
|
|
|
|
# import pdb; pdb.set_trace()
|
|
|
|
db.session.commit()
|
|
|
|
if form.validate_on_submit():
|
|
|
|
form.save(commit=False)
|
|
|
|
messages.success('Reset user password successfully!')
|
|
|
|
else:
|
|
|
|
messages.error('Error modifying user password!')
|
|
|
|
|
|
|
|
db.session.commit()
|
|
|
|
return flask.redirect(flask.url_for('core.user-profile'))
|
|
|
|
|
|
|
|
|
2021-12-27 08:15:06 +00:00
|
|
|
core.add_url_rule('/login/', view_func=LoginView.as_view('login'))
|
2021-12-28 12:05:41 +00:00
|
|
|
core.add_url_rule('/logout/', view_func=LogoutView.as_view('logout'))
|
2021-12-27 07:54:55 +00:00
|
|
|
core.add_url_rule('/profile/', view_func=UserProfileView.as_view('user-profile'))
|
2022-04-11 17:48:59 +00:00
|
|
|
core.add_url_rule('/set_password/', view_func=UserPasswordView.as_view('set-password'))
|