summaryrefslogtreecommitdiffstats
path: root/apps
diff options
context:
space:
mode:
authorJeremy Kerr <jk@ozlabs.org>2013-10-13 13:04:31 +0800
committerJeremy Kerr <jk@ozlabs.org>2013-10-13 13:04:31 +0800
commit9d22330effa8d433ec39edc37ca4e3a1e3206b85 (patch)
tree3a9017cb0b48ecb5bbc7212f8c88cf6053fbaa0f /apps
parentdf1abefaaa678cedc4ce0293fd84bb4164c9852d (diff)
downloadpatchwork-9d22330effa8d433ec39edc37ca4e3a1e3206b85.tar.bz2
patchwork-9d22330effa8d433ec39edc37ca4e3a1e3206b85.tar.xz
views/base: Implement limit for submitter autocompletion
Add a limit parameter to the completion view, and pass a limit of 20 results in the javascript. Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
Diffstat (limited to 'apps')
-rw-r--r--apps/patchwork/tests/person.py9
-rw-r--r--apps/patchwork/views/base.py23
2 files changed, 27 insertions, 5 deletions
diff --git a/apps/patchwork/tests/person.py b/apps/patchwork/tests/person.py
index 63bbadb..d948096 100644
--- a/apps/patchwork/tests/person.py
+++ b/apps/patchwork/tests/person.py
@@ -44,3 +44,12 @@ class SubmitterCompletionTest(TestCase):
data = json.loads(response.content)
self.assertEquals(len(data), 1)
self.assertEquals(data[0]['fields']['email'], 'test2@example.com')
+
+ def testCompleteLimit(self):
+ for i in range(3,10):
+ person = Person(email = 'test%d@example.com' % i)
+ person.save()
+ response = self.client.get('/submitter/', {'q': 'test', 'l': 5})
+ self.assertEquals(response.status_code, 200)
+ data = json.loads(response.content)
+ self.assertEquals(len(data), 5)
diff --git a/apps/patchwork/views/base.py b/apps/patchwork/views/base.py
index 634e383..7707653 100644
--- a/apps/patchwork/views/base.py
+++ b/apps/patchwork/views/base.py
@@ -86,12 +86,25 @@ def confirm(request, key):
def submitter_complete(request):
search = request.GET.get('q', '')
+ limit = request.GET.get('l', None)
response = HttpResponse(mimetype = "text/plain")
- if len(search) > 3:
- queryset = Person.objects.filter(Q(name__icontains = search) |
- Q(email__icontains = search))
- json_serializer = serializers.get_serializer("json")()
- json_serializer.serialize(queryset, ensure_ascii=False, stream=response)
+
+ if len(search) <= 3:
+ return response
+
+ queryset = Person.objects.filter(Q(name__icontains = search) |
+ Q(email__icontains = search))
+ if limit is not None:
+ try:
+ limit = int(limit)
+ except ValueError:
+ limit = None
+
+ if limit is not None and limit > 0:
+ queryset = queryset[:limit]
+
+ json_serializer = serializers.get_serializer("json")()
+ json_serializer.serialize(queryset, ensure_ascii=False, stream=response)
return response
help_pages = {'': 'index.html',