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<type>[A-Za-z0-9._-]+)/$', HaystackSearch(), name='haystack_search_type'),
+
)
{% if query %}
<h1>{% trans "Results" %}</h1>
{% ifequal type 'item' %}
- <p><b>Items ({{item_count}}) | <a href="{% url "haystack_search_collection" %}?q={{ query }}&page=1">Collections ({{collection_count}})</a></b></p>
+ <p><b>Items ({{item_count}}) | <a href="{% url "haystack_search_type" "collection" %}?q={{ query }}&page=1">Collections ({{collection_count}})</a></b></p>
{% else %}
{% ifequal type 'collection'%}
- <p><b><a href="{% url "haystack_search_item" %}?q={{ query }}&page=1">Items ({{item_count}}) </a>| Collections ({{collection_count}})</b></p>
+ <p><b><a href="{% url "haystack_search_type" "item" %}?q={{ query }}&page=1">Items ({{item_count}}) </a>| Collections ({{collection_count}})</b></p>
{% endifequal %}
{% endifequal %}
{% with object_list as items %}
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
+