Quellcode durchsuchen

HUE-1327 [search] disallow changing of collection index to non-existing index

- Validate collection or core
- Change collection type to core or collection based on
  whether collection or core is provided
Abraham Elmahrek vor 12 Jahren
Ursprung
Commit
891fbea

+ 9 - 0
apps/search/src/search/forms.py

@@ -17,7 +17,9 @@
 
 
 from django import forms
+from django.utils.translation import ugettext as _
 from search.models import Collection
+from search_controller import SearchController
 
 
 class QueryForm(forms.Form):
@@ -63,3 +65,10 @@ class CollectionForm(forms.ModelForm):
   class Meta:
     model = Collection
     exclude = ('facets', 'result', 'sorting', 'properties', 'cores')
+
+  def clean_name(self):
+    searcher = SearchController()
+    name = self.cleaned_data['name']
+    if not searcher.is_collection(name) and not searcher.is_core(name):
+      raise forms.ValidationError(_('No live Solr collection or core by the name %s') % name)
+    return name

+ 7 - 1
apps/search/src/search/search_controller.py

@@ -119,4 +119,10 @@ class SearchController(object):
     except Exception, e:
       LOG.warn('Error copying collection: %s' % e)
 
-    return id
+  def is_collection(self, collection_name):
+    solr_collections = SolrApi(SOLR_URL.get()).collections()
+    return collection_name in solr_collections
+
+  def is_core(self, core_name):
+    solr_cores = SolrApi(SOLR_URL.get()).cores()
+    return core_name in solr_cores

+ 4 - 1
apps/search/src/search/views.py

@@ -201,7 +201,10 @@ def admin_collection_properties(request, collection_id):
   if request.method == 'POST':
     collection_form = CollectionForm(request.POST, instance=hue_collection)
     if collection_form.is_valid():
-      hue_collection = collection_form.save()
+      searcher = SearchController()
+      hue_collection = collection_form.save(commit=False)
+      hue_collection.is_core_only = not searcher.is_collection(hue_collection.name)
+      hue_collection.save()
       return redirect(reverse('search:admin_collection_properties', kwargs={'collection_id': hue_collection.id}))
     else:
       request.error(_('Errors on the form: %s') % collection_form.errors)