From 4c18b61fad6628589a6bd1a64895868ce3409c94 Mon Sep 17 00:00:00 2001 From: Stephen Burrows Date: Thu, 9 Jun 2011 13:16:56 -0400 Subject: [PATCH] Added docs for result list template. Moved success/error handling into hooks on sobol. Added slug to JSON response dict. Added script suppression option to _list.html. --- philo/contrib/sobol/__init__.py | 7 ++++ philo/contrib/sobol/models.py | 1 + philo/contrib/sobol/search.py | 4 +- .../contrib/sobol/static/sobol/ajax_search.js | 41 +++++++++++-------- .../sobol/templates/sobol/search/_list.html | 26 +++++++----- 5 files changed, 50 insertions(+), 29 deletions(-) diff --git a/philo/contrib/sobol/__init__.py b/philo/contrib/sobol/__init__.py index 74ca4f1..0458a83 100644 --- a/philo/contrib/sobol/__init__.py +++ b/philo/contrib/sobol/__init__.py @@ -10,6 +10,13 @@ Settings :setting:`SOBOL_USE_EVENTLET` If :mod:`eventlet` is installed and this setting is ``True``, sobol web searches will use :mod:`eventlet.green.urllib2` instead of the built-in :mod:`urllib2` module. Default: ``False``. +Templates +--------- + +For convenience, :mod:`.sobol` provides a template at ``sobol/search/_list.html`` which can be used with an ``{% include %}`` tag inside a full search page template to list the search results. The ``_list.html`` template also uses a basic jQuery script (``static/sobol/ajax_search.js``) to handle AJAX search result loading if the AJAX API of the current :class:`.SearchView` is enabled. If you want to use ``_list.html``, but want to provide your own version of jQuery or your own AJAX loading script, or if you want to include the basic script somewhere else (like inside the ````) simply do the following:: + + {% include "sobol/search/_list.html" with suppress_scripts=1 %} + """ from philo.contrib.sobol.search import * \ No newline at end of file diff --git a/philo/contrib/sobol/models.py b/philo/contrib/sobol/models.py index 27f91d1..1bef3cd 100644 --- a/philo/contrib/sobol/models.py +++ b/philo/contrib/sobol/models.py @@ -287,6 +287,7 @@ class SearchView(MultiView): search_instance = get_search_instance(slug, search_string) return HttpResponse(json.dumps({ + 'search': search_instance.slug, 'results': [result.render() for result in search_instance.results], 'hasMoreResults': search_instance.has_more_results, 'moreResultsURL': (u"?%s" % search_instance.more_results_querydict.urlencode()) if search_instance.more_results_querydict else None, diff --git a/philo/contrib/sobol/search.py b/philo/contrib/sobol/search.py index b117eaa..2c31158 100644 --- a/philo/contrib/sobol/search.py +++ b/philo/contrib/sobol/search.py @@ -116,7 +116,9 @@ def get_search_instance(slug, search_arg): cached = cache.get(key) if cached: return cached - return search(search_arg) + instance = search(search_arg) + instance.slug = slug + return instance diff --git a/philo/contrib/sobol/static/sobol/ajax_search.js b/philo/contrib/sobol/static/sobol/ajax_search.js index d4885b6..33fdf4f 100644 --- a/philo/contrib/sobol/static/sobol/ajax_search.js +++ b/philo/contrib/sobol/static/sobol/ajax_search.js @@ -1,33 +1,40 @@ (function($){ - var sobol = window.sobol = {} - sobol.setup = function(){ + var sobol = window.sobol = {}; + sobol.search = function(){ var searches = sobol.searches = $('article.search'); - for (i=0;i"; - if(data['hasMoreResults'] && data['moreResultsURL']) s.innerHTML += ""; - } else { - $(s).addClass('empty') - s.innerHTML += "

No results found.

" - } + sobol.onSuccess($(s), data); }, error: function(data, textStatus, errorThrown){ - $(s).removeClass('loading'); - text = errorThrown ? errorThrown : textStatus ? textStatus : "Error occurred." - if (errorThrown) { - s.innerHTML += "

" + errorThrown + "

" - }; + sobol.onError($(s), textStatus, errorThrown); } }); }()); }; + } + sobol.onSuccess = function(ele, data){ + // hook for success! + ele.removeClass('loading') + if (data['results'].length) { + ele[0].innerHTML += "
" + data['results'].join("") + "
"; + if(data['hasMoreResults'] && data['moreResultsURL']) ele[0].innerHTML += ""; + } else { + ele.addClass('empty'); + ele[0].innerHTML += "

No results found.

"; + ele.slideUp(); + } }; - $(sobol.setup); + sobol.onError = function(ele, textStatus, errorThrown){ + // Hook for error... + ele.removeClass('loading'); + text = errorThrown ? errorThrown : textStatus ? textStatus : "Error occurred."; + ele[0].innerHTML += "

" + text + "

"; + }; + $(sobol.search); }(jQuery)); \ No newline at end of file diff --git a/philo/contrib/sobol/templates/sobol/search/_list.html b/philo/contrib/sobol/templates/sobol/search/_list.html index f78d861..8fed939 100644 --- a/philo/contrib/sobol/templates/sobol/search/_list.html +++ b/philo/contrib/sobol/templates/sobol/search/_list.html @@ -1,21 +1,25 @@ {% with node.view.enable_ajax_api as ajax %} -{% if ajax %}{% endif %} +{% if ajax and not suppress_scripts %}{% endif %} {% for search in searches %} -
+

{{ search }}

{% if not ajax %} -
- {% for result in search.results %} - {{ result }} - {% endfor %} -
- {% if search.has_more_results and search.more_results_url %} - + {% if search.results %} +
+ {% for result in search.results %} + {{ result }} + {% endfor %} +
+ {% if search.has_more_results and search.more_results_url %} + + {% endif %} + {% else %} +

No results found.

{% endif %} {% endif %}
-- 2.20.1