add search for hids

This commit is contained in:
Cayo Puigdefabregas 2024-10-09 13:05:18 +02:00
parent 1abd846922
commit e0258cbf02
1 changed files with 25 additions and 5 deletions

View File

@ -3,6 +3,7 @@ import json
from django.utils.translation import gettext_lazy as _
from django.views.generic.edit import FormView
from django.shortcuts import Http404
from django.db.models import Q
from dashboard.mixins import InventaryMixin, DetailsMixin
from evidence.models import Annotation
@ -40,7 +41,7 @@ class LotDashboardView(InventaryMixin, DetailsMixin):
chids = self.object.devicelot_set.all().values_list(
"device_id", flat=True
).distinct()
chids_page = chids[offset:offset+limit]
return [Device(id=x) for x in chids_page], chids.count()
@ -50,21 +51,24 @@ class SearchView(InventaryMixin):
section = "Search"
title = _("Search Devices")
breadcrumb = "Devices / Search Devices"
def get_devices(self, user, offset, limit):
post = dict(self.request.POST)
query = post.get("search")
if not query:
return [], 0
matches = search(
self.request.user.institution,
query[0],
offset,
limit
)
if not matches.size():
return self.search_hids(query, offset, limit)
annotations = []
for x in matches:
annotations.extend(self.get_annotations(x))
@ -76,10 +80,26 @@ class SearchView(InventaryMixin):
def get_annotations(self, xp):
snap = xp.document.get_data()
uuid = json.loads(snap).get('uuid')
return Annotation.objects.filter(
type=Annotation.Type.SYSTEM,
owner=self.request.user.institution,
uuid=uuid
).values_list("value", flat=True).distinct()
def search_hids(self, query, offset, limit):
qry = Q()
for i in query[0].split(" "):
if i:
qry |= Q(value__startswith=i)
chids = Annotation.objects.filter(
type=Annotation.Type.SYSTEM,
owner=self.request.user.institution
).filter(
qry
).values_list("value", flat=True).distinct()
chids_page = chids[offset:offset+limit]
return [Device(id=x) for x in chids_page], chids.count()