]> git.parisson.com Git - telemeta.git/commitdiff
Make search view a CBV
authorGuillaume Pellerin <yomguy@parisson.com>
Thu, 13 Nov 2014 00:44:24 +0000 (01:44 +0100)
committerGuillaume Pellerin <yomguy@parisson.com>
Thu, 13 Nov 2014 00:44:24 +0000 (01:44 +0100)
telemeta/templates/telemeta/base.html
telemeta/templates/telemeta/search_criteria.html
telemeta/templates/telemeta/search_results.html
telemeta/urls.py
telemeta/views/core.py
telemeta/views/home.py

index e8bb9628a68a4a5052f8355f19373f87e9ff9f98..c498ae67bc5e448912b46e02ce0e4627e415df72 100644 (file)
 
 <div id="quick_search">
 <form action="{% url "telemeta-search" %}" id="_quickSearchForm" method="get">
-
     <div class="input-group">
-      <input type="text" class="form-control">
+      <input type="text" class="form-control" name="pattern">
       <span class="input-group-btn">
-        <button class="btn btn-default" type="button" onclick="document.getElementById('_quickSearchForm').submit();"><span class="glyphicon glyphicon-search"></span></button>
+        <button class="btn btn-default" type="button" onclick="$('_quickSearchForm').submit();"><span class="glyphicon glyphicon-search"></span></button>
       </span>
-    </div><!-- /input-group -->
-
+    </div>
 </form>
-
 </div>
 
 <div id="menu">
index 3477c00475110782ccb8c6d1212706290176e20b..89db2d6fa509c67117c5f5c4f380b4ac86dee6eb 100644 (file)
@@ -64,7 +64,7 @@ $(document).ready(function() {
 {% endblock %}
 
 {% block content %}
-<form action="{% url "telemeta-search" %}" id="searchform">{% csrf_token %}
+<form action="{% url "telemeta-search" %}" id="searchform" method="get">
 <fieldset>
 
     <p>
index e2e662ef2cbac7eedb812ad128dfc5addec469f7..fa0fa270ff132e257d4376d010d071a103ef97db 100644 (file)
@@ -1,21 +1,30 @@
 {% extends "telemeta/base.html" %}
 {% load telemeta_utils %}
 {% load i18n %}
+{% load bootstrap_pagination %}
 
 {% block head_title %}{% trans "Search Results" %} - {{ block.super }}{% endblock %}
 
 {% block title %}
- <img src="{{ STATIC_URL }}telemeta/images/search_red.png" alt="search-results" style="vertical-align:middle" /> {% trans "Search Results" %}
+<div class='row'>
+<div class="col-md-5">
+  <img src="{{ STATIC_URL }}telemeta/images/search_red.png" alt="search-results" style="vertical-align:middle" /> {% trans "Search Results" %} ({{ page_obj.start_index }} - {{ page_obj.end_index }} / {{ object_list.count }})
+</div>
+<div class="col-md-7">
+{% bootstrap_paginate page_obj range=10 show_first_last="True" %}
+</div>
+<div class="col-md-0">&nbsp;</div>
+</div>
 {% endblock %}
 
 {% block title_buttons %}
     {% ifequal type 'items' %}
-     <a href="{% url "telemeta-search-items" %}?{{criteria|with_no_sound|build_query_string}}" class="component_icon button icon_filter">{% trans "All" %}</a>
-     <a href="{% url "telemeta-search-items" %}?{{criteria|with_sound|build_query_string}}" class="component_icon button icon_filter">{% trans "Sounds" %}</a>
+     <a href="{% url "telemeta-search-type" "items" %}?{{criteria|with_no_sound|build_query_string}}" class="component_icon button icon_filter">{% trans "All" %}</a>
+     <a href="{% url "telemeta-search-type" "items" %}?{{criteria|with_sound|build_query_string}}" class="component_icon button icon_filter">{% trans "Sounds" %}</a>
     {% endifequal %}
     {% ifequal type 'collections' %}
-     <a href="{% url "telemeta-search-collections" %}?{{criteria|with_no_sound|build_query_string}}" class="component_icon button icon_filter">{% trans "All" %}</a>
-     <a href="{% url "telemeta-search-collections" %}?{{criteria|with_sound|build_query_string}}" class="component_icon button icon_filter">{% trans "Sounds" %}</a>
+     <a href="{% url "telemeta-search-type" "collections" %}?{{criteria|with_no_sound|build_query_string}}" class="component_icon button icon_filter">{% trans "All" %}</a>
+     <a href="{% url "telemeta-search-type" "collections" %}?{{criteria|with_sound|build_query_string}}" class="component_icon button icon_filter">{% trans "Sounds" %}</a>
     {% endifequal %}
 {% endblock %}
 
@@ -64,7 +73,7 @@
 {% endif %}
 
 {% ifequal type 'items' %}
-<p><b>Items ({{items_num}}) | <a href="{% url "telemeta-search-collections" %}?{{criteria|build_query_string}}">Collections ({{collections_num}})</a> | <a href="{% url "telemeta-search-corpus" %}?{{criteria|build_query_string}}">Corpus ({{corpus_num}})</a> | <a href="{% url "telemeta-search-fonds" %}?{{criteria|build_query_string}}">Fonds ({{fonds_num}})</a></b></p>
+<p><b>Items ({{items_num}}) | <a href="{% url "telemeta-search-type" "collections" %}?{{criteria|build_query_string}}">Collections ({{collections_num}})</a> | <a href="{% url "telemeta-search-type" "corpus" %}?{{criteria|build_query_string}}">Corpus ({{corpus_num}})</a> | <a href="{% url "telemeta-search-type" "fonds" %}?{{criteria|build_query_string}}">Fonds ({{fonds_num}})</a></b></p>
   {% with object_list as items %}
    <div class="fullpage">
       {% include "telemeta/inc/mediaitem_list.html" %}
@@ -73,7 +82,7 @@
 {% endifequal %}
 
 {% ifequal type 'collections' %}
-<p><b><a href="{% url "telemeta-search-items" %}?{{criteria|build_query_string}}">Items ({{items_num}})</a> | Collections ({{collections_num}}) | <a href="{% url "telemeta-search-corpus" %}?{{criteria|build_query_string}}">Corpus ({{corpus_num}})</a> | <a href="{% url "telemeta-search-fonds" %}?{{criteria|build_query_string}}">Fonds ({{fonds_num}})</a>
+<p><b><a href="{% url "telemeta-search-type" "items" %}?{{criteria|build_query_string}}">Items ({{items_num}})</a> | Collections ({{collections_num}}) | <a href="{% url "telemeta-search-type" "corpus" %}?{{criteria|build_query_string}}">Corpus ({{corpus_num}})</a> | <a href="{% url "telemeta-search-type" "fonds" %}?{{criteria|build_query_string}}">Fonds ({{fonds_num}})</a>
   </b></p>
   {% with object_list as collections %}
    <div class="fullpage">
 {% endifequal %}
 
 {% if type == 'corpus' %}
-<p><b><a href="{% url "telemeta-search-items" %}?{{criteria|build_query_string}}">Items ({{items_num}})</a> | <a href="{% url "telemeta-search-collections" %}?{{criteria|build_query_string}}">Collections ({{collections_num}})</a> | Corpus ({{corpus_num}}) | <a href="{% url "telemeta-search-fonds" %}?{{criteria|build_query_string}}">Fonds ({{fonds_num}})</a>
+<p><b><a href="{% url "telemeta-search-type" "items" %}?{{criteria|build_query_string}}">Items ({{items_num}})</a> | <a href="{% url "telemeta-search-type" "collections" %}?{{criteria|build_query_string}}">Collections ({{collections_num}})</a> | Corpus ({{corpus_num}}) | <a href="{% url "telemeta-search-type" "fonds" %}?{{criteria|build_query_string}}">Fonds ({{fonds_num}})</a>
 </b></p>
 {% endif %}
 
 {% if type == 'fonds' %}
-<p><b><a href="{% url "telemeta-search-items" %}?{{criteria|build_query_string}}">Items ({{items_num}})</a> | <a href="{% url "telemeta-search-collections" %}?{{criteria|build_query_string}}">Collections ({{collections_num}})</a> | <a href="{% url "telemeta-search-corpus" %}?{{criteria|build_query_string}}">Corpus ({{corpus_num}})</a> | Fonds ({{fonds_num}})
+<p><b><a href="{% url "telemeta-search-type" "items" %}?{{criteria|build_query_string}}">Items ({{items_num}})</a> | <a href="{% url "telemeta-search-type" "collections" %}?{{criteria|build_query_string}}">Collections ({{collections_num}})</a> | <a href="{% url "telemeta-search-type" "corpus" %}?{{criteria|build_query_string}}">Corpus ({{corpus_num}})</a> | Fonds ({{fonds_num}})
 </b></p>
 {% endif %}
 
index 60a524769bbd7ddb2f10be13d1b6405e47cf626d..08e4e642d54ce7be30c5cfaa3e3074ae1cc66cc6 100644 (file)
@@ -139,13 +139,13 @@ urlpatterns = patterns('',
 
     # search
     # url(r'^archives/$', home_view.search, name="telemeta-archives"),
-    url(r'^search/$', home_view.search, name="telemeta-search"),
-    url(r'^search/collections/$', home_view.search, {'type': 'collections'}, name="telemeta-search-collections"),
-    url(r'^search/items/$', home_view.search, {'type': 'items'}, name="telemeta-search-items"),
-    url(r'^search/corpus/$', home_view.search, {'type': 'corpus'}, name="telemeta-search-corpus"),
-    url(r'^search/fonds/$', home_view.search, {'type': 'fonds'}, name="telemeta-search-fonds"),
+    url(r'^search/$', SearchView.as_view(), name="telemeta-search"),
+    url(r'^search/(?P<type>[A-Za-z0-9._-]+)/$', SearchView.as_view(), name="telemeta-search-type"),
+    url(r'^search/items/$', home_view.search, {'type': 'items'}, name="telemeta-search-items"),
+    url(r'^search/corpus/$', home_view.search, {'type': 'corpus'}, name="telemeta-search-corpus"),
+    url(r'^search/fonds/$', home_view.search, {'type': 'fonds'}, name="telemeta-search-fonds"),
 
-    url(r'^search/criteria/$', home_view.edit_search, name="telemeta-search-criteria"),
+    url(r'^search_criteria/$', home_view.edit_search, name="telemeta-search-criteria"),
     url(r'^complete_location/$', home_view.complete_location, name="telemeta-complete-location"),
 
     # administration
index cc17efd3639daed20649b269fd4af4f467d3f105..f5c5eae3ebcec85607b0dc8311eb2b4cad206928 100644 (file)
@@ -75,7 +75,6 @@ from django.utils.translation import ugettext_lazy as _
 from django.forms.models import model_to_dict
 from django.views.generic.edit import DeletionMixin, BaseDeleteView
 
-
 from telemeta.models import *
 import telemeta.models
 import telemeta.interop.oai as oai
index e427217ab6448ae35b1f7ec14733ac9b060bead6..2a63cbfae77f672e43bc70c020c611bfe4dca039 100644 (file)
@@ -278,3 +278,140 @@ class HomeView(object):
         users = User.objects.all().order_by('last_name')
         return render(request, 'telemeta/users.html', {'users': users})
 
+
+class SearchView(ListView):
+    """Perform a search through resources"""
+
+    template_name='telemeta/search_results.html'
+    paginate_by = 20
+
+    def get_queryset(self):
+        input = self.request.GET
+        self.criteria = {}
+
+        self.type = None
+        if 'type' in self.kwargs:
+            self.type = self.kwargs['type']
+
+        self.collections = MediaCollection.objects.enriched()
+        self.items = MediaItem.objects.enriched()
+        self.corpus = MediaCorpus.objects.all()
+        self.fonds  = MediaFonds.objects.all()
+
+        switch = {
+            'pattern': lambda value: (
+                self.collections.quick_search(value),
+                self.items.quick_search(value),
+                self.corpus.quick_search(value),
+                self.fonds.quick_search(value),
+                ),
+            'title': lambda value: (
+                self.collections.word_search('title', value),
+                self.items.by_title(value),
+                self.corpus.word_search('title', value),
+                self.fonds.word_search('title', value)),
+            'location': lambda value: (
+                self.collections.by_location(Location.objects.get(name=value)),
+                self.items.by_location(Location.objects.get(name=value))),
+            'continent': lambda value: (
+                self.collections.by_continent(value),
+                self.items.filter(continent = value)),
+            'ethnic_group': lambda value: (
+                self.collections.by_ethnic_group(value),
+                self.items.filter(ethnic_group = value),
+                EthnicGroup.objects.get(pk=value)),
+            'creator': lambda value: (
+                self.collections.word_search('creator', value),
+                self.items.word_search('collection__creator', value)),
+            'collector': lambda value: (
+                self.collections.by_fuzzy_collector(value),
+                self.items.by_fuzzy_collector(value)),
+            'rec_year_from': lambda value: (
+                self.collections.by_recording_year(int(value), int(input.get('rec_year_to', value))),
+                self.items.by_recording_date(datetime.date(int(value), 1, 1),
+                                        datetime.date(int(input.get('rec_year_to', value)), 12, 31))),
+            'rec_year_to': lambda value: (self.collections, self.items),
+            'pub_year_from': lambda value: (
+                self.collections.by_publish_year(int(value), int(input.get('pub_year_to', value))),
+                self.items.by_publish_year(int(value), int(input.get('pub_year_to', value)))),
+            'pub_year_to': lambda value: (self.collections, self.items),
+            'sound': lambda value: (
+                self.collections.sound(),
+                self.items.sound()),
+            'instrument': lambda value: (
+                self.collections.by_instrument(value),
+                self.items.by_instrument(value)),
+        }
+
+        for key, value in input.items():
+            func = switch.get(key)
+            if func and value and value != "0":
+                try:
+                    res = func(value)
+                    if len(res)  > 4:
+                        self.collections, self.items, self.corpus, self.fonds, value = res
+                    elif len(res) == 4:
+                        self.collections, self.items, self.corpus, self.fonds = res
+                    elif len(res) == 3:
+                        self.collections, self.items, value = res
+                        self.corpus = self.corpus.none()
+                        self.fonds = self.fonds.none()
+                    else:
+                        self.collections, self.items = res
+                        self.corpus = self.corpus.none()
+                        self.fonds = self.fonds.none()
+
+                except ObjectDoesNotExist:
+                    self.collections = self.collections.none()
+                    self.items = self.items.none()
+                    self.corpus = self.corpus.none()
+                    self.fonds = self.fonds.none()
+
+                self.criteria[key] = value
+
+        # Save the search
+        user = self.request.user
+        if user:
+            if user.is_authenticated():
+                search = Search(username=user)
+                search.save()
+                if self.criteria:
+                    for key in self.criteria.keys():
+                        value = self.criteria[key]
+                        if key == 'ethnic_group':
+                            try:
+                                group = EthnicGroup.objects.get(value=value)
+                                value = group.id
+                            except:
+                                value = ''
+                        criter = Criteria(key=key, value=value)
+                        criter.save()
+                        search.criteria.add(criter)
+                    search.save()
+
+        if self.type is None:
+            if self.collections.count():
+                self.type = 'collections'
+            else:
+                self.type = 'items'
+
+        if self.type == 'items':
+            objects = self.items
+        elif self.type == 'collections':
+            objects = self.collections
+        elif self.type == 'corpus':
+            objects = self.corpus
+        elif self.type == 'fonds':
+            objects = self.fonds
+
+        return objects
+
+    def get_context_data(self, *args, **kwargs):
+        context = super(SearchView, self).get_context_data(*args, **kwargs)
+        context['criteria'] = self.criteria
+        context['collections_num'] =  self.collections.count()
+        context['items_num'] = self.items.count()
+        context['corpus_num']  = self.corpus.count()
+        context['fonds_num'] = self.fonds.count()
+        context['type'] = self.type
+        return context