From 3d43e71fe49ca05f4cc480ed2b2e7e404cb6b0e0 Mon Sep 17 00:00:00 2001 From: Davor Spasovski Date: Tue, 4 Sep 2012 15:18:32 -0700 Subject: [PATCH] added sort by popularity (bug 785976) --- media/js/mkt/init.js | 14 ++++++++++++ mkt/search/templates/search/results.html | 21 ++++++++++++++---- mkt/search/tests/test_views.py | 27 +++++++++++++++++------- 3 files changed, 50 insertions(+), 12 deletions(-) diff --git a/media/js/mkt/init.js b/media/js/mkt/init.js index de3622c4130..9c7ef0d4842 100644 --- a/media/js/mkt/init.js +++ b/media/js/mkt/init.js @@ -128,6 +128,20 @@ z.page.on('fragmentloaded', function() { // Dismiss looks like back but actually just dismisses an overlay. $('#filters').hide(); } else if ($this.hasClass('filter')) { + // Yea... + var sortoption = z.getVars(location.href); + + // This will not scale if we have more than two. + $('#filter-sort li a').removeClass('sel'); + switch(sortoption.sort) { + case 'relevancy': + case 'popularity': + $('#filter-sort li.popularity a').addClass('sel'); + break; + case 'rating': + $('#filter-sort li.rating a').addClass('sel'); + + } $('#filters').show(); } else if ($this.hasClass('search')) { z.body.addClass('show-search'); diff --git a/mkt/search/templates/search/results.html b/mkt/search/templates/search/results.html index 8a278c38077..993dbc02197 100644 --- a/mkt/search/templates/search/results.html +++ b/mkt/search/templates/search/results.html @@ -68,10 +68,23 @@

{{ _('Filter by category') }}

{# We're not iterating over all the sort options on purpose. #}

{{ _('Sort by') }}

diff --git a/mkt/search/tests/test_views.py b/mkt/search/tests/test_views.py index e3c8bdd58d7..c13d95ad284 100644 --- a/mkt/search/tests/test_views.py +++ b/mkt/search/tests/test_views.py @@ -106,19 +106,30 @@ def check_cat_filter(self, params): doc = pq(r.content)('#filter-categories') a = pq(r.content)('#filter-categories').children('li').eq(0).find('a') - # Note: PyQuery's `hasClass` matches children's classes, so yeah. - eq_(a.attr('class'), 'sel' if not cat_selected else None, - "'Any Category' should be selected") - eq_(a.length, 1) - eq_(a.text(), 'Any Category') - a = doc('li:last').find('a') + # :last will no longer work + a = doc('li').eq(1).find('a') eq_(a.text(), unicode(self.cat.name)) - eq_(a.attr('class'), 'sel' if cat_selected else None, - '%r should be selected' % unicode(self.cat.name)) + if cat_selected: + eq_(a.filter('.sel').length, 1, + '%r should be selected' % unicode(self.cat.name)) + else: + eq_(a.filter('.button').length, 1, + '%r should be selected' % unicode(self.cat.name)) + params.update(cat=self.cat.id) eq_(a.attr('href'), urlparams(self.url, **params)) + sorts = pq(r.content)('#filter-sort') + href = sorts('li:first-child a').attr('href') + + if cat_selected: + self.assertNotEqual(href.find('sort=popularity'), -1, + 'Category found - first sort option should be Popularity') + else: + eq_(href, '/search/?sort=None', + 'Category found - first sort option should be Relevancy') + def test_no_cat(self): self.check_cat_filter({})