Skip to content

Commit

Permalink
fix for #7. implementing a comment_removed signal for other apps to l…
Browse files Browse the repository at this point in the history
…isten for. comment_removed is sent when a comment is deleted or when a comment's is_removed attribute is flipped from True to False.
  • Loading branch information
andy committed Jul 10, 2012
1 parent 79a669e commit eb34f76
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 13 deletions.
14 changes: 1 addition & 13 deletions ella_comments/listing_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,6 @@ class MostCommentedListingHandler(CommentListingHandler, RedisListingHandler):
class LastCommentedListingHandler(CommentListingHandler, RedisListingHandler):
PREFIX = 'lastcom'

def comment_pre_save(instance, **kwargs):
if instance.pk:
try:
old_instance = instance.__class__._default_manager.get(pk=instance.pk)
except instance.__class__.DoesNotExist:
return
instance.__pub_info = {
'is_public': old_instance.is_public,
'is_removed': old_instance.is_removed,
}

def comment_post_save(instance, **kwargs):
if hasattr(instance, '__pub_info'):
is_public = instance.is_public and not instance.is_removed
Expand Down Expand Up @@ -138,13 +127,12 @@ def comment_posted(comment, **kwargs):
def connect_signals():
from django.contrib.comments.signals import comment_was_posted
from ella.core.signals import content_published, content_unpublished
from django.db.models.signals import pre_save, post_save
from django.db.models.signals import post_save
content_published.connect(publishable_published)
content_unpublished.connect(publishable_unpublished)

comment_was_posted.connect(comment_posted)

pre_save.connect(comment_pre_save, sender=comments.get_model())
post_save.connect(comment_post_save, sender=comments.get_model())

if client:
Expand Down
26 changes: 26 additions & 0 deletions ella_comments/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import operator

from django.db import models
from django.db.models.signals import pre_save, post_save, post_delete
from django.contrib import comments
from django.contrib.contenttypes.models import ContentType
from django.utils.translation import ugettext_lazy as _
Expand All @@ -13,6 +14,7 @@
from threadedcomments.models import PATH_DIGITS

from ella_comments.listing_handlers import COMCOUNT_KEY
from ella_comments.signals import comment_removed

DEFAULT_COMMENT_OPTIONS = {
'blocked': False,
Expand Down Expand Up @@ -163,3 +165,27 @@ class Meta:
def __unicode__(self):
return u"%s: %s" % (_("Comment Options"), self.target)

# signal handlers for sending comment_removed signals
def comment_pre_save(instance, **kwargs):
if instance.pk:
try:
old_instance = instance.__class__._default_manager.get(pk=instance.pk)
except instance.__class__.DoesNotExist:
return
instance.__pub_info = {
'is_public': old_instance.is_public,
'is_removed': old_instance.is_removed,
}

def comment_post_save(instance, **kwargs):
if hasattr(instance, '__pub_info'):
# if this is a newly removed comment, send the comment_removed signal
if not instance.__pub_info['is_removed'] and instance.is_removed:
comment_removed.send(sender=instance.__class__, comment=instance)

def comment_post_delete(instance, **kwargs):
comment_removed.send(sender=instance.__class__, comment=instance)

pre_save.connect(comment_pre_save, sender=comments.get_model())
post_save.connect(comment_post_save, sender=comments.get_model())
post_delete.connect(comment_post_delete, sender=comments.get_model())
3 changes: 3 additions & 0 deletions ella_comments/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.dispatch import Signal

comment_removed = Signal(providing_args=['comment'])
49 changes: 49 additions & 0 deletions test_ella_comments/test_signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from django.test import TestCase

from ella.utils.test_helpers import create_basic_categories, create_and_place_a_publishable

from test_ella_comments.helpers import create_comment
from ella_comments.signals import comment_removed

from nose import tools

SIGNALED_COMMENT = None
def handle_signal(comment, **kwargs):
global SIGNALED_COMMENT
SIGNALED_COMMENT = comment.id

def reset_signaled_comment():
global SIGNALED_COMMENT
SIGNALED_COMMENT = None

class TestSignals(TestCase):
def setUp(self):
super(TestSignals, self).setUp()
reset_signaled_comment()
comment_removed.connect(handle_signal)
create_basic_categories(self)
create_and_place_a_publishable(self)
self.comment = create_comment(self.publishable, self.publishable.content_type)

def tearDown(self):
comment_removed.disconnect(handle_signal)
reset_signaled_comment()
super(TestSignals, self).tearDown()

def test_deleted_comment_sends_signal(self):
id = self.comment.id
tools.assert_equals(SIGNALED_COMMENT, None)
self.comment.delete()
tools.assert_equals(SIGNALED_COMMENT, id)

def test_moderated_comment_sends_signal(self):
id = self.comment.id
tools.assert_equals(SIGNALED_COMMENT, None)
self.comment.is_removed = True
self.comment.save()
tools.assert_equals(SIGNALED_COMMENT, id)

def test_general_comment_save_doesnt_send_signal(self):
tools.assert_equals(SIGNALED_COMMENT, None)
self.comment.save()
tools.assert_equals(SIGNALED_COMMENT, None)

0 comments on commit eb34f76

Please sign in to comment.