add save snapshots for imports and uploads

This commit is contained in:
Cayo Puigdefabregas 2024-10-15 17:56:34 +02:00
parent fae269eb8d
commit 003d224c3e
3 changed files with 48 additions and 4 deletions

View file

@ -57,10 +57,10 @@ class BaseDeviceFormSet(forms.BaseFormSet):
if not commit:
return doc
path_name = save_in_disk(doc, self.user.name)
path_name = save_in_disk(doc, self.user.institution.name, place="placeholder")
create_index(doc, self.user)
create_annotation(doc, user, commit=commit)
move_json(path_name, self.user.name)
move_json(path_name, self.user.institution.name, place="placeholder")
return doc

View file

@ -154,10 +154,11 @@ class ImportForm(forms.Form):
if commit:
for doc, cred in table:
path_name = save_in_disk(doc, self.user.name)
path_name = save_in_disk(doc, self.user.institution.name, place="placeholder")
cred.save()
create_index(doc, self.user)
move_json(path_name, self.user.name)
move_json(path_name, self.user.institution.name, place="placeholder")
return table
return

43
utils/save_snapshots.py Normal file
View file

@ -0,0 +1,43 @@
import os
import json
import shutil
from datetime import datetime
from django.conf import settings
def move_json(path_name, user, place="snapshots"):
if place != "snapshots":
place = "placeholders"
tmp_snapshots = settings.EVIDENCES_DIR
path_dir = os.path.join(tmp_snapshots, user, place)
if os.path.isfile(path_name):
shutil.copy(path_name, path_dir)
os.remove(path_name)
def save_in_disk(data, user, place="snapshots"):
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.EVIDENCES_DIR
if place != "snapshots":
place = "placeholders"
name_file = f"{year}-{month}-{day}-{hour}-{minutes}_{uuid}.json"
path_dir = os.path.join(tmp_snapshots, user, place, "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