Skip to content

Commit

Permalink
Now calling Sound.post_comment_delete via post_delete signal on Comment
Browse files Browse the repository at this point in the history
  • Loading branch information
ffont committed Oct 23, 2015
1 parent dfaa9cc commit 69f101f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
16 changes: 10 additions & 6 deletions comments/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,26 @@
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.db.models.signals import post_delete


class Comment(models.Model):
user = models.ForeignKey(User)

content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField(db_index=True)
content_object = generic.GenericForeignKey()

comment = models.TextField()

parent = models.ForeignKey('self', null=True, blank=True, related_name='replies', default=None)

parent = models.ForeignKey('self', null=True, blank=True, related_name='replies', default=None)
created = models.DateTimeField(db_index=True, auto_now_add=True)

def __unicode__(self):
return u"%s comment on %s - %s" % (self.user, self.content_type, self.content_type)

class Meta:
ordering = ('-created', )
ordering = ('-created', )


def on_delete_comment(sender, instance, **kwargs):
if instance.content_object.__class__.__name__ == 'Sound':
instance.content_object.post_delete_comment()
post_delete.connect(on_delete_comment, sender=Comment)
7 changes: 1 addition & 6 deletions comments/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#

from comments.models import Comment
from django.conf import settings
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
Expand All @@ -43,11 +42,6 @@ def delete(request, comment_id):
raise PermissionDenied
comment.delete()
messages.success(request, 'Comment deleted.')

if comment.content_type == ContentType.objects.get_for_model(Sound):
sound = comment.content_object
sound.post_delete_comment()

next = request.GET.get("next")
page = request.GET.get("page")
return HttpResponseRedirect(next+"?page="+page)
Expand All @@ -69,6 +63,7 @@ def for_user(request, username):
comment.sound_object = sound_lookup[comment.object_id]
return render_to_response('sounds/comments_for_user.html', combine_dicts(paginator_obj, locals()), context_instance=RequestContext(request))


def all(request):
""" This is all very hacky because GenericRelations don't allow you to span
relations with select_related... hence we get the content_objects and then
Expand Down
13 changes: 13 additions & 0 deletions sounds/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,19 @@ def test_post_delete_comment(self):
self.assertEqual(2, sound.num_comments)
self.assertEqual(sound.is_index_dirty, True)

def test_delete_comment(self):
sound = Sound.objects.get(id=19)
user = User.objects.get(id=2)
current_num_comments = sound.num_comments
comment = Comment(content_object=sound,
user=user,
comment="Test comment")
sound.add_comment(comment)
comment.delete()
sound = Sound.objects.get(id=19)
self.assertEqual(current_num_comments, sound.num_comments)
self.assertEqual(sound.is_index_dirty, True)


def create_user_and_sounds(num_sounds=1, num_packs=0):
user = User.objects.create_user("testuser", password="testpass")
Expand Down

0 comments on commit 69f101f

Please sign in to comment.