diff --git a/remo/reports/models.py b/remo/reports/models.py index 37e9c20c1..3efa98553 100644 --- a/remo/reports/models.py +++ b/remo/reports/models.py @@ -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) diff --git a/remo/reports/templates/emails/user_notification_on_add_ng_report_comment.txt b/remo/reports/templates/emails/user_notification_on_add_ng_report_comment.txt new file mode 100644 index 000000000..c2dae311e --- /dev/null +++ b/remo/reports/templates/emails/user_notification_on_add_ng_report_comment.txt @@ -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 }} diff --git a/remo/reports/tests/__init__.py b/remo/reports/tests/__init__.py index ba6ed6912..bd9ba5d7e 100644 --- a/remo/reports/tests/__init__.py +++ b/remo/reports/tests/__init__.py @@ -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 @@ -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 diff --git a/remo/reports/tests/test_models.py b/remo/reports/tests/test_models.py index 02abf00b8..cdbc99a90 100644 --- a/remo/reports/tests/test_models.py +++ b/remo/reports/tests/test_models.py @@ -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) @@ -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)