Ticket #12018: 12018.diff
File 12018.diff, 6.8 KB (added by , 13 years ago) |
---|
-
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 60 60 from django.core.mail import send_mail 61 61 from django.contrib.comments import signals 62 62 from django.db.models.base import ModelBase 63 from django.template import Context, loader63 from django.template import RequestContext, loader 64 64 from django.contrib import comments 65 65 from django.contrib.sites.models import Site 66 66 … … class CommentModerator(object): 227 227 return True 228 228 return False 229 229 230 def email(self, comment, content_object, request ):230 def email(self, comment, content_object, request, extra_context=None): 231 231 """ 232 232 Send email notification of a new comment to site staff when email 233 233 notifications have been requested. … … class CommentModerator(object): 237 237 return 238 238 recipient_list = [manager_tuple[1] for manager_tuple in settings.MANAGERS] 239 239 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 {}) 242 243 subject = '[%s] New comment posted on "%s"' % (Site.objects.get_current().name, 243 244 content_object) 244 message = t.render( c)245 message = t.render(RequestContext(request, context)) 245 246 send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, recipient_list, fail_silently=True) 246 247 247 248 class 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 159 159 post on the content object, and ``False`` otherwise (in which 160 160 case the comment will be immediately deleted). 161 161 162 .. method:: CommentModerator.email(comment, content_object, request )162 .. method:: CommentModerator.email(comment, content_object, request, extra_context=None) 163 163 164 164 If email notification of the new comment should be sent to 165 165 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 4 4 from django.contrib.comments.moderation import moderator, CommentModerator, AlreadyModerated 5 5 from regressiontests.comment_tests.models import Entry 6 6 from django.core import mail 7 from django.template import loader, RequestContext 8 from django.contrib.auth.models import User 9 from django.conf import settings 10 import django.template.context 7 11 8 12 class EntryModerator1(CommentModerator): 9 13 email_notification = True … … class EntryModerator6(CommentModerator): 27 31 auto_close_field = 'pub_date' 28 32 close_after = 0 29 33 34 class 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 30 42 class CommentUtilsModeratorTests(CommentTestCase): 31 43 fixtures = ["comment_utils.xml"] 32 44 … … class CommentUtilsModeratorTests(CommentTestCase): 90 102 def testAutoCloseFieldImmediate(self): 91 103 moderator.register(Entry, EntryModerator6) 92 104 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 107 class 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 1 1 A comment has been posted on {{ content_object }}. 2 2 The comment reads as follows: 3 3 {{ comment }} 4 5 Extra Context: {{ foo }} 6 Request User: {{ request.user }}