From 0d0005b31532f4d976c3524f16878ba5f3d536a1 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 2 Mar 2021 11:42:07 +0100 Subject: [PATCH] adding file env for download --- .../resources/documents/documents.py | 27 ++++++++++++++----- .../documents/templates/documents/env | 14 ++++++++++ tests/test_documents.py | 7 +++-- 3 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 ereuse_devicehub/resources/documents/templates/documents/env diff --git a/ereuse_devicehub/resources/documents/documents.py b/ereuse_devicehub/resources/documents/documents.py index 3a2f9b6a..8aa345e4 100644 --- a/ereuse_devicehub/resources/documents/documents.py +++ b/ereuse_devicehub/resources/documents/documents.py @@ -1,7 +1,8 @@ import csv -import datetime import enum import uuid +import datetime +import pathlib from collections import OrderedDict from io import StringIO from typing import Callable, Iterable, Tuple @@ -251,7 +252,7 @@ class StampsView(View): url = urlutils.URL(request.url) url.normalize() url.path_parts = url.path_parts[:-2] + ['check', ''] - url_path = url.to_text() + url_path = url.to_text() return flask.render_template('documents/stamp.html', rq_url=url_path) @@ -291,9 +292,23 @@ class InternalStatsView(DeviceView): output.headers['Content-type'] = 'text/csv' return output -class WbConfDocumentView(DeviceView): + +class WbConfDocumentView(View): def get(self, wbtype: str): - return jsonify('') + if not wbtype.lower() in ['usodyrate', 'usodywipe']: + return jsonify('') + + data = {'token': '111', + 'host': 'localhost', + 'inventory': 'dbtest' + } + data['erase'] = False if wbtype == 'usodyrate' else True + + env = flask.render_template('documents/env', **data) + output = make_response(env) + output.headers['Content-Disposition'] = 'attachment; filename=.env' + output.headers['Content-type'] = 'text/plain' + return output class DocumentDef(Resource): @@ -363,8 +378,8 @@ class DocumentDef(Resource): actions_view = app.auth.requires_auth(actions_view) self.add_url_rule('/actions/', defaults=d, view_func=actions_view, methods=get) - wbconf_view = ActionsDocumentView.as_view('WbConfDocumentView', + wbconf_view = WbConfDocumentView.as_view('WbConfDocumentView', definition=self, auth=app.auth) wbconf_view = app.auth.requires_auth(wbconf_view) - self.add_url_rule('/wbconf/', defaults=d, view_func=wbconf_view, methods=get) + self.add_url_rule('/wbconf/', view_func=wbconf_view, methods=get) diff --git a/ereuse_devicehub/resources/documents/templates/documents/env b/ereuse_devicehub/resources/documents/templates/documents/env new file mode 100644 index 00000000..d7b43547 --- /dev/null +++ b/ereuse_devicehub/resources/documents/templates/documents/env @@ -0,0 +1,14 @@ +DH_TOKEN='{{token}}' +DH_HOST='{{host}}' +DH_INVENTORY='{{inventory}}' +DEVICEHUB_URL=https://${DB_HOST}/${DB_INVENTORY}/ + +WB_BENCHMARK = True +WB_STRESS_TEST = 0 +WB_SMART_TEST = 'short' + +WB_ERASE = {{erase}} +WB_ERASE_STEPS = 1 +WB_ERASE_LEADING_ZEROS = False + +WB_DEBUG = True diff --git a/tests/test_documents.py b/tests/test_documents.py index 9639688a..fc5df617 100644 --- a/tests/test_documents.py +++ b/tests/test_documents.py @@ -495,5 +495,8 @@ def test_get_document_internal_stats(user: UserClient, user2: UserClient): def test_get_wbconf(user: UserClient): """Tests for get env file for usb wb.""" - csv_str, _ = user.get(res=documents.DocumentDef.t, - item='wbconf/') + env, _ = user.get(res=documents.DocumentDef.t, item='wbconf/usodyrate', accept=ANY) + assert 'WB_ERASE = False' in env + + env, _ = user.get(res=documents.DocumentDef.t, item='wbconf/usodywipe', accept=ANY) + assert 'WB_ERASE = True' in env