Просмотр исходного кода

HUE-1347 [search] Not remembering collection after a trip to the customize collection page

Romain Rigaux 12 лет назад
Родитель
Сommit
4606538246

+ 5 - 7
apps/search/src/search/forms.py

@@ -23,7 +23,7 @@ from search_controller import SearchController
 
 
 class QueryForm(forms.Form):
-  collection = forms.ChoiceField() # collection_id
+  collection = forms.ChoiceField() # Aka collection_id
 
   query = forms.CharField(label='', max_length=256, required=False, initial='',
                           widget=forms.TextInput(attrs={'class': 'search-query input-xxlarge', 'placeholder': 'Search...'}))
@@ -34,19 +34,17 @@ class QueryForm(forms.Form):
   facets = forms.CharField(label='', required=False, initial='', widget=forms.HiddenInput(), help_text='Show hide facet search')
 
   def __init__(self, *args, **kwargs):
+    self.initial_collection = kwargs.pop('initial_collection')
     super(QueryForm, self).__init__(*args, **kwargs)
     choices = [(core.id, core.label) for core in Collection.objects.filter(enabled=True)]
-    initial_choice = self._initial_core(choices)
-    self.fields['collection'] = forms.ChoiceField(choices=choices, initial=initial_choice, required=False, label='', widget=forms.Select(attrs={'class':'hide'}))
+    # Beware: initial not working, set in the js
+    self.fields['collection'] = forms.ChoiceField(choices=choices, initial=self.initial_collection, required=False, label='', widget=forms.Select(attrs={'class':'hide'}))
 
   def clean_collection(self):
     if self.cleaned_data.get('collection'):
       return self.cleaned_data['collection']
     else:
-      return self._initial_core(self.fields['collection'].choices)
-
-  def _initial_core(self, choices):
-    return choices and choices[0][0] or None
+      return self.initial_collection
 
 
 class HighlightingForm(forms.Form):

+ 1 - 0
apps/search/src/search/templates/search.mako

@@ -265,6 +265,7 @@ ${ commonheader(_('Search'), "search", user, "40px") | n,unicode }
 
     % if hue_collection:
       $(".current-collection").text("${ hue_collection.label }");
+      $("#id_collection").val(${ hue_collection.id });
 
       % if user.is_superuser:
         var collectionUrl = $(".dropdown-collection[data-value=${ hue_collection.id }]").data("settings-url");

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

@@ -49,7 +49,8 @@ def index(request):
     else:
       return no_collections(request)
 
-  search_form = QueryForm(request.GET)
+  initial_collection = request.COOKIES.get('hueSearchLastCollection', 0)
+  search_form = QueryForm(request.GET, initial_collection=initial_collection)
   response = {}
   error = {}
   solr_query = {}
@@ -57,10 +58,6 @@ def index(request):
 
   if search_form.is_valid():
     collection_id = search_form.cleaned_data['collection']
-    if request.GET.get('collection') is None:
-      cookie_collection_id = request.COOKIES.get('hueSearchLastCollection', collection_id)
-      if hue_collections.filter(id=cookie_collection_id).exists():
-        collection_id = cookie_collection_id
     solr_query['q'] = search_form.cleaned_data['query']
     solr_query['fq'] = search_form.cleaned_data['fq']
     if search_form.cleaned_data['sort']:
@@ -76,8 +73,7 @@ def index(request):
     except Exception, e:
       error['message'] = unicode(str(e), "utf8")
   else:
-    hue_collection = hue_collections[0]
-    collection_id = hue_collection.id
+    error['message'] = _('There is no collection to search.')
 
   if hue_collection is not None:
     response = augment_solr_response(response, hue_collection.facets.get_data())
@@ -96,6 +92,7 @@ def index(request):
     'json': json,
   })
 
+
 def no_collections(request):
   return render('no_collections.mako', request, {})