From e0258cbf02e8ca408825dd23bb2baa0f6f332342 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 9 Oct 2024 13:05:18 +0200 Subject: [PATCH] add search for hids --- dashboard/views.py | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/dashboard/views.py b/dashboard/views.py index c188cc4..b34e2a6 100644 --- a/dashboard/views.py +++ b/dashboard/views.py @@ -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()