add view for download Workbench settings

This commit is contained in:
Cayo Puigdefabregas 2022-05-11 11:48:53 +02:00
parent cbf8d68461
commit 7ee2f2962b
6 changed files with 167 additions and 0 deletions

View File

@ -64,6 +64,16 @@
<hr class="dropdown-divider"> <hr class="dropdown-divider">
</li> </li>
<li>
<a class="dropdown-item d-flex align-items-center" href="{{ url_for('workbench.settings') }}">
<i class="bi bi-tools"></i>
<span>Workbench Settings</span>
</a>
</li>
<li>
<hr class="dropdown-divider">
</li>
<li> <li>
<a class="dropdown-item d-flex align-items-center" href="https://help.usody.com/" target="_blank"> <a class="dropdown-item d-flex align-items-center" href="https://help.usody.com/" target="_blank">
<i class="bi bi-question-circle"></i> <i class="bi bi-question-circle"></i>

View File

@ -0,0 +1,59 @@
{% extends "ereuse_devicehub/base_site.html" %}
{% block main %}
<div class="pagetitle">
<h1>{{ title }}</h1>
<nav>
<ol class="breadcrumb">
<li class="breadcrumb-item">{{ page_title }}</li>
</ol>
</nav>
</div><!-- End Page Title -->
<section class="section profile">
<div class="row">
<div class="col-xl-6">
<div class="card">
<div class="card-body">
<div class="pt-6 pb-2">
<h5 class="card-title text-center pb-0 fs-4">Download your settings for Workbench</h5>
<p class="text-center small">Please select one of this options</p>
<div class="row pt-3">
<div class="col-5">
<a href="{{ url_for('workbench.settings') }}?opt=register" class="btn btn-primary">Register devices</a>
</div>
<div class="col">
<p class="small">Download the settings only for register devices.</p>
</div>
</div>
<div class="row pt-3">
<div class="col-5">
<a href="{{ url_for('workbench.settings') }}?opt=soft" class="btn btn-primary">Register and erase soft disks</a><br />
</div>
<div class="col">
<p class="small">Download the settings for register devices and erase disks. This is less safe and more fast.</p>
</div>
</div>
<div class="row pt-3">
<div class="col-5">
<a href="{{ url_for('workbench.settings') }}?opt=hard" class="btn btn-primary">Register and erase hard disks</a><br />
</div>
<div class="col">
<p class="small">Download the settings for register devices and erase disks. This is more safe and more slow.</p>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-xl-8">
</div>
</div>
</section>
{% endblock main %}

View File

View File

@ -0,0 +1,94 @@
import time
import flask
from flask import Blueprint
from flask import current_app as app
from flask import g, make_response, request
from flask_login import login_required
from ereuse_devicehub import auth
from ereuse_devicehub.db import db
from ereuse_devicehub.resources.enums import SessionType
from ereuse_devicehub.resources.user.models import Session
from ereuse_devicehub.views import GenericMixView
workbench = Blueprint('workbench', __name__, url_prefix='/workbench')
class SettingsView(GenericMixView):
decorators = [login_required]
template_name = 'workbench/settings.html'
page_title = "Workbench Settings"
def dispatch_request(self):
self.get_context()
self.context.update(
{
'page_title': self.page_title,
}
)
self.opt = request.values.get('opt')
if self.opt in ['register', 'soft', 'hard']:
return self.download()
return flask.render_template(self.template_name, **self.context)
def download(self):
self.wbContext = {
'token': self.get_token(),
'host': app.config['HOST'],
'inventory': app.config['SCHEMA'],
'benchmark': False,
'stress_test': 1,
'erase': '',
'steps': 0,
'leading_zeros': False,
}
options = {"register": self.register, "soft": self.soft, "hard": self.hard}
return options[self.opt]()
def register(self):
data = flask.render_template('workbench/wbSettings.ini', **self.wbContext)
return self.response_download(data)
def soft(self):
self.wbContext['erase'] = 'EraseBasic'
self.wbContext['steps'] = 1
data = flask.render_template('workbench/wbSettings.ini', **self.wbContext)
return self.response_download(data)
def hard(self):
self.wbContext['erase'] = 'EraseSectors'
self.wbContext['steps'] = 1
self.wbContext['leading_zeros'] = True
data = flask.render_template('workbench/wbSettings.ini', **self.wbContext)
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
workbench.add_url_rule('/settings/', view_func=SettingsView.as_view('settings'))

View File

@ -10,11 +10,13 @@ from ereuse_devicehub.devicehub import Devicehub
from ereuse_devicehub.inventory.views import devices from ereuse_devicehub.inventory.views import devices
from ereuse_devicehub.labels.views import labels from ereuse_devicehub.labels.views import labels
from ereuse_devicehub.views import core from ereuse_devicehub.views import core
from ereuse_devicehub.workbench.views import workbench
app = Devicehub(inventory=DevicehubConfig.DB_SCHEMA) app = Devicehub(inventory=DevicehubConfig.DB_SCHEMA)
app.register_blueprint(core) app.register_blueprint(core)
app.register_blueprint(devices) app.register_blueprint(devices)
app.register_blueprint(labels) app.register_blueprint(labels)
app.register_blueprint(workbench)
# configure & enable CSRF of Flask-WTF # configure & enable CSRF of Flask-WTF
# NOTE: enable by blueprint to exclude API views # NOTE: enable by blueprint to exclude API views

View File

@ -24,6 +24,7 @@ from ereuse_devicehub.resources.enums import SessionType
from ereuse_devicehub.resources.tag import Tag from ereuse_devicehub.resources.tag import Tag
from ereuse_devicehub.resources.user.models import Session, User from ereuse_devicehub.resources.user.models import Session, User
from ereuse_devicehub.views import core from ereuse_devicehub.views import core
from ereuse_devicehub.workbench.views import workbench
STARTT = datetime(year=2000, month=1, day=1, hour=1) STARTT = datetime(year=2000, month=1, day=1, hour=1)
"""A dummy starting time to use in tests.""" """A dummy starting time to use in tests."""
@ -58,6 +59,7 @@ def _app(config: TestConfig) -> Devicehub:
app.register_blueprint(core) app.register_blueprint(core)
app.register_blueprint(devices) app.register_blueprint(devices)
app.register_blueprint(labels) app.register_blueprint(labels)
app.register_blueprint(workbench)
app.config["SQLALCHEMY_RECORD_QUERIES"] = True app.config["SQLALCHEMY_RECORD_QUERIES"] = True
app.config['PROFILE'] = True app.config['PROFILE'] = True
# app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[30]) # app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[30])