From 9a24eb5fd53f5b7fa5f9f0ad6be2c9ac1937aaf5 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 7 Oct 2020 18:28:57 +0200 Subject: [PATCH] save snapshot in a temporal file --- ereuse_devicehub/resources/action/views.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/ereuse_devicehub/resources/action/views.py b/ereuse_devicehub/resources/action/views.py index ba727dc3..5c7e5a53 100644 --- a/ereuse_devicehub/resources/action/views.py +++ b/ereuse_devicehub/resources/action/views.py @@ -1,3 +1,5 @@ +import os +from time import time from distutils.version import StrictVersion from typing import List from uuid import UUID @@ -17,6 +19,23 @@ from ereuse_devicehub.resources.user.exceptions import InsufficientPermission SUPPORTED_WORKBENCH = StrictVersion('11.0') +TMP_SNAPSHOTS = 'tmp/snapshots' + + +def save_snapshot_on_file(snapshot_json): + """ + This function allow save a snapshot in json format un a TMP_SNAPSHOTS directory + The file need to be saved with one name format with the stamptime and uuid joins + """ + if not os.path.isdir(TMP_SNAPSHOTS): + os.system('mkdir -p {}'.format(TMP_SNAPSHOTS)) + + name_file = "{uuid}_{time}.json".format(uuid=snapshot_json['uuid'], time=int(time())) + path_name = "{tmp}/{file}".format(tmp=TMP_SNAPSHOTS, file=name_file) + snapshot_file = open(path_name, 'w') + snapshot_file.write(json.dumps(snapshot_json)) + snapshot_file.close() + class ActionView(View): def post(self):