Skip to content

Commit

Permalink
Merge branch 'feature/moderated-comment-notification' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Fantomas42 committed Mar 27, 2017
2 parents 3dcf7f9 + b762761 commit af09202
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 103 deletions.
195 changes: 115 additions & 80 deletions zinnia/moderator.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,100 +33,135 @@ class EntryCommentModerator(CommentModerator):
auto_moderate_comments = AUTO_MODERATE_COMMENTS
mail_comment_notification_recipients = MAIL_COMMENT_NOTIFICATION_RECIPIENTS

def email(self, comment, content_object, request):
def moderate(self, comment, entry, request):
"""
Determine if a new comment should be marked as non-public
and await approval.
Return ``True`` to put the comment into the moderator queue,
or ``False`` to allow it to be showed up immediately.
"""
if self.auto_moderate_comments:
return True

if check_is_spam(comment, entry, request,
self.spam_checker_backends):
return True

return False

def email(self, comment, entry, request):
"""
Send email notifications needed.
"""
if comment.is_public:
current_language = get_language()
try:
activate(settings.LANGUAGE_CODE)
if self.mail_comment_notification_recipients:
self.do_email_notification(comment, content_object,
request)
if self.email_authors:
self.do_email_authors(comment, content_object,
request)
if self.email_reply:
self.do_email_reply(comment, content_object, request)
finally:
activate(current_language)

def do_email_notification(self, comment, content_object, request):
current_language = get_language()
try:
activate(settings.LANGUAGE_CODE)
site = Site.objects.get_current()
if self.auto_moderate_comments or comment.is_public:
self.do_email_notification(comment, entry, site)
if comment.is_public:
self.do_email_authors(comment, entry, site)
self.do_email_reply(comment, entry, site)
finally:
activate(current_language)

def do_email_notification(self, comment, entry, site):
"""
Send email notification of a new comment to site staff.
"""
site = Site.objects.get_current()
if not self.mail_comment_notification_recipients:
return

template = loader.get_template(
'comments/comment_notification_email.txt')
context = {'comment': comment, 'site': site,
'protocol': PROTOCOL,
'content_object': content_object}
'comments/zinnia/entry/email/notification.txt')
context = {
'comment': comment,
'entry': entry,
'site': site,
'protocol': PROTOCOL
}
subject = _('[%(site)s] New comment posted on "%(title)s"') % \
{'site': site.name, 'title': content_object.title}
{'site': site.name, 'title': entry.title}
message = template.render(context)
send_mail(subject, message, settings.DEFAULT_FROM_EMAIL,
self.mail_comment_notification_recipients,
fail_silently=not settings.DEBUG)

def do_email_authors(self, comment, content_object, request):
send_mail(
subject, message,
settings.DEFAULT_FROM_EMAIL,
self.mail_comment_notification_recipients,
fail_silently=not settings.DEBUG
)

def do_email_authors(self, comment, entry, site):
"""
Send email notification of a new comment to the authors of the entry.
Send email notification of a new comment to
the authors of the entry.
"""
if not self.email_authors:
return

exclude_list = self.mail_comment_notification_recipients + ['']
recipient_list = set(
[author.email for author in content_object.authors.all()]) - \
set(exclude_list)
if recipient_list:
site = Site.objects.get_current()
template = loader.get_template(
'comments/comment_authors_email.txt')
context = {'comment': comment, 'site': site,
'protocol': PROTOCOL,
'content_object': content_object}
subject = _('[%(site)s] New comment posted on "%(title)s"') % \
{'site': site.name, 'title': content_object.title}
message = template.render(context)
send_mail(subject, message, settings.DEFAULT_FROM_EMAIL,
recipient_list, fail_silently=not settings.DEBUG)

def do_email_reply(self, comment, content_object, request):
"""
Send email notification of a new comment to the authors of
the previous comments.
recipient_list = (
set([author.email for author in entry.authors.all()])
- set(exclude_list)
)
if not recipient_list:
return

template = loader.get_template(
'comments/zinnia/entry/email/authors.txt')
context = {
'comment': comment,
'entry': entry,
'site': site,
'protocol': PROTOCOL
}
subject = _('[%(site)s] New comment posted on "%(title)s"') % \
{'site': site.name, 'title': entry.title}
message = template.render(context)

send_mail(
subject, message,
settings.DEFAULT_FROM_EMAIL,
recipient_list,
fail_silently=not settings.DEBUG
)

def do_email_reply(self, comment, entry, site):
"""
exclude_list = self.mail_comment_notification_recipients + \
[author.email for author in content_object.authors.all()] + \
[comment.email]
recipient_list = set(
[other_comment.email for other_comment in content_object.comments
if other_comment.email]) - set(exclude_list)
if recipient_list:
site = Site.objects.get_current()
template = loader.get_template('comments/comment_reply_email.txt')
context = {'comment': comment, 'site': site,
'protocol': PROTOCOL,
'content_object': content_object}
subject = _('[%(site)s] New comment posted on "%(title)s"') % \
{'site': site.name, 'title': content_object.title}
message = template.render(context)
mail = EmailMessage(subject, message,
settings.DEFAULT_FROM_EMAIL,
bcc=recipient_list)
mail.send(fail_silently=not settings.DEBUG)

def moderate(self, comment, content_object, request):
Send email notification of a new comment to
the authors of the previous comments.
"""
Determine if a new comment should be marked as non-public
and await approval.
Return ``True`` to put the comment into the moderator queue,
or ``False`` to allow it to be showed up immediately.
"""
if self.auto_moderate_comments:
return True
if not self.email_reply:
return

if check_is_spam(comment, content_object, request,
self.spam_checker_backends):
return True
exclude_list = (
self.mail_comment_notification_recipients
+ [author.email for author in entry.authors.all()]
+ [comment.email]
)
recipient_list = (
set([other_comment.email
for other_comment in entry.comments
if other_comment.email])
- set(exclude_list)
)
if not recipient_list:
return

return False
template = loader.get_template(
'comments/zinnia/entry/email/reply.txt')
context = {
'comment': comment,
'entry': entry,
'site': site,
'protocol': PROTOCOL
}
subject = _('[%(site)s] New comment posted on "%(title)s"') % \
{'site': site.name, 'title': entry.title}
message = template.render(context)

mail = EmailMessage(
subject, message,
settings.DEFAULT_FROM_EMAIL,
bcc=recipient_list)
mail.send(fail_silently=not settings.DEBUG)
6 changes: 0 additions & 6 deletions zinnia/templates/comments/comment_authors_email.txt

This file was deleted.

6 changes: 0 additions & 6 deletions zinnia/templates/comments/comment_reply_email.txt

This file was deleted.

6 changes: 6 additions & 0 deletions zinnia/templates/comments/zinnia/entry/email/authors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% load i18n %}{% trans "Author" %}: {{ comment.name }} {{ comment.url }}

{% trans "Comment" %}:
{{ comment.comment }}

{% trans "View this comment" %}: {{ protocol }}://{{ site }}{{ entry.get_absolute_url }}#comment-{{ comment.pk }}-by-{{ comment.user_name|slugify }}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{% trans "Comment" %}:
{{ comment.comment }}

{% trans "View this comment" %}: {{ protocol }}://{{ site }}{{ content_object.get_absolute_url }}#comment-{{ comment.pk }}-by-{{ comment.user_name|slugify }}
{% trans "View this comment" %}: {{ protocol }}://{{ site }}{{ entry.get_absolute_url }}#comment-{{ comment.pk }}-by-{{ comment.user_name|slugify }}

{% trans "Flag this comment" %}: {{ protocol }}://{{ site }}{% url 'comments-flag' comment.pk %}

Expand Down
6 changes: 6 additions & 0 deletions zinnia/templates/comments/zinnia/entry/email/reply.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% load i18n %}{% trans "Author" %}: {{ comment.name }} {{ comment.url }}

{% trans "Comment" %}:
{{ comment.comment }}

{% trans "View this comment" %}: {{ protocol }}://{{ site }}{{ entry.get_absolute_url }}#comment-{{ comment.pk }}-by-{{ comment.user_name|slugify }}
20 changes: 10 additions & 10 deletions zinnia/tests/test_moderator.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def test_do_email_notification(self):
self.assertEqual(len(mail.outbox), 0)
moderator = EntryCommentModerator(Entry)
moderator.mail_comment_notification_recipients = ['admin@example.com']
moderator.do_email_notification(comment, self.entry, 'request')
moderator.do_email_notification(comment, self.entry, self.site)
self.assertEqual(len(mail.outbox), 1)

def test_do_email_authors(self):
Expand All @@ -90,10 +90,10 @@ def test_do_email_authors(self):
moderator.email_authors = True
moderator.mail_comment_notification_recipients = [
'admin@example.com', 'webmaster@example.com']
moderator.do_email_authors(comment, self.entry, 'request')
moderator.do_email_authors(comment, self.entry, self.site)
self.assertEqual(len(mail.outbox), 0)
moderator.mail_comment_notification_recipients = []
moderator.do_email_authors(comment, self.entry, 'request')
moderator.do_email_authors(comment, self.entry, self.site)
self.assertEqual(len(mail.outbox), 1)

def test_do_email_authors_without_email(self):
Expand All @@ -111,15 +111,15 @@ def test_do_email_authors_without_email(self):
contributor = Author.objects.create(username='contributor',
email='contrib@example.com')
self.entry.authors.add(contributor)
moderator.do_email_authors(comment, self.entry, 'request')
moderator.do_email_authors(comment, self.entry, self.site)
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(
set(mail.outbox[0].to),
set(['admin@example.com', 'contrib@example.com']))
mail.outbox = []
contributor.email = ''
contributor.save()
moderator.do_email_authors(comment, self.entry, 'request')
moderator.do_email_authors(comment, self.entry, self.site)
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(mail.outbox[0].to, ['admin@example.com'])

Expand All @@ -129,32 +129,32 @@ def test_do_email_reply(self):
content_object=self.entry, submit_date=timezone.now(),
site=self.site)
moderator = EntryCommentModerator(Entry)
moderator.email_notification_reply = True
moderator.email_reply = True
moderator.mail_comment_notification_recipients = [
'admin@example.com', 'webmaster@example.com']
moderator.do_email_reply(comment, self.entry, 'request')
moderator.do_email_reply(comment, self.entry, self.site)
self.assertEqual(len(mail.outbox), 0)

comment = comments.get_model().objects.create(
comment='My Comment 2', user_email='user_1@example.com',
content_object=self.entry, is_public=True,
submit_date=timezone.now(), site=self.site)
moderator.do_email_reply(comment, self.entry, 'request')
moderator.do_email_reply(comment, self.entry, self.site)
self.assertEqual(len(mail.outbox), 0)

comment = comments.get_model().objects.create(
comment='My Comment 3', user_email='user_2@example.com',
content_object=self.entry, is_public=True,
submit_date=timezone.now(), site=self.site)
moderator.do_email_reply(comment, self.entry, 'request')
moderator.do_email_reply(comment, self.entry, self.site)
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(mail.outbox[0].bcc, ['user_1@example.com'])

comment = comments.get_model().objects.create(
comment='My Comment 4', user=self.author, is_public=True,
content_object=self.entry, submit_date=timezone.now(),
site=self.site)
moderator.do_email_reply(comment, self.entry, 'request')
moderator.do_email_reply(comment, self.entry, self.site)
self.assertEqual(len(mail.outbox), 2)
self.assertEqual(
set(mail.outbox[1].bcc),
Expand Down

0 comments on commit af09202

Please sign in to comment.