From b07b5877b9c50c35523e97f1f8b808e3ba04ca38 Mon Sep 17 00:00:00 2001 From: Sam Keen Date: Tue, 20 Jul 2010 12:36:41 -0700 Subject: [PATCH] Migrate reviews' RSS view to zamboni (bug 577345 --- apps/amo/tests/test_redirects.py | 5 ++ apps/reviews/feeds.py | 82 +++++++++++++++++++ .../templates/reviews/review_list.html | 5 ++ apps/reviews/urls.py | 3 +- urls.py | 3 + 5 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 apps/reviews/feeds.py diff --git a/apps/amo/tests/test_redirects.py b/apps/amo/tests/test_redirects.py index 70b19f2a53..8c326826dd 100644 --- a/apps/amo/tests/test_redirects.py +++ b/apps/amo/tests/test_redirects.py @@ -175,3 +175,8 @@ class TestRedirects(test.TestCase): r = self.client.get('/addons/versions/4/format:rss', follow=True) self.assertRedirects(r, '/en-US/firefox/addon/4/versions/format:rss', status_code=301) + + def test_addons_reviews_rss(self): + r = self.client.get('/addons/reviews/4/format:rss', follow=True) + self.assertRedirects(r, '/en-US/firefox/addon/4/reviews/format:rss', + status_code=301) diff --git a/apps/reviews/feeds.py b/apps/reviews/feeds.py new file mode 100644 index 0000000000..302a48b84b --- /dev/null +++ b/apps/reviews/feeds.py @@ -0,0 +1,82 @@ +from django.contrib.syndication.views import Feed +from django.shortcuts import get_object_or_404 + +from tower import ugettext as _ + +from amo.urlresolvers import reverse +from amo.helpers import absolutify, url + +from addons.models import Addon, Review + +import urllib + + +class ReviewsRss(Feed): + + addon = None + + def get_object(self, request, addon_id): + """Get the Addon for which we are about to output + the RSS feed of it Review""" + self.addon = get_object_or_404(Addon.objects.valid(), pk=addon_id) + return self.addon + + def title(self, addon): + """Title for the feed""" + return _('Reviews for %s') % addon.name + + def link(self, addon): + """Link for the feed""" + return absolutify(url('home')) + + def description(self, addon): + """Description for the feed""" + return _('Review History for this Addon') + + def items(self, addon): + """Return the Reviews for this Addon to be output as RSS 's""" + qs = (Review.objects.valid().filter(addon=addon).order_by('-created')) + return qs.all()[:30] + + def item_link(self, review): + """Link for a particular review ()""" + review_id = urllib.quote(str(review.id)) + return reverse('reviews.list', args=[self.addon.id]) + review_id + + def item_title(self, review): + """Title for particular review ()""" + rating = '' + tag_line = '' + if(hasattr(review, 'rating') and review.rating): + # L10n: This describes the number of stars given out of 5 + rating = _('Rated %d out of 5 stars') % review.rating + if(hasattr(review, 'title') and review.title): + tag_line = review.title + divider = ' : ' if rating and tag_line else '' + return "{rating}{divider}{tag_line}".format(rating=rating, + divider=divider, + tag_line=tag_line) + + def item_description(self, review): + """Description for particular review (<item><description>)""" + return review.body + + def item_guid(self, review): + """Guid for a particuar review (<item><guid>)""" + guid_url = absolutify(reverse('reviews.list', args=[self.addon.id])) + return guid_url + urllib.quote(str(review.id)) + + def item_author_name(self, review): + """Author for a particuar review (<item><dc:creator>)""" + author = '' + if(review.user.nickname): + author = review.user.nickname.strip() + else: + author = "{first} {last}".format( + first=review.user.firstname.strip(), + last=review.user.lastname.strip()).strip() + return author + + def item_pubdate(self, review): + """Pubdate for a particuar review (<item><pubDate>)""" + return review.created diff --git a/apps/reviews/templates/reviews/review_list.html b/apps/reviews/templates/reviews/review_list.html index 90d5a12131..57862232f2 100644 --- a/apps/reviews/templates/reviews/review_list.html +++ b/apps/reviews/templates/reviews/review_list.html @@ -5,6 +5,11 @@ {% block bodyclass %}reviews inverse{% endblock %} +{% block rss_feed %} +<link rel="alternate" type="application/rss+xml" + title="RSS" href="{{ url('reviews.list', addon.id) }}format:rss"> +{% endblock %} + {% block content %} <div class="primary" role="main"> <header> diff --git a/apps/reviews/urls.py b/apps/reviews/urls.py index 89182ab818..3aab4c355d 100644 --- a/apps/reviews/urls.py +++ b/apps/reviews/urls.py @@ -1,5 +1,5 @@ from django.conf.urls.defaults import patterns, url, include - +from reviews.feeds import ReviewsRss from . import views @@ -13,5 +13,6 @@ detail_patterns = patterns('', urlpatterns = patterns('', url('^$', views.review_list, name='reviews.list'), url('^(?P<review_id>\d+)/', include(detail_patterns)), + url('^format:rss$', ReviewsRss(), name='reviews.list.rss'), url('^user:(?P<user_id>\d+)$', views.review_list, name='reviews.user'), ) diff --git a/urls.py b/urls.py index 6350d90435..53dacf31fe 100644 --- a/urls.py +++ b/urls.py @@ -80,6 +80,9 @@ urlpatterns = patterns('', ('^addons/versions/(\d+)/format:rss$', lambda r, id: redirect('addons.versions.rss', id, permanent=True)), + ('^addons/reviews/(\d+)/format:rss$', + lambda r, id: redirect('reviews.list.rss', id, permanent=True)), + ('^search-engines.*$', lambda r: redirect('browse.search-tools', permanent=True)),