response details of device with annotations from api
This commit is contained in:
parent
9b96955c30
commit
a2b5415149
|
@ -7,6 +7,7 @@ app_name = 'api'
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('v1/snapshot/', views.NewSnapshotView.as_view(), name='new_snapshot'),
|
path('v1/snapshot/', views.NewSnapshotView.as_view(), name='new_snapshot'),
|
||||||
|
path('v1/device/<str:pk>/', views.DetailsDeviceView.as_view(), name='device'),
|
||||||
path('v1/tokens/', views.TokenView.as_view(), name='tokens'),
|
path('v1/tokens/', views.TokenView.as_view(), name='tokens'),
|
||||||
path('v1/tokens/new', views.TokenNewView.as_view(), name='new_token'),
|
path('v1/tokens/new', views.TokenNewView.as_view(), name='new_token'),
|
||||||
path("v1/tokens/<int:pk>/edit", views.EditTokenView.as_view(), name="edit_token"),
|
path("v1/tokens/<int:pk>/edit", views.EditTokenView.as_view(), name="edit_token"),
|
||||||
|
|
52
api/views.py
52
api/views.py
|
@ -205,19 +205,57 @@ class EditTokenView(DashboardView, UpdateView):
|
||||||
return kwargs
|
return kwargs
|
||||||
|
|
||||||
|
|
||||||
class DetailsComputerView(ApiMixing):
|
class DetailsDeviceView(ApiMixing):
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
response = self.auth()
|
response = self.auth()
|
||||||
if response:
|
if response:
|
||||||
return response
|
return response
|
||||||
|
|
||||||
try:
|
self.pk = kwargs['pk']
|
||||||
data = json.loads(request.body)
|
self.object = Device(id=self.pk)
|
||||||
except:
|
|
||||||
pass
|
if not self.object.last_evidence:
|
||||||
|
return JsonResponse({}, status=404)
|
||||||
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):
|
def post(self, request, *args, **kwargs):
|
||||||
return JsonResponse({}, status=404)
|
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
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
from django.db import models, connection
|
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 evidence.models import Annotation, Evidence
|
||||||
from user.models import User
|
|
||||||
from lot.models import DeviceLot
|
from lot.models import DeviceLot
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ import json
|
||||||
from dmidecode import DMIParse
|
from dmidecode import DMIParse
|
||||||
from django.db import models
|
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.xapian import search
|
||||||
from evidence.parse_details import ParseSnapshot
|
from evidence.parse_details import ParseSnapshot
|
||||||
from user.models import User, Institution
|
from user.models import User, Institution
|
||||||
|
@ -67,7 +67,7 @@ class Evidence:
|
||||||
for xa in matches:
|
for xa in matches:
|
||||||
self.doc = json.loads(xa.document.get_data())
|
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"]
|
dmidecode_raw = self.doc["data"]["dmidecode"]
|
||||||
self.dmi = DMIParse(dmidecode_raw)
|
self.dmi = DMIParse(dmidecode_raw)
|
||||||
|
|
||||||
|
@ -80,7 +80,7 @@ class Evidence:
|
||||||
self.created = self.annotations.last().created
|
self.created = self.annotations.last().created
|
||||||
|
|
||||||
def get_components(self):
|
def get_components(self):
|
||||||
if self.doc.get("software") != "workbench-script":
|
if self.is_legacy():
|
||||||
return self.doc.get('components', [])
|
return self.doc.get('components', [])
|
||||||
self.set_components()
|
self.set_components()
|
||||||
return self.components
|
return self.components
|
||||||
|
@ -92,7 +92,7 @@ class Evidence:
|
||||||
return ""
|
return ""
|
||||||
return list(self.doc.get('kv').values())[0]
|
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.doc['device']['manufacturer']
|
||||||
|
|
||||||
return self.dmi.manufacturer().strip()
|
return self.dmi.manufacturer().strip()
|
||||||
|
@ -104,13 +104,13 @@ class Evidence:
|
||||||
return ""
|
return ""
|
||||||
return list(self.doc.get('kv').values())[1]
|
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.doc['device']['model']
|
||||||
|
|
||||||
return self.dmi.model().strip()
|
return self.dmi.model().strip()
|
||||||
|
|
||||||
def get_chassis(self):
|
def get_chassis(self):
|
||||||
if self.doc.get("software") != "workbench-script":
|
if self.is_legacy():
|
||||||
return self.doc['device']['model']
|
return self.doc['device']['model']
|
||||||
|
|
||||||
chassis = self.dmi.get("Chassis")[0].get("Type", '_virtual')
|
chassis = self.dmi.get("Chassis")[0].get("Type", '_virtual')
|
||||||
|
@ -132,3 +132,6 @@ class Evidence:
|
||||||
def set_components(self):
|
def set_components(self):
|
||||||
snapshot = ParseSnapshot(self.doc).snapshot_json
|
snapshot = ParseSnapshot(self.doc).snapshot_json
|
||||||
self.components = snapshot['components']
|
self.components = snapshot['components']
|
||||||
|
|
||||||
|
def is_legacy(self):
|
||||||
|
return self.doc.get("software") != "workbench-script"
|
||||||
|
|
Loading…
Reference in New Issue