devicehub-django/snapshot/parse.py

70 lines
1.9 KiB
Python
Raw Normal View History

2024-07-11 15:40:45 +00:00
import os
import json
import shutil
2024-07-15 14:23:14 +00:00
import xapian
2024-07-11 15:40:45 +00:00
import hashlib
2024-07-01 10:17:23 +00:00
2024-07-11 15:40:45 +00:00
from datetime import datetime
2024-07-18 15:21:22 +00:00
from snapshot.xapian import search, index
2024-07-15 14:23:14 +00:00
from snapshot.models import Snapshot, Annotation
2024-07-18 15:21:22 +00:00
from device.models import Device
from utils.constants import ALGOS
2024-07-11 15:40:45 +00:00
class Build:
def __init__(self, snapshot_json, user):
2024-07-01 10:17:23 +00:00
self.json = snapshot_json
2024-07-18 15:21:22 +00:00
self.uuid = self.json['uuid']
2024-07-11 15:40:45 +00:00
self.user = user
self.hid = None
2024-07-15 14:23:14 +00:00
self.index()
2024-07-18 15:21:22 +00:00
self.create_annotations()
2024-07-11 15:40:45 +00:00
2024-07-15 14:23:14 +00:00
def index(self):
snap = json.dumps(self.json)
2024-07-18 15:21:22 +00:00
index(self.uuid, snap)
2024-07-15 14:23:14 +00:00
def get_hid_14(self):
device = self.json['device']
manufacturer = device.get("manufacturer", '')
model = device.get("model", '')
chassis = device.get("chassis", '')
serial_number = device.get("serialNumber", '')
sku = device.get("sku", '')
hid = f"{manufacturer}{model}{chassis}{serial_number}{sku}"
return hashlib.sha3_256(hid.encode()).hexdigest()
2024-07-18 15:21:22 +00:00
def create_annotations(self):
algorithms = {
'hidalgo1': self.get_hid_14(),
}
annotation = Annotation.objects.filter(
owner=self.user,
type=Annotation.Type.SYSTEM,
key='hidalgo1',
value = algorithms['hidalgo1']
).first()
if annotation:
device = annotation.device
else:
device = Device.objects.create(
type=self.json["device"]["type"],
manufacturer=self.json["device"]["manufacturer"],
model=self.json["device"]["model"],
owner=self.user
)
for k, v in algorithms.items():
Annotation.objects.create(
uuid=self.uuid,
owner=self.user,
device=device,
type=Annotation.Type.SYSTEM,
key=k,
value=v
)
2024-07-11 15:40:45 +00:00