diff --git a/evidence/management/commands/up_snapshots.py b/evidence/management/commands/up_snapshots.py index bea6ae1..f2dbb22 100644 --- a/evidence/management/commands/up_snapshots.py +++ b/evidence/management/commands/up_snapshots.py @@ -1,12 +1,18 @@ - import os import json +import logging from django.core.management.base import BaseCommand from django.contrib.auth import get_user_model +from django.conf import settings + +from utils.save_snapshots import move_json, save_in_disk from evidence.parse import Build +logger = logging.getLogger('django') + + User = get_user_model() @@ -31,9 +37,11 @@ class Command(BaseCommand): elif os.path.isdir(path): self.read_directory(path) + else: + raise ValueError(f"The path {path} is neither a file nor a directory") self.parsing() - + def read_directory(self, directory): for filename in os.listdir(directory): filepath = os.path.join(directory, filename) @@ -53,6 +61,15 @@ class Command(BaseCommand): except Exception: raise Exception(f"Oops! Something went wrong there") + def parsing(self): - for s in self.snapshots: - self.devices.append(Build(s, self.user)) + for s, p in self.snapshots: + try: + self.devices.append(Build(s, self.user)) + move_json(p, self.user.institution.name) + except Exception as err: + if settings.DEBUG: + logger.exception("%s", err) + snapshot_id = s.get("uuid", "") + txt = "It is not possible to parse snapshot: %s" + logger.error(txt, snapshot_id)