bug 562667, New collections helpers.
barometer and collection_favorite
This commit is contained in:
Родитель
2ab472afc1
Коммит
529f6753f4
|
@ -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
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
<div class="barometer">
|
||||
|
||||
<form method="post" action="{{ up_action }}">
|
||||
<input class="{{ up_class }}" value="{{ c.up_votes }}" type="submit"
|
||||
title="{{ up_title }}"/>
|
||||
</form>
|
||||
|
||||
<div class="bars">
|
||||
|
||||
{% if total_votes %}
|
||||
|
||||
<div class="upbar" style="width: {{ up_width }}%;">
|
||||
<span class="votecount">
|
||||
{{ _('{0} positive votes for this collection')|f(up_ratio) }}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div class="downbar" style="width: {{ down_width }}%;">
|
||||
<span class="votecount">
|
||||
{{ _('{0} negative votes for this collection')|f(down_ratio) }}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{% else %}
|
||||
|
||||
<div class="novotes">
|
||||
<span class="votecount">
|
||||
{{ _('No votes for this collection yet.') }}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
<form method="post" action="{{ down_action }}">
|
||||
<input class="{{ down_class }}" value="{{ c.down_votes }}" type="submit"
|
||||
title="{{ down_title }}">
|
||||
</form>
|
||||
</div>
|
|
@ -0,0 +1,8 @@
|
|||
<form class="favorite" method="post" action="{{ action }}">
|
||||
<div>
|
||||
<input value="{{ c.uuid }}" type="hidden" name="uuid" />
|
||||
<button class="{{ button_class }}" type="submit">
|
||||
{{ text }}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
|
@ -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',
|
||||
|
|
Загрузка…
Ссылка в новой задаче