search_controller.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/usr/bin/env python
  2. # -- coding: utf-8 --
  3. # Licensed to Cloudera, Inc. under one
  4. # or more contributor license agreements. See the NOTICE file
  5. # distributed with this work for additional information
  6. # regarding copyright ownership. Cloudera, Inc. licenses this file
  7. # to you under the Apache License, Version 2.0 (the
  8. # "License"); you may not use this file except in compliance
  9. # with the License. You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. try:
  19. import json
  20. except ImportError:
  21. import simplejson as json
  22. import logging
  23. from desktop.lib.exceptions_renderable import PopupException
  24. from search.api import SolrApi
  25. from search.conf import SOLR_URL
  26. from search.models import Collection
  27. from django.utils.translation import ugettext as _
  28. LOG = logging.getLogger(__name__)
  29. class SearchController(object):
  30. """
  31. Glue the models to the views.
  32. """
  33. def __init__(self):
  34. pass
  35. def get_new_collections(self):
  36. try:
  37. solr_collections = SolrApi(SOLR_URL.get()).collections()
  38. for name in Collection.objects.values_list('name', flat=True):
  39. solr_collections.pop(name, None)
  40. except Exception, e:
  41. LOG.warn('No Zookeeper servlet running on Solr server: %s' % e)
  42. solr_collections = []
  43. return solr_collections
  44. def get_new_cores(self):
  45. try:
  46. solr_cores = SolrApi(SOLR_URL.get()).cores()
  47. for name in Collection.objects.values_list('name', flat=True):
  48. solr_cores.pop(name, None)
  49. except Exception, e:
  50. solr_cores = []
  51. LOG.warn('No Single core setup on Solr server: %s' % e)
  52. return solr_cores
  53. def add_new_collection(self, attrs):
  54. if attrs['type'] == 'collection':
  55. collections = self.get_new_collections()
  56. collection = collections[attrs['name']]
  57. hue_collection, created = Collection.objects.get_or_create(name=attrs['name'], solr_properties=collection, is_enabled=True)
  58. return hue_collection
  59. elif attrs['type'] == 'core':
  60. cores = self.get_new_cores()
  61. core = cores[attrs['name']]
  62. hue_collection, created = Collection.objects.get_or_create(name=attrs['name'], solr_properties=core, is_enabled=True, is_core_only=True)
  63. return hue_collection
  64. else:
  65. raise PopupException(_('Collection type does not exit: %s') % attrs)
  66. def delete_collection(self, collection_id):
  67. id = collection_id
  68. try:
  69. Collection.objects.get(id=collection_id).delete()
  70. except Exception, e:
  71. LOG.warn('Error deleting collection: %s' % e)
  72. id = -1
  73. return id
  74. def copy_collection(self, collection_id):
  75. id = -1
  76. try:
  77. copy = Collection.objects.get(id=collection_id)
  78. copy.label += _(' (Copy)')
  79. copy.id = copy.pk = None
  80. copy.save()
  81. facets = copy.facets
  82. facets.id = None
  83. facets.save()
  84. copy.facets = facets
  85. result = copy.result
  86. result.id = None
  87. result.save()
  88. copy.result = result
  89. sorting = copy.sorting
  90. sorting.id = None
  91. sorting.save()
  92. copy.sorting = sorting
  93. copy.save()
  94. id = copy.id
  95. except Exception, e:
  96. LOG.warn('Error copying collection: %s' % e)
  97. def is_collection(self, collection_name):
  98. solr_collections = SolrApi(SOLR_URL.get()).collections()
  99. return collection_name in solr_collections
  100. def is_core(self, core_name):
  101. solr_cores = SolrApi(SOLR_URL.get()).cores()
  102. return core_name in solr_cores