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/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 @@