]> git.parisson.com Git - telemeta.git/commitdiff
improved search; search results are now paginated, displaying either items or collect...
authorolivier <>
Fri, 7 Mar 2008 12:39:29 +0000 (12:39 +0000)
committerolivier <>
Fri, 7 Mar 2008 12:39:29 +0000 (12:39 +0000)
telemeta/templates/inc/collection_list.html
telemeta/templates/inc/mediaitem_list.html
telemeta/templates/search_results.html
telemeta/templatetags/telemeta_utils.py
telemeta/urls.py
telemeta/web/base.py

index 64a5cc3f187454b10bc3cb5722548436248a4d3c..699456fb3b48a2cd430082128d612a02c06ebc54 100644 (file)
@@ -1,9 +1,10 @@
+{% load telemeta_utils %}
 {% if collections %}
     {% if hits %}
     <p>Records {{ first_on_page }} to {{ last_on_page }} on {{ hits }} 
     &nbsp;&nbsp;
-    {% if has_previous %} <a href="?page={{ previous }}">&lt;&lt;</a> {% endif %}
-    {% if has_next %} <a href="?page={{ next }}">&gt;&gt;</a> {% endif %}
+    {% if has_previous %} <a href="?page={{ previous }}&amp;{{criteria|build_query_string}}">&lt;&lt;</a> {% endif %}
+    {% if has_next %} <a href="?page={{ next }}&amp;{{criteria|build_query_string}}">&gt;&gt;</a> {% endif %}
     </p>
     {% endif %}
     <ul>
index 699ba3c9bd73767ba2eff75b549b611470be1dea..fcb9dd419386b95feb316390f662c09f6bb5fe7f 100644 (file)
@@ -1,9 +1,10 @@
+{% load telemeta_utils %}
 {% if items %}
     {% if hits %}
     <p>Records {{ first_on_page }} to {{ last_on_page }} on {{ hits }} 
     &nbsp;&nbsp;
-    {% if has_previous %} <a href="?page={{ previous }}">&lt;&lt;</a> {% endif %}
-    {% if has_next %} <a href="?page={{ next }}">&gt;&gt;</a> {% endif %}
+    {% if has_previous %} <a href="?page={{ previous }}&amp;{{criteria|build_query_string}}">&lt;&lt;</a> {% endif %}
+    {% if has_next %} <a href="?page={{ next }}&amp;{{criteria|build_query_string}}">&gt;&gt;</a> {% endif %}
     </p>
     {% endif %}
     <ul>
index 45bbe1824d94617e8c68c8a315058a6763aeafd2..405f6dc4e3f03e01f13b0e004fff4c609583336e 100644 (file)
@@ -1,16 +1,61 @@
 {% extends "base.html" %}
+{% load telemeta_utils %}
 
 {% block content %}
-<h3>Search: {{ pattern }}</h3>
-{% if collections %}
-    <h4>Collections</h4>
-    {% include "inc/collection_list.html" %}
-{% endif %}
-{% if items %}
-    <h4>Items</h4>
-    {% include "inc/mediaitem_list.html" %}
+<h3>Search results</h3>
+
+{% if criteria %}
+<ul>
+  {% if criteria.pattern %}
+    <li><b>Pattern:</b> {{criteria.pattern}}</li>
+  {% endif %}
+  {% if criteria.continent %}
+    <li><b>Continent:</b> {{criteria.continent}}</li>
+  {% endif %}
+  {% if criteria.country %}
+    <li><b>Country:</b> {{criteria.country}}</li>
+  {% endif %}
+  {% if criteria.ethnic_group %}
+    <li><b>Ethnic group:</b> {{criteria.ethnic_group}}</li>
+  {% endif %}
+  {% if criteria.creator %}
+    <li><b>Creator:</b> {{criteria.creator}}</li>
+  {% endif %}
+  {% if criteria.title %}
+    <li><b>Title:</b> {{criteria.title}}</li>
+  {% endif %}
+  {% if criteria.rec_date %}
+    <li><b>Recording date:</b> {{criteria.rec_date}}</li>
+  {% endif %}
+  {% if criteria.pub_date %}
+    <li><b>Publishing date:</b> {{criteria.pub_date}}</li>
+  {% endif %}
+</ul>
 {% endif %}
-{% if not collections and not items %}
-No result
+
+{% ifequal type 'items' %}
+
+  <p><b>Items ({{items_num}})</b> | 
+  <a href="{% url telemeta-search-collections %}?{{criteria|build_query_string}}">
+    Collections ({{collections_num}})</a></p>
+
+  {% with object_list as items %}
+      {% include "inc/mediaitem_list.html" %}
+  {% endwith %}    
+
+{% else %}
+
+  <p><a href="{% url telemeta-search-items %}?{{criteria|build_query_string}}">
+    Items ({{items_num}})</a> |
+  <b>Collections ({{collections_num}})</b></p>
+
+  {% with object_list as collections %}
+      {% include "inc/collection_list.html" %}
+  {% endwith %}
+
+{% endifequal %}
+
+{% if not object_list %}
+No record
 {% endif %}
 {% endblock %}
index 95a4a78d37a2b029ff52b9f79501ec83af054f9e..21cf430e0e8497fa94280f636255e7e4e99bc5fc 100644 (file)
@@ -54,4 +54,15 @@ def escapejs(value):
         value = value.replace(bad, good)
     return value
 
+@register.filter
+def build_query_string(dict):
+    """Build an HTTP query string out of a dict"""
+    import urllib
+    args = []
+    for k, v in dict.iteritems():
+        if not isinstance(v, basestring):
+            v = str(v)
+        args.append(urllib.quote(k) + '=' + urllib.quote(v))
+
+    return "&amp;".join(args)
 
index 4da2f93809133dcca46e423ac2c2371f4400cc3b..e1f9dd3dec3e0b03da13299f9ef9560b2e7ab755 100644 (file)
@@ -82,6 +82,10 @@ urlpatterns = patterns('',
 
     # search
     url(r'^search/$', web_view.search, name="telemeta-search"),
+    url(r'^search/collections/$', web_view.search, {'type': 'collections'}, 
+        name="telemeta-search-collections"),
+    url(r'^search/items/$', web_view.search, {'type': 'items'}, 
+        name="telemeta-search-items"),
     url(r'^search/criteria/$', web_view.edit_search, name="telemeta-search-criteria"),
 
     # administration        
index bef9c344191bf816b71bb3f75ac17f1eeb7bd5b4..d6645a02f02866597478b09ec026d4ad8ea1cd5c 100644 (file)
@@ -109,47 +109,54 @@ class WebView(Component):
             {'continents': continents, 'countries': countries, 
             'ethnic_groups': ethnic_groups})
 
-    def search(self, request):
+    def search(self, request, type = 'items'):
         """Perform a search through collections and items metadata"""
         collections = MediaCollection.objects
         items = MediaItem.objects
         input = request.REQUEST
+        criteria = {}
+
+        switch = {
+            'pattern': lambda value: ( 
+                collections.quick_search(value), 
+                items.quick_search(value)),
+            'country': lambda value: (
+                collections.by_country(value), 
+                items.filter(etat = value)),
+            'continent': lambda value: (
+                collections.by_continent(value), 
+                items.filter(continent = value)),
+            'ethnic_group': lambda value: (
+                collections.none(), 
+                items.filter(ethnie_grsocial = value)),
+            'creator': lambda value: (
+                collections.filter(creator__icontains=value),
+                items.filter(auteur__icontains=value)),
+            'rec_date': lambda value: (
+                collections.by_recording_date(value), 
+                items.by_recording_date(value)),
+            'pud_date': lambda value: (
+                collections.by_publish_date(value), 
+                items.none())
+        }
+        
+        for key, value in input.items():
+            if key == 'continent' and input.get('country'):
+                continue
+            func = switch.get(key)
+            if func and value:
+                collections, items = func(value)
+                criteria[key] = value
+
+        if type == 'items':
+            objects = items
+        else:
+            objects = collections
 
-        if input.has_key('pattern') and input['pattern']:
-            collections = collections.quick_search(input['pattern'])
-            items = items.quick_search(input['pattern'])
-
-        if input.has_key('country') and input['country']:
-            collections = collections.by_country(input['country'])
-            items = items.filter(etat = input['country'])
-        elif input.has_key('continent') and input['continent']:
-            collections = collections.by_continent(input['continent'])
-            items = items.filter(continent = input['continent'])
-      
-        if (input.has_key('ethnic_group') and input['ethnic_group']):
-            collections = collections.none()
-            items = items.filter(ethnie_grsocial = input['ethnic_group'])
-
-        if (input.has_key('creator') and input['creator']):
-            collections = collections.filter(creator__icontains=input['creator'])
-            items = items.filter(auteur__icontains=input['creator'])
-
-        if (input.has_key('creator') and input['creator']):
-            collections = collections.filter(creator__icontains=input['creator'])
-            items = items.filter(auteur__icontains=input['creator'])
-
-        if (input.has_key('rec_date') and input['rec_date']):
-            collections = collections.by_recording_date(input['rec_date'])
-            items = items.by_recording_date(input['rec_date'])
-
-        if (input.has_key('pub_date') and input['pub_date']):
-            collections = collections.by_publish_date(input['pub_date'])
-            items = items.none()
-
-        return render_to_response('search_results.html', 
-                    {'criteria': input, 
-                    'collections': collections[:50], 'more_collections': collections.count() > 50,
-                     'items': items[:50], 'more_items': items.count() > 50})
+        return list_detail.object_list(request, objects, 
+            template_name='search_results.html', paginate_by=20,
+            extra_context={'criteria': criteria, 'collections_num': collections.count(), 
+                'items_num': items.count(), 'type' : type})
 
     def __get_enumerations_list(self):
         from django.db.models import get_models