From 86307ca2a7d643d8975d5c9cab551e30d0dcbb07 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Fri, 5 Sep 2014 15:09:51 +0200 Subject: FS#30773 - package search: put exact match at the top This might not be the cleanest or best performing solution, but it works. Signed-off-by: Florian Pritz --- packages/views/search.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/packages/views/search.py b/packages/views/search.py index b377817..a01de4d 100644 --- a/packages/views/search.py +++ b/packages/views/search.py @@ -116,6 +116,18 @@ class SearchListView(ListView): packages = packages.order_by(sort) else: packages = packages.order_by('pkgname') + + if self.form.cleaned_data['q']: + # put exact matches first + qs1 = packages.filter(pkgname__iexact=self.form.cleaned_data['q']) + qs2 = packages.exclude(pkgname__iexact=self.form.cleaned_data['q']) + + # https://stackoverflow.com/questions/18235419/how-to-chain-django-querysets-preserving-individual-order + len(qs1) + for q in qs2: + qs1._result_cache.append(q) + packages = qs1 + return packages # Form had errors so don't return any results -- cgit v1.2.3-24-g4f1b