Permalink
Browse files

Move email notification out of moderator, to Django signals

This makes the email notification independant of the moderator registration.
  • Loading branch information...
vdboor committed Nov 11, 2012
1 parent 3330629 commit 23af7adf94ca52eb14a940c485e3dcb8fc3c9751
Showing with 37 additions and 25 deletions.
  1. +36 −0 fluent_comments/models.py
  2. +1 −25 fluent_comments/moderation.py
View
@@ -0,0 +1,36 @@
+from django.conf import settings
+from django.contrib.sites.models import get_current_site
+from django.core.mail import send_mail
+from django.dispatch import receiver
+from django.contrib.comments import signals
+from django.shortcuts import render
+from fluent_comments import appsettings
+
+
+@receiver(signals.comment_was_posted)
+def on_comment_posted(sender, comment, request, **kwargs):
+ """
+ Send email notification of a new comment to site staff when email notifications have been requested.
+ """
+ # This code is copied from django.contrib.comments.moderation.
+ # That code doesn't offer a RequestContext, which makes it really
+ # hard to generate proper URL's with FQDN in the email
+ #
+ # Instead of implementing this feature in the moderator class, the signal is used instead
+ # so the notification feature works regardless of a manual moderator.register() call in the project.
+ if not appsettings.FLUENT_COMMENTS_USE_EMAIL_MODERATION:
+ return
+
+ recipient_list = [manager_tuple[1] for manager_tuple in settings.MANAGERS]
+ site = get_current_site(request)
+ content_object = comment.content_object
+
+ subject = '[{0}] New comment posted on "{1}"'.format(site.name, content_object)
+ context = {
+ 'site': site,
+ 'comment': comment,
+ 'content_object': content_object
+ }
+
+ message = render(request, "comments/comment_notification_email.txt", context)
+ send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, recipient_list, fail_silently=True)
@@ -1,11 +1,8 @@
from urlparse import urljoin
-from django.conf import settings
from django.contrib.comments.moderation import CommentModerator, moderator
from django.contrib.sites.models import get_current_site
from django.core.exceptions import ImproperlyConfigured
-from django.core.mail import send_mail
from django.http import HttpRequest
-from django.shortcuts import render
from django.utils.encoding import smart_str
from akismet import Akismet
from fluent_comments import appsettings
@@ -31,7 +28,7 @@ class FluentCommentsModerator(CommentModerator):
close_after = appsettings.FLUENT_COMMENTS_CLOSE_AFTER_DAYS
moderate_after = appsettings.FLUENT_COMMENTS_MODERATE_AFTER_DAYS
- email_notification = appsettings.FLUENT_COMMENTS_USE_EMAIL_MODERATION
+ email_notification = False # Using signals instead
akismet_check = appsettings.FLUENT_CONTENTS_USE_AKISMET
akismet_check_action = appsettings.FLUENT_COMMENTS_AKISMET_ACTION
@@ -74,27 +71,6 @@ def moderate(self, comment, content_object, request):
return False
- def email(self, comment, content_object, request):
- """
- Send email notification of a new comment to site staff when email notifications have been requested.
- """
- # This code is copied from django.contrib.comments.moderation,
- # since it doesn't offer a RequestContext, making it really hard to generate URL's with FQDN in the email
- if not self.email_notification:
- return
-
- recipient_list = [manager_tuple[1] for manager_tuple in settings.MANAGERS]
- site = get_current_site(request)
- subject = '[{0}] New comment posted on "{1}"'.format(site.name, content_object)
- context = {
- 'site': site,
- 'comment': comment,
- 'content_object': content_object
- }
- message = render(request, "comments/comment_notification_email.txt", context)
- send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, recipient_list, fail_silently=True)
-
-
def _akismet_check(self, comment, content_object, request):
"""
Connects to Akismet and returns True if Akismet marks this comment as

0 comments on commit 23af7ad

Please sign in to comment.