From: Stephen Burrows Date: Wed, 8 Jun 2011 21:31:27 +0000 (-0400) Subject: Removed json version of results from the ajax API. Improved checks for search existen... X-Git-Tag: philo-0.9~5^2~4 X-Git-Url: http://git.ithinksw.org/philo.git/commitdiff_plain/4fd0861aa9d51262d5afcdc40ea48e7ce7ec7aaa Removed json version of results from the ajax API. Improved checks for search existence. Added default search list template and a static js file for the ajax api. --- diff --git a/philo/contrib/sobol/models.py b/philo/contrib/sobol/models.py index 43b78b4..27f91d1 100644 --- a/philo/contrib/sobol/models.py +++ b/philo/contrib/sobol/models.py @@ -241,11 +241,12 @@ class SearchView(MultiView): search_instances = [] for slug in self.searches: - search_instance = get_search_instance(slug, search_string) - search_instances.append(search_instance) + if slug in registry: + search_instance = get_search_instance(slug, search_string) + search_instances.append(search_instance) - if self.enable_ajax_api: - search_instance.ajax_api_url = "%s?%s=%s" % (self.reverse('ajax_api_view', kwargs={'slug': slug}, node=request.node), SEARCH_ARG_GET_KEY, search_string) + if self.enable_ajax_api: + search_instance.ajax_api_url = "%s?%s=%s" % (self.reverse('ajax_api_view', kwargs={'slug': slug}, node=request.node), SEARCH_ARG_GET_KEY, search_string) if eventlet and not self.enable_ajax_api: pool = eventlet.GreenPool() @@ -280,14 +281,13 @@ class SearchView(MultiView): """ search_string = request.GET.get(SEARCH_ARG_GET_KEY) - if not request.is_ajax() or not self.enable_ajax_api or slug not in self.searches or search_string is None: + if not request.is_ajax() or not self.enable_ajax_api or slug not in registry or slug not in self.searches or search_string is None: raise Http404 search_instance = get_search_instance(slug, search_string) return HttpResponse(json.dumps({ - 'results': [result.get_context() for result in search_instance.results], - 'rendered': [result.render() for result in search_instance.results], - 'hasMoreResults': search.has_more_results(), - 'moreResultsURL': (u"?%s" % search.more_results_querydict.urlencode()) if search.more_results_querydict else None, + '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, }), mimetype="application/json") \ No newline at end of file diff --git a/philo/contrib/sobol/search.py b/philo/contrib/sobol/search.py index 5cc8090..b117eaa 100644 --- a/philo/contrib/sobol/search.py +++ b/philo/contrib/sobol/search.py @@ -187,7 +187,7 @@ class BaseSearchMetaclass(type): if 'verbose_name' not in attrs: attrs['verbose_name'] = capfirst(' '.join(convert_camelcase(name).rsplit(' ', 1)[:-1])) if 'slug' not in attrs: - attrs['slug'] = name.lower() + attrs['slug'] = name[:-6].lower() if name.endswith("Search") else name.lower() return super(BaseSearchMetaclass, cls).__new__(cls, name, bases, attrs) @@ -199,8 +199,8 @@ class BaseSearch(object): """ __metaclass__ = BaseSearchMetaclass - #: The number of results to return from the complete list. Default: 10 - result_limit = 10 + #: The number of results to return from the complete list. Default: 5 + result_limit = 5 #: How long the items for the search should be cached (in minutes). Default: 48 hours. _cache_timeout = 60*48 #: The path to the template which will be used to render the :class:`Result`\ s for this search. @@ -272,19 +272,23 @@ class BaseSearch(object): """Returns any extra context to be used when rendering the ``result``.""" return {} + @property def has_more_results(self): """Returns ``True`` if there are more results than :attr:`result_limit` and ``False`` otherwise.""" return len(self.results) > self.result_limit @property def more_results_url(self): - """Returns the actual url for more results. This should be accessed through :attr:`more_results_querydict` in the template so that the click can be tracked.""" - raise NotImplementedError + """Returns the actual url for more results. This should be accessed through :attr:`more_results_querydict` in the template so that the click can be tracked. By default, simply returns ``None``.""" + return None @property def more_results_querydict(self): """Returns a :class:`QueryDict` for tracking whether people click on a 'more results' link.""" - return make_tracking_querydict(self.search_arg, self.more_results_url) + url = self.more_results_url + if url: + return make_tracking_querydict(self.search_arg, url) + return None def __unicode__(self): return self.verbose_name @@ -344,6 +348,7 @@ class GoogleSearch(JSONSearch): _cache_timeout = 60 verbose_name = "Google search (current site)" result_template = "sobol/search/googlesearch.html" + _more_results_url = None @property def query_format_str(self): diff --git a/philo/contrib/sobol/static/sobol/ajax_search.js b/philo/contrib/sobol/static/sobol/ajax_search.js new file mode 100644 index 0000000..d4885b6 --- /dev/null +++ b/philo/contrib/sobol/static/sobol/ajax_search.js @@ -0,0 +1,33 @@ +(function($){ + var sobol = window.sobol = {} + sobol.setup = 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.

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

" + errorThrown + "

" + }; + } + }); + }()); + }; + }; + $(sobol.setup); +}(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 new file mode 100644 index 0000000..f78d861 --- /dev/null +++ b/philo/contrib/sobol/templates/sobol/search/_list.html @@ -0,0 +1,23 @@ +{% with node.view.enable_ajax_api as ajax %} +{% if ajax %}{% 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 %} + + {% endif %} + {% endif %} +
+{% endfor %} +{% endwith %} \ No newline at end of file diff --git a/philo/contrib/sobol/templates/sobol/search/basesearch.html b/philo/contrib/sobol/templates/sobol/search/basesearch.html index 9469143..e7661ac 100644 --- a/philo/contrib/sobol/templates/sobol/search/basesearch.html +++ b/philo/contrib/sobol/templates/sobol/search/basesearch.html @@ -1 +1 @@ -{% if url %}{% endif %}{{ title }}{% if url %}{% endif %} \ No newline at end of file +
{% if url %}{% endif %}{{ title }}{% if url %}{% endif %}
\ No newline at end of file diff --git a/philo/contrib/sobol/templates/sobol/search/googlesearch.html b/philo/contrib/sobol/templates/sobol/search/googlesearch.html index 1b22388..8d23132 100644 --- a/philo/contrib/sobol/templates/sobol/search/googlesearch.html +++ b/philo/contrib/sobol/templates/sobol/search/googlesearch.html @@ -1,4 +1,2 @@ - \ No newline at end of file +
{{ title|safe }}
+
{{ result.content|safe }}
\ No newline at end of file