More search fixes.
authorRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Mon, 17 May 2010 10:28:54 +0000 (12:28 +0200)
committerRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Mon, 17 May 2010 10:28:54 +0000 (12:28 +0200)
Added selection from multiple exact matches (#268),
added info about minimum length of a query,
Some minor fixes.

apps/catalogue/urls.py
apps/catalogue/views.py
wolnelektury/templates/catalogue/search_multiple_hits.html [new file with mode: 0644]
wolnelektury/templates/catalogue/search_too_short.html [new file with mode: 0644]
wolnelektury/templates/catalogue/tagged_object_list.html

index ea93cb3..25389f4 100644 (file)
@@ -17,7 +17,6 @@ urlpatterns = patterns('catalogue.views',
     url(r'^polki/nowa/$', 'new_set', name='new_set'),
     url(r'^tags/$', 'tags_starting_with', name='hint'),
     url(r'^szukaj/$', 'search', name='search'),
-    url(r'^nie_ma/(?P<tags>[a-zA-Z0-9-/]*)$', 'search_no_hits', name='search_no_hits'),
 
     # tools
     url(r'^zegar', 'clock', name='clock'),
index 4601117..0a048af 100644 (file)
@@ -22,6 +22,7 @@ from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
 from django.utils import simplejson
 from django.utils.functional import Promise
 from django.utils.encoding import force_unicode
+from django.utils.http import urlquote_plus
 from django.views.decorators import cache
 
 from catalogue import models
@@ -216,6 +217,24 @@ def _tags_starting_with(prefix, user):
     return list(books) + list(tags) + list(book_stubs)
         
 
+
+def _get_result_link(match, tag_list):
+    if isinstance(match, models.Book) or isinstance(match, models.BookStub):
+        return match.get_absolute_url()
+    else:
+        return reverse('catalogue.views.tagged_object_list', 
+            kwargs={'tags': '/'.join(tag.slug for tag in tag_list + [match])}
+        )
+
+def _get_result_type(match):
+    if isinstance(match, models.Book) or isinstance(match, models.BookStub):
+        type = 'book'
+    else:
+        type = match.category
+    return dict(models.TAG_CATEGORIES)[type]
+    
+
+
 def search(request):
     tags = request.GET.get('tags', '')
     prefix = request.GET.get('q', '')
@@ -227,38 +246,26 @@ def search(request):
 
     # Prefix must have at least 2 characters
     if len(prefix) < 2:
-        return HttpResponseRedirect(reverse('catalogue.views.search_no_hits', 
-            kwargs={'tags': '/'.join(tag.slug for tag in tag_list)}
-        ))
+        return render_to_response('catalogue/search_too_short.html', {'tags':tag_list, 'prefix':prefix},
+            context_instance=RequestContext(request))
     
     result = _tags_exact_matches(prefix, request.user)
-    if (not result):
+    
+    if len(result) > 1:
+        # multiple exact matches
+        return render_to_response('catalogue/search_multiple_hits.html', 
+            {'tags':tag_list, 'prefix':prefix, 'results':((x, _get_result_link(x, tag_list), _get_result_type(x)) for x in result)},
+            context_instance=RequestContext(request))
+    
+    if not result:
+        # no exact matches
         result = _tags_starting_with(prefix, request.user)
     
-    if len(result) > 0:
-        tag = result[0]
-        if isinstance(tag, models.Book) or isinstance(tag, models.BookStub):
-            return HttpResponseRedirect(tag.get_absolute_url())
-        else:
-            tag_list.append(tag)
-            
-            return HttpResponseRedirect(reverse('catalogue.views.tagged_object_list', 
-                kwargs={'tags': '/'.join(tag.slug for tag in tag_list)}
-            ))
+    if result:
+        return HttpResponseRedirect(_get_result_link(result[0], tag_list))
     else:
-        return HttpResponseRedirect(reverse('catalogue.views.search_no_hits', 
-            kwargs={'tags': '/'.join(tag.slug for tag in tag_list)}
-        ))
-
-
-def search_no_hits(request, tags):
-    try:
-        tag_list = models.Tag.get_tag_list(tags)
-    except:
-        tag_list = []
-
-    return render_to_response('catalogue/search_no_hits.html', {'tags':tag_list},
-        context_instance=RequestContext(request))
+        return render_to_response('catalogue/search_no_hits.html', {'tags':tag_list, 'prefix':prefix},
+            context_instance=RequestContext(request))
 
 
 def tags_starting_with(request):
@@ -483,7 +490,7 @@ def register(request):
 @cache.never_cache
 def logout_then_redirect(request):
     auth.logout(request)
-    return HttpResponseRedirect(request.GET.get('next', '/'))
+    return HttpResponseRedirect(urlquote_plus(request.GET.get('next', '/'), safe='/?='))
 
 
 
diff --git a/wolnelektury/templates/catalogue/search_multiple_hits.html b/wolnelektury/templates/catalogue/search_multiple_hits.html
new file mode 100644 (file)
index 0000000..cc93d56
--- /dev/null
@@ -0,0 +1,27 @@
+{% extends "base.html" %}
+{% load catalogue_tags pagination_tags %}
+
+{% block title %}Wyszukiwanie w WolneLektury.pl{% endblock %}
+
+{% block bodyid %}tagged-object-list{% endblock %}
+
+{% block body %}
+    <h1>{% title_from_tags tags %}</h1>
+    {% breadcrumbs tags %}
+    
+    <div id="books-list">
+        <p>Znaleziono więcej niż jeden wynik odpowiadający kryteriom wyszukiwania.</p>
+               <ul class='matches'>
+        {% for match, link, type in results %}
+            <li><a href='{{ link }}'>{{ match.name }}</a> ({{ type }})</li>
+        {% endfor %}
+               </ul>
+    </div>
+
+    <div id="set-window">
+        <div class="header"><a href="#" class="jqmClose">Zamknij</a></div>
+        <div class="target">
+            <p><img src="{{ STATIC_URL }}img/indicator.gif" alt="*"/> Ładowanie</p>
+        </div>
+    </div>
+{% endblock %}
\ No newline at end of file
diff --git a/wolnelektury/templates/catalogue/search_too_short.html b/wolnelektury/templates/catalogue/search_too_short.html
new file mode 100644 (file)
index 0000000..db9e585
--- /dev/null
@@ -0,0 +1,23 @@
+{% extends "base.html" %}
+{% load catalogue_tags pagination_tags %}
+
+{% block title %}Wyszukiwanie w WolneLektury.pl{% endblock %}
+
+{% block bodyid %}tagged-object-list{% endblock %}
+
+{% block body %}
+    <h1>{% title_from_tags tags %}</h1>
+    {% breadcrumbs tags %}
+    
+    <div id="books-list">
+        <p>Przepraszamy! Wyszukiwana fraza musi mieć co najmniej dwa znaki.</p>
+        {% include "info/join_us.html" %}
+    </div>
+
+    <div id="set-window">
+        <div class="header"><a href="#" class="jqmClose">Zamknij</a></div>
+        <div class="target">
+            <p><img src="{{ STATIC_URL }}img/indicator.gif" alt="*"/> Ładowanie</p>
+        </div>
+    </div>
+{% endblock %}
\ No newline at end of file
index a051381..91824ed 100644 (file)
@@ -15,7 +15,6 @@
         <h2>Twoja półka jest pusta</h2>
         <p>Możesz wrzucić książkę na półkę, wchodząc na stronę danej lektury i klikając na przycisk „Na półkę!”.</p>
     </div>
-       {% else
     {% else %}
     {% autopaginate object_list 10 %}
     <div id="books-list">