xapian #1

Merged
cayop merged 26 commits from xapian into master 2024-09-17 10:11:28 +00:00
4 changed files with 116 additions and 15 deletions
Showing only changes of commit e2f9855954 - Show all commits

View File

@ -1,17 +1,98 @@
import json
import uuid
import hashlib
import datetime
from django import forms from django import forms
from snapshot.models import Annotation from snapshot.models import Annotation
from snapshot.xapian import search, index
DEVICE_TYPES = [
class DeviceForm2(forms.ModelForm): ("Desktop", "Desktop"),
class Meta: ("Laptop", "Laptop"),
model = Annotation ("Server", "Server"),
fields = ['key', 'value'] ("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): class DeviceForm(forms.Form):
name = forms.CharField() type = forms.ChoiceField(choices = DEVICE_TYPES, required=False)
value = forms.CharField() 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) { function addForm(button) {
var formCount = parseInt(document.getElementById('id_form-TOTAL_FORMS').value); var formCount = parseInt(document.getElementById('id_form-TOTAL_FORMS').value);
var formCopy = $(document.querySelector('#id_form-0-name')).parent().parent().parent()[0].cloneNode(true); var formCopy = $(document.querySelector('#id_form-0-name')).parent().parent().parent()[0].cloneNode(true);
// formCopy.style.display = 'block'; formCopy.querySelectorAll('input').forEach(function(input) {
formCopy.innerHTML = formCopy.innerHTML.replace(/__prefix__/g, formCount); 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('formset-container').appendChild(formCopy);
document.getElementById('id_form-TOTAL_FORMS').value = formCount + 1; document.getElementById('id_form-TOTAL_FORMS').value = formCount + 1;
} }
@ -44,6 +49,21 @@
</a> </a>
</div> </div>
</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 %} {% for f in form %}
<div class="row mb-2"> <div class="row mb-2">
<div class="col"> <div class="col">

View File

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

View File

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