From dbc5a8910f77980c1475cb1bfd3f9702718e4b1c Mon Sep 17 00:00:00 2001 From: olivier <> Date: Fri, 7 Mar 2008 12:39:29 +0000 Subject: [PATCH] improved search; search results are now paginated, displaying either items or collections, with a tab view --- telemeta/templates/inc/collection_list.html | 5 +- telemeta/templates/inc/mediaitem_list.html | 5 +- telemeta/templates/search_results.html | 65 ++++++++++++++--- telemeta/templatetags/telemeta_utils.py | 11 +++ telemeta/urls.py | 4 ++ telemeta/web/base.py | 79 +++++++++++---------- 6 files changed, 119 insertions(+), 50 deletions(-) diff --git a/telemeta/templates/inc/collection_list.html b/telemeta/templates/inc/collection_list.html index 64a5cc3f..699456fb 100644 --- a/telemeta/templates/inc/collection_list.html +++ b/telemeta/templates/inc/collection_list.html @@ -1,9 +1,10 @@ +{% load telemeta_utils %} {% if collections %} {% if hits %}
Records {{ first_on_page }} to {{ last_on_page }} on {{ hits }} - {% if has_previous %} << {% endif %} - {% if has_next %} >> {% endif %} + {% if has_previous %} << {% endif %} + {% if has_next %} >> {% endif %}
{% endif %}Records {{ first_on_page }} to {{ last_on_page }} on {{ hits }} - {% if has_previous %} << {% endif %} - {% if has_next %} >> {% endif %} + {% if has_previous %} << {% endif %} + {% if has_next %} >> {% endif %}
{% endif %}Items ({{items_num}}) | + + Collections ({{collections_num}})
+ + {% with object_list as items %} + {% include "inc/mediaitem_list.html" %} + {% endwith %} + +{% else %} + ++ Items ({{items_num}}) | + Collections ({{collections_num}})
+ + {% with object_list as collections %} + {% include "inc/collection_list.html" %} + {% endwith %} + +{% endifequal %} + +{% if not object_list %} +No record {% endif %} {% endblock %} diff --git a/telemeta/templatetags/telemeta_utils.py b/telemeta/templatetags/telemeta_utils.py index 95a4a78d..21cf430e 100644 --- a/telemeta/templatetags/telemeta_utils.py +++ b/telemeta/templatetags/telemeta_utils.py @@ -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 "&".join(args) diff --git a/telemeta/urls.py b/telemeta/urls.py index 4da2f938..e1f9dd3d 100644 --- a/telemeta/urls.py +++ b/telemeta/urls.py @@ -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 diff --git a/telemeta/web/base.py b/telemeta/web/base.py index bef9c344..d6645a02 100644 --- a/telemeta/web/base.py +++ b/telemeta/web/base.py @@ -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 -- 2.39.5