From dc6cd10101e4341c755b9b8315c40e4be5b0426e Mon Sep 17 00:00:00 2001 From: afilsaime Date: Thu, 23 Apr 2015 11:46:15 +0200 Subject: [PATCH] the haystack engine now use a single view instead of 1 view per archive type --- telemeta/haystack_urls.py | 6 +++--- telemeta/templates/search/search.html | 4 ++-- telemeta/views/haystack_search.py | 29 ++++++++++++++------------- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/telemeta/haystack_urls.py b/telemeta/haystack_urls.py index 51374276..76147a4e 100644 --- a/telemeta/haystack_urls.py +++ b/telemeta/haystack_urls.py @@ -8,7 +8,7 @@ from telemeta.forms.haystack_form import HaySearchFormItem, HaySearchFormCollect urlpatterns = patterns('', - url(r'^$', HaystackSearchItem(form_class=HaySearchFormItem), name='haystack_search_item'), - url(r'^item/$', HaystackSearchItem(form_class=HaySearchFormItem), name='haystack_search_item'), - url(r'^collection/$', HaystackSearchCollection(form_class=HaySearchFormCollection), name='haystack_search_collection'), + url(r'^$', HaystackSearch(form_class=HaySearchFormItem), name='haystack_search'), + url(r'^(?P[A-Za-z0-9._-]+)/$', HaystackSearch(), name='haystack_search_type'), + ) diff --git a/telemeta/templates/search/search.html b/telemeta/templates/search/search.html index dbec1e69..ff2cb576 100644 --- a/telemeta/templates/search/search.html +++ b/telemeta/templates/search/search.html @@ -23,10 +23,10 @@ {% if query %}

{% trans "Results" %}

{% ifequal type 'item' %} -

Items ({{item_count}}) | Collections ({{collection_count}})

+

Items ({{item_count}}) | Collections ({{collection_count}})

{% else %} {% ifequal type 'collection'%} -

Items ({{item_count}}) | Collections ({{collection_count}})

+

Items ({{item_count}}) | Collections ({{collection_count}})

{% endifequal %} {% endifequal %} {% with object_list as items %} diff --git a/telemeta/views/haystack_search.py b/telemeta/views/haystack_search.py index cfa3c50f..eda86b2d 100644 --- a/telemeta/views/haystack_search.py +++ b/telemeta/views/haystack_search.py @@ -5,27 +5,28 @@ from haystack.query import SearchQuerySet from telemeta.models import * from telemeta.forms.haystack_form import HaySearchFormItem, HaySearchFormCollection -class HaystackSearchItem(SearchView): +class HaystackSearch(SearchView): - def get_query(self): - return super(HaystackSearchItem, self).get_query() - - def extra_context(self): - extra = super(HaystackSearchItem, self).extra_context() - extra['collection_count']=SearchQuerySet().load_all().models(MediaCollection).filter(content__contains=self.get_query()).count() - extra['item_count']=SearchQuerySet().load_all().models(MediaItem).filter(content__contains=self.get_query()).count() - extra['type']='item' - return extra + def __call__(self,request,type=None): + self.type = type + if(self.type=='collection'): + self.form_class=HaySearchFormCollection + else: + self.form_class=HaySearchFormItem + return super(HaystackSearch,self).__call__(request) -class HaystackSearchCollection(SearchView): def get_query(self): - return super(HaystackSearchCollection, self).get_query() + return super(HaystackSearch, self).get_query() def extra_context(self): - extra = super(HaystackSearchCollection, self).extra_context() + extra = super(HaystackSearch, self).extra_context() extra['collection_count']=SearchQuerySet().load_all().models(MediaCollection).filter(content__contains=self.get_query()).count() extra['item_count']=SearchQuerySet().load_all().models(MediaItem).filter(content__contains=self.get_query()).count() - extra['type']='collection' + if self.type=='collection': + extra['type']='collection' + else: + extra['type']='item' return extra + -- 2.39.5