浏览代码

[search] Fixing collection renaming

Romain Rigaux 12 年之前
父节点
当前提交
bd46843

+ 1 - 0
apps/search/src/search/api.py

@@ -85,6 +85,7 @@ class SolrApi(object):
 
   def collection(self, core):
     try:
+      # TODO: if core get from here, if collection from Zookeeper
       return self._root.get('admin/cores', params={'wt': 'json', 'core': core})
     except RestException, e:
       raise PopupException('Error while accessing Solr: %s' % e)

+ 2 - 2
apps/search/src/search/conf.py

@@ -37,6 +37,6 @@ SOLR_URL = Config(
 #    )
 #  )
 #)
-                                     
+
 # ZOOKEEPER URL
-# for COLLECTIONS                                     
+# for COLLECTIONS

+ 1 - 1
apps/search/src/search/forms.py

@@ -62,4 +62,4 @@ class HighlightingForm(forms.Form):
 class CollectionForm(forms.ModelForm):
   class Meta:
     model = Collection
-    exclude = ('facets', 'result', 'sorting', 'properties')
+    exclude = ('facets', 'result', 'sorting', 'properties', 'cores')

+ 17 - 6
apps/search/src/search/models.py

@@ -186,9 +186,10 @@ class Sorting(models.Model):
 
 
 class CollectionManager(models.Manager):
-  def get_or_create(self, name):
+
+  def get_or_create(self, name, solr_properties, is_core_only=False, is_enabled=True):
     try:
-      return self.get(name=name)
+      return self.get(name=name), False
     except Collection.DoesNotExist:
       facets = Facet.objects.create(data=json.dumps({
                    'properties': {'isEnabled': False, 'limit': 10, 'mincount': 1, 'sort': 'count'},
@@ -352,8 +353,18 @@ margin-top: 2px;
                   """
               }))
       sorting = Sorting.objects.create(data=json.dumps({'properties': {'is_enabled': False}, 'fields': []}))
+      cores = json.dumps(solr_properties)
 
-      return Collection.objects.create(name=name, label=name, facets=facets, result=result, sorting=sorting)
+      return Collection.objects.create(
+          name=name,
+          label=name,
+          is_enabled=is_enabled,
+          cores=cores,
+          is_core_only=is_core_only,
+          facets=facets,
+          result=result,
+          sorting=sorting
+      ), True
 
 
 class Collection(models.Model):
@@ -362,11 +373,11 @@ class Collection(models.Model):
   name = models.CharField(max_length=40, verbose_name=_t('Solr name'))
   label = models.CharField(max_length=100)
   is_core_only = models.BooleanField(default=False)
-  cores = models.TextField(default=json.dumps({}), verbose_name=_t('Core data'), help_text=_t('Cores or shards data'))
+  cores = models.TextField(default=json.dumps({}), verbose_name=_t('Collection with cores data'), help_text=_t('Solr json'))
   properties = models.TextField(
       default=json.dumps({}), verbose_name=_t('Properties'),
-      help_text=_t('Properties (e.g. results by pages number)'))
-  
+      help_text=_t('Hue properties (e.g. results by pages number)'))
+
   facets = models.ForeignKey(Facet)
   result = models.ForeignKey(Result)
   sorting = models.ForeignKey(Sorting)

+ 10 - 21
apps/search/src/search/search_controler.py

@@ -24,14 +24,10 @@ except ImportError:
 import logging
 
 from desktop.lib.exceptions_renderable import PopupException
-from desktop.lib.rest.http_client import HttpClient, RestException
-from desktop.lib.rest.resource import Resource
 
 from search.api import SolrApi
 from search.conf import SOLR_URL
-from search.decorators import allow_admin_only
-from search.forms import QueryForm, CollectionForm, HighlightingForm
-from search.models import Collection, augment_solr_response
+from search.models import Collection
 
 
 LOG = logging.getLogger(__name__)
@@ -41,7 +37,6 @@ class SearchController(object):
   """
   Glue the models to the views.
   """
-  
   def __init__(self):
     pass
 
@@ -49,27 +44,21 @@ class SearchController(object):
     solr_collections = SolrApi(SOLR_URL.get()).collections()
     for name in Collection.objects.values_list('name', flat=True):
       solr_collections.pop(name)
-    
+
     return solr_collections
 
-  def get_new_cores(self):    
+  def get_new_cores(self):
     # TODO
-    solr_cores = []    
-    
+    solr_cores = []
+
     return solr_cores
 
-  def add_new_collection(self, attrs):    
+  def add_new_collection(self, attrs):
     if attrs['type'] == 'collection':
       collections = self.get_new_collections()
       collection = collections[attrs['name']]
-      hue_collection, created = Collection.objects.get_or_create(
-          name=attrs['name'],
-#          label=attrs['name'],
-#          cores=json.dumps(collection)
-      )
-      print hue_collection
-      print 'aa'
-      hue_collection.label = attrs['name']
-      hue_collection.cores = json.dumps(collection)
-      hue_collection.save()
+
+      hue_collection, created = Collection.objects.get_or_create(name=attrs['name'], solr_properties=collection, is_enabled=True)
       return hue_collection
+    else:
+      raise PopupException(_('Collection type does not exit: %s') % attrs)

+ 3 - 3
apps/search/src/search/templates/admin.mako

@@ -22,18 +22,18 @@
 <%namespace name="macros" file="macros.mako" />
 <%namespace name="actionbar" file="actionbar.mako" />
 
-${ commonheader(_('Search'), "search", user) | n,unicode }
+${ commonheader(_('Manage collections'), "search", user) | n,unicode }
 
 <link rel="stylesheet" href="/search/static/css/admin.css">
 
 <div class="container-fluid">
 
-  <h1>${_('Search Admin - Cores')}</h1>
+  <h1>${_('Search Admin - Collections')}</h1>
   <%actionbar:render>
     <%def name="search()">
       <input type="text" placeholder="${_('Filter collections by name...')}" class="input-xxlarge search-query" id="filterInput">
     </%def>
-    
+
   </%actionbar:render>
   <div class="row-fluid">
     <div class="span12">

+ 5 - 5
apps/search/src/search/templates/admin_collections_wizard.mako

@@ -44,7 +44,7 @@ ${ commonheader(_('Search'), "search", user) | n,unicode }
     </div>
   </div>
   % endif
-  
+
   % if cores:
   <h1>${_('Import a new core')}</h1>
 
@@ -58,17 +58,17 @@ ${ commonheader(_('Search'), "search", user) | n,unicode }
       % endfor
       </ul>
     </div>
-  </div>  
+  </div>
   % endif
-  
+
   % if not collections and not cores:
   <h1>${_('No available indexes')}</h1>
 
   <div class="row-fluid">
     ${ _('Already installed all the collections. You can change the indexes URL in hue.ini.') }
-  </div>      
+  </div>
   % endif
-  
+
 </div>
 
 <style type="text/css">

+ 11 - 8
apps/search/src/search/templates/search.mako

@@ -31,9 +31,9 @@ ${ commonheader(_('Search'), "search", user, "40px") | n,unicode }
 <div class="search-bar">
   % if user.is_superuser:
     <div class="pull-right" style="margin-top: 4px">
+      <a class="change-settings" href="#"><i class="icon-edit"></i> ${ _('Customize collection display') }</a>
       <a href="${ url('search:admin_collections') }"><i class="icon-edit"></i> ${ _('Collection manager') }</a>
       <a href="${ url('search:admin_collections_wizard') }"><i class="icon-edit"></i> ${ _('Add collection') }</a>
-      <a class="change-settings" href="#"><i class="icon-edit"></i> ${ _('Customize result display') }</a>
     </div>
   % endif
   <form class="form-search" style="margin: 0">
@@ -207,8 +207,9 @@ ${ commonheader(_('Search'), "search", user, "40px") | n,unicode }
   % endif
 </div>
 
-
-${ hue_collection.result.get_extracode() | n,unicode }
+% if hue_collection:
+  ${ hue_collection.result.get_extracode() | n,unicode }
+% endif
 
 <script>
   $(document).ready(function () {
@@ -240,7 +241,7 @@ ${ hue_collection.result.get_extracode() | n,unicode }
     });
     $(".dateFacetHeader").after(orderedDateFacets);
 
-    $(".current-collection").text("${ current_collection }");
+    $(".current-collection").text("${ hue_collection.label }");
     % if user.is_superuser:
         $(".dropdown-collection").each(function () {
           if ($(this).data("value") == $("select[name='collection']").val()) {
@@ -251,12 +252,12 @@ ${ hue_collection.result.get_extracode() | n,unicode }
 
     $(".dropdown-collection").click(function (e) {
       e.preventDefault();
-      $(".current-collection").text($(this).text());
-      $("select[name='collection']").val($(this).data("value"));
+      var solrName = $(this).data("value");
+      $("select[name='collection']").val(solrName);
       % if user.is_superuser:
           $(".change-settings").attr("href", $(this).data("settings-url"));
       % endif
-      $.cookie("hueSearchLastCollection", $(this).text(), {expires: 90});
+      $.cookie("hueSearchLastCollection", solrName, {expires: 90});
       $("form").submit();
     });
 
@@ -266,7 +267,7 @@ ${ hue_collection.result.get_extracode() | n,unicode }
     });
     $("#recordsPerPage").val($("input[name='rows']").val());
 
-    var sortingData = ${ hue_collection.sorting.data | n,unicode };
+    var sortingData = ${ hue_collection and hue_collection.sorting.data or '[]' | n,unicode };
     if (sortingData && sortingData.fields && sortingData.fields.length > 0) {
       $.each(sortingData.fields, function (index, item) {
         $("<option>").attr("value", item.label).text(item.label).data("field", item.field).data("asc", item.asc).appendTo($(".sort-by"));
@@ -321,6 +322,7 @@ ${ hue_collection.result.get_extracode() | n,unicode }
       $("#id_query").addClass("deletable");
     }
 
+    % if hue_collection:
     $("#id_query").on("keyup", function() {
       var query = $("#id_query").val();
       if ($.trim(query) != "") {
@@ -338,6 +340,7 @@ ${ hue_collection.result.get_extracode() | n,unicode }
         $("#id_query").removeClass("deletable");
       }
     });
+    % endif
 
   });
 </script>

+ 18 - 27
apps/search/src/search/views.py

@@ -40,7 +40,7 @@ from search.search_controler import SearchController
 LOG = logging.getLogger(__name__)
 
 
-def index(request):  
+def index(request):
   hue_collections = Collection.objects.all()
 
   if not hue_collections:
@@ -48,14 +48,12 @@ def index(request):
       return admin_collections_wizard(request)
     else:
       raise PopupException(_('No collections! If user message, if admin send to wizard.'))
-#    collections = SolrApi(SOLR_URL.get()).collections()
-#    for collection in collections['status']:
-#      Collection.objects.get_or_create(name=collection)
 
   search_form = QueryForm(request.GET)
   response = {}
   error = {}
   solr_query = {}
+  hue_collection = None
 
   if search_form.is_valid():
     collection = search_form.cleaned_data['collection']
@@ -71,24 +69,23 @@ def index(request):
     solr_query['facets'] = search_form.cleaned_data['facets'] or 1
 
     try:
-      hue_collection = Collection.objects.get_or_create(name=collection)
+      hue_collection = Collection.objects.get(name=collection)
       response = SolrApi(SOLR_URL.get()).query(solr_query, hue_collection)
     except Exception, e:
       error['message'] = unicode(str(e), "utf8")
-  elif hue_collections:    
+  else:
     hue_collection = hue_collections[0]
     collection = hue_collections.name
-  else:
-    #collection = request.COOKIES.get('hueSearchLastCollection', collections['status'].keys()[0])
-    #hue_collection = Collection.objects.get_or_create(name=collection)
-    raise PopupException(_('Please configure hue.ini to point to a Solr URL.'))
+
+  if hue_collection is not None:
+    response = augment_solr_response(response, hue_collection.facets.get_data())
 
   if request.GET.get('format') == 'json':
-    return HttpResponse(json.dumps(augment_solr_response(response, hue_collection.facets.get_data())), mimetype="application/json")
+    return HttpResponse(json.dumps(response), mimetype="application/json")
 
   return render('search.mako', request, {
     'search_form': search_form,
-    'response': augment_solr_response(response, hue_collection.facets.get_data()),
+    'response': response,
     'error': error,
     'solr_query': solr_query,
     'hue_collection': hue_collection,
@@ -105,47 +102,41 @@ def admin_collections(request):
   return render('admin_collections.mako', request, {
     'hue_collections': hue_collections,
   })
-  
-  
+
+
 @allow_admin_only
 def admin_collections_wizard(request):
   searcher = SearchController()
-    
-  if request.method == 'POST':  
+
+  if request.method == 'POST':
     result = {'status': -1, 'message': 'Error'}
-    try:      
+    try:
       searcher.add_new_collection(request.POST.copy())
       result['status'] = 0
       request.info(_('Collection added!'))
     except Exception, e:
       result['message'] = unicode(str(e), "utf8")
-    return HttpResponse(json.dumps(result), mimetype="application/json")    
+    return HttpResponse(json.dumps(result), mimetype="application/json")
   else:
     collections = searcher.get_new_collections()
     cores = searcher.get_new_cores()
     return render('admin_collections_wizard.mako', request, {
       'collections': collections,
-      'cores': cores, 
-    })  
-  
+      'cores': cores,
+    })
+
 
 @allow_admin_only
 def admin(request):
-  # To cross check both
-  collections = SolrApi(SOLR_URL.get()).collections()
   hue_collections = Collection.objects.all()
 
   return render('admin.mako', request, {
-    'collections': collections,
     'hue_collections': hue_collections,
   })
 
 
 @allow_admin_only
 def admin_collection_properties(request, collection):
-  # TODO HACK !!
-  collection = 'collection3_shard2_replica1'
-  
   solr_collection = SolrApi(SOLR_URL.get()).collection(collection)
   hue_collection = Collection.objects.get(name=collection)