mirror of
https://github.com/nottinghamtec/PyRIGS.git
synced 2026-01-23 00:12:15 +00:00
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.
This commit is contained in:
@@ -9,6 +9,7 @@ from django.core import serializers
|
|||||||
import simplejson
|
import simplejson
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
import datetime
|
import datetime
|
||||||
|
import operator
|
||||||
|
|
||||||
from RIGS import models, forms
|
from RIGS import models, forms
|
||||||
|
|
||||||
@@ -253,28 +254,38 @@ class SecureAPIRequest(generic.View):
|
|||||||
# Supply data for autocomplete ajax request in json form
|
# Supply data for autocomplete ajax request in json form
|
||||||
term = request.GET.get('term', None)
|
term = request.GET.get('term', None)
|
||||||
if term:
|
if term:
|
||||||
if fields is None:
|
if fields is None: # Default to just name
|
||||||
fields = ['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 = []
|
results = []
|
||||||
# This code is all bollocks, suggest rewriting it
|
query = reduce(operator.and_, queries)
|
||||||
for field in fields:
|
objects = self.models[model].objects.filter(query)
|
||||||
filter = field + "__icontains"
|
print objects.query
|
||||||
objects = all_objects.filter(**{filter: term})
|
for o in objects:
|
||||||
for o in objects:
|
data = {
|
||||||
data = {
|
'pk': o.pk,
|
||||||
'pk': o.pk,
|
'value': o.pk,
|
||||||
'value': o.pk,
|
'label': o.name,
|
||||||
'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
|
# return a data response
|
||||||
data['update'] = reverse("%s_update" % model, kwargs={'pk': o.pk})
|
json = simplejson.dumps(results)
|
||||||
except NoReverseMatch:
|
|
||||||
pass
|
|
||||||
|
|
||||||
results.append(data)
|
|
||||||
json = simplejson.dumps(results[:20])
|
|
||||||
return HttpResponse(json, content_type="application/json") # Always json
|
return HttpResponse(json, content_type="application/json") # Always json
|
||||||
|
|
||||||
start = request.GET.get('start', None)
|
start = request.GET.get('start', None)
|
||||||
|
|||||||
Reference in New Issue
Block a user