save the device from web to index

This commit is contained in:
Cayo Puigdefabregas 2024-07-23 15:37:40 +02:00
parent 45ebb6f169
commit 70ded10852
4 changed files with 116 additions and 15 deletions

View File

@ -1,17 +1,98 @@
import json
import uuid
import hashlib
import datetime
from django import forms
from snapshot.models import Annotation
from snapshot.xapian import search, index
class DeviceForm2(forms.ModelForm):
class Meta:
model = Annotation
fields = ['key', 'value']
DEVICE_TYPES = [
("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"),
]
class DeviceForm(forms.Form):
name = forms.CharField()
value = forms.CharField()
type = forms.ChoiceField(choices = DEVICE_TYPES, required=False)
amount = forms.IntegerField(required=True, initial=1)
tag = forms.CharField(required=False)
name = forms.CharField(required=False)
value = forms.CharField(required=False)
DeviceFormSet = forms.formset_factory(form=DeviceForm, extra=1)
class BaseDeviceFormSet(forms.BaseFormSet):
def save(self, user, commit=True):
self.user = user
doc = {}
device = {}
kv = {}
self.uuid = str(uuid.uuid4())
tag = hashlib.sha3_256(self.uuid.encode()).hexdigest()
for f in self.forms:
d = f.cleaned_data
if not d:
continue
if d.get("type"):
device["type"] = d["type"]
if d.get("amount"):
device["amount"] = d["amount"]
if d.get("name"):
kv[d["name"]] = d.get("value", '')
if d.get("tag"):
tag = d["tag"]
if not device:
return
device["manufacturer"] = ""
device["model"] = tag
doc["device"] = device
if kv:
doc["kv"] = kv
date = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
if doc:
doc["uuid"] = self.uuid
doc["endTime"] = date
doc["software"] = "DeviceHub"
doc["type"] = "WebSnapshot"
if not commit:
return doc
self.index(doc)
self.create_annotations(tag)
return doc
def index(self, doc):
snap = json.dumps(doc)
index(self.uuid, snap)
def create_annotations(self, tag):
Annotation.objects.create(
uuid=self.uuid,
owner=self.user,
type=Annotation.Type.SYSTEM,
key='Web',
value=tag
)
DeviceFormSet = forms.formset_factory(form=DeviceForm, formset=BaseDeviceFormSet, extra=1)

View File

@ -12,8 +12,13 @@
function addForm(button) {
var formCount = parseInt(document.getElementById('id_form-TOTAL_FORMS').value);
var formCopy = $(document.querySelector('#id_form-0-name')).parent().parent().parent()[0].cloneNode(true);
// formCopy.style.display = 'block';
formCopy.innerHTML = formCopy.innerHTML.replace(/__prefix__/g, formCount);
formCopy.querySelectorAll('input').forEach(function(input) {
var name = input.name.replace(/form-\d+/g, 'form-' + formCount);
var id = 'id_' + name;
input.name = name;
input.id = id;
input.value = '';
});
document.getElementById('formset-container').appendChild(formCopy);
document.getElementById('id_form-TOTAL_FORMS').value = formCount + 1;
}
@ -44,6 +49,21 @@
</a>
</div>
</div>
<div class="row mb-2">
<div class="col">
{% bootstrap_field form.0.type %}
</div>
</div>
<div class="row mb-2">
<div class="col">
{% bootstrap_field form.0.amount %}
</div>
</div>
<div class="row mb-2">
<div class="col">
{% bootstrap_field form.0.tag %}
</div>
</div>
{% for f in form %}
<div class="row mb-2">
<div class="col">

View File

@ -22,17 +22,14 @@ class NewDeviceView(DashboardView, FormView):
title = _("New Device")
breadcrumb = "Device / New Device"
success_url = reverse_lazy('device:add')
# success_url = reverse_lazy('dashboard:unassigned_devices')
form_class = DeviceFormSet
def form_valid(self, form):
# import pdb; pdb.set_trace()
# form.instance.owner = self.request.user
form.save(self.request.user)
response = super().form_valid(form)
return response
def form_invalid(self, form):
import pdb; pdb.set_trace()
response = super().form_invalid(form)
return response

View File

@ -1,7 +1,6 @@
import os
import json
import shutil
import xapian
import hashlib
from datetime import datetime
@ -39,6 +38,7 @@ class Build:
'hidalgo1': self.get_hid_14(),
}
# TODO is neccesary?
annotation = Annotation.objects.filter(
owner=self.user,
type=Annotation.Type.SYSTEM,
@ -47,6 +47,9 @@ class Build:
).first()
for k, v in algorithms.items():
if annotation and k == annotation.key:
continue
Annotation.objects.create(
uuid=self.uuid,
owner=self.user,