Add more information on deploying; fix format.
[ganeti_webmgr.git] / ganeti_web / views / search.py
blob831e30c6fd743ef6b4e8d6028545ce559919fbf6
1 from haystack.query import SearchQuerySet
3 from django.contrib.auth.decorators import login_required
4 from django.http import (HttpResponse, HttpResponseRedirect,
5 HttpResponseNotFound)
6 from django.utils import simplejson as json
8 from ganeti_web.models import VirtualMachine, Cluster, Node
11 @login_required
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:
23 'value': 'foo',
24 'type': 'vm',
27 'value': 'bar',
28 'type': 'vm',
31 'value': 'herp',
32 'type': 'cluster',
35 'value': 'derp',
36 'type': 'node',
39 '''
40 # Get the query from the GET param
41 query = request.GET.get('term', None)
43 # Start out with an empty result objects list
44 result_objects = []
46 # If a query actually does exist, construct the result objects
47 if query is not None:
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:
54 result_object = {}
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'
62 else:
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')
71 @login_required
72 def detail_lookup(request):
73 '''
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
79 'node'.
80 `hostname`: Hostname of the object.
81 '''
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
87 obj = None
89 # Try getting the object, 404 if it can't be found
90 try:
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]
102 else:
103 return HttpResponseNotFound()
104 except IndexError:
105 return HttpResponseNotFound()
107 # Redirect to the absolute URL of the object
108 return HttpResponseRedirect(obj.get_absolute_url())