diff --git a/apps/bandwagon/helpers.py b/apps/bandwagon/helpers.py index 787d4d6d3b..81223605cc 100644 --- a/apps/bandwagon/helpers.py +++ b/apps/bandwagon/helpers.py @@ -1,8 +1,12 @@ +import math import random import jinja2 - from jingo import register, env +from tower import ugettext as _ + +from amo.helpers import login_link +from cake.urlresolvers import remora_url @register.function @@ -11,3 +15,77 @@ def user_collection_list(collections=[], heading=''): c = {'collections': collections, 'heading': heading} t = env.get_template('bandwagon/users/collection_list.html').render(**c) return jinja2.Markup(t) + + +@register.inclusion_tag('bandwagon/collection_favorite.html') +@jinja2.contextfunction +def collection_favorite(context, collection): + c = dict(context.items()) + user = c['request'].amo_user + is_subscribed = collection.is_subscribed(user) + + button_class = 'add-to-fav' + + if is_subscribed: + button_class += ' fav' + text = _('Remove from Favorites') + action = remora_url('collections/unsubscribe') + + else: + text = _('Add to Favorites') + action = remora_url('collections/subscribe') + + c.update(locals()) + c.update({'c': collection}) + return c + + +@register.inclusion_tag('bandwagon/barometer.html') +@jinja2.contextfunction +def barometer(context, collection): + """Shows a barometer for a collection.""" + c = dict(context.items()) + request = c['request'] + + user_vote = 0 # Non-zero if logged in and voted. + + if request.user.is_authenticated(): + # TODO: Use reverse when bandwagon is on Zamboni. + base_action = remora_url(u'collections/vote/%s' % collection.url_slug) + up_action = base_action + '/up' + down_action = base_action + '/down' + cancel = base_action + '/cancel' + up_title = _('Add a positive vote for this collection') + down_title = _('Add a negative vote for this collection') + cancel_title = _('Remove my vote for this collection') + + votes = request.amo_user.votes.filter(collection=collection) + if votes: + user_vote = votes[0] + + else: + up_action = down_action = cancel_action = login_link(c) + login_title = _('Log in to vote for this collection') + up_title = down_title = cancel_title = login_title + + + up_class = 'upvotes' + down_class = 'downvotes' + + total_votes = collection.up_votes + collection.down_votes + + if total_votes: + up_ratio = int(math.ceil(round(100 * collection.up_votes + / total_votes, 2))) + down_ratio = 100 - up_ratio + + up_width = max(up_ratio - 1, 0) + down_width = max(down_ratio - 1, 0) + + if user_vote: + up_class += ' voted' + down_class += ' voted' + up_class + c.update(locals()) + c.update({ 'c': collection, }) + return c diff --git a/apps/bandwagon/templates/bandwagon/barometer.html b/apps/bandwagon/templates/bandwagon/barometer.html new file mode 100644 index 0000000000..3c812c7e44 --- /dev/null +++ b/apps/bandwagon/templates/bandwagon/barometer.html @@ -0,0 +1,39 @@ +
+ +
+ +
+ +
+ + {% if total_votes %} + +
+ + {{ _('{0} positive votes for this collection')|f(up_ratio) }} + +
+ +
+ + {{ _('{0} negative votes for this collection')|f(down_ratio) }} + +
+ + {% else %} + +
+ + {{ _('No votes for this collection yet.') }} + +
+ + {% endif %} +
+ +
+ +
+
diff --git a/apps/bandwagon/templates/bandwagon/collection_favorite.html b/apps/bandwagon/templates/bandwagon/collection_favorite.html new file mode 100644 index 0000000000..f9678b447d --- /dev/null +++ b/apps/bandwagon/templates/bandwagon/collection_favorite.html @@ -0,0 +1,8 @@ +
+
+ + +
+
diff --git a/apps/bandwagon/tests/test_helpers.py b/apps/bandwagon/tests/test_helpers.py index 1ebc3b1015..05ec13d3df 100644 --- a/apps/bandwagon/tests/test_helpers.py +++ b/apps/bandwagon/tests/test_helpers.py @@ -1,14 +1,60 @@ from django import test from nose.tools import eq_ +from mock import Mock +from pyquery import PyQuery as pq +import jingo import amo -from bandwagon.helpers import user_collection_list +from bandwagon.helpers import (user_collection_list, barometer, + collection_favorite) from bandwagon.models import Collection +from cake.urlresolvers import remora_url +from users.models import UserProfile class TestHelpers(test.TestCase): + fixtures = ['base/fixtures', 'users/test_backends'] + + def test_collection_favorite(self): + c = {} + u = UserProfile() + u.nickname = 'unique' + u.save() + c['request'] = Mock() + c['request'].amo_user = u + collection = Collection.objects.get(pk=57274) + + # Not subscribed yet. + doc = pq(collection_favorite(c, collection)) + eq_(doc('button').text(), u'Add to Favorites') + + # Subscribed. + collection.subscriptions.create(user=u) + doc = pq(collection_favorite(c, collection)) + eq_(doc('button').text(), u'Remove from Favorites') + + def test_barometer(self): + jingo.load_helpers() + collection = Collection.objects.get(pk=57274) + collection.up_votes = 1 + # Mock logged out. + c = dict(request=Mock()) + c['request'].path = 'yermom' + c['request'].user.is_authenticated = lambda: False + doc = pq(barometer(c, collection)) + eq_(doc('form')[0].action, '/users/login?to=yermom') + + # Mock logged in. + c['request'].amo_user.votes.filter = lambda collection: [1] + c['request'].user.is_authenticated = lambda: True + barometer(c, collection) + doc = pq(barometer(c, collection)) + eq_(doc('form')[0].action, + remora_url('collections/vote/%s/up' % collection.uuid)) + + def test_user_collection_list(self): c1 = Collection(uuid='eb4e3cd8-5cf1-4832-86fb-a90fc6d3765c') c2 = Collection(uuid='61780943-e159-4206-8acd-0ae9f63f294c',