devicehub-django/evidence/models.py

119 lines
3.4 KiB
Python
Raw Normal View History

2024-07-18 15:21:22 +00:00
import json
2024-09-18 16:01:46 +00:00
from dmidecode import DMIParse
2024-06-12 07:32:49 +00:00
from django.db import models
2024-07-18 15:21:22 +00:00
2024-09-18 16:01:46 +00:00
from utils.constants import STR_SM_SIZE, STR_EXTEND_SIZE, CHASSIS_DH
2024-07-26 15:59:34 +00:00
from evidence.xapian import search
from evidence.parse_details import ParseSnapshot
2024-09-18 16:01:46 +00:00
from user.models import Institution
2024-06-12 07:32:49 +00:00
2024-07-31 11:28:46 +00:00
class Annotation(models.Model):
class Type(models.IntegerChoices):
SYSTEM= 0, "System"
USER = 1, "User"
DOCUMENT = 2, "Document"
created = models.DateTimeField(auto_now_add=True)
uuid = models.UUIDField()
2024-09-18 16:01:46 +00:00
owner = models.ForeignKey(Institution, on_delete=models.CASCADE)
2024-07-31 11:28:46 +00:00
type = models.SmallIntegerField(choices=Type)
key = models.CharField(max_length=STR_EXTEND_SIZE)
value = models.CharField(max_length=STR_EXTEND_SIZE)
class Meta:
constraints = [
models.UniqueConstraint(fields=["type", "key", "uuid"], name="unique_type_key_uuid")
]
2024-07-26 15:59:34 +00:00
class Evidence:
2024-07-18 15:21:22 +00:00
def __init__(self, uuid):
self.uuid = uuid
self.owner = None
self.doc = None
self.created = None
2024-09-18 16:01:46 +00:00
self.dmi = None
2024-07-18 15:21:22 +00:00
self.annotations = []
self.components = []
self.default = "n/a"
2024-06-12 07:32:49 +00:00
2024-07-18 15:21:22 +00:00
self.get_owner()
self.get_time()
2024-06-12 07:32:49 +00:00
2024-07-18 15:21:22 +00:00
def get_annotations(self):
self.annotations = Annotation.objects.filter(
uuid=self.uuid
).order_by("created")
2024-07-31 11:28:46 +00:00
2024-07-18 15:21:22 +00:00
def get_owner(self):
if not self.annotations:
self.get_annotations()
a = self.annotations.first()
if a:
self.owner = a.owner
2024-06-12 07:32:49 +00:00
2024-07-18 15:21:22 +00:00
def get_doc(self):
self.doc = {}
qry = 'uuid:"{}"'.format(self.uuid)
matches = search(qry, limit=1)
if matches.size() < 0:
return
for xa in matches:
self.doc = json.loads(xa.document.get_data())
2024-09-18 16:01:46 +00:00
if self.doc.get("software") == "EreuseWorkbench":
dmidecode_raw = self.doc["data"]["dmidecode"]
self.dmi = DMIParse(dmidecode_raw)
2024-07-18 15:21:22 +00:00
def get_time(self):
if not self.doc:
self.get_doc()
self.created = self.doc.get("endTime")
if not self.created:
self.created = self.annotations.last().created
def get_components(self):
if self.doc.get("software") != "EreuseWorkbench":
return self.doc.get('components', [])
self.set_components()
return self.components
2024-07-01 10:17:23 +00:00
2024-09-18 16:01:46 +00:00
def get_manufacturer(self):
if self.doc.get("software") != "EreuseWorkbench":
return self.doc['device']['manufacturer']
2024-09-18 16:01:46 +00:00
return self.dmi.manufacturer().strip()
2024-09-18 16:01:46 +00:00
def get_model(self):
if self.doc.get("software") != "EreuseWorkbench":
return self.doc['device']['model']
2024-09-18 16:01:46 +00:00
return self.dmi.model().strip()
def get_chassis(self):
if self.doc.get("software") != "EreuseWorkbench":
return self.doc['device']['model']
chassis = self.dmi.get("Chassis")[0].get("Type", '_virtual')
2024-09-18 16:01:46 +00:00
lower_type = chassis.lower()
2024-09-18 16:01:46 +00:00
for k, v in CHASSIS_DH.items():
if lower_type in v:
return k
return ""
2024-07-31 11:28:46 +00:00
@classmethod
def get_all(cls, user):
return Annotation.objects.filter(
2024-09-18 16:01:46 +00:00
owner=user.institution,
2024-07-31 11:28:46 +00:00
type=Annotation.Type.SYSTEM,
).order_by("-created").values_list("uuid", flat=True).distinct()
def set_components(self):
snapshot = ParseSnapshot(self.doc).snapshot_json
self.components = snapshot['components']