diff --git a/device/forms.py b/device/forms.py index b74d29b..cf42dbc 100644 --- a/device/forms.py +++ b/device/forms.py @@ -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) diff --git a/device/templates/new_device.html b/device/templates/new_device.html index 870bf79..0d6cf7c 100644 --- a/device/templates/new_device.html +++ b/device/templates/new_device.html @@ -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 @@ +