Used get_current_site in comments feed class

This commit is contained in:
Claude Paroz 2012-09-28 14:35:26 +02:00
parent b8244c654c
commit 07ffe78143
3 changed files with 31 additions and 12 deletions

View File

@ -1,30 +1,27 @@
from django.conf import settings
from django.contrib.syndication.views import Feed from django.contrib.syndication.views import Feed
from django.contrib.sites.models import Site from django.contrib.sites.models import get_current_site
from django.contrib import comments from django.contrib import comments
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
class LatestCommentFeed(Feed): class LatestCommentFeed(Feed):
"""Feed of latest comments on the current site.""" """Feed of latest comments on the current site."""
def __call__(self, request, *args, **kwargs):
self.site = get_current_site(request)
return super(LatestCommentFeed, self).__call__(request, *args, **kwargs)
def title(self): def title(self):
if not hasattr(self, '_site'): return _("%(site_name)s comments") % dict(site_name=self.site.name)
self._site = Site.objects.get_current()
return _("%(site_name)s comments") % dict(site_name=self._site.name)
def link(self): def link(self):
if not hasattr(self, '_site'): return "http://%s/" % (self.site.domain)
self._site = Site.objects.get_current()
return "http://%s/" % (self._site.domain)
def description(self): def description(self):
if not hasattr(self, '_site'): return _("Latest comments on %(site_name)s") % dict(site_name=self.site.name)
self._site = Site.objects.get_current()
return _("Latest comments on %(site_name)s") % dict(site_name=self._site.name)
def items(self): def items(self):
qs = comments.get_model().objects.filter( qs = comments.get_model().objects.filter(
site__pk = settings.SITE_ID, site__pk = self.site.pk,
is_public = True, is_public = True,
is_removed = False, is_removed = False,
) )

View File

@ -1,12 +1,32 @@
from __future__ import absolute_import from __future__ import absolute_import
from django.conf import settings
from django.contrib.comments.models import Comment
from django.contrib.contenttypes.models import ContentType
from django.contrib.sites.models import Site
from . import CommentTestCase from . import CommentTestCase
from ..models import Article
class CommentFeedTests(CommentTestCase): class CommentFeedTests(CommentTestCase):
urls = 'regressiontests.comment_tests.urls' urls = 'regressiontests.comment_tests.urls'
feed_url = '/rss/comments/' feed_url = '/rss/comments/'
def setUp(self):
site_2 = Site.objects.create(id=settings.SITE_ID+1,
domain="example2.com", name="example2.com")
# A comment for another site
c5 = Comment.objects.create(
content_type = ContentType.objects.get_for_model(Article),
object_pk = "1",
user_name = "Joe Somebody",
user_email = "jsomebody@example.com",
user_url = "http://example.com/~joe/",
comment = "A comment for the second site.",
site = site_2,
)
def test_feed(self): def test_feed(self):
response = self.client.get(self.feed_url) response = self.client.get(self.feed_url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
@ -15,3 +35,4 @@ class CommentFeedTests(CommentTestCase):
self.assertContains(response, '<title>example.com comments</title>') self.assertContains(response, '<title>example.com comments</title>')
self.assertContains(response, '<link>http://example.com/</link>') self.assertContains(response, '<link>http://example.com/</link>')
self.assertContains(response, '</rss>') self.assertContains(response, '</rss>')
self.assertNotContains(response, "A comment for the second site.")

View File

@ -15,6 +15,7 @@ urlpatterns = patterns('',
url(r'^flag/(\d+)/$', views.custom_flag_comment), url(r'^flag/(\d+)/$', views.custom_flag_comment),
url(r'^delete/(\d+)/$', views.custom_delete_comment), url(r'^delete/(\d+)/$', views.custom_delete_comment),
url(r'^approve/(\d+)/$', views.custom_approve_comment), url(r'^approve/(\d+)/$', views.custom_approve_comment),
url(r'^cr/(\d+)/(.+)/$', 'django.contrib.contenttypes.views.shortcut', name='comments-url-redirect'),
) )
urlpatterns += patterns('', urlpatterns += patterns('',