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