views.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. #!/usr/bin/env python
  2. # Licensed to Cloudera, Inc. under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. Cloudera, Inc. licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. try:
  18. import json
  19. except ImportError:
  20. import simplejson as json
  21. import logging
  22. from django.core.urlresolvers import reverse
  23. from django.http import HttpResponse
  24. from django.utils.translation import ugettext as _
  25. from django.shortcuts import redirect
  26. from desktop.lib.django_util import render
  27. from desktop.lib.exceptions_renderable import PopupException
  28. from search.api import SolrApi
  29. from search.conf import SOLR_URL
  30. from search.decorators import allow_admin_only
  31. from search.forms import QueryForm, CollectionForm, HighlightingForm
  32. from search.models import Collection, augment_solr_response
  33. from search.search_controller import SearchController
  34. LOG = logging.getLogger(__name__)
  35. def index(request):
  36. hue_collections = Collection.objects.all()
  37. if not hue_collections:
  38. if request.user.is_superuser:
  39. return admin_collections(request, True)
  40. else:
  41. return no_collections(request)
  42. search_form = QueryForm(request.GET)
  43. response = {}
  44. error = {}
  45. solr_query = {}
  46. hue_collection = None
  47. if search_form.is_valid():
  48. collection_id = search_form.cleaned_data['collection']
  49. if request.GET.get('collection') is None:
  50. collection_id = request.COOKIES.get('hueSearchLastCollection', collection_id)
  51. solr_query['q'] = search_form.cleaned_data['query']
  52. solr_query['fq'] = search_form.cleaned_data['fq']
  53. if search_form.cleaned_data['sort']:
  54. solr_query['sort'] = search_form.cleaned_data['sort']
  55. solr_query['rows'] = search_form.cleaned_data['rows'] or 15
  56. solr_query['start'] = search_form.cleaned_data['start'] or 0
  57. solr_query['facets'] = search_form.cleaned_data['facets'] or 1
  58. try:
  59. hue_collection = Collection.objects.get(id=collection_id)
  60. solr_query['collection'] = hue_collection.name
  61. response = SolrApi(SOLR_URL.get()).query(solr_query, hue_collection)
  62. except Exception, e:
  63. error['message'] = unicode(str(e), "utf8")
  64. else:
  65. hue_collection = hue_collections[0]
  66. collection_id = hue_collection.id
  67. if hue_collection is not None:
  68. response = augment_solr_response(response, hue_collection.facets.get_data())
  69. print response
  70. if request.GET.get('format') == 'json':
  71. return HttpResponse(json.dumps(response), mimetype="application/json")
  72. return render('search.mako', request, {
  73. 'search_form': search_form,
  74. 'response': response,
  75. 'error': error,
  76. 'solr_query': solr_query,
  77. 'hue_collection': hue_collection,
  78. 'hue_collections': hue_collections,
  79. 'current_collection': collection_id,
  80. 'json': json,
  81. })
  82. def no_collections(request):
  83. return render('no_collections.mako', request, {})
  84. @allow_admin_only
  85. def admin_collections(request, is_redirect=False):
  86. existing_hue_collections = Collection.objects.all()
  87. if request.GET.get('format') == 'json':
  88. collections = []
  89. for collection in existing_hue_collections:
  90. massaged_collection = {
  91. 'id': collection.id,
  92. 'name': collection.name,
  93. 'label': collection.label,
  94. 'isCoreOnly': collection.is_core_only,
  95. 'absoluteUrl': collection.get_absolute_url()
  96. }
  97. collections.append(massaged_collection)
  98. return HttpResponse(json.dumps(collections), mimetype="application/json")
  99. return render('admin_collections.mako', request, {
  100. 'existing_hue_collections': existing_hue_collections,
  101. 'is_redirect': is_redirect
  102. })
  103. @allow_admin_only
  104. def admin_collections_import(request):
  105. if request.method == 'POST':
  106. searcher = SearchController()
  107. status = 0
  108. err_message = _('Error')
  109. result = {
  110. 'status': status,
  111. 'message': err_message
  112. }
  113. importables = json.loads(request.POST["selected"])
  114. for imp in importables:
  115. try:
  116. searcher.add_new_collection(imp)
  117. status += 1
  118. except Exception, e:
  119. err_message += unicode(str(e), "utf8") + "\n"
  120. result['message'] = status == len(importables) and _('Imported successfully') or _('Imported with errors: ') + err_message
  121. return HttpResponse(json.dumps(result), mimetype="application/json")
  122. else:
  123. if request.GET.get('format') == 'json':
  124. searcher = SearchController()
  125. new_solr_collections = searcher.get_new_collections()
  126. massaged_collections = []
  127. for coll in new_solr_collections:
  128. massaged_collections.append({
  129. 'type': 'collection',
  130. 'name': coll
  131. })
  132. new_solr_cores = searcher.get_new_cores()
  133. massaged_cores = []
  134. for core in new_solr_cores:
  135. massaged_cores.append({
  136. 'type': 'core',
  137. 'name': core
  138. })
  139. response = {
  140. 'newSolrCollections': list(massaged_collections),
  141. 'newSolrCores': list(massaged_cores)
  142. }
  143. return HttpResponse(json.dumps(response), mimetype="application/json")
  144. else:
  145. return admin_collections(request, True)
  146. @allow_admin_only
  147. def admin_collection_delete(request):
  148. if request.method != 'POST':
  149. raise PopupException(_('POST request required.'))
  150. id = request.POST.get('id')
  151. searcher = SearchController()
  152. response = {
  153. 'id': searcher.delete_collection(id)
  154. }
  155. return HttpResponse(json.dumps(response), mimetype="application/json")
  156. @allow_admin_only
  157. def admin_collection_copy(request):
  158. if request.method != 'POST':
  159. raise PopupException(_('POST request required.'))
  160. id = request.POST.get('id')
  161. searcher = SearchController()
  162. response = {
  163. 'id': searcher.copy_collection(id)
  164. }
  165. return HttpResponse(json.dumps(response), mimetype="application/json")
  166. @allow_admin_only
  167. def admin_collection_properties(request, collection_id):
  168. hue_collection = Collection.objects.get(id=collection_id)
  169. solr_collection = SolrApi(SOLR_URL.get()).collection_or_core(hue_collection)
  170. if request.method == 'POST':
  171. collection_form = CollectionForm(request.POST, instance=hue_collection)
  172. if collection_form.is_valid():
  173. hue_collection = collection_form.save()
  174. return redirect(reverse('search:admin_collection_properties', kwargs={'collection_id': hue_collection.id}))
  175. else:
  176. request.error(_('Errors on the form: %s') % collection_form.errors)
  177. else:
  178. collection_form = CollectionForm(instance=hue_collection)
  179. return render('admin_collection_properties.mako', request, {
  180. 'solr_collection': solr_collection,
  181. 'hue_collection': hue_collection,
  182. 'collection_form': collection_form,
  183. })
  184. @allow_admin_only
  185. def admin_collection_template(request, collection_id):
  186. hue_collection = Collection.objects.get(id=collection_id)
  187. solr_collection = SolrApi(SOLR_URL.get()).collection_or_core(hue_collection)
  188. if request.method == 'POST':
  189. hue_collection.result.update_from_post(request.POST)
  190. hue_collection.result.save()
  191. return HttpResponse(json.dumps({}), mimetype="application/json")
  192. solr_query = {}
  193. solr_query['collection'] = hue_collection.name
  194. solr_query['q'] = ''
  195. solr_query['fq'] = ''
  196. solr_query['rows'] = 5
  197. solr_query['start'] = 0
  198. solr_query['facets'] = 0
  199. response = SolrApi(SOLR_URL.get()).query(solr_query, hue_collection)
  200. return render('admin_collection_template.mako', request, {
  201. 'solr_collection': solr_collection,
  202. 'hue_collection': hue_collection,
  203. 'sample_data': json.dumps(response["response"]["docs"]),
  204. })
  205. @allow_admin_only
  206. def admin_collection_facets(request, collection_id):
  207. hue_collection = Collection.objects.get(id=collection_id)
  208. solr_collection = SolrApi(SOLR_URL.get()).collection(hue_collection.name)
  209. if request.method == 'POST':
  210. hue_collection.facets.update_from_post(request.POST)
  211. hue_collection.facets.save()
  212. return HttpResponse(json.dumps({}), mimetype="application/json")
  213. return render('admin_collection_facets.mako', request, {
  214. 'solr_collection': solr_collection,
  215. 'hue_collection': hue_collection,
  216. })
  217. @allow_admin_only
  218. def admin_collection_sorting(request, collection_id):
  219. hue_collection = Collection.objects.get(id=collection_id)
  220. solr_collection = SolrApi(SOLR_URL.get()).collection(hue_collection.name)
  221. if request.method == 'POST':
  222. hue_collection.sorting.update_from_post(request.POST)
  223. hue_collection.sorting.save()
  224. return HttpResponse(json.dumps({}), mimetype="application/json")
  225. return render('admin_collection_sorting.mako', request, {
  226. 'solr_collection': solr_collection,
  227. 'hue_collection': hue_collection,
  228. })
  229. @allow_admin_only
  230. def admin_collection_highlighting(request, collection_id):
  231. hue_collection = Collection.objects.get(id=collection_id)
  232. solr_collection = SolrApi(SOLR_URL.get()).collection(hue_collection.name)
  233. if request.method == 'POST':
  234. hue_collection.result.update_from_post(request.POST)
  235. hue_collection.result.save()
  236. return HttpResponse(json.dumps({}), mimetype="application/json")
  237. return render('admin_collection_highlighting.mako', request, {
  238. 'solr_collection': solr_collection,
  239. 'hue_collection': hue_collection,
  240. })
  241. # Ajax below
  242. @allow_admin_only
  243. def admin_collection_solr_properties(request, collection_id):
  244. hue_collection = Collection.objects.get(id=collection_id)
  245. solr_collection = SolrApi(SOLR_URL.get()).collection_or_core(hue_collection)
  246. content = render('admin_collection_properties_solr_properties.mako', request, {
  247. 'solr_collection': solr_collection,
  248. 'hue_collection': hue_collection,
  249. }, force_template=True).content
  250. return HttpResponse(json.dumps({'content': content}), mimetype="application/json")
  251. @allow_admin_only
  252. def admin_collection_schema(request, collection_id):
  253. hue_collection = Collection.objects.get(id=collection_id)
  254. solr_schema = SolrApi(SOLR_URL.get()).schema(hue_collection.name)
  255. content = {
  256. 'solr_schema': solr_schema.decode('utf-8')
  257. }
  258. return HttpResponse(json.dumps(content), mimetype="application/json")
  259. # TODO security
  260. def query_suggest(request, collection_id, query=""):
  261. hue_collection = Collection.objects.get(id=collection_id)
  262. result = {'status': -1, 'message': 'Error'}
  263. solr_query = {}
  264. solr_query['collection'] = collection
  265. solr_query['q'] = query
  266. try:
  267. response = SolrApi(SOLR_URL.get()).suggest(solr_query, hue_collection)
  268. result['message'] = response
  269. result['status'] = 0
  270. except Exception, e:
  271. result['message'] = unicode(str(e), "utf8")
  272. return HttpResponse(json.dumps(result), mimetype="application/json")