1 from haystack
.query
import SearchQuerySet
3 from django
.contrib
.auth
.decorators
import login_required
4 from django
.http
import (HttpResponse
, HttpResponseRedirect
,
6 from django
.utils
import simplejson
as json
8 from ganeti_web
.models
import VirtualMachine
, Cluster
, Node
12 def suggestions(request
):
13 ''' Return a list of search results for the autocomplete search box.
15 Return a list of search results for the query in the GET parameter `term`
16 as a JSON object. If `term` does not exist, just return a blank list.
18 The format consists of a list of objects representing the object name and
19 the object type. Here's an example:
40 # Get the query from the GET param
41 query
= request
.GET
.get('term', None)
43 # Start out with an empty result objects list
46 # If a query actually does exist, construct the result objects
49 # Perform the actual query on the Haystack search query set
50 results
= SearchQuerySet().autocomplete(content_auto
=query
)
52 # Construct the result objects
53 for result
in results
:
55 result_object
['value'] = result
.content_auto
56 if result
.model_name
== 'virtualmachine':
57 result_object
['type'] = 'vm'
58 elif result
.model_name
== 'cluster':
59 result_object
['type'] = 'cluster'
60 elif result
.model_name
== 'node':
61 result_object
['type'] = 'node'
63 result_object
['type'] = 'unknown'
64 result_objects
.append(result_object
)
66 # Return the results list as a json object
67 return HttpResponse(json
.dumps(result_objects
, indent
=4),
68 mimetype
='application/json')
72 def detail_lookup(request
):
74 Look up and redirect to the detail page for the given object.
76 There must be two supplied GET parameters:
77 `type`: which declares the type of object we're looking up, and
78 the possible values should be either 'vm', 'cluster', or
80 `hostname`: Hostname of the object.
82 # Grab the GET parameters
83 object_type
= request
.GET
.get('type', None)
84 hostname
= request
.GET
.get('hostname', None)
86 # Variable for the queried object
89 # Try getting the object, 404 if it can't be found
92 # If the object type is a vm or node, we need to select the related
93 # cluster so we don't make an additional db query
94 if object_type
== 'vm':
95 obj
= VirtualMachine
.objects
.filter(hostname
=hostname
)\
96 .select_related('cluster')[0]
97 elif object_type
== 'node':
98 obj
= Node
.objects
.filter(hostname
=hostname
)\
99 .select_related('cluster')[0]
100 elif object_type
== 'cluster':
101 obj
= Cluster
.objects
.filter(hostname
=hostname
)[0]
103 return HttpResponseNotFound()
105 return HttpResponseNotFound()
107 # Redirect to the absolute URL of the object
108 return HttpResponseRedirect(obj
.get_absolute_url())