def save(self, request): """ Saves a new comment and sends any notification emails. """ comment = self.get_comment_object() obj = comment.content_object if request.user.is_authenticated(): comment.user = request.user comment.by_author = request.user == getattr(obj, "user", None) comment.ip_address = ip_for_request(request) comment.replied_to_id = self.data.get("replied_to") comment.save() comment_was_posted.send(sender=comment.__class__, comment=comment, request=request) notify_emails = split_addresses(settings.COMMENTS_NOTIFICATION_EMAILS) if notify_emails: subject = ugettext("New comment for: ") + str(obj) context = { "comment": comment, "comment_url": add_cache_bypass(comment.get_absolute_url()), "request": request, "obj": obj, } send_mail_template(subject, "email/comment_notification", settings.DEFAULT_FROM_EMAIL, notify_emails, context) return comment
def test_comment_email_sending_3(self): """ After the editor and coordinator post a comment, an additional coordinator posts a comment. One email is sent to the first coordinator, and a distinct email is sent to the editor. """ app, request = self._set_up_email_test_objects() request.user = UserFactory() self.assertEqual(len(mail.outbox), 0) _ = self._create_comment(app, self.coordinator1) _ = self._create_comment(app, self.editor) comment3 = self._create_comment(app, self.coordinator2) comment_was_posted.send(sender=Comment, comment=comment3, request=request) self.assertEqual(len(mail.outbox), 2) # Either order of email sending is fine. try: self.assertEqual(mail.outbox[0].to, [self.coordinator1.email]) self.assertEqual(mail.outbox[1].to, [self.editor.email]) except AssertionError: self.assertEqual(mail.outbox[1].to, [self.coordinator1.email]) self.assertEqual(mail.outbox[0].to, [self.editor.email])
def save(self, request): """ Saves a new comment and sends any notification emails. """ comment = self.get_comment_object() obj = comment.content_object if request.user.is_authenticated(): comment.user = request.user comment.user_name = best_name(comment.user) comment.by_author = request.user == getattr(obj, "user", None) comment.ip_address = ip_for_request(request) comment.replied_to_id = self.data.get("replied_to") comment.save() comment_was_posted.send(sender=comment.__class__, comment=comment, request=request) notify_emails = split_addresses(settings.COMMENTS_NOTIFICATION_EMAILS) notify_emails.append(obj.user.email) reply_to_comment = comment.replied_to if reply_to_comment is not None: notify_emails.append(reply_to_comment.user.email) if notify_emails: subject = "[HydroShare Support] New comment by {c_name} for: {res_obj}".format( c_name=comment.user_name, res_obj=str(obj)) context = { "comment": comment, "comment_url": add_cache_bypass(comment.get_absolute_url()), "request": request, "obj": obj, } send_mail_template(subject, "email/comment_notification", settings.DEFAULT_FROM_EMAIL, notify_emails, context) return comment
def test_comment_email_sending_4(self): """ A comment made on an application that's any further along the process than PENDING (i.e. a coordinator has taken some action on it) should fire an email to the coordinator who took the last action on it. """ app, request = self._set_up_email_test_objects() request.user = UserFactory() self.assertEqual(len(mail.outbox), 0) # Create a coordinator with a test client session coordinator = EditorCraftRoom(self, Terms=True, Coordinator=True) self.partner.coordinator = coordinator.user self.partner.save() # Approve the application url = reverse("applications:evaluate", kwargs={"pk": app.pk}) response = self.client.post(url, data={"status": Application.QUESTION}, follow=True) comment4 = self._create_comment(app, self.editor) comment_was_posted.send(sender=Comment, comment=comment4, request=request) self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].to, [coordinator.user.email])
def save(self): # resp object is a dictionary. The code key indicates the possible # four states the comment can be in: # * Comment created (http 201), # * Confirmation sent by mail (http 204), # * Comment in moderation (http 202), # * Comment rejected (http 403). site = get_current_site(self.request) resp = { 'code': -1, 'comment': self.form.get_comment_object(site_id=site.id) } resp['comment'].ip_address = self.request.META.get("REMOTE_ADDR", None) if self.request.user.is_authenticated: resp['comment'].user = self.request.user if 'comment_id' in self.data: # an edit comment operation. resp['comment'].comment_id = self.data.get('comment_id') # Signal that the comment is about to be saved responses = comment_will_be_posted.send(sender=TmpXtdComment, comment=resp['comment'], request=self.request) for (receiver, response) in responses: if response is False: resp['code'] = 403 # Rejected. return resp # Replicate logic from django_comments_xtd.views.on_comment_was_posted. if (not settings.COMMENTS_XTD_CONFIRM_EMAIL or self.request.user.is_authenticated): if not views._comment_exists(resp['comment']): new_comment = views._create_comment(resp['comment']) resp['comment'].xtd_comment = new_comment confirmation_received.send(sender=TmpXtdComment, comment=resp['comment'], request=self.request) comment_was_posted.send(sender=new_comment.__class__, comment=new_comment, request=self.request) if resp['comment'].is_public: resp['code'] = 201 views.notify_comment_followers(new_comment) else: resp['code'] = 202 else: key = signed.dumps(resp['comment'], compress=True, extra_key=settings.COMMENTS_XTD_SALT) views.send_email_confirmation_request(resp['comment'], key, site) resp['code'] = 204 # Confirmation sent by mail. return resp
def save(self): # resp object is a dictionary. The code key indicates the possible # four states the comment can be in: # * Comment created (http 201), # * Confirmation sent by mail (http 204), # * Comment in moderation (http 202), # * Comment rejected (http 403). site = get_current_site(self.request) resp = { 'code': -1, 'comment': self.form.get_comment_object(site_id=site.id) } resp['comment'].ip_address = self.request.META.get("REMOTE_ADDR", None) if self.request.user.is_authenticated: resp['comment'].user = self.request.user # Signal that the comment is about to be saved responses = comment_will_be_posted.send(sender=TmpXtdComment, comment=resp['comment'], request=self.request) for (receiver, response) in responses: if response is False: resp['code'] = 403 # Rejected. return resp # Replicate logic from django_comments_xtd.views.on_comment_was_posted. if ( not settings.COMMENTS_XTD_CONFIRM_EMAIL or self.request.user.is_authenticated ): if not views._comment_exists(resp['comment']): new_comment = views._create_comment(resp['comment']) resp['comment'].xtd_comment = new_comment confirmation_received.send(sender=TmpXtdComment, comment=resp['comment'], request=self.request) comment_was_posted.send(sender=new_comment.__class__, comment=new_comment, request=self.request) if resp['comment'].is_public: resp['code'] = 201 views.notify_comment_followers(new_comment) else: resp['code'] = 202 else: key = signed.dumps(resp['comment'], compress=True, extra_key=settings.COMMENTS_XTD_SALT) views.send_email_confirmation_request(resp['comment'], key, site) resp['code'] = 204 # Confirmation sent by mail. return resp
def test_comment_email_sending_5(self): """ A comment from the applying editor made on an application that has had no actions taken on it and no existing comments should not fire an email to anyone. """ app, request = self._set_up_email_test_objects() request.user = UserFactory() self.assertEqual(len(mail.outbox), 0) comment5 = self._create_comment(app, self.editor) comment_was_posted.send(sender=Comment, comment=comment5, request=request) self.assertEqual(len(mail.outbox), 0)
def test_comment_email_sending_1(self): """ A coordinator posts a comment to an Editor's application and an email is send to that Editor. An email is not sent to the coordinator. """ app, request = self._set_up_email_test_objects() request.user = UserFactory() self.assertEqual(len(mail.outbox), 0) comment1 = self._create_comment(app, self.coordinator1) comment_was_posted.send(sender=Comment, comment=comment1, request=request) self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].to, [self.editor.email])
def save(self, request): """ Saves a new comment and sends any notification emails. """ comment = self.get_comment_object() obj = comment.content_object comment.user = request.user user_name = request.user.get_full_name() if not user_name: user_name = request.user.username comment.user_name = user_name # comment.email = request.user.email comment.by_author = request.user == getattr(obj, "user", None) comment.ip_address = ip_for_request(request) comment.replied_to_id = self.data.get("replied_to") # Mezzanine's duplicate check that also checks `replied_to_id`. lookup = { "content_type": comment.content_type, "object_pk": comment.object_pk, "user_name": comment.user_name, "user_email": comment.user_email, "user_url": comment.user_url, "replied_to_id": comment.replied_to_id, } for duplicate in self.get_comment_model().objects.filter(**lookup): if (duplicate.submit_date.date() == comment.submit_date.date() and duplicate.comment == comment.comment): return duplicate comment.save() comment_was_posted.send(sender=comment.__class__, comment=comment, request=request) notify_emails = split_addresses(settings.COMMENTS_NOTIFICATION_EMAILS) if notify_emails: subject = ugettext("New comment for: ") + str(obj) context = { "comment": comment, "comment_url": add_cache_bypass(comment.get_absolute_url()), "request": request, "obj": obj, } send_mail_template(subject, "email/comment_notification", settings.DEFAULT_FROM_EMAIL, notify_emails, context) return comment
def save(self, request): """ Saves a new comment and sends any notification emails. """ comment = self.get_comment_object() obj = comment.content_object if is_authenticated(request.user): comment.user = request.user comment.by_author = request.user == getattr(obj, "user", None) comment.ip_address = ip_for_request(request) comment.replied_to_id = self.data.get("replied_to") # Mezzanine's duplicate check that also checks `replied_to_id`. lookup = { "content_type": comment.content_type, "object_pk": comment.object_pk, "user_name": comment.user_name, "user_email": comment.user_email, "user_url": comment.user_url, "replied_to_id": comment.replied_to_id, } for duplicate in self.get_comment_model().objects.filter(**lookup): if (duplicate.submit_date.date() == comment.submit_date.date() and duplicate.comment == comment.comment): return duplicate comment.save() comment_was_posted.send(sender=comment.__class__, comment=comment, request=request) notify_emails = split_addresses(settings.COMMENTS_NOTIFICATION_EMAILS) if notify_emails: subject = ugettext("New comment for: ") + str(obj) context = { "comment": comment, "comment_url": add_cache_bypass(comment.get_absolute_url()), "request": request, "obj": obj, } send_mail_template( subject, "email/comment_notification", settings.DEFAULT_FROM_EMAIL, notify_emails, context, ) return comment
def test_comment_email_sending_2(self): """ After a coordinator posts a comment, the Editor posts an additional comment. An email is sent to the coordinator who posted the earlier comment. An email is not sent to the editor. """ app, request = self._set_up_email_test_objects() request.user = UserFactory() self.assertEqual(len(mail.outbox), 0) _ = self._create_comment(app, self.coordinator1) comment2 = self._create_comment(app, self.editor) comment_was_posted.send(sender=Comment, comment=comment2, request=request) self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].to, [self.coordinator1.email])
def test_noparent_rootnode_wasadded(self): comment = create_tcomment() comment.save() self.request.POST = {'parent': ''} responses = comment_was_posted.send(sender=comment.__class__, comment=comment, request=self.request) self._assert_responses_true(responses) self.assertTrue(comment.node.is_root())
def test_comment_email_sending_6(self): """ In case the coordinator is changed for a Partner, then the previous coordinator should not receive comment notification email. Also now the new coordinator should receive the email. """ app, request = self._set_up_email_test_objects() request.user = UserFactory() self.assertEqual(len(mail.outbox), 0) # Setting up coordinator1 as coordinator for partner self.partner.coordinator = self.coordinator1 self.partner.save() # Coordinator posts a comment, then Editor posts an additional comment # An email is sent to the coordinator who posted the earlier comment _ = self._create_comment(app, self.coordinator1) comment1 = self._create_comment(app, self.editor) comment_was_posted.send(sender=Comment, comment=comment1, request=request) self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].to, [self.coordinator1.email]) # Create a coordinator with a test client session # and set it as the coordinator for partner coordinator = EditorCraftRoom(self, Terms=True, Coordinator=True) self.partner.coordinator = coordinator.user self.partner.save() # Evaluate the application url = reverse("applications:evaluate", kwargs={"pk": app.pk}) response = self.client.post(url, data={"status": Application.QUESTION}, follow=True) # Editor makes another comment # Now the New Coordinator will receive the Email comment2 = self._create_comment(app, self.editor) comment_was_posted.send(sender=Comment, comment=comment2, request=request) self.assertEqual(mail.outbox[1].to, [coordinator.user.email])
def test_noparent_rootnode_wasadded(self): comment = create_tcomment() comment.save() self.request.POST = {'parent': ''} responses = comment_was_posted.send( sender=comment.__class__, comment=comment, request=self.request) self._assert_responses_true(responses) self.assertTrue(comment.node.is_root())
def post(self, request, *args, **kwargs): #serializer_class = serializers.WriteCommentSerializer result = {} data = request.POST blog = Blog.objects.get(id=4) self.form = get_form()(blog, data=data) #self.form = django_comments.get_form()(blog) print(self.form, "FORM") if self.form.is_valid(): #self.form = form(blog) pass else: print(self.form.errors) site = get_current_site(request) resp = { 'code': -1, 'comment': self.form.get_comment_object(site_id=site.id) } resp['comment'].ip_address = request.META.get("REMOTE_ADDR", None) if request.user.is_authenticated: resp['comment'].user = request.user if (not settings.COMMENTS_XTD_CONFIRM_EMAIL or request.user.is_authenticated): if not comments_views._comment_exists(resp['comment']): new_comment = comments_views._create_comment(resp['comment']) resp['comment'].xtd_comment = new_comment confirmation_received.send(sender=TmpXtdComment, comment=resp['comment'], request=request) comment_was_posted.send(sender=new_comment.__class__, comment=new_comment, request=request) if resp['comment'].is_public: resp['code'] = 201 #views.notify_comment_followers(new_comment) else: resp['code'] = 202 return JsonResponse(result)
def test_parent_childnode_wasadded(self): parent_comment = create_tcomment() parent_comment.save() parent_node = TCommentNode.add_root(comment=parent_comment) comment = create_tcomment() comment.save() self.request.POST = {'parent': parent_node.pk} responses = comment_was_posted.send(sender=comment.__class__, comment=comment, request=self.request) self._assert_responses_true(responses) self.assertEqual(parent_node, comment.node.get_parent())
def test_parent_childnode_wasadded(self): parent_comment = create_tcomment() parent_comment.save() parent_node = TCommentNode.add_root(comment=parent_comment) comment = create_tcomment() comment.save() self.request.POST = {'parent': parent_node.pk} responses = comment_was_posted.send( sender=comment.__class__, comment=comment, request=self.request) self._assert_responses_true(responses) self.assertEqual(parent_node, comment.node.get_parent())