save snapshots on disk from api

This commit is contained in:
Cayo Puigdefabregas 2024-10-15 11:05:43 +02:00
parent d04216ad79
commit 69a54b92fb
2 changed files with 40 additions and 3 deletions

View File

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

View File

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