From f9e2c5f561ce9691efc74a4047162f618eb142cb Mon Sep 17 00:00:00 2001 From: Chris Van Date: Mon, 5 Mar 2012 17:41:35 -0800 Subject: [PATCH] show weekly/monthly subscribers for collections search results (bug 733129) --- apps/bandwagon/helpers.py | 6 +-- .../bandwagon/collection_listing_items.html | 43 ++++++++++++---- apps/search/forms.py | 5 +- apps/search/templates/search/collections.html | 3 +- apps/search/tests/test_views.py | 50 +++++++++++++++++-- 5 files changed, 86 insertions(+), 21 deletions(-) diff --git a/apps/bandwagon/helpers.py b/apps/bandwagon/helpers.py index fa7d998b65..47e8d97523 100644 --- a/apps/bandwagon/helpers.py +++ b/apps/bandwagon/helpers.py @@ -13,11 +13,9 @@ from amo.urlresolvers import reverse @register.inclusion_tag('bandwagon/collection_listing_items.html') @jinja2.contextfunction -def collection_listing_items(context, collections, show_weekly=False, - show_date=None): +def collection_listing_items(context, collections, field=None): c = dict(context.items()) - c.update(collections=collections, show_weekly=show_weekly, - show_date=show_date) + c.update(collections=collections, field=field) return c diff --git a/apps/bandwagon/templates/bandwagon/collection_listing_items.html b/apps/bandwagon/templates/bandwagon/collection_listing_items.html index 09771e4e48..15a6c43e4f 100644 --- a/apps/bandwagon/templates/bandwagon/collection_listing_items.html +++ b/apps/bandwagon/templates/bandwagon/collection_listing_items.html @@ -4,18 +4,41 @@ diff --git a/apps/search/forms.py b/apps/search/forms.py index 6a7f3d0b96..5b22b5913e 100644 --- a/apps/search/forms.py +++ b/apps/search/forms.py @@ -28,7 +28,10 @@ collection_sort_by = ( ('rating', _lazy(u'Highest Rated')), ('created', _lazy(u'Newest')), ) -es_collection_sort_by = collection_sort_by + (('name', _lazy(u'Name')),) +es_collection_sort_by = collection_sort_by + ( + ('updated', _lazy(u'Recently Updated')), + ('name', _lazy(u'Name')), +) per_page = (20, 50, ) diff --git a/apps/search/templates/search/collections.html b/apps/search/templates/search/collections.html index fa30e79a66..16304d250d 100644 --- a/apps/search/templates/search/collections.html +++ b/apps/search/templates/search/collections.html @@ -36,8 +36,7 @@ - {{ collection_listing_items(pager.object_list, - show_date=opts.sort) }} + {{ collection_listing_items(pager.object_list, field=opts.sort) }} {% else %} diff --git a/apps/search/tests/test_views.py b/apps/search/tests/test_views.py index f589945976..75b22ee8b6 100644 --- a/apps/search/tests/test_views.py +++ b/apps/search/tests/test_views.py @@ -6,10 +6,12 @@ from django.conf import settings from django.http import QueryDict from django.test import client +from jingo.helpers import datetime as datetime_filter from mock import Mock, patch from nose import SkipTest from nose.tools import eq_ from pyquery import PyQuery as pq +from tower import strip_whitespace import waffle import amo @@ -166,9 +168,12 @@ class TestSearchboxTarget(amo.tests.ESTestCase): class SearchBase(amo.tests.ESTestCase): - def get_results(self, r): + def get_results(self, r, sort=True): """Return pks of add-ons shown on search results page.""" - return sorted(a.id for a in r.context['pager'].object_list) + results = [a.id for a in r.context['pager'].object_list] + if sort: + results = sorted(results) + return results def check_sort_links(self, key, title=None, sort_by=None, reverse=True, params={}): @@ -838,6 +843,10 @@ class TestCollectionSearch(SearchBase): r = self.client.get(urlparams(self.url, sort='newest')) self.assertRedirects(r, urlparams(self.url, sort='created'), 301) + def test_sort_order_unknown(self): + self._generate() + self.check_sort_links('xxx') + def test_sort_order_default(self): self._generate() self.check_sort_links(None, sort_by='weekly_subscribers') @@ -880,9 +889,42 @@ class TestCollectionSearch(SearchBase): self._generate() self.check_sort_links('updated', sort_by='modified') - def test_sort_order_unknown(self): + def test_created_timestamp(self): self._generate() - self.check_sort_links('xxx') + r = self.client.get(urlparams(self.url, sort='created')) + items = pq(r.content)('.primary .item') + for idx, c in enumerate(r.context['pager'].object_list): + eq_(strip_whitespace(items.eq(idx).find('.modified').text()), + 'Added %s' % strip_whitespace(datetime_filter(c.created))) + + def test_updated_timestamp(self): + self._generate() + r = self.client.get(urlparams(self.url, sort='updated')) + items = pq(r.content)('.primary .item') + for idx, c in enumerate(r.context['pager'].object_list): + eq_(strip_whitespace(items.eq(idx).find('.modified').text()), + 'Updated %s' % strip_whitespace(datetime_filter(c.modified))) + + def check_followers_count(self, sort, column): + # Checks that we show the correct type/number of followers. + r = self.client.get(urlparams(self.url, sort=sort)) + items = pq(r.content)('.primary .item') + for idx, c in enumerate(r.context['pager'].object_list): + eq_(items.eq(idx).find('.followers').text().split()[0], + numberfmt(getattr(c, column))) + + def test_followers_all(self): + self._generate() + for sort in ('', 'all', 'rating', 'created', 'modified', 'name'): + self.check_followers_count(sort, column='subscribers') + + def test_followers_monthly(self): + self._generate() + self.check_followers_count('monthly', column='monthly_subscribers') + + def test_followers_weekly(self): + self._generate() + self.check_followers_count('weekly', column='weekly_subscribers') def test_heading(self): # One is a lonely number. But that's all we need.