From 277cda97c6c87f7d4ce2826585062acb700969db Mon Sep 17 00:00:00 2001 From: Jeff Balogh Date: Thu, 9 Dec 2010 15:31:09 -0800 Subject: [PATCH] fix search within personas/collections (bug 617038) --- apps/browse/views.py | 19 +++---------------- apps/devhub/templates/devhub/search.html | 12 ++++++------ apps/search/forms.py | 24 ++++++++++++++++++++++-- apps/search/helpers.py | 7 +++++-- apps/search/tests/test_views.py | 24 ++++++++++++++++++++++++ templates/search.html | 15 ++++++++------- 6 files changed, 68 insertions(+), 33 deletions(-) diff --git a/apps/browse/views.py b/apps/browse/views.py index 9007c495d3..04a92d142f 100644 --- a/apps/browse/views.py +++ b/apps/browse/views.py @@ -111,14 +111,10 @@ def language_tools(request, category=None): addons = (Addon.objects.public() .filter(appsupport__app=request.APP.id, type__in=types, target_locale__isnull=False).exclude(target_locale='')) - locales = _get_locales(addons) - - search_cat = '%s,0' % amo.ADDON_DICT - return jingo.render(request, 'browse/language_tools.html', {'locales': locales, 'addons': addons, - 'search_cat': search_cat}) + 'search_cat': '%s,0' % amo.ADDON_DICT}) def themes(request, category=None): @@ -137,16 +133,12 @@ def themes(request, category=None): count = addons.with_index(addons='type_status_inactive_idx').count() themes = amo.utils.paginate(request, addons, count=count) - - # Pre-selected category for search form - search_cat = '%s,0' % amo.ADDON_THEME - return jingo.render(request, 'browse/themes.html', {'categories': categories, 'themes': themes, 'category': category, 'sorting': filter.field, 'sort_opts': filter.opts, - 'search_cat': search_cat}) + 'search_cat': '%s,0' % amo.ADDON_THEME}) def extensions(request, category=None): @@ -166,9 +158,7 @@ def extensions(request, category=None): count = addons.with_index(addons='type_status_inactive_idx').count() addons = amo.utils.paginate(request, addons, count=count) - search_cat = '%s,%s' % (TYPE, category.id if category else 0) - return jingo.render(request, 'browse/extensions.html', {'category': category, 'addons': addons, 'sorting': filter.field, @@ -198,12 +188,9 @@ def category_landing(request, category): .filter(categories__id=category.id)) filter = CategoryLandingFilter(request, base, category, key='browse', default='featured') - - search_cat = '%s,%s' % (category.type, category.id) - return jingo.render(request, 'browse/category_landing.html', {'category': category, 'filter': filter, - 'search_cat': search_cat}) + 'search_cat': '%s,%s' % (category.type, category.id)}) def creatured(request, category): diff --git a/apps/devhub/templates/devhub/search.html b/apps/devhub/templates/devhub/search.html index fb25196bdf..9374de2dc4 100644 --- a/apps/devhub/templates/devhub/search.html +++ b/apps/devhub/templates/devhub/search.html @@ -1,10 +1,10 @@ -{% set search_form = SearchForm(request) %} +{% set search_form = SimpleSearchForm(request, None) %}
- - + {# search-query #} - -
\ No newline at end of file + + diff --git a/apps/search/forms.py b/apps/search/forms.py index baa963a40f..312cab2346 100644 --- a/apps/search/forms.py +++ b/apps/search/forms.py @@ -92,13 +92,33 @@ def get_search_groups(app): return top_level[:1] + sub + top_level[1:], top_level +SEARCH_CHOICES = ( + ('all', _lazy('search for add-ons')), + ('collections', _lazy('search for collections')), + ('personas', _lazy('search for personas'))) + + +class SimpleSearchForm(forms.Form): + """Powers the search box on every page.""" + q = forms.CharField(required=False) + cat = forms.CharField(required=False, widget=forms.HiddenInput) + choices = dict(SEARCH_CHOICES) + + def clean_cat(self): + self.data = dict(self.data.items()) + return self.data.setdefault('cat', 'all') + + def placeholder(self): + val = self.clean_cat() + return self.choices.get(val, self.choices['all']) + + def SearchForm(request): current_app = request.APP or amo.FIREFOX search_groups, top_level = get_search_groups(current_app) - class _SearchForm(forms.Form): - q = forms.CharField(required=False) + class _SearchForm(SimpleSearchForm): cat = forms.ChoiceField(choices=search_groups, required=False) diff --git a/apps/search/helpers.py b/apps/search/helpers.py index 698f3cd0e2..e6fb8e2f80 100644 --- a/apps/search/helpers.py +++ b/apps/search/helpers.py @@ -8,8 +8,11 @@ from . import forms @jingo.register.function -def SearchForm(request): - return forms.SearchForm(request) +def SimpleSearchForm(request, search_cat): + data = request.GET + if search_cat and 'cat' not in request.GET: + data = dict(request.GET, cat=search_cat) + return forms.SimpleSearchForm(data) @jingo.register.function diff --git a/apps/search/tests/test_views.py b/apps/search/tests/test_views.py index 4b6784b345..9953567ac7 100644 --- a/apps/search/tests/test_views.py +++ b/apps/search/tests/test_views.py @@ -309,3 +309,27 @@ class TagTest(SphinxTestCase): r = self.client.get(url, follow=True) new_related_tags = pq(r.content)('#refine-tags li a').text() assert old_related_tags != new_related_tags + + +class TestSearchboxTarget(test_utils.TestCase): + # Check that we search within addons/personas/collections as appropriate. + + def check(self, url, placeholder, cat): + doc = pq(self.client.get(url).content)('.header-search form') + eq_(doc('input[name=q]').attr('placeholder'), placeholder) + eq_(doc('input[name=cat]').val(), cat) + + def test_addons_is_default(self): + self.check(reverse('home'), 'search for add-ons', 'all') + + def test_themes(self): + self.check(reverse('browse.themes'), 'search for add-ons', + '%s,0' % amo.ADDON_THEME) + + def test_collections(self): + self.check(reverse('collections.list'), 'search for collections', + 'collections') + + def test_personas(self): + self.check(reverse('browse.personas'), 'search for personas', + 'personas') diff --git a/templates/search.html b/templates/search.html index 7dc9d0381b..d109ec96e3 100644 --- a/templates/search.html +++ b/templates/search.html @@ -1,10 +1,11 @@ -{% set search_form = SearchForm(request) %} -
+{% set search_form = SimpleSearchForm(request, search_cat) %} + - + placeholder="{{ search_form.placeholder() }}" + value="{{ search_form.q.data or '' }}"> + {{ search_form.cat|safe }} - -
\ No newline at end of file + +