added rating metadata on 'stars' detailed filter (bug 709643)

This commit is contained in:
Davor Spasovski 2011-12-19 22:41:03 -08:00
Родитель 2a14c4cddb
Коммит ca77e4e03c
3 изменённых файлов: 23 добавлений и 7 удалений

Просмотреть файл

@ -15,13 +15,9 @@ def stars(num, large=False):
return _('Not yet rated')
else:
num = min(5, int(round(num)))
rating = '<span itemprop="rating">%s</span>' % num
title = _('Rated %s out of 5 stars') % num
msg = _('Rated %s out of 5 stars') % rating
size = 'large ' if large else ''
s = (u'<span class="stars {size}stars-{num}" title="{title}">{msg}</span>'
.format(num=num, size=size, title=title, msg=msg))
return jinja2.Markup(s) # Inspected by #10
t = jingo.env.get_template('reviews/impala/reviews_rating.html')
# These are getting renamed for contextual sense in the template.
return jinja2.Markup(t.render(rating=num, detailpage=large))
@jingo.register.function

Просмотреть файл

@ -0,0 +1,12 @@
{% set title = _('Rated {0} out of 5 stars')|f(rating) %}
{% if detailpage %}
<span class="stars large stars-{{ rating }}" title="{{ title }}">
<meta itemprop="worstRating" content="1">
{% trans %}
Rated <span itemprop="ratingValue">{{ rating }}</span> out of
<span itemprop="bestRating">5</span> stars
{% endtrans %}
</span>
{% else %}
<span class="stars stars-{{ rating }}" title="{{ title }}">{{ title }}</span>
{% endif %}

Просмотреть файл

@ -29,6 +29,14 @@ def test_stars():
eq_(doc.text(), msg)
def test_stars_details_page():
doc = pq(render('{{ num|stars(large=True) }}', {'num': 2}))
eq_(doc('.stars').attr('class'), 'stars large stars-2')
eq_(doc('meta[itemprop="worstRating"]').attr('content'), '1')
eq_(doc('span[itemprop="ratingValue"]').text(), '2')
eq_(doc('span[itemprop="bestRating"]').text(), '5')
def test_stars_max():
doc = pq(render('{{ num|stars }}', {'num': 5.3}))
eq_(doc.attr('class'), 'stars stars-5')