X-Git-Url: http://git.ithinksw.org/philo.git/blobdiff_plain/943e8bc4af0c11b0ace3811199e3b0844c4c3fbc..700615d15881697c10a77ca65c84a84a5dd1e4d6:/philo/contrib/sobol/models.py diff --git a/philo/contrib/sobol/models.py b/philo/contrib/sobol/models.py index dbf37ef..cd773a5 100644 --- a/philo/contrib/sobol/models.py +++ b/philo/contrib/sobol/models.py @@ -1,4 +1,5 @@ import datetime +import itertools from django.conf import settings from django.conf.urls.defaults import patterns, url @@ -12,7 +13,7 @@ from django.utils.datastructures import SortedDict from philo.contrib.sobol import registry from philo.contrib.sobol.forms import SearchForm -from philo.contrib.sobol.utils import HASH_REDIRECT_GET_KEY, URL_REDIRECT_GET_KEY, SEARCH_ARG_GET_KEY, check_redirect_hash +from philo.contrib.sobol.utils import HASH_REDIRECT_GET_KEY, URL_REDIRECT_GET_KEY, SEARCH_ARG_GET_KEY, check_redirect_hash, RegistryIterator from philo.exceptions import ViewCanNotProvideSubpath from philo.models import MultiView, Page from philo.models.fields import SlugMultipleChoiceField @@ -133,9 +134,29 @@ class Click(models.Model): get_latest_by = 'datetime' +class RegistryChoiceField(SlugMultipleChoiceField): + def _get_choices(self): + if isinstance(self._choices, RegistryIterator): + return self._choices.copy() + elif hasattr(self._choices, 'next'): + choices, self._choices = itertools.tee(self._choices) + return choices + else: + return self._choices + choices = property(_get_choices) + + +try: + from south.modelsinspector import add_introspection_rules +except ImportError: + pass +else: + add_introspection_rules([], ["^philo\.contrib\.shipherd\.models\.RegistryChoiceField"]) + + class SearchView(MultiView): results_page = models.ForeignKey(Page, related_name='search_results_related') - searches = SlugMultipleChoiceField(choices=registry.iterchoices()) + searches = RegistryChoiceField(choices=registry.iterchoices()) enable_ajax_api = models.BooleanField("Enable AJAX API", default=True, help_text="Search results will be available only by AJAX, not as template variables.") placeholder_text = models.CharField(max_length=75, default="Search") @@ -204,7 +225,7 @@ class SearchView(MultiView): }) else: context.update({ - 'searches': [{'verbose_name': verbose_name, 'slug': slug, 'url': self.reverse('ajax_api_view', kwargs={'slug': slug}, node=request.node), 'result_template': registry[slug].result_template} for slug, verbose_name in registry.iterchoices() if slug in self.searches] + 'searches': [{'verbose_name': verbose_name, 'slug': slug, 'url': "%s?%s=%s" % (self.reverse('ajax_api_view', kwargs={'slug': slug}, node=request.node), SEARCH_ARG_GET_KEY, search_string), 'result_template': registry[slug].result_template} for slug, verbose_name in registry.iterchoices() if slug in self.searches] }) else: form = SearchForm() @@ -224,7 +245,8 @@ class SearchView(MultiView): raise Http404 search_instance = self.get_search_instance(slug, search_string) - response = HttpResponse(json.dumps({ + + return HttpResponse(json.dumps({ 'results': [result.get_context() for result in search_instance.results], - })) - return response \ No newline at end of file + 'rendered': [result.render() for result in search_instance.results] + })) \ No newline at end of file