2022-05-11 09:48:53 +00:00
|
|
|
import time
|
|
|
|
|
|
|
|
import flask
|
|
|
|
from flask import Blueprint
|
|
|
|
from flask import current_app as app
|
2022-10-13 16:21:44 +00:00
|
|
|
from flask import g, make_response, request, url_for
|
|
|
|
from flask.views import View
|
2022-05-11 09:48:53 +00:00
|
|
|
from flask_login import login_required
|
|
|
|
|
|
|
|
from ereuse_devicehub import auth
|
|
|
|
from ereuse_devicehub.db import db
|
2022-10-13 16:21:44 +00:00
|
|
|
from ereuse_devicehub.resources.device.models import Placeholder
|
2022-05-11 09:48:53 +00:00
|
|
|
from ereuse_devicehub.resources.enums import SessionType
|
|
|
|
from ereuse_devicehub.resources.user.models import Session
|
2022-05-16 16:38:36 +00:00
|
|
|
from ereuse_devicehub.views import GenericMixin
|
2022-09-07 07:45:34 +00:00
|
|
|
from ereuse_devicehub.workbench import isos
|
2022-10-13 16:21:44 +00:00
|
|
|
from ereuse_devicehub.workbench.forms import KangarooForm
|
2022-05-11 09:48:53 +00:00
|
|
|
|
|
|
|
workbench = Blueprint('workbench', __name__, url_prefix='/workbench')
|
|
|
|
|
|
|
|
|
2022-05-16 16:38:36 +00:00
|
|
|
class SettingsView(GenericMixin):
|
2022-05-11 09:48:53 +00:00
|
|
|
decorators = [login_required]
|
2022-10-13 16:21:44 +00:00
|
|
|
methods = ['GET', 'POST']
|
2022-05-11 09:48:53 +00:00
|
|
|
template_name = 'workbench/settings.html'
|
2023-03-20 16:32:58 +00:00
|
|
|
page_title = "Setup"
|
2022-05-11 09:48:53 +00:00
|
|
|
|
|
|
|
def dispatch_request(self):
|
|
|
|
self.get_context()
|
2022-10-13 16:21:44 +00:00
|
|
|
form_kangaroo = KangarooForm()
|
2022-05-11 09:48:53 +00:00
|
|
|
self.context.update(
|
|
|
|
{
|
|
|
|
'page_title': self.page_title,
|
2022-08-04 12:38:43 +00:00
|
|
|
'demo': g.user.email == app.config['EMAIL_DEMO'],
|
2022-10-17 09:35:14 +00:00
|
|
|
'iso_demo': isos['demo'],
|
|
|
|
'iso_register': isos['register'],
|
2023-05-30 20:37:30 +00:00
|
|
|
'iso_erease2': isos['erease2'],
|
|
|
|
'iso_erease4': isos['erease4'],
|
2022-10-13 16:21:44 +00:00
|
|
|
'form': form_kangaroo,
|
2022-05-11 09:48:53 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-10-13 16:21:44 +00:00
|
|
|
if form_kangaroo.validate_on_submit():
|
|
|
|
form_kangaroo.save()
|
|
|
|
|
2022-05-11 09:48:53 +00:00
|
|
|
self.opt = request.values.get('opt')
|
2022-10-21 15:43:21 +00:00
|
|
|
options = [
|
|
|
|
'register',
|
|
|
|
'erease_basic',
|
|
|
|
'baseline_erease',
|
|
|
|
'enhanced_erease',
|
|
|
|
]
|
|
|
|
if self.opt in options:
|
2022-05-11 09:48:53 +00:00
|
|
|
return self.download()
|
|
|
|
|
|
|
|
return flask.render_template(self.template_name, **self.context)
|
|
|
|
|
|
|
|
def download(self):
|
2022-11-07 16:08:03 +00:00
|
|
|
url = "https://{}/api/inventory/".format(app.config['API_HOST'])
|
2022-05-11 09:48:53 +00:00
|
|
|
self.wbContext = {
|
|
|
|
'token': self.get_token(),
|
2022-05-12 15:19:22 +00:00
|
|
|
'url': url,
|
2022-08-05 09:07:54 +00:00
|
|
|
'erease_basic': None,
|
2022-10-21 15:43:21 +00:00
|
|
|
'baseline_erease': None,
|
2022-05-11 09:48:53 +00:00
|
|
|
}
|
2022-08-05 09:07:54 +00:00
|
|
|
# if is a v14 version
|
|
|
|
# TODO when not use more v14, we can remove this if
|
2023-05-30 20:29:01 +00:00
|
|
|
template = 'workbench/wbSettings_BM.ini'
|
2022-08-05 09:07:54 +00:00
|
|
|
if 'erease' in self.opt:
|
2022-11-07 16:08:03 +00:00
|
|
|
url = "https://{}/actions/".format(app.config['API_HOST'])
|
2022-08-05 09:07:54 +00:00
|
|
|
self.wbContext['url'] = url
|
2022-08-11 16:10:46 +00:00
|
|
|
self.wbContext['host'] = app.config['HOST']
|
2022-11-08 17:35:47 +00:00
|
|
|
self.wbContext['api_host'] = app.config['API_HOST']
|
2022-08-11 16:10:46 +00:00
|
|
|
self.wbContext['schema'] = app.config['SCHEMA']
|
2022-10-21 15:43:21 +00:00
|
|
|
|
2022-08-05 09:07:54 +00:00
|
|
|
if self.opt == 'erease_basic':
|
2023-05-30 20:29:01 +00:00
|
|
|
template = 'workbench/wbSettings_BE.ini'
|
|
|
|
if self.opt == 'baseline_erease':
|
|
|
|
template = 'workbench/wbSettings_BSE.ini'
|
2022-10-21 15:43:21 +00:00
|
|
|
if self.opt == 'enhanced_erease':
|
2023-05-30 20:29:01 +00:00
|
|
|
template = 'workbench/wbSettings_ESE.ini'
|
2022-05-11 09:48:53 +00:00
|
|
|
|
2023-05-30 20:29:01 +00:00
|
|
|
data = flask.render_template(template, **self.wbContext)
|
2022-05-11 09:48:53 +00:00
|
|
|
return self.response_download(data)
|
|
|
|
|
|
|
|
def response_download(self, data):
|
|
|
|
bfile = str.encode(data)
|
|
|
|
output = make_response(bfile)
|
|
|
|
output.headers['Content-Disposition'] = 'attachment; filename=settings.ini'
|
|
|
|
output.headers['Content-type'] = 'text/plain'
|
|
|
|
return output
|
|
|
|
|
|
|
|
def get_token(self):
|
|
|
|
if not g.user.sessions:
|
|
|
|
ses = Session(user=g.user)
|
|
|
|
db.session.add(ses)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
tk = ''
|
|
|
|
now = time.time()
|
|
|
|
for s in g.user.sessions:
|
|
|
|
if s.type == SessionType.Internal and (s.expired == 0 or s.expired > now):
|
|
|
|
tk = s.token
|
|
|
|
break
|
|
|
|
|
|
|
|
assert tk != ''
|
|
|
|
|
|
|
|
token = auth.Auth.encode(tk)
|
|
|
|
return token
|
|
|
|
|
|
|
|
|
2022-10-13 16:21:44 +00:00
|
|
|
class ErasureHostView(View):
|
|
|
|
decorators = [login_required]
|
|
|
|
methods = ['GET']
|
|
|
|
|
|
|
|
def dispatch_request(self, id):
|
|
|
|
self.placeholder = (
|
|
|
|
Placeholder.query.filter(Placeholder.id == id)
|
|
|
|
.filter(Placeholder.kangaroo.is_(True))
|
|
|
|
.filter(Placeholder.owner_id == g.user.id)
|
|
|
|
.one()
|
|
|
|
)
|
|
|
|
self.placeholder.kangaroo = False
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return flask.redirect(url_for('workbench.settings'))
|
|
|
|
|
|
|
|
|
2022-08-04 12:09:59 +00:00
|
|
|
workbench.add_url_rule('/', view_func=SettingsView.as_view('settings'))
|
2022-10-13 16:21:44 +00:00
|
|
|
workbench.add_url_rule(
|
|
|
|
'/erasure_host/<int:id>/', view_func=ErasureHostView.as_view('erasure_host')
|
|
|
|
)
|