mirror of
git://git.yoctoproject.org/layerindex-web.git
synced 2025-07-19 20:59:01 +02:00
Reimplement simplesearch
We don't need a whole module for this, rewrite as a simple function. Signed-off-by: Paul Eggleton <paul.eggleton@linux.intel.com>
This commit is contained in:
parent
c0b85ba29c
commit
9fe3787027
|
@ -1,39 +0,0 @@
|
||||||
# Borrowed from http://julienphalip.com/post/2825034077/adding-search-to-a-django-site-in-a-snap
|
|
||||||
# Copyright 2011 Julien Phalip. All Rights Reserved.
|
|
||||||
|
|
||||||
import re
|
|
||||||
from django.db.models import Q
|
|
||||||
|
|
||||||
def normalize_query(query_string,
|
|
||||||
findterms=re.compile(r'"([^"]+)"|(\S+)').findall,
|
|
||||||
normspace=re.compile(r'\s{2,}').sub):
|
|
||||||
''' Splits the query string in invidual keywords, getting rid of unecessary spaces
|
|
||||||
and grouping quoted words together.
|
|
||||||
Example:
|
|
||||||
|
|
||||||
>>> normalize_query(' some random words "with quotes " and spaces')
|
|
||||||
['some', 'random', 'words', 'with quotes', 'and', 'spaces']
|
|
||||||
|
|
||||||
'''
|
|
||||||
return [normspace(' ', (t[0] or t[1]).strip()) for t in findterms(query_string)]
|
|
||||||
|
|
||||||
def get_query(query_string, search_fields):
|
|
||||||
''' Returns a query, that is a combination of Q objects. That combination
|
|
||||||
aims to search keywords within a model by testing the given search fields.
|
|
||||||
|
|
||||||
'''
|
|
||||||
query = None # Query to search for every search term
|
|
||||||
terms = normalize_query(query_string)
|
|
||||||
for term in terms:
|
|
||||||
or_query = None # Query to search for a given term in each field
|
|
||||||
for field_name in search_fields:
|
|
||||||
q = Q(**{"%s__icontains" % field_name: term})
|
|
||||||
if or_query is None:
|
|
||||||
or_query = q
|
|
||||||
else:
|
|
||||||
or_query = or_query | q
|
|
||||||
if query is None:
|
|
||||||
query = or_query
|
|
||||||
else:
|
|
||||||
query = query & or_query
|
|
||||||
return query
|
|
|
@ -537,3 +537,23 @@ class ProgressReader():
|
||||||
if self.logger is not None:
|
if self.logger is not None:
|
||||||
self.logger.warning('Failed to read progress: %s' % str(e))
|
self.logger.warning('Failed to read progress: %s' % str(e))
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
def string_to_query(querystr, fieldnames):
|
||||||
|
# Inspired by http://julienphalip.com/post/2825034077/adding-search-to-a-django-site-in-a-snap
|
||||||
|
# (reimplemented a bit more simply)
|
||||||
|
from django.db.models import Q
|
||||||
|
keywords = [item for item in re.split(r"\s|\"(.*)?\"|'.*?'", querystr) if item]
|
||||||
|
query = None
|
||||||
|
for keyword in keywords:
|
||||||
|
fquery = None
|
||||||
|
for fieldname in fieldnames:
|
||||||
|
q = Q(**{'%s__icontains' % fieldname: keyword})
|
||||||
|
if fquery is None:
|
||||||
|
fquery = q
|
||||||
|
else:
|
||||||
|
fquery = fquery | q
|
||||||
|
if query is None:
|
||||||
|
query = fquery
|
||||||
|
else:
|
||||||
|
query = query & fquery
|
||||||
|
return query
|
||||||
|
|
|
@ -57,7 +57,7 @@ from layerindex.models import (BBAppend, BBClass, Branch, ClassicRecipe,
|
||||||
UserProfile, PatchDisposition)
|
UserProfile, PatchDisposition)
|
||||||
|
|
||||||
|
|
||||||
from . import simplesearch, tasks, utils
|
from . import tasks, utils
|
||||||
|
|
||||||
def edit_layernote_view(request, template_name, slug, pk=None):
|
def edit_layernote_view(request, template_name, slug, pk=None):
|
||||||
layeritem = get_object_or_404(LayerItem, name=slug)
|
layeritem = get_object_or_404(LayerItem, name=slug)
|
||||||
|
@ -495,13 +495,13 @@ class RecipeSearchView(ListView):
|
||||||
qs0 = recipes_preferred_count(qs0)
|
qs0 = recipes_preferred_count(qs0)
|
||||||
|
|
||||||
# Then keyword somewhere in the name
|
# Then keyword somewhere in the name
|
||||||
entry_query = simplesearch.get_query(query_string, ['pn'])
|
entry_query = utils.string_to_query(query_string, ['pn'])
|
||||||
qs1 = init_qs.filter(entry_query).order_by(*order_by)
|
qs1 = init_qs.filter(entry_query).order_by(*order_by)
|
||||||
if preferred:
|
if preferred:
|
||||||
qs1 = recipes_preferred_count(qs1)
|
qs1 = recipes_preferred_count(qs1)
|
||||||
|
|
||||||
# Then keyword somewhere in summary or description
|
# Then keyword somewhere in summary or description
|
||||||
entry_query = simplesearch.get_query(query_string, ['description', 'summary'])
|
entry_query = utils.string_to_query(query_string, ['description', 'summary'])
|
||||||
qs2 = init_qs.filter(entry_query).order_by(*order_by)
|
qs2 = init_qs.filter(entry_query).order_by(*order_by)
|
||||||
if preferred:
|
if preferred:
|
||||||
qs2 = recipes_preferred_count(qs2)
|
qs2 = recipes_preferred_count(qs2)
|
||||||
|
@ -765,7 +765,7 @@ class MachineSearchView(ListView):
|
||||||
query_string = ""
|
query_string = ""
|
||||||
init_qs = Machine.objects.filter(layerbranch__branch__name=self.kwargs['branch'])
|
init_qs = Machine.objects.filter(layerbranch__branch__name=self.kwargs['branch'])
|
||||||
if query_string.strip():
|
if query_string.strip():
|
||||||
entry_query = simplesearch.get_query(query_string, ['name', 'description'])
|
entry_query = utils.string_to_query(query_string, ['name', 'description'])
|
||||||
return init_qs.filter(entry_query).order_by('name', 'layerbranch__layer')
|
return init_qs.filter(entry_query).order_by('name', 'layerbranch__layer')
|
||||||
else:
|
else:
|
||||||
if 'q' in self.request.GET:
|
if 'q' in self.request.GET:
|
||||||
|
@ -817,7 +817,7 @@ class DistroSearchView(ListView):
|
||||||
query_string = ""
|
query_string = ""
|
||||||
init_qs = Distro.objects.filter(layerbranch__branch__name=self.kwargs['branch'])
|
init_qs = Distro.objects.filter(layerbranch__branch__name=self.kwargs['branch'])
|
||||||
if query_string.strip():
|
if query_string.strip():
|
||||||
entry_query = simplesearch.get_query(query_string, ['name', 'description'])
|
entry_query = utils.string_to_query(query_string, ['name', 'description'])
|
||||||
return init_qs.filter(entry_query).order_by('name', 'layerbranch__layer')
|
return init_qs.filter(entry_query).order_by('name', 'layerbranch__layer')
|
||||||
|
|
||||||
if 'q' in self.request.GET:
|
if 'q' in self.request.GET:
|
||||||
|
@ -845,7 +845,7 @@ class ClassSearchView(ListView):
|
||||||
query_string = ""
|
query_string = ""
|
||||||
init_qs = BBClass.objects.filter(layerbranch__branch__name=self.kwargs['branch'])
|
init_qs = BBClass.objects.filter(layerbranch__branch__name=self.kwargs['branch'])
|
||||||
if query_string.strip():
|
if query_string.strip():
|
||||||
entry_query = simplesearch.get_query(query_string, ['name'])
|
entry_query = utils.string_to_query(query_string, ['name'])
|
||||||
return init_qs.filter(entry_query).order_by('name', 'layerbranch__layer')
|
return init_qs.filter(entry_query).order_by('name', 'layerbranch__layer')
|
||||||
|
|
||||||
if 'q' in self.request.GET:
|
if 'q' in self.request.GET:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user