devicehub-django/device/models.py

192 lines
5.4 KiB
Python
Raw Normal View History

from django.db import models, connection
2024-07-18 15:21:22 +00:00
from utils.constants import STR_SM_SIZE, STR_SIZE, STR_EXTEND_SIZE, ALGOS
2024-07-26 15:59:34 +00:00
from evidence.models import Annotation, Evidence
2024-06-12 07:32:49 +00:00
from user.models import User
2024-07-19 15:40:01 +00:00
from lot.models import DeviceLot
2024-06-12 07:32:49 +00:00
2024-07-19 15:40:01 +00:00
class Device:
2024-07-05 13:32:07 +00:00
class Types(models.TextChoices):
DESKTOP = "Desktop"
LAPTOP = "Laptop"
SERVER = "Server"
GRAPHICCARD = "GraphicCard"
HARDDRIVE = "HardDrive"
SOLIDSTATEDRIVE = "SolidStateDrive"
MOTHERBOARD = "Motherboard"
NETWORKADAPTER = "NetworkAdapter"
PROCESSOR = "Processor"
RAMMODULE = "RamModule"
SOUNDCARD = "SoundCard"
DISPLAY = "Display"
BATTERY = "Battery"
CAMERA = "Camera"
2024-07-18 15:21:22 +00:00
def __init__(self, *args, **kwargs):
2024-07-19 15:40:01 +00:00
# the id is the chid of the device
self.id = kwargs["id"]
self.pk = self.id
self.algorithm = None
self.owner = None
2024-07-18 15:21:22 +00:00
self.annotations = []
self.hids = []
self.uuids = []
2024-07-26 15:59:34 +00:00
self.evidences = []
2024-07-31 11:21:54 +00:00
self.lots = []
2024-07-26 15:59:34 +00:00
self.last_evidence = None
self.get_last_evidence()
2024-07-18 15:21:22 +00:00
def initial(self):
self.get_annotations()
self.get_uuids()
self.get_hids()
2024-07-26 15:59:34 +00:00
self.get_evidences()
2024-07-31 11:21:54 +00:00
self.get_lots()
2024-08-05 13:06:34 +00:00
2024-07-18 15:21:22 +00:00
def get_annotations(self):
2024-07-19 15:40:01 +00:00
if self.annotations:
return self.annotations
2024-08-05 13:06:34 +00:00
2024-07-18 15:21:22 +00:00
self.annotations = Annotation.objects.filter(
2024-07-19 15:40:01 +00:00
type=Annotation.Type.SYSTEM,
value=self.id
2024-07-18 15:21:22 +00:00
).order_by("-created")
2024-07-19 15:40:01 +00:00
if self.annotations.count():
self.algorithm = self.annotations[0].key
self.owner = self.annotations[0].owner
return self.annotations
2024-07-30 11:37:08 +00:00
def get_user_annotations(self):
if not self.uuids:
self.get_uuids()
annotations = Annotation.objects.filter(
uuid__in=self.uuids,
owner=self.owner,
type=Annotation.Type.USER
)
return annotations
2024-08-05 13:06:34 +00:00
2024-07-30 11:37:08 +00:00
def get_user_documents(self):
if not self.uuids:
self.get_uuids()
annotations = Annotation.objects.filter(
uuid__in=self.uuids,
owner=self.owner,
type=Annotation.Type.DOCUMENT
)
return annotations
2024-08-05 13:06:34 +00:00
2024-07-18 15:21:22 +00:00
def get_uuids(self):
2024-07-19 15:40:01 +00:00
for a in self.get_annotations():
2024-07-30 11:37:08 +00:00
if a.uuid not in self.uuids:
2024-07-18 15:21:22 +00:00
self.uuids.append(a.uuid)
def get_hids(self):
2024-07-19 15:40:01 +00:00
annotations = self.get_annotations()
2024-07-18 15:21:22 +00:00
2024-07-19 15:40:01 +00:00
self.hids = annotations.filter(
2024-07-18 15:21:22 +00:00
type=Annotation.Type.SYSTEM,
key__in=ALGOS.keys(),
).values_list("value", flat=True)
2024-07-26 15:59:34 +00:00
def get_evidences(self):
2024-07-18 15:21:22 +00:00
if not self.uuids:
self.get_uuids()
2024-07-26 15:59:34 +00:00
self.evidences = [Evidence(u) for u in self.uuids]
2024-07-18 15:21:22 +00:00
2024-07-26 15:59:34 +00:00
def get_last_evidence(self):
2024-07-19 15:40:01 +00:00
annotations = self.get_annotations()
if annotations:
annotation = annotations.first()
2024-07-26 15:59:34 +00:00
self.last_evidence = Evidence(annotation.uuid)
2024-07-18 15:21:22 +00:00
2024-07-19 15:40:01 +00:00
def last_uuid(self):
return self.uuids[0]
2024-07-18 15:21:22 +00:00
2024-07-31 11:21:54 +00:00
def get_lots(self):
self.lots = [x.lot for x in DeviceLot.objects.filter(device_id=self.id)]
2024-07-18 15:21:22 +00:00
@classmethod
def get_unassigned(cls, user, offset=0, limit=None):
sql = """
SELECT DISTINCT t1.value from evidence_annotation as t1
left join lot_devicelot as t2 on t1.value = t2.device_id
where t2.device_id is null and owner_id=={user} and type=={type}
""".format(
user=user.id,
type=Annotation.Type.SYSTEM,
)
if limit:
sql += " limit {} offset {}".format(int(limit), int(offset))
sql += ";"
annotations = []
with connection.cursor() as cursor:
cursor.execute(sql)
annotations = cursor.fetchall()
devices = [cls(id=x[0]) for x in annotations]
count = cls.get_unassigned_count(user)
return devices, count
@classmethod
def get_unassigned_count(cls, user):
sql = """
SELECT count(DISTINCT t1.value) from evidence_annotation as t1
left join lot_devicelot as t2 on t1.value = t2.device_id
where t2.device_id is null and owner_id=={user} and type=={type};
""".format(
user=user.id,
2024-07-19 15:40:01 +00:00
type=Annotation.Type.SYSTEM,
)
2024-07-19 15:40:01 +00:00
with connection.cursor() as cursor:
cursor.execute(sql)
return cursor.fetchall()[0][0]
2024-07-19 15:40:01 +00:00
2024-08-05 13:06:34 +00:00
@property
def is_websnapshot(self):
if not self.last_evidence:
self.get_last_evidence()
return self.last_evidence.doc['type'] == "WebSnapshot"
@property
def last_user_evidence(self):
if not self.last_evidence:
self.get_last_evidence()
return self.last_evidence.doc['kv'].items()
2024-07-19 15:40:01 +00:00
@property
def manufacturer(self):
2024-07-26 15:59:34 +00:00
if not self.last_evidence:
self.get_last_evidence()
return self.last_evidence.doc['device']['manufacturer']
2024-07-19 15:40:01 +00:00
@property
def type(self):
2024-07-26 15:59:34 +00:00
if not self.last_evidence:
self.get_last_evidence()
return self.last_evidence.doc['device']['type']
2024-07-19 15:40:01 +00:00
@property
def model(self):
2024-07-26 15:59:34 +00:00
if not self.last_evidence:
self.get_last_evidence()
return self.last_evidence.doc['device']['model']
2024-07-19 15:40:01 +00:00
@property
def type(self):
2024-07-26 15:59:34 +00:00
if not self.last_evidence:
self.get_last_evidence()
return self.last_evidence.doc['device']['type']