From 9829ebe7c72c26e81568695c1d34da7eb197b56b Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 24 Jun 2022 15:05:38 +0200 Subject: [PATCH] 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 %}