Merge branch 'master' of git://github.com/melinath/philo
[philo.git] / contrib / sobol / models.py
index e5852d7..ee8187d 100644 (file)
@@ -2,7 +2,7 @@ from django.conf.urls.defaults import patterns, url
 from django.contrib import messages
 from django.core.exceptions import ValidationError
 from django.db import models
-from django.http import HttpResponseRedirect, Http404
+from django.http import HttpResponseRedirect, Http404, HttpResponse
 from django.utils import simplejson as json
 from django.utils.datastructures import SortedDict
 from philo.contrib.sobol import registry
@@ -130,13 +130,13 @@ class Click(models.Model):
 class SearchView(MultiView):
        results_page = models.ForeignKey(Page, related_name='search_results_related')
        searches = SlugMultipleChoiceField(choices=registry.iterchoices())
-       enable_ajax_api = models.BooleanField("Enable AJAX API", default=True)
+       enable_ajax_api = models.BooleanField("Enable AJAX API", default=True, help_text="Search results will be available <i>only</i> by AJAX, not as template variables.")
        placeholder_text = models.CharField(max_length=75, default="Search")
        
        search_form = SearchForm
        
        def __unicode__(self):
-               return u"%s (%s)" % (self.placeholder_text, u", ".join([display for slug, display in registry.iterchoices()]))
+               return u"%s (%s)" % (self.placeholder_text, u", ".join([display for slug, display in registry.iterchoices() if slug in self.searches]))
        
        def get_reverse_params(self, obj):
                raise ViewCanNotProvideSubpath
@@ -148,7 +148,7 @@ class SearchView(MultiView):
                )
                if self.enable_ajax_api:
                        urlpatterns += patterns('',
-                               url(r'^(?P<slug>[\w-]+)', self.ajax_api_view, name='ajax_api_view')
+                               url(r'^(?P<slug>[\w-]+)$', self.ajax_api_view, name='ajax_api_view')
                        )
                return urlpatterns
        
@@ -196,6 +196,10 @@ class SearchView(MultiView):
                                        context.update({
                                                'searches': search_instances
                                        })
+                               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]
+                                       })
                else:
                        form = SearchForm()
                
@@ -214,8 +218,7 @@ class SearchView(MultiView):
                        raise Http404
                
                search_instance = self.get_search_instance(slug, search_string)
-               response = json.dumps({
-                       'results': search_instance.results,
-                       'template': search_instance.get_template()
-               })
+               response = HttpResponse(json.dumps({
+                       'results': [result.get_context() for result in search_instance.results],
+               }))
                return response
\ No newline at end of file