Skip to content
This repository has been archived by the owner on Oct 28, 2020. It is now read-only.

Commit

Permalink
Merge pull request #613 from akatsoulas/966977
Browse files Browse the repository at this point in the history
[fix bug 966977] Send an email when a user adds a comment on a NGReport.
  • Loading branch information
akatsoulas committed Feb 4, 2014
2 parents 4f9f80a + 7ade5a4 commit 57be8b1
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 1 deletion.
31 changes: 31 additions & 0 deletions remo/reports/models.py
Expand Up @@ -448,3 +448,34 @@ def update_passive_report_functional_areas(sender, instance, action, pk_set,

if action == 'post_clear':
report.functional_areas.clear()


@receiver(post_save, sender=NGReportComment,
dispatch_uid='email_commenters_on_add_ng_report_comment_signal')
def email_commenters_on_add_ng_report_comment(sender, instance, **kwargs):
"""Email a user when a comment is added to a continuous report instance."""
subject = '[Report] User {0} commented on {1}'
email_template = 'emails/user_notification_on_add_ng_report_comment.txt'
report = instance.report

# Send an email to all users commented so far on the report except fom
# the user who made the comment. Dedup the list with unique IDs.
commenters = set(NGReportComment.objects.filter(report=report)
.exclude(user=instance.user)
.values_list('user', flat=True))

# Add the owner of the report in the list
if report.user.id not in commenters:
commenters.add(report.user.id)

for user_id in commenters:
user = User.objects.get(pk=user_id)
if (user.userprofile.receive_email_on_add_comment and
user != instance.user):
ctx_data = {'report': report, 'user': user,
'commenter': instance.user,
'comment': instance.comment,
'created_on': instance.created_on}
subject = subject.format(instance.user.get_full_name(), report)
send_remo_mail.delay([user_id], subject,
email_template, ctx_data)
@@ -0,0 +1,15 @@
Hey there {{ user.first_name|capitalize }},

This email was generated automatically to inform you that {{ commenter.get_full_name() }}
added a comment on the report [1] for {{ report }}
on {{ created_on|format_datetime }}.

The comment was:

{{ comment|indent(indentfirst=True) }}

Cheers!

Your lovely ReMo bot.

[1] {{ report.get_absolute_url()|absolutify }}
20 changes: 19 additions & 1 deletion remo/reports/tests/__init__.py
Expand Up @@ -11,7 +11,9 @@
from remo.profiles.tests import UserFactory
from remo.reports.models import (Report, ReportComment, ReportEvent,
ReportLink, Activity, Campaign, NGReport,
NGReportComment, email_mentor_on_add_report)
NGReportComment,
email_commenters_on_add_ng_report_comment,
email_mentor_on_add_report)


EMPTY_REPORT = False
Expand Down Expand Up @@ -123,3 +125,19 @@ class NGReportCommentFactory(factory.django.DjangoModelFactory):
user = factory.SubFactory(UserFactory, userprofile__initial_council=True)
report = factory.SubFactory(NGReportFactory)
comment = factory.Sequence(lambda n: 'Comment #{0}'.format(n))


class NGReportCommentFactoryNoSignals(NGReportCommentFactory):

@classmethod
def _create(cls, target_class, *args, **kwargs):
dispatch_uid = 'email_commenters_on_add_ng_report_comment_signal'
post_save.disconnect(email_commenters_on_add_ng_report_comment,
NGReportComment,
dispatch_uid=dispatch_uid)
comment = super(NGReportCommentFactory, cls)._create(target_class,
*args, **kwargs)
post_save.connect(email_commenters_on_add_ng_report_comment,
NGReportComment,
dispatch_uid=dispatch_uid)
return comment
59 changes: 59 additions & 0 deletions remo/reports/tests/test_models.py
Expand Up @@ -13,6 +13,7 @@
from remo.reports import ACTIVITY_EVENT_ATTEND, ACTIVITY_EVENT_CREATE
from remo.reports.models import Activity, NGReport, OVERDUE_DAY
from remo.reports.tests import (NGReportFactory, NGReportCommentFactory,
NGReportCommentFactoryNoSignals,
ReportCommentFactory, ReportFactory)


Expand Down Expand Up @@ -362,3 +363,61 @@ def test_edit_event_report_functional_areas(self):
self.assertQuerysetEqual(report.functional_areas.all(),
[e.name for e in categories.all()[:1]],
lambda x: x.name)

def test_send_email_on_report_comment_settings_True_one_user(self):
"""Test sending email when a new comment is added on a NGReport
and the user has the option enabled in his/her settings.
"""
commenter = UserFactory.create()
reporter = UserFactory.create(
userprofile__receive_email_on_add_comment=True)
report = NGReportFactory.create(user=reporter)
NGReportCommentFactory.create(user=commenter, report=report,
comment='This is a comment')

eq_(len(mail.outbox), 1)
eq_(reporter.email, mail.outbox[0].to[0])
msg = ('[Report] User {0} commented on {1}'
.format(commenter.get_full_name(), report))
eq_(mail.outbox[0].subject, msg)

def test_send_email_on_report_comment_settings_False_one_user(self):
"""Test sending email when a new comment is added on a NGReport
and the user has the option disabled in his/her settings.
"""
comment_user = UserFactory.create()
user = UserFactory.create(
userprofile__receive_email_on_add_comment=False)
report = NGReportFactory.create(user=user)
NGReportCommentFactory.create(user=comment_user, report=report,
comment='This is a comment')

eq_(len(mail.outbox), 0)

def test_send_email_on_report_comment_settings_True_multiple_users(self):
"""Test sending email when a new comment is added on a NGReport
and the users have the option enabled in their settings.
"""
commenter = UserFactory.create()
reporter = UserFactory.create(
userprofile__receive_email_on_add_comment=True)
report = NGReportFactory.create(user=reporter)
users_with_comments = UserFactory.create_batch(
2, userprofile__receive_email_on_add_comment=True)
# disconnect the signals in order to add two users in NGReportComment
for user_obj in users_with_comments:
NGReportCommentFactoryNoSignals.create(
user=user_obj, report=report, comment='This is a comment')
NGReportCommentFactory.create(user=commenter, report=report,
comment='This is a comment')

eq_(len(mail.outbox), 3)
eq_(reporter.email, mail.outbox[0].to[0])
eq_(users_with_comments[0].email, mail.outbox[1].to[0])
eq_(users_with_comments[1].email, mail.outbox[2].to[0])
msg = ('[Report] User {0} commented on {1}'
.format(commenter.get_full_name(), report))
eq_(mail.outbox[0].subject, msg)

0 comments on commit 57be8b1

Please sign in to comment.