diff --git a/conntrackt/views.py b/conntrackt/views.py --- a/conntrackt/views.py +++ b/conntrackt/views.py @@ -27,6 +27,7 @@ import json # Django imports. from django.contrib.auth.decorators import permission_required from django.contrib import messages +from django.core.exceptions import ValidationError from django.core.urlresolvers import reverse, reverse_lazy from django.db.models import Q from django.http import HttpResponse @@ -1039,7 +1040,10 @@ class APISearchView(MultiplePermissionsR "all": ("conntrackt.view",), } - def get(self, request, search_term): + # Raise authorisation denied exception for unmet permissions. + raise_exception = True + + def get(self, request, search_term=""): """ Implements response handling for a GET request. """ @@ -1051,6 +1055,11 @@ class APISearchView(MultiplePermissionsR # Set-up a list that will contain found items. items = [] + # Fetch the maximum number of items that should be returned. + limit = int(request.GET.get("limit", 0)) + if limit < 0: + raise ValidationError("Limit may not be a negative value.") + # Don't perform search with empty search term. if search_term != "": @@ -1058,6 +1067,11 @@ class APISearchView(MultiplePermissionsR entities = Entity.objects.search(search_term).select_related("project") projects = Project.objects.search(search_term) + # If maximum number of items was provided, narrow-down the results. + if limit > 0: + entities = entities[:limit] + projects = projects[:limit] + # Add found entities. for entity in entities: items.append({"name": entity.name,