2024-06-12 07:32:49 +00:00
|
|
|
from django.db import models
|
2024-07-18 15:21:22 +00:00
|
|
|
|
|
|
|
from utils.constants import STR_SM_SIZE, STR_SIZE, STR_EXTEND_SIZE, ALGOS
|
|
|
|
from snapshot.models import Annotation, Snapshot
|
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 = []
|
|
|
|
self.snapshots = []
|
2024-07-19 15:40:01 +00:00
|
|
|
self.last_snapshot = None
|
|
|
|
self.get_last_snapshot()
|
2024-07-18 15:21:22 +00:00
|
|
|
|
|
|
|
def initial(self):
|
|
|
|
self.get_annotations()
|
|
|
|
self.get_uuids()
|
|
|
|
self.get_hids()
|
|
|
|
self.get_snapshots()
|
|
|
|
|
|
|
|
def get_annotations(self):
|
2024-07-19 15:40:01 +00:00
|
|
|
if self.annotations:
|
|
|
|
return self.annotations
|
|
|
|
|
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-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-18 15:21:22 +00:00
|
|
|
if not a.uuid in self.uuids:
|
|
|
|
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)
|
|
|
|
|
|
|
|
def get_snapshots(self):
|
|
|
|
if not self.uuids:
|
|
|
|
self.get_uuids()
|
|
|
|
|
|
|
|
self.snapshots = [Snapshot(u) for u in self.uuids]
|
|
|
|
|
|
|
|
def get_last_snapshot(self):
|
2024-07-19 15:40:01 +00:00
|
|
|
annotations = self.get_annotations()
|
|
|
|
if annotations:
|
|
|
|
annotation = annotations.first()
|
|
|
|
self.last_snapshot = Snapshot(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
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_unassigned(cls, user):
|
2024-07-19 15:40:01 +00:00
|
|
|
chids = DeviceLot.objects.filter(lot__owner=user).values_list("device_id", flat=True).distinct()
|
|
|
|
annotations = Annotation.objects.filter(
|
|
|
|
owner=user,
|
|
|
|
type=Annotation.Type.SYSTEM,
|
|
|
|
).exclude(value__in=chids).values_list("value", flat=True).distinct()
|
|
|
|
return [cls(id=x) for x in annotations]
|
|
|
|
|
|
|
|
# return cls.objects.filter(
|
|
|
|
# owner=user
|
|
|
|
# ).annotate(num_lots=models.Count('lot')).filter(num_lots=0)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def manufacturer(self):
|
|
|
|
if not self.last_snapshot:
|
|
|
|
self.get_last_snapshot()
|
|
|
|
return self.last_snapshot.doc['device']['manufacturer']
|
|
|
|
|
|
|
|
@property
|
|
|
|
def type(self):
|
|
|
|
if not self.last_snapshot:
|
|
|
|
self.get_last_snapshot()
|
|
|
|
return self.last_snapshot.doc['device']['type']
|
|
|
|
|
|
|
|
@property
|
|
|
|
def model(self):
|
|
|
|
if not self.last_snapshot:
|
|
|
|
self.get_last_snapshot()
|
|
|
|
return self.last_snapshot.doc['device']['model']
|
|
|
|
|
|
|
|
@property
|
|
|
|
def type(self):
|
|
|
|
if not self.last_snapshot:
|
|
|
|
self.get_last_snapshot()
|
|
|
|
return self.last_snapshot.doc['device']['type']
|