Skip to contentSkip to conversation

views: ensure we only show results once in recipe search

PATCH New Paul Eggleton 1 participant 2 messages Oct 19, 2016

lore
Share & tools
lore
#1 lore raw

PATCH 2/2 views: ensure exact matches on name are shown first in recipe search

In reply to Paul Eggleton

Improves slightly on 3155206e54413f72df3b3b41280eafd332a58ba4 by doing an exact match on name and showing that first - now when you search for "git" you really do get the git recipe first in the list.

---
 layerindex/views.py | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
layerindex/views.py +7 -2
index 3e78c58..7045a12 100644
@@ -378,6 +378,9 @@ class RecipeSearchView(ListView):
         if query_string.strip():
             order_by = ('pn', 'layerbranch__layer')
 
+            qs0 = init_qs.filter(pn=query_string).order_by(*order_by)
+            qs0 = recipes_preferred_count(qs0)
+
             entry_query = simplesearch.get_query(query_string, ['pn'])
             qs1 = init_qs.filter(entry_query).order_by(*order_by)
             qs1 = recipes_preferred_count(qs1)
@@ -386,7 +389,7 @@ class RecipeSearchView(ListView):
             qs2 = init_qs.filter(entry_query).order_by(*order_by)
             qs2 = recipes_preferred_count(qs2)
 
-            qs = list(utils.chain_unique(qs1, qs2))
+            qs = list(utils.chain_unique(qs0, qs1, qs2))
         else:
             if 'q' in self.request.GET:
                 qs = init_qs.order_by('pn', 'layerbranch__layer')
@@ -732,13 +735,15 @@ class ClassicRecipeSearchView(RecipeSearchView):
         if query_string.strip():
             order_by = ('pn', 'layerbranch__layer')
 
+            qs0 = init_qs.filter(pn==query_string).order_by(*order_by)
+
             entry_query = simplesearch.get_query(query_string, ['pn'])
             qs1 = init_qs.filter(entry_query).order_by(*order_by)
 
             entry_query = simplesearch.get_query(query_string, ['summary', 'description'])
             qs2 = init_qs.filter(entry_query).order_by(*order_by)
 
-            qs = list(utils.chain_unique(qs1, qs2))
+            qs = list(utils.chain_unique(qs0, qs1, qs2))
         else:
             if 'q' in self.request.GET:
                 qs = init_qs.order_by('pn', 'layerbranch__layer')
Signature · 1 line
2.5.5
#0 lore raw

PATCH 1/2 views: ensure we only show results once in recipe search

Usage of itertools.chain() that was introduced in 3155206e54413f72df3b3b41280eafd332a58ba4 in order to prioritise matches in the recipe name resulted in recipes showing up twice in the results if they matched in both the name and the recipe name. Use a custom chaining function that skips duplicate results in order to fix this.

Fixes [YOCTO #10177].

---
 layerindex/utils.py | 10 ++++++++++
 layerindex/views.py |  9 ++++-----
 2 files changed, 14 insertions(+), 5 deletions(-)
layerindex/utils.py +10 -0
index 484d3fe..e3be631 100644
@@ -237,3 +237,13 @@ def lock_file(fn):
 
 def unlock_file(lock):
     fcntl.flock(lock, fcntl.LOCK_UN)
+
+def chain_unique(*iterables):
+    """Chain unique objects in a list of querysets, preserving order"""
+    seen = set()
+    for element in iterables:
+        for item in element:
+            k = item.id
+            if k not in seen:
+                seen.add(k)
+                yield item
layerindex/views.py +4 -5
index 06045ae..3e78c58 100644
@@ -12,7 +12,6 @@ from django.core.exceptions import PermissionDenied
 from django.template import RequestContext
 from layerindex.models import Branch, LayerItem, LayerMaintainer, LayerBranch, LayerDependency, LayerNote, Recipe, Machine, Distro, BBClass, BBAppend, RecipeChange, RecipeChangeset, ClassicRecipe
 from datetime import datetime
-from itertools import chain
 from django.views.generic import TemplateView, DetailView, ListView
 from django.views.generic.edit import CreateView, DeleteView, UpdateView
 from django.views.generic.base import RedirectView
@@ -27,6 +26,7 @@ from django.utils.decorators import method_decorator
 from django.contrib.auth.decorators import login_required
 from django.contrib import messages
 from reversion.models import Revision
+from . import utils
 from . import simplesearch
 import settings
 from django.dispatch import receiver
@@ -213,12 +213,11 @@ def bulk_change_edit_view(request, template_name, pk):
 def bulk_change_patch_view(request, pk):
     import os
     import os.path
-    from layerindex.utils import runcmd
     changeset = get_object_or_404(RecipeChangeset, pk=pk)
     # FIXME this couples the web server and machine running the update script together,
     # but given that it's a separate script the way is open to decouple them in future
     try:
-        ret = runcmd('%s bulkchange.py %d %s' % (sys.executable, int(pk), settings.TEMP_BASE_DIR), os.path.dirname(__file__))
+        ret = utils.runcmd('%s bulkchange.py %d %s' % (sys.executable, int(pk), settings.TEMP_BASE_DIR), os.path.dirname(__file__))
         if ret:
             fn = ret.splitlines()[-1].decode('utf-8')
             if os.path.exists(fn):
@@ -387,7 +386,7 @@ class RecipeSearchView(ListView):
             qs2 = init_qs.filter(entry_query).order_by(*order_by)
             qs2 = recipes_preferred_count(qs2)
 
-            qs = list(chain(qs1, qs2))
+            qs = list(utils.chain_unique(qs1, qs2))
         else:
             if 'q' in self.request.GET:
                 qs = init_qs.order_by('pn', 'layerbranch__layer')
@@ -739,7 +738,7 @@ class ClassicRecipeSearchView(RecipeSearchView):
             entry_query = simplesearch.get_query(query_string, ['summary', 'description'])
             qs2 = init_qs.filter(entry_query).order_by(*order_by)
 
-            qs = list(chain(qs1, qs2))
+            qs = list(utils.chain_unique(qs1, qs2))
         else:
             if 'q' in self.request.GET:
                 qs = init_qs.order_by('pn', 'layerbranch__layer')
Signature · 1 line
2.5.5