]> git.parisson.com Git - telemeta.git/commitdiff
add MediaCollection index and Collections display in haystack search results
authorafilsaime <shadow_kungfu@hotmail.fr>
Thu, 23 Apr 2015 08:27:04 +0000 (10:27 +0200)
committerafilsaime <shadow_kungfu@hotmail.fr>
Thu, 23 Apr 2015 08:27:04 +0000 (10:27 +0200)
telemeta/forms/haystack_form.py
telemeta/haystack_urls.py
telemeta/search_indexes.py
telemeta/templates/search/mediaitem_listhaystack.html
telemeta/templates/search/search.html
telemeta/views/haystack_search.py

index cf1f5e59b5bac5380d2a943bae7d0fa70185424a..8a59095044bc00826ffaab7216a47b4297339800 100644 (file)
@@ -1,10 +1,11 @@
 # -*- coding: utf-8 -*-
 from django import forms
+from telemeta.models import *
 from haystack.inputs import AutoQuery, Exact, Clean
-from haystack.forms import SearchForm
+from haystack.forms import *
 from haystack.query import SearchQuerySet
 
-class HaySearchForm(SearchForm):
+class HaySearchFormItem(SearchForm):
 
     def search(self):
         sqs=SearchQuerySet().load_all()
@@ -13,7 +14,20 @@ class HaySearchForm(SearchForm):
             return sqs
 
         if self.cleaned_data['q']:
-            sqs=sqs.filter(content__contains=self.cleaned_data['q'])
+            sqs=sqs.models(MediaItem).filter(content__contains=self.cleaned_data['q'])
+
+        return sqs
+
+class HaySearchFormCollection(SearchForm):
+
+    def search(self):
+        sqs=SearchQuerySet().load_all()
+
+        if not self.is_valid():
+            return sqs
+
+        if self.cleaned_data['q']:
+            sqs=sqs.models(MediaCollection).filter(content__contains=self.cleaned_data['q'])
 
         return sqs
 
index 684abda79e35554a97f71fe266eb07f43c96f061..51374276549b39b89b8b0fc3dcc0dedc432677b1 100644 (file)
@@ -3,9 +3,12 @@ from django.conf.urls import patterns, url, include
 from telemeta.views.haystack_search import *
 from haystack.query import SearchQuerySet
 from haystack.views import SearchView
-from telemeta.forms.haystack_form import HaySearchForm
+from haystack.forms import *
+from telemeta.forms.haystack_form import HaySearchFormItem, HaySearchFormCollection
 
 
 urlpatterns = patterns('',
-    url(r'^$', SearchView(form_class=HaySearchForm), name='haystack_search'),
+    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'),
 )
index f73988bb15063838b72aac55f24e0de61d0cd640..9738d5522ed405d303d803596b5ba456cd995057 100644 (file)
@@ -11,3 +11,10 @@ class MediaItemIndex(indexes.SearchIndex, indexes.Indexable):
         return MediaItem
 
 
+class MediaCollectionIndex(indexes.SearchIndex, indexes.Indexable):
+
+    text = indexes.NgramField(document=True, use_template=True)
+    #rec_date = indexes.DateTimeField(use_template=True, null=True)
+
+    def get_model(self):
+        return MediaCollection
\ No newline at end of file
index da50f0f62258a1c4bb2fc445c31740e0e3bac857..8d516e3a5c041a2bccbeb60e1e4ca378c4561475 100644 (file)
@@ -24,7 +24,7 @@
         <a href="{% url "telemeta-item-detail" result.object.public_id %}">{{ result.object.title }}</a>
     </td>
     <td align="center">
-        {% if result.file %}
+        {% if result.object.file %}
          <a href="{% url "telemeta-item-detail" result.object.public_id %}">
           <center><span class="glyphicon glyphicon-volume-up" style="font-size: 1.3em;"></span></center>
         {% endif %}
index f3d1cc59e0c60578806879bdbcfdf007f40b59f4..dbec1e69f6c8f4ea9ce5f6f258a5d300fbaa956f 100644 (file)
 
         {% if query %}
             <h1>{% trans "Results" %}</h1>
-           <div class="fullpage">
-               {% include "search/mediaitem_listhaystack.html" %}
-           </div>
-
-            {% if page.has_previous or page.has_next %}
-                <div>
-                    {% if page.has_previous %}<a href="?q={{ query }}&amp;page={{ page.previous_page_number }}">{% endif %}&laquo; Previous{% if page.has_previous %}</a>{% endif %}
-                    |
-                    {% if page.has_next %}<a href="?q={{ query }}&amp;page={{ page.next_page_number }}">{% endif %}Next &raquo;{% if page.has_next %}</a>{% endif %}
+            {% 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>
+             {% 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>
+                {% endifequal %}
+             {% endifequal %}
+             {% with object_list as items %}
+                <div class="fullpage">
+                    {% include "search/mediaitem_listhaystack.html" %}
                 </div>
-            {% endif %}
+
+                {% if page.has_previous or page.has_next %}
+                    <div>
+                        {% if page.has_previous %}<a href="?q={{ query }}&amp;page={{ page.previous_page_number }}">{% endif %}&laquo; Previous{% if page.has_previous %}</a>{% endif %}
+                        |
+                        {% if page.has_next %}<a href="?q={{ query }}&amp;page={{ page.next_page_number }}">{% endif %}Next &raquo;{% if page.has_next %}</a>{% endif %}
+                    </div>
+                {% endif %}
+            {% endwith %}
         {% else %}
             {# Show some example queries to run, maybe query syntax, something else? #}
         {% endif %}
index ab0def17aa76e3c46c44bfcab4716990b677491a..cfa3c50f7ca78477e0889addba4a65defc594a22 100644 (file)
@@ -2,13 +2,30 @@
 
 from haystack.views import SearchView
 from haystack.query import SearchQuerySet
+from telemeta.models import *
+from telemeta.forms.haystack_form import HaySearchFormItem, HaySearchFormCollection
 
-class HaystackSearch(SearchView):
+class HaystackSearchItem(SearchView):
 
     def get_query(self):
-        query=super(HaystackSearch, self).get_query()
-        return query
+        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
 
 
+class HaystackSearchCollection(SearchView):
 
+    def get_query(self):
+        return super(HaystackSearchCollection, self).get_query()
+
+    def extra_context(self):
+        extra = super(HaystackSearchCollection, 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'
+        return extra