Ticket #12018: 12018.diff

File 12018.diff, 6.8 KB (added by Thejaswi Puthraya, 13 years ago)

patch against the latest git checkout

  • django/contrib/comments/moderation.py

    diff --git a/django/contrib/comments/moderation.py b/django/contrib/comments/moderation.py
    index 17d61da..1a679d7 100644
    a b from django.conf import settings  
    6060from django.core.mail import send_mail
    6161from django.contrib.comments import signals
    6262from django.db.models.base import ModelBase
    63 from django.template import Context, loader
     63from django.template import RequestContext, loader
    6464from django.contrib import comments
    6565from django.contrib.sites.models import Site
    6666
    class CommentModerator(object):  
    227227                return True
    228228        return False
    229229
    230     def email(self, comment, content_object, request):
     230    def email(self, comment, content_object, request, extra_context=None):
    231231        """
    232232        Send email notification of a new comment to site staff when email
    233233        notifications have been requested.
    class CommentModerator(object):  
    237237            return
    238238        recipient_list = [manager_tuple[1] for manager_tuple in settings.MANAGERS]
    239239        t = loader.get_template('comments/comment_notification_email.txt')
    240         c = Context({ 'comment': comment,
    241                       'content_object': content_object })
     240
     241        context = { 'comment': comment, 'content_object': content_object }
     242        context.update(extra_context or {})
    242243        subject = '[%s] New comment posted on "%s"' % (Site.objects.get_current().name,
    243244                                                          content_object)
    244         message = t.render(c)
     245        message = t.render(RequestContext(request, context))
    245246        send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, recipient_list, fail_silently=True)
    246247
    247248class Moderator(object):
  • docs/ref/contrib/comments/moderation.txt

    diff --git a/docs/ref/contrib/comments/moderation.txt b/docs/ref/contrib/comments/moderation.txt
    index 4f4b326..2329bd0 100644
    a b object the comment will be attached to, and ``request``, which is the  
    159159    post on the content object, and ``False`` otherwise (in which
    160160    case the comment will be immediately deleted).
    161161
    162 .. method:: CommentModerator.email(comment, content_object, request)
     162.. method:: CommentModerator.email(comment, content_object, request, extra_context=None)
    163163
    164164    If email notification of the new comment should be sent to
    165165    site staff or moderators, this method is responsible for
  • tests/regressiontests/comment_tests/tests/comment_utils_moderators_tests.py

    diff --git a/tests/regressiontests/comment_tests/tests/comment_utils_moderators_tests.py b/tests/regressiontests/comment_tests/tests/comment_utils_moderators_tests.py
    index 4177163..388c61d 100644
    a b from django.contrib.comments.models import Comment  
    44from django.contrib.comments.moderation import moderator, CommentModerator, AlreadyModerated
    55from regressiontests.comment_tests.models import Entry
    66from django.core import mail
     7from django.template import loader, RequestContext
     8from django.contrib.auth.models import User
     9from django.conf import settings
     10import django.template.context
    711
    812class EntryModerator1(CommentModerator):
    913    email_notification = True
    class EntryModerator6(CommentModerator):  
    2731    auto_close_field = 'pub_date'
    2832    close_after = 0
    2933
     34class EntryModerator7(CommentModerator):
     35    email_notification = True
     36
     37    def email(self, comment, content_object, request, extra_context=None):
     38        extra_context = extra_context or {}
     39        extra_context["foo"] = "bar"
     40        super(EntryModerator7, self).email(comment, content_object, request, extra_context=extra_context)
     41
    3042class CommentUtilsModeratorTests(CommentTestCase):
    3143    fixtures = ["comment_utils.xml"]
    3244
    class CommentUtilsModeratorTests(CommentTestCase):  
    90102    def testAutoCloseFieldImmediate(self):
    91103        moderator.register(Entry, EntryModerator6)
    92104        c1, c2 = self.createSomeComments()
    93         self.assertEqual(Comment.objects.all().count(), 0)
    94  No newline at end of file
     105        self.assertEqual(Comment.objects.all().count(), 0)
     106
     107class CommentUtilsModeratorEmailTemplateTests(CommentUtilsModeratorTests):
     108    def setUp(self):
     109        super(CommentUtilsModeratorEmailTemplateTests, self).setUp()
     110        self.orig_context_processors = settings.TEMPLATE_CONTEXT_PROCESSORS
     111        settings.TEMPLATE_CONTEXT_PROCESSORS += ("django.core.context_processors.request",)
     112        django.template.context._standard_context_processors = None
     113        self.user = User.objects.create(username="frank_nobody",
     114                                        email="fnobody@example.com",
     115                                        password="",
     116                                        is_active=True,
     117                                        is_superuser=False,
     118                                        is_staff=False)
     119        self.user.set_password("password")
     120        self.user.save()
     121        logged_in = self.client.login(username="frank_nobody", password="password")
     122        self.assertTrue(logged_in)       
     123
     124    def tearDown(self):
     125        super(CommentUtilsModeratorEmailTemplateTests, self).tearDown()
     126        settings.TEMPLATE_CONTEXT_PROCESSORS = self.orig_context_processors
     127        django.template.context._standard_context_processors = None
     128
     129    def testEmailTemplate(self):
     130        moderator.register(Entry, EntryModerator7)
     131        c1, c2 = self.createSomeComments()
     132        c1.user = self.user
     133        c1.save()
     134        # Send the user along as the test client's request does not have request.user
     135        response = self.client.get("/", user=self.user)
     136        request = response.request
     137        content_type = c1.content_type
     138        content_object = content_type.get_object_for_this_type(pk=c1.object_pk)
     139        request_context = RequestContext(request, {})
     140        template_contents = loader.render_to_string("comments/comment_notification_email.txt",
     141                                                    {"comment": c1,
     142                                                     "content_object": content_object,
     143                                                     "foo": "bar"},
     144                                                    context_instance=request_context)
     145        self.assertEquals(template_contents, mail.outbox[0].body)
  • tests/templates/comments/comment_notification_email.txt

    diff --git a/tests/templates/comments/comment_notification_email.txt b/tests/templates/comments/comment_notification_email.txt
    index 63f1493..38eb138 100644
    a b  
    11A comment has been posted on {{ content_object }}.
    22The comment reads as follows:
    33{{ comment }}
     4
     5Extra Context: {{ foo }}
     6Request User: {{ request.user }}
Back to Top