From 69a54b92fb5318aab85f9e4429d3b9ad832cdb84 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 15 Oct 2024 11:05:43 +0200 Subject: [PATCH] save snapshots on disk from api --- api/views.py | 42 +++++++++++++++++++++++++++++++++++++++--- dhub/settings.py | 1 + 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/api/views.py b/api/views.py index fe68079..0ae7e04 100644 --- a/api/views.py +++ b/api/views.py @@ -1,4 +1,8 @@ +import os import json +import shutil + +from datetime import datetime from django.urls import reverse_lazy from django.shortcuts import get_object_or_404, redirect @@ -23,8 +27,37 @@ from api.models import Token from api.tables import TokensTable +def move_json(path_name, user): + tmp_snapshots = settings.SNAPSHOTS_DIR + path_dir = os.path.join(tmp_snapshots, user) + + if os.path.isfile(path_name): + shutil.copy(path_name, path_dir) + os.remove(path_name) + + def save_in_disk(data, user): - pass + uuid = data.get('uuid', '') + now = datetime.now() + year = now.year + month = now.month + day = now.day + hour = now.hour + minutes = now.minute + tmp_snapshots = settings.SNAPSHOTS_DIR + + name_file = f"{year}-{month}-{day}-{hour}-{minutes}_{user}_{uuid}.json" + path_dir = os.path.join(tmp_snapshots, user, "errors") + path_name = os.path.join(path_dir, name_file) + + if not os.path.isdir(path_dir): + os.system(f'mkdir -p {path_dir}') + + with open(path_name, 'w') as snapshot_file: + snapshot_file.write(json.dumps(data)) + + return path_name + @csrf_exempt @@ -59,10 +92,11 @@ def NewSnapshot(request): ).first() if exist_annotation: - raise ValidationError("error: the snapshot {} exist".format(data['uuid'])) + txt = "error: the snapshot {} exist".format(data['uuid']) + return JsonResponse({'status': txt}, status=500) # Process snapshot - # save_in_disk(data, tk.user) + path_name = save_in_disk(data, tk.owner.institution.name) try: Build(data, tk.owner) @@ -91,6 +125,8 @@ def NewSnapshot(request): # TODO replace with public_url when available "public_url": url } + move_json(path_name, tk.owner.institution.name) + return JsonResponse(response, status=200) diff --git a/dhub/settings.py b/dhub/settings.py index 3b48d2c..3a55985 100644 --- a/dhub/settings.py +++ b/dhub/settings.py @@ -62,6 +62,7 @@ EMAIL_FILE_PATH = config('EMAIL_FILE_PATH', default='/tmp/app-messages') ENABLE_EMAIL = config("ENABLE_EMAIL", default=True, cast=bool) +SNAPSHOTS_DIR = config("SNAPSHOTS_DIR", default="/tmp/") # Application definition