From 7ee2f2962bb2884b1a4f333728b96f1ac1bf4bc0 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 11 May 2022 11:48:53 +0200 Subject: [PATCH] add view for download Workbench settings --- .../templates/ereuse_devicehub/base_site.html | 10 ++ .../templates/workbench/settings.html | 59 ++++++++++++ ereuse_devicehub/workbench/__init__.py | 0 ereuse_devicehub/workbench/views.py | 94 +++++++++++++++++++ examples/app.py | 2 + tests/conftest.py | 2 + 6 files changed, 167 insertions(+) create mode 100644 ereuse_devicehub/templates/workbench/settings.html create mode 100644 ereuse_devicehub/workbench/__init__.py create mode 100644 ereuse_devicehub/workbench/views.py diff --git a/ereuse_devicehub/templates/ereuse_devicehub/base_site.html b/ereuse_devicehub/templates/ereuse_devicehub/base_site.html index fe014107..1774e6df 100644 --- a/ereuse_devicehub/templates/ereuse_devicehub/base_site.html +++ b/ereuse_devicehub/templates/ereuse_devicehub/base_site.html @@ -64,6 +64,16 @@ +
  • + + + Workbench Settings + +
  • +
  • + +
  • +
  • diff --git a/ereuse_devicehub/templates/workbench/settings.html b/ereuse_devicehub/templates/workbench/settings.html new file mode 100644 index 00000000..80278615 --- /dev/null +++ b/ereuse_devicehub/templates/workbench/settings.html @@ -0,0 +1,59 @@ +{% extends "ereuse_devicehub/base_site.html" %} +{% block main %} + +
    +

    {{ title }}

    + +
    + +
    +
    +
    + +
    +
    + +
    +
    Download your settings for Workbench
    +

    Please select one of this options

    +
    +
    + +
    +

    Download the settings for register devices and erase disks. This is less safe and more fast.

    +
    +
    +
    + +
    +

    Download the settings for register devices and erase disks. This is more safe and more slow.

    +
    +
    +
    + +
    + +
    + +
    + +
    +
    +
    +
    +{% endblock main %} diff --git a/ereuse_devicehub/workbench/__init__.py b/ereuse_devicehub/workbench/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/ereuse_devicehub/workbench/views.py b/ereuse_devicehub/workbench/views.py new file mode 100644 index 00000000..ccbdbdc5 --- /dev/null +++ b/ereuse_devicehub/workbench/views.py @@ -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')) diff --git a/examples/app.py b/examples/app.py index 415d86ce..b0c9b253 100644 --- a/examples/app.py +++ b/examples/app.py @@ -10,11 +10,13 @@ from ereuse_devicehub.devicehub import Devicehub from ereuse_devicehub.inventory.views import devices from ereuse_devicehub.labels.views import labels from ereuse_devicehub.views import core +from ereuse_devicehub.workbench.views import workbench app = Devicehub(inventory=DevicehubConfig.DB_SCHEMA) app.register_blueprint(core) app.register_blueprint(devices) app.register_blueprint(labels) +app.register_blueprint(workbench) # configure & enable CSRF of Flask-WTF # NOTE: enable by blueprint to exclude API views diff --git a/tests/conftest.py b/tests/conftest.py index a8d04506..53fdbaa9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -24,6 +24,7 @@ from ereuse_devicehub.resources.enums import SessionType from ereuse_devicehub.resources.tag import Tag from ereuse_devicehub.resources.user.models import Session, User from ereuse_devicehub.views import core +from ereuse_devicehub.workbench.views import workbench STARTT = datetime(year=2000, month=1, day=1, hour=1) """A dummy starting time to use in tests.""" @@ -58,6 +59,7 @@ def _app(config: TestConfig) -> Devicehub: app.register_blueprint(core) app.register_blueprint(devices) app.register_blueprint(labels) + app.register_blueprint(workbench) app.config["SQLALCHEMY_RECORD_QUERIES"] = True app.config['PROFILE'] = True # app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[30])