File diff 3ec1ad3d78a8 → b2d842037a63
conntrackt/views.py
Show inline comments
 
@@ -24,12 +24,13 @@ from StringIO import StringIO
 
from zipfile import ZipFile, ZIP_DEFLATED
 
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
 
from django.shortcuts import render_to_response, get_object_or_404
 
from django.views.generic import TemplateView, DetailView, CreateView, UpdateView, DeleteView, View
 

	
 
@@ -1036,31 +1037,44 @@ class APISearchView(MultiplePermissionsR
 

	
 
    # Required permissions.
 
    permissions = {
 
        "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.
 
        """
 

	
 
        # Retrieve the search term, and strip it if it was provided.
 
        if search_term:
 
            search_term = search_term.strip()
 

	
 
        # 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 != "":
 

	
 
            # Run the search on entities and projects.
 
            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,
 
                              "project": entity.project.name,
 
                              "type": "entity",
 
                              "url": entity.get_absolute_url(),})