add validations in public page of device

This commit is contained in:
Cayo Puigdefabregas 2023-04-26 12:24:38 +02:00
parent ad1e5e06d9
commit e649d65b5d
2 changed files with 121 additions and 1 deletions

View file

@ -19,6 +19,7 @@
<!-- JS Files -->
<script src="{{ url_for('static', filename='js/jquery-3.6.0.min.js') }}"></script>
<script src="{{ url_for('static', filename='vendor/bootstrap/js/bootstrap.bundle.min.js') }}"></script>
<!-- Vendor CSS Files -->
<link href="{{ url_for('static', filename='vendor/bootstrap/css/bootstrap.min.css') }}" rel="stylesheet">
@ -40,8 +41,27 @@
<body>
<main>
<nav class="header-nav ms-auto">
<ul class="d-flex align-items-right">
<li class="nav-item">
{% if user.is_anonymous %}
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#validateModal">Validate</button>
{% else %}
<button class="btn btn-primary" id="buttonRol" data-bs-toggle="modal" data-bs-target="#rolsModal">Select your rol</button>
{% endif %}
</li>
</ul>
{{ rol }}
</nav>
<section class="container mt-3">
<div class="row">
<div class="col">
{% if not rol and rols %}
<button class="btn btn-primary">Select a Rol</button>
{% endif %}
</div>
</div>
<div class="row">
<div class="col">
@ -215,6 +235,70 @@
</div>
</div>
</div>
{% if user.is_anonymous %}
<div class="modal fade" id="validateModal" tabindex="-1" style="display: none;" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Validate as <span id="title-action"></span></h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<a class="btn btn-primary" type="button"
href="{{ url_for('core.login') }}?next={{ device_real.url.to_text() }}">
User of system
</a> <br />
<a class="btn btn-primary mt-3" type="button" href="#">
User with Metamax Wallet
</a>
</div>
<div class="modal-footer"></div>
</div>
</div>
</div>
{% else %}
<div class="modal fade" id="rolsModal" tabindex="-1" style="display: none;" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<form action="{{ device_real.url.to_text() }}" method="get">
<div class="modal-header">
<h5 class="modal-title">Select your Rol <span id="title-action"></span></h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<select name="rol">
{% for k, v in rols %}
<option value="{{ k }}" {% if v==rol %}selected=selected{% endif %}>{{ v }}</option>
{% endfor %}
</select>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<input type="submit" class="btn btn-primary" value="Send" />
</div>
</form>
</div>
</div>
</div>
{% endif %}
</body>
<!-- Custom Code -->
{% if not user.is_anonymous and not rol %}
<script>
$(document).ready(() => {
$("#buttonRol").click();
});
</script>
{% endif %}
</html>

View file

@ -4,9 +4,10 @@ from itertools import filterfalse
import flask
import marshmallow
from ereuseapi.methods import API
from flask import Response
from flask import current_app as app
from flask import g, render_template, request
from flask import g, render_template, request, session
from flask.json import jsonify
from flask_sqlalchemy import Pagination
from marshmallow import Schema as MarshmallowSchema
@ -136,7 +137,39 @@ class DeviceView(View):
else:
return self.one_private(id)
def get_rols(self):
if not g.user.is_authenticated:
return []
if 'trublo' not in app.blueprints.keys():
return []
if not session.get('token_dlt'):
return []
token_dlt = session.get('token_dlt')
api_dlt = app.config.get('API_DLT')
if not token_dlt or not api_dlt:
return []
api = API(api_dlt, token_dlt, "ethereum")
result = api.check_user_roles()
if result.get('Status') != 200:
return []
if 'Success' not in result.get('Data', {}).get('status'):
return []
rols = result.get('Data', {}).get('data', {})
return [(k, k) for k, v in rols.items() if v]
def one_public(self, id: int):
rols = self.get_rols()
# rols = [("isOperator", "isOperator"), ("Inspector", "Inspector"), ("Recicler", "Recicler")]
rol = len(rols) == 1 and rols[0][0] or None
if 'rol' in request.args:
rol = dict(rols).get(request.args.get('rol'))
devices = Device.query.filter_by(devicehub_id=id, active=True).all()
if not devices:
devices = [Device.query.filter_by(dhid_bk=id, active=True).one()]
@ -159,6 +192,9 @@ class DeviceView(View):
device_real=device_real,
states=states,
abstract=abstract,
rols=rols,
rol=rol,
user=g.user,
)
@auth.Auth.requires_auth