From bdf3020f551dedbc623b10c849821f54d86e3ebc Mon Sep 17 00:00:00 2001 From: Tom Price Date: Thu, 30 Apr 2015 23:27:31 +0100 Subject: [PATCH 1/3] See diff --- RIGS/views.py | 1 + 1 file changed, 1 insertion(+) diff --git a/RIGS/views.py b/RIGS/views.py index 472f20ec..6dd51575 100644 --- a/RIGS/views.py +++ b/RIGS/views.py @@ -257,6 +257,7 @@ class SecureAPIRequest(generic.View): fields = ['name'] all_objects = self.models[model].objects results = [] + # This code is all bollocks, suggest rewriting it for field in fields: filter = field + "__icontains" objects = all_objects.filter(**{filter: term}) From 8ade2b234c1e843fedc9efd1c3662965f2c3bafb Mon Sep 17 00:00:00 2001 From: Tom Price Date: Tue, 19 May 2015 17:23:05 +0100 Subject: [PATCH 2/3] Enable searching by multiple options seperated by a space. No idea what the effect will be if we have somebody with more than one space in their name. This behavoir might be quite unpredictable. --- RIGS/views.py | 49 ++++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/RIGS/views.py b/RIGS/views.py index 6dd51575..cebaca28 100644 --- a/RIGS/views.py +++ b/RIGS/views.py @@ -9,6 +9,7 @@ from django.core import serializers import simplejson from django.contrib import messages import datetime +import operator from RIGS import models, forms @@ -253,28 +254,38 @@ class SecureAPIRequest(generic.View): # Supply data for autocomplete ajax request in json form term = request.GET.get('term', None) if term: - if fields is None: + if fields is None: # Default to just name fields = ['name'] - all_objects = self.models[model].objects + + # Build a list of Q objects for use later + queries = [] + for part in term.split(" "): + qs = [] + for field in fields: + q = Q(**{field + "__icontains": part}) + qs.append(q) + queries.append(reduce(operator.or_, qs)) + + + # Build the data response list results = [] - # This code is all bollocks, suggest rewriting it - for field in fields: - filter = field + "__icontains" - objects = all_objects.filter(**{filter: term}) - for o in objects: - data = { - 'pk': o.pk, - 'value': o.pk, - 'label': o.name, - } + query = reduce(operator.and_, queries) + objects = self.models[model].objects.filter(query) + print objects.query + for o in objects: + data = { + 'pk': o.pk, + 'value': o.pk, + 'label': o.name, + } + try: # See if there is a valid update URL + data['update'] = reverse("%s_update" % model, kwargs={'pk': o.pk}) + except NoReverseMatch: + pass + results.append(data) - try: # See if there is an update url or don't bother with it otherwise - data['update'] = reverse("%s_update" % model, kwargs={'pk': o.pk}) - except NoReverseMatch: - pass - - results.append(data) - json = simplejson.dumps(results[:20]) + # return a data response + json = simplejson.dumps(results) return HttpResponse(json, content_type="application/json") # Always json start = request.GET.get('start', None) From 6d237de7436487b138054474ea728c20f384d79e Mon Sep 17 00:00:00 2001 From: Tom Price Date: Tue, 19 May 2015 17:54:15 +0100 Subject: [PATCH 3/3] Remove debug print statement --- RIGS/views.py | 1 - 1 file changed, 1 deletion(-) diff --git a/RIGS/views.py b/RIGS/views.py index cebaca28..f7df6c3e 100644 --- a/RIGS/views.py +++ b/RIGS/views.py @@ -271,7 +271,6 @@ class SecureAPIRequest(generic.View): results = [] query = reduce(operator.and_, queries) objects = self.models[model].objects.filter(query) - print objects.query for o in objects: data = { 'pk': o.pk,