diff options
Diffstat (limited to 'apps')
-rw-r--r-- | apps/patchwork/tests/person.py | 9 | ||||
-rw-r--r-- | apps/patchwork/views/base.py | 23 |
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', |