def test_comment_update_signal(self): """ update comment, emit signal """ def comment_pre_update_handler(sender, comment, **kwargs): self._comment_old = comment comment_pre_update.connect(comment_pre_update_handler) def comment_post_update_handler(sender, comment, **kwargs): self._comment_new = comment comment_post_update.connect(comment_post_update_handler) utils.login(self) comment_posted = utils.create_comment(user=self.user, topic=self.topic) form_data = { 'comment': 'barfoo', } response = self.client.post( reverse('spirit:comment-update', kwargs={ 'pk': comment_posted.pk, }), form_data) self.assertEqual(repr(self._comment_new), repr(Comment.objects.get(pk=comment_posted.pk))) self.assertEqual(repr(self._comment_old), repr(comment_posted))
def test_comment_update_signal(self): """ update comment, emit signal """ def comment_pre_update_handler(sender, comment, **kwargs): self._comment_old = comment comment_pre_update.connect(comment_pre_update_handler) def comment_post_update_handler(sender, comment, **kwargs): self._comment_new = comment comment_post_update.connect(comment_post_update_handler) utils.login(self) comment_posted = utils.create_comment(user=self.user, topic=self.topic) form_data = {'comment': 'barfoo', } response = self.client.post(reverse('spirit:comment-update', kwargs={'pk': comment_posted.pk, }), form_data) self.assertEqual(repr(self._comment_new), repr(Comment.objects.get(pk=comment_posted.pk))) self.assertEqual(repr(self._comment_old), repr(comment_posted))
comment_html = models.TextField(_("comment html")) date = models.DateTimeField(auto_now_add=True) class Meta: app_label = 'spirit' ordering = ['-date', ] verbose_name = _("comment history") verbose_name_plural = _("comments history") def get_absolute_url(self): return reverse('spirit:comment-history', kwargs={'pk': str(self.id), }) def __unicode__(self): return "%s: %s..." % (self.comment_fk.user.username, self.comment_html[:50]) def comment_pre_update_handler(sender, comment, **kwargs): # save original comment exists = CommentHistory.objects.filter(comment_fk=comment).exists() if not exists: CommentHistory.objects.create(comment_fk=comment, comment_html=comment.comment_html, date=comment.date) def comment_post_update_handler(sender, comment, **kwargs): CommentHistory.objects.create(comment_fk=comment, comment_html=comment.comment_html) comment_pre_update.connect(comment_pre_update_handler, dispatch_uid=__name__) comment_post_update.connect(comment_post_update_handler, dispatch_uid=__name__)