diff --git a/device/forms.py b/device/forms.py index 38d649e..6c4d6e2 100644 --- a/device/forms.py +++ b/device/forms.py @@ -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 diff --git a/evidence/forms.py b/evidence/forms.py index a47024e..cf90a71 100644 --- a/evidence/forms.py +++ b/evidence/forms.py @@ -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 diff --git a/utils/save_snapshots.py b/utils/save_snapshots.py new file mode 100644 index 0000000..8c02f06 --- /dev/null +++ b/utils/save_snapshots.py @@ -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