From 9829ebe7c72c26e81568695c1d34da7eb197b56b Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 24 Jun 2022 15:05:38 +0200 Subject: [PATCH 1/2] add download snapshots --- ereuse_devicehub/inventory/views.py | 34 ++++++++++++++++++- .../templates/inventory/snapshots_list.html | 8 +++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 66cb32a1..da4df610 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -1,11 +1,15 @@ import csv import logging +import os from distutils.util import strtobool from io import StringIO +from pathlib import Path import flask import flask_weasyprint -from flask import Blueprint, g, make_response, request, url_for +from flask import Blueprint +from flask import current_app as app +from flask import g, make_response, request, url_for from flask.views import View from flask_login import current_user, login_required from werkzeug.exceptions import NotFound @@ -479,6 +483,7 @@ class ExportsView(View): 'certificates': self.erasure, 'lots': self.lots_export, 'devices_lots': self.devices_lots_export, + 'snapshot': self.snapshot, } if export_id not in export_ids: @@ -685,6 +690,33 @@ class ExportsView(View): data, "Devices_Incoming_and_Outgoing_Lots_Spreadsheet.csv" ) + def snapshot(self): + uuid = request.args.get('id') + if not uuid: + messages.error('Snapshot not exist!') + return flask.redirect(request.referrer) + + user = g.user.email + name_file = f"*_{user}_{uuid}.json" + tmp_snapshots = app.config['TMP_SNAPSHOTS'] + path_dir_base = os.path.join(tmp_snapshots, user) + + for _file in Path(path_dir_base).glob(name_file): + with open(_file) as file_snapshot: + snapshot = file_snapshot.read() + data = StringIO() + data.write(snapshot) + bfile = data.getvalue().encode('utf-8') + output = make_response(bfile) + output.headers['Content-Disposition'] = 'attachment; filename={}'.format( + name_file + ) + output.headers['Content-type'] = 'text/json' + return output + + messages.error('Snapshot not exist!') + return flask.redirect(request.referrer) + class SnapshotListView(GenericMixin): template_name = 'inventory/snapshots_list.html' diff --git a/ereuse_devicehub/templates/inventory/snapshots_list.html b/ereuse_devicehub/templates/inventory/snapshots_list.html index f8a34b23..77e37ae5 100644 --- a/ereuse_devicehub/templates/inventory/snapshots_list.html +++ b/ereuse_devicehub/templates/inventory/snapshots_list.html @@ -32,6 +32,7 @@ System UUID Status Time + @@ -68,6 +69,13 @@ {{ snap.status }} {{ snap.created.strftime('%H:%M %d-%m-%Y') }} + + {% if snap.snapshot_uuid %} + + + + {% endif %} + {% endfor %} From 5435d21828a45182e585d9bb4d200784036a31e2 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 24 Jun 2022 15:23:24 +0200 Subject: [PATCH 2/2] add test for download snapshot --- CHANGELOG.md | 1 + tests/test_render_2_0.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index bae2b54e..3193c889 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ ml). ## testing - [add] #305 add button download iso Workbench. +- [add] #306 add link for download json snapshot - [changed] #302 add system uuid for check the identity of one device. ## [2.2.0] - 2022-06-24 diff --git a/tests/test_render_2_0.py b/tests/test_render_2_0.py index 94aa9d7f..d300249c 100644 --- a/tests/test_render_2_0.py +++ b/tests/test_render_2_0.py @@ -1385,3 +1385,18 @@ def test_export_lots(user3: UserClientFlask): assert fixture_csv[0] == export_csv[0], 'Headers are not equal' assert fixture_csv[1][1:] == export_csv[1][1:], 'Computer information are not equal' UUID(export_csv[1][0]) + + +@pytest.mark.mvp +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_export_snapshot_json(user3: UserClientFlask): + file_name = 'real-eee-1001pxd.snapshot.12.json' + snap = create_device(user3, file_name) + + snapshot = conftest.yaml2json(file_name.split(".json")[0]) + snapshot = json.dumps(snapshot) + + uri = "/inventory/export/snapshot/?id={}".format(snap.uuid) + body, status = user3.get(uri) + assert status == '200 OK' + assert body == snapshot