fix parsing

This commit is contained in:
Cayo Puigdefabregas 2024-12-03 16:37:56 +01:00
parent bed40d3ee0
commit 7fd42db3e4
3 changed files with 28 additions and 10 deletions

View File

@ -85,17 +85,21 @@ class NewSnapshotView(ApiMixing):
# except Exception:
# return JsonResponse({'error': 'Invalid Snapshot'}, status=400)
if not data.get("uuid"):
ev_uuid = data.get("uuid")
if data.get("credentialSubject"):
ev_uuid = data["credentialSubject"].get("uuid")
if not ev_uuid:
txt = "error: the snapshot not have uuid"
logger.error("%s", txt)
return JsonResponse({'status': txt}, status=500)
exist_annotation = Annotation.objects.filter(
uuid=data['uuid']
uuid=ev_uuid
).first()
if exist_annotation:
txt = "error: the snapshot {} exist".format(data['uuid'])
txt = "error: the snapshot {} exist".format(ev_uuid)
logger.warning("%s", txt)
return JsonResponse({'status': txt}, status=500)
@ -105,14 +109,14 @@ class NewSnapshotView(ApiMixing):
except Exception as err:
if settings.DEBUG:
logger.exception("%s", err)
snapshot_id = data.get("uuid", "")
snapshot_id = ev_uuid
txt = "It is not possible to parse snapshot: %s."
logger.error(txt, snapshot_id)
text = "fail: It is not possible to parse snapshot"
return JsonResponse({'status': text}, status=500)
annotation = Annotation.objects.filter(
uuid=data['uuid'],
uuid=ev_uuid,
type=Annotation.Type.SYSTEM,
# TODO this is hardcoded, it should select the user preferred algorithm
key="hidalgo1",
@ -121,7 +125,7 @@ class NewSnapshotView(ApiMixing):
if not annotation:
logger.error("Error: No annotation for uuid: %s", data["uuid"])
logger.error("Error: No annotation for uuid: %s", ev_uuid)
return JsonResponse({'status': 'fail'}, status=500)
url_args = reverse_lazy("device:details", args=(annotation.value,))

View File

@ -23,7 +23,18 @@ def get_mac(inxi):
class Build:
def __init__(self, evidence_json, user, check=False):
self.json = evidence_json
self.evidence = evidence_json.copy()
self.json = evidence_json.copy()
if evidence_json.get("credentialSubject"):
self.json.update(evidence_json["credentialSubject"])
if evidence_json.get("evidence"):
self.json["data"] = {}
for ev in evidence_json["evidence"]:
k = ev.get("operation")
if not k:
continue
self.json["data"][k] = ev.get("output")
self.uuid = self.json['uuid']
self.user = user
self.hid = None
@ -36,7 +47,7 @@ class Build:
self.create_annotations()
def index(self):
snap = json.dumps(self.json)
snap = json.dumps(self.evidence)
index(self.user.institution, self.uuid, snap)
def generate_chids(self):
@ -87,7 +98,7 @@ class Build:
except Exception:
logger.error("No inxi in snapshot %s", self.uuid)
return ""
machine = get_inxi_key(self.inxi, 'Machine')
for m in machine:
system = get_inxi(m, "System")

View File

@ -19,7 +19,10 @@ def move_json(path_name, user, place="snapshots"):
def save_in_disk(data, user, place="snapshots"):
uuid = data.get('uuid', '')
uuid = data.get("uuid")
if data.get("credentialSubject"):
uuid = data["credentialSubject"].get("uuid")
now = datetime.now()
year = now.year
month = now.month