mirror of
https://github.com/Hopiu/django-watson.git
synced 2026-04-28 08:54:44 +00:00
Added in support for empty search redirect.
This commit is contained in:
parent
7694d38879
commit
f07ce3080f
2 changed files with 58 additions and 5 deletions
|
|
@ -5,6 +5,7 @@ from unittest import skipIf, skipUnless
|
|||
from django.db import models
|
||||
from django.test import TestCase
|
||||
from django.core.management import call_command
|
||||
from django.conf.urls.defaults import *
|
||||
|
||||
import watson
|
||||
from watson.registration import RegistrationError, get_backend
|
||||
|
|
@ -381,12 +382,62 @@ class RankingTest(SearchTestBase):
|
|||
def testRankingWithSearch(self):
|
||||
self.assertEqual(
|
||||
[entry.title for entry in watson.search("FOO")],
|
||||
[u"u'title model1 instance11 foo bar foo", u"u'title model1 instance12 foo bar"]
|
||||
[u"title model1 instance11 foo bar foo", u"title model1 instance12 foo bar"]
|
||||
)
|
||||
|
||||
@skipUnless(get_backend().supports_ranking, "search backend does not support ranking")
|
||||
def testRankingWithFilter(self):
|
||||
self.assertEqual(
|
||||
[entry.title for entry in watson.filter(TestModel1, "FOO")],
|
||||
[u"u'title model1 instance11 foo bar foo", u"u'title model1 instance12 foo bar"]
|
||||
)
|
||||
[u"title model1 instance11 foo bar foo", u"title model1 instance12 foo bar"]
|
||||
)
|
||||
|
||||
|
||||
urlpatterns = patterns("watson.views",
|
||||
|
||||
url("^simple/$", "search", name="search_simple"),
|
||||
|
||||
url("^custom/$", "search", name="search_custom", kwargs={
|
||||
"query_param": "foo",
|
||||
"empty_query_redirect": "/simple/",
|
||||
}),
|
||||
|
||||
)
|
||||
|
||||
|
||||
class SiteSearchTest(SearchTestBase):
|
||||
|
||||
urls = "watson.tests"
|
||||
|
||||
def testSiteSearch(self):
|
||||
# Test a search than should find everything.
|
||||
response = self.client.get("/simple/?q=title")
|
||||
self.assertContains(response, "instance11")
|
||||
self.assertContains(response, "instance12")
|
||||
self.assertContains(response, "instance21")
|
||||
self.assertContains(response, "instance22")
|
||||
self.assertTemplateUsed(response, "watson/result_list.html")
|
||||
# Test a search that should find one thing.
|
||||
response = self.client.get("/simple/?q=instance11")
|
||||
self.assertContains(response, "instance11")
|
||||
self.assertNotContains(response, "instance12")
|
||||
self.assertNotContains(response, "instance21")
|
||||
self.assertNotContains(response, "instance22")
|
||||
# Test a search that should find nothing.
|
||||
response = self.client.get("/simple/?q=foo")
|
||||
self.assertNotContains(response, "instance11")
|
||||
self.assertNotContains(response, "instance12")
|
||||
self.assertNotContains(response, "instance21")
|
||||
self.assertNotContains(response, "instance22")
|
||||
|
||||
def testSiteSearchCustom(self):
|
||||
# Test a search than should find everything.
|
||||
response = self.client.get("/custom/?foo=title")
|
||||
self.assertContains(response, "instance11")
|
||||
self.assertContains(response, "instance12")
|
||||
self.assertContains(response, "instance21")
|
||||
self.assertContains(response, "instance22")
|
||||
self.assertTemplateUsed(response, "watson/result_list.html")
|
||||
# Test a search that should find nothing.
|
||||
response = self.client.get("/custom/?q=foo")
|
||||
self.assertRedirects(response, "/simple/")
|
||||
|
|
@ -1,18 +1,20 @@
|
|||
"""Views used by the built-in site search functionality."""
|
||||
|
||||
from django.shortcuts import render
|
||||
from django.shortcuts import render, redirect
|
||||
|
||||
import watson
|
||||
from watson.models import SearchEntry
|
||||
|
||||
|
||||
def search(request, query_param="q", template_name="watson/result_list.html"):
|
||||
def search(request, query_param="q", template_name="watson/result_list.html", empty_query_redirect=None):
|
||||
"""Renders a list of matching search entries."""
|
||||
query = request.GET.get(query_param, u"")
|
||||
# Check for blank queries.
|
||||
if query:
|
||||
search_entries = watson.search(query)
|
||||
else:
|
||||
if empty_query_redirect:
|
||||
return redirect(empty_query_redirect)
|
||||
search_entries = SearchEntry.objects.none()
|
||||
# Render the template.
|
||||
return render(request, template_name, {
|
||||
|
|
|
|||
Loading…
Reference in a new issue