2021-12-28 08:39:12 +00:00
|
|
|
import flask
|
2023-01-31 11:38:47 +00:00
|
|
|
from decouple import config
|
2022-10-27 18:10:08 +00:00
|
|
|
from flask import Blueprint
|
|
|
|
from flask import current_app as app
|
2023-05-19 15:05:02 +00:00
|
|
|
from flask import g, session
|
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
|
2022-04-29 11:10:44 +00:00
|
|
|
from sqlalchemy import or_
|
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
|
2023-02-13 19:35:31 +00:00
|
|
|
from ereuse_devicehub.forms import LoginForm, PasswordForm, SanitizationEntityForm
|
2022-04-29 11:10:44 +00:00
|
|
|
from ereuse_devicehub.resources.action.models import Trade
|
2023-04-28 10:10:25 +00:00
|
|
|
from ereuse_devicehub.resources.lot.models import Lot, ShareLot
|
2022-10-27 18:10:08 +00:00
|
|
|
from ereuse_devicehub.resources.user.models import User
|
2021-12-28 08:39:12 +00:00
|
|
|
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):
|
2024-02-08 17:40:02 +00:00
|
|
|
# if session.get('_user_id'):
|
|
|
|
# next_url = flask.request.args.get('next')
|
|
|
|
# return flask.redirect(next_url or flask.url_for('inventory.devicelist'))
|
2024-01-29 17:49:02 +00:00
|
|
|
|
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-10-27 18:10:08 +00:00
|
|
|
|
|
|
|
url_register = "#"
|
2022-11-11 16:42:29 +00:00
|
|
|
url_reset_password = "#"
|
|
|
|
|
2022-10-27 18:10:08 +00:00
|
|
|
if 'register' in app.blueprints.keys():
|
2023-01-31 11:38:47 +00:00
|
|
|
url_register = config("PRICES_PAGE", "#")
|
2022-10-27 18:10:08 +00:00
|
|
|
|
2022-11-11 16:42:29 +00:00
|
|
|
if 'reset_password' in app.blueprints.keys():
|
|
|
|
url_reset_password = flask.url_for('reset_password.reset-password')
|
|
|
|
|
|
|
|
context = {
|
|
|
|
'form': form,
|
|
|
|
'version': __version__,
|
|
|
|
'url_register': url_register,
|
|
|
|
'url_reset_password': url_reset_password,
|
|
|
|
}
|
2022-10-27 18:10:08 +00:00
|
|
|
|
|
|
|
return flask.render_template(self.template_name, **context)
|
2021-12-27 08:15:06 +00:00
|
|
|
|
|
|
|
|
2021-12-28 12:05:41 +00:00
|
|
|
class LogoutView(View):
|
|
|
|
def dispatch_request(self):
|
2023-11-06 15:48:03 +00:00
|
|
|
session_vars = [
|
|
|
|
'token_dlt',
|
|
|
|
'eth_pub_key',
|
|
|
|
'rols',
|
|
|
|
'oidc',
|
|
|
|
'iota_abac_did',
|
|
|
|
'iota_abac_attributes',
|
|
|
|
]
|
2023-05-19 15:05:02 +00:00
|
|
|
[session.pop(i, '') for i in session_vars]
|
2023-05-29 11:48:43 +00:00
|
|
|
next_url = flask.request.args.get('next')
|
2021-12-28 12:05:41 +00:00
|
|
|
logout_user()
|
2023-05-29 11:48:43 +00:00
|
|
|
return flask.redirect(next_url or flask.url_for('core.login'))
|
2021-12-28 12:05:41 +00:00
|
|
|
|
|
|
|
|
2022-05-16 15:52:31 +00:00
|
|
|
class GenericMixin(View):
|
2022-05-18 09:01:58 +00:00
|
|
|
methods = ['GET']
|
2021-12-28 08:44:04 +00:00
|
|
|
decorators = [login_required]
|
2021-12-27 07:54:55 +00:00
|
|
|
|
2022-04-29 11:10:44 +00:00
|
|
|
def get_lots(self):
|
|
|
|
return (
|
|
|
|
Lot.query.outerjoin(Trade)
|
|
|
|
.filter(
|
|
|
|
or_(
|
|
|
|
Trade.user_from == g.user,
|
|
|
|
Trade.user_to == g.user,
|
|
|
|
Lot.owner_id == g.user.id,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.distinct()
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_context(self):
|
|
|
|
self.context = {
|
|
|
|
'lots': self.get_lots(),
|
2022-03-15 10:46:32 +00:00
|
|
|
'version': __version__,
|
2023-04-28 10:10:25 +00:00
|
|
|
'share_lots': ShareLot.query.filter_by(user_to=g.user),
|
2021-12-28 12:26:56 +00:00
|
|
|
}
|
2022-04-11 15:16:20 +00:00
|
|
|
|
2022-04-29 11:10:44 +00:00
|
|
|
return self.context
|
|
|
|
|
|
|
|
|
2022-05-16 15:52:31 +00:00
|
|
|
class UserProfileView(GenericMixin):
|
2022-04-29 11:10:44 +00:00
|
|
|
decorators = [login_required]
|
|
|
|
template_name = 'ereuse_devicehub/user_profile.html'
|
|
|
|
|
|
|
|
def dispatch_request(self):
|
|
|
|
self.get_context()
|
2023-02-13 19:35:31 +00:00
|
|
|
sanitization_form = SanitizationEntityForm()
|
|
|
|
if g.user.sanitization_entity:
|
2023-03-07 15:41:24 +00:00
|
|
|
sanitization = g.user.sanitization_entity
|
2023-02-14 11:15:21 +00:00
|
|
|
sanitization_form = SanitizationEntityForm(obj=sanitization)
|
2023-05-16 08:05:50 +00:00
|
|
|
oidc = 'oidc' in app.blueprints.keys()
|
2022-04-29 11:10:44 +00:00
|
|
|
self.context.update(
|
|
|
|
{
|
|
|
|
'current_user': current_user,
|
|
|
|
'password_form': PasswordForm(),
|
2023-02-13 19:35:31 +00:00
|
|
|
'sanitization_form': sanitization_form,
|
2023-05-16 08:05:50 +00:00
|
|
|
'oidc': oidc,
|
2022-04-29 11:10:44 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
return flask.render_template(self.template_name, **self.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()
|
|
|
|
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'))
|
|
|
|
|
|
|
|
|
2023-02-13 19:35:31 +00:00
|
|
|
class SanitizationEntityView(View):
|
|
|
|
methods = ['POST']
|
|
|
|
decorators = [login_required]
|
|
|
|
|
|
|
|
def dispatch_request(self):
|
|
|
|
form = SanitizationEntityForm()
|
|
|
|
if form.validate_on_submit():
|
2023-03-07 15:41:24 +00:00
|
|
|
form.save()
|
2023-03-08 11:34:52 +00:00
|
|
|
messages.success('Sanitization data updated successfully!')
|
2023-02-13 19:35:31 +00:00
|
|
|
else:
|
2023-03-08 11:34:52 +00:00
|
|
|
messages.error('Error modifying Sanitization data!')
|
2023-03-09 17:07:16 +00:00
|
|
|
if form.errors:
|
|
|
|
for k in form.errors.keys():
|
2023-03-13 16:04:57 +00:00
|
|
|
errors = ", ".join(form.errors[k])
|
|
|
|
txt = "{}: {}".format(k, errors)
|
2023-03-09 17:07:16 +00:00
|
|
|
messages.error(txt)
|
2023-02-13 19:35:31 +00:00
|
|
|
|
|
|
|
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'))
|
2023-02-13 19:35:31 +00:00
|
|
|
core.add_url_rule(
|
|
|
|
'/set_sanitization/', view_func=SanitizationEntityView.as_view('set-sanitization')
|
|
|
|
)
|