]> git.parisson.com Git - telemeta.git/commitdiff
the haystack engine now use a single view instead of 1 view per archive type
authorafilsaime <shadow_kungfu@hotmail.fr>
Thu, 23 Apr 2015 09:46:15 +0000 (11:46 +0200)
committerafilsaime <shadow_kungfu@hotmail.fr>
Thu, 23 Apr 2015 09:46:15 +0000 (11:46 +0200)
telemeta/haystack_urls.py
telemeta/templates/search/search.html
telemeta/views/haystack_search.py

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