diff --git a/api/urls.py b/api/urls.py index c3cb111..296a768 100644 --- a/api/urls.py +++ b/api/urls.py @@ -7,6 +7,7 @@ app_name = 'api' urlpatterns = [ path('v1/snapshot/', views.NewSnapshotView.as_view(), name='new_snapshot'), + path('v1/device//', views.DetailsDeviceView.as_view(), name='device'), path('v1/tokens/', views.TokenView.as_view(), name='tokens'), path('v1/tokens/new', views.TokenNewView.as_view(), name='new_token'), path("v1/tokens//edit", views.EditTokenView.as_view(), name="edit_token"), diff --git a/api/views.py b/api/views.py index 6bf9cfb..795d221 100644 --- a/api/views.py +++ b/api/views.py @@ -205,19 +205,57 @@ class EditTokenView(DashboardView, UpdateView): return kwargs -class DetailsComputerView(ApiMixing): +class DetailsDeviceView(ApiMixing): def get(self, request, *args, **kwargs): response = self.auth() if response: return response - try: - data = json.loads(request.body) - except: - pass - - return JsonResponse({}, status=404) + self.pk = kwargs['pk'] + self.object = Device(id=self.pk) + + if not self.object.last_evidence: + return JsonResponse({}, status=404) + + if self.object.owner != self.tk.owner.institution: + return JsonResponse({}, status=403) + + data = self.get_data() + return JsonResponse(data, status=200) def post(self, request, *args, **kwargs): return JsonResponse({}, status=404) + + def get_data(self): + data = {} + self.object.initial() + self.object.get_last_evidence() + evidence = self.object.last_evidence + + if evidence.is_legacy(): + data.update({ + "device": evidence.get("device"), + "components": evidence.get("components"), + }) + else: + evidence.get_doc() + snapshot = ParseSnapshot(evidence.doc).snapshot_json + data.update({ + "device": snapshot.get("device"), + "components": snapshot.get("components"), + }) + + uuids = Annotation.objects.filter( + owner=self.tk.owner.institution, + value=self.pk + ).values("uuid") + + annotations = Annotation.objects.filter( + uuid__in=uuids, + owner=self.tk.owner.institution, + type = Annotation.Type.USER + ).values_list("key", "value") + + data.update({"annotations": list(annotations)}) + return data diff --git a/device/models.py b/device/models.py index 1f43476..da53de8 100644 --- a/device/models.py +++ b/device/models.py @@ -1,8 +1,7 @@ from django.db import models, connection -from utils.constants import STR_SM_SIZE, STR_SIZE, STR_EXTEND_SIZE, ALGOS +from utils.constants import ALGOS from evidence.models import Annotation, Evidence -from user.models import User from lot.models import DeviceLot diff --git a/evidence/models.py b/evidence/models.py index 35f97aa..1f31406 100644 --- a/evidence/models.py +++ b/evidence/models.py @@ -3,7 +3,7 @@ import json from dmidecode import DMIParse from django.db import models -from utils.constants import STR_SM_SIZE, STR_EXTEND_SIZE, CHASSIS_DH +from utils.constants import STR_EXTEND_SIZE, CHASSIS_DH from evidence.xapian import search from evidence.parse_details import ParseSnapshot from user.models import User, Institution @@ -67,7 +67,7 @@ class Evidence: for xa in matches: self.doc = json.loads(xa.document.get_data()) - if self.doc.get("software") == "workbench-script": + if not self.is_legacy(): dmidecode_raw = self.doc["data"]["dmidecode"] self.dmi = DMIParse(dmidecode_raw) @@ -80,7 +80,7 @@ class Evidence: self.created = self.annotations.last().created def get_components(self): - if self.doc.get("software") != "workbench-script": + if self.is_legacy(): return self.doc.get('components', []) self.set_components() return self.components @@ -92,7 +92,7 @@ class Evidence: return "" return list(self.doc.get('kv').values())[0] - if self.doc.get("software") != "workbench-script": + if self.is_legacy(): return self.doc['device']['manufacturer'] return self.dmi.manufacturer().strip() @@ -104,13 +104,13 @@ class Evidence: return "" return list(self.doc.get('kv').values())[1] - if self.doc.get("software") != "workbench-script": + if self.is_legacy(): return self.doc['device']['model'] return self.dmi.model().strip() def get_chassis(self): - if self.doc.get("software") != "workbench-script": + if self.is_legacy(): return self.doc['device']['model'] chassis = self.dmi.get("Chassis")[0].get("Type", '_virtual') @@ -132,3 +132,6 @@ class Evidence: def set_components(self): snapshot = ParseSnapshot(self.doc).snapshot_json self.components = snapshot['components'] + + def is_legacy(self): + return self.doc.get("software") != "workbench-script"