From 1abd846922d7cda584d8a870d1baad8cb22d5bb2 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 9 Oct 2024 12:18:09 +0200 Subject: [PATCH] search in xapian --- dashboard/mixins.py | 15 ++++++----- dashboard/templates/base.html | 11 ++++++++ dashboard/urls.py | 1 + dashboard/views.py | 50 ++++++++++++++++++++++++++++++++++- evidence/xapian.py | 1 - 5 files changed, 70 insertions(+), 8 deletions(-) diff --git a/dashboard/mixins.py b/dashboard/mixins.py index 8b70934..40f27bf 100644 --- a/dashboard/mixins.py +++ b/dashboard/mixins.py @@ -28,7 +28,7 @@ class DashboardView(LoginRequiredMixin): title = "" subtitle = "" section = "" - + def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context.update({ @@ -72,14 +72,17 @@ class DetailsMixin(DashboardView, TemplateView): class InventaryMixin(DashboardView, TemplateView): def post(self, request, *args, **kwargs): - dev_ids = dict(self.request.POST).get("devices", []) - self.request.session["devices"] = dev_ids - url = self.request.POST.get("url") + post = dict(self.request.POST) + url = post.get("url") + if url: + dev_ids = post.get("devices", []) + self.request.session["devices"] = dev_ids + try: - resource = resolve(url) + resource = resolve(url[0]) if resource and dev_ids: - return redirect(url) + return redirect(url[0]) except Exception: pass return super().get(request, *args, **kwargs) diff --git a/dashboard/templates/base.html b/dashboard/templates/base.html index c8b0a38..0a38562 100644 --- a/dashboard/templates/base.html +++ b/dashboard/templates/base.html @@ -179,6 +179,17 @@ {% endblock messages %}

{{ title }}

+ +
+ {% csrf_token %} +
+ + + + +
+
+
diff --git a/dashboard/urls.py b/dashboard/urls.py index 681b73e..835e708 100644 --- a/dashboard/urls.py +++ b/dashboard/urls.py @@ -6,4 +6,5 @@ app_name = 'dashboard' urlpatterns = [ path("", views.UnassignedDevicesView.as_view(), name="unassigned_devices"), path("/", views.LotDashboardView.as_view(), name="lot"), + path("search", views.SearchView.as_view(), name="search"), ] diff --git a/dashboard/views.py b/dashboard/views.py index 7a9a397..c188cc4 100644 --- a/dashboard/views.py +++ b/dashboard/views.py @@ -1,7 +1,12 @@ +import json + from django.utils.translation import gettext_lazy as _ +from django.views.generic.edit import FormView from django.shortcuts import Http404 from dashboard.mixins import InventaryMixin, DetailsMixin +from evidence.models import Annotation +from evidence.xapian import search from device.models import Device from lot.models import Lot @@ -32,6 +37,49 @@ class LotDashboardView(InventaryMixin, DetailsMixin): return context def get_devices(self, user, offset, limit): - chids = self.object.devicelot_set.all().values_list("device_id", flat=True).distinct() + 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() + + +class SearchView(InventaryMixin): + template_name = "unassigned_devices.html" + 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 + ) + + annotations = [] + for x in matches: + annotations.extend(self.get_annotations(x)) + + devices = [Device(id=x) for x in set(annotations)] + count = matches.size() + return devices, count + + 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() + diff --git a/evidence/xapian.py b/evidence/xapian.py index 27c03c5..3c0361c 100644 --- a/evidence/xapian.py +++ b/evidence/xapian.py @@ -18,7 +18,6 @@ def search(institution, qs, offset=0, limit=10): qp.set_stemmer(xapian.Stem("english")) qp.set_stemming_strategy(xapian.QueryParser.STEM_SOME) qp.add_prefix("uuid", "uuid") - # qp.add_prefix("snapshot", "snapshot") query = qp.parse_query(qs) institution_term = "U{}".format(institution.id) final_query = xapian.Query(