Ticket #15920: comments_banned.diff

File comments_banned.diff, 2.9 KB (added by daniellindsley, 14 years ago)

Patch & tests to add respecting the setting.

  • django/contrib/comments/templatetags/comments.py

    diff --git a/django/contrib/comments/templatetags/comments.py b/django/contrib/comments/templatetags/comments.py
    index 8bc61cd..7c5299e 100644
    a b class BaseCommentNode(template.Node):  
    9191            qs = qs.filter(is_public=True)
    9292        if getattr(settings, 'COMMENTS_HIDE_REMOVED', True) and 'is_removed' in field_names:
    9393            qs = qs.filter(is_removed=False)
     94        if getattr(settings, 'COMMENTS_BANNED_USERS_GROUP', None):
     95            where = ['(user_id IS NULL OR user_id NOT IN (SELECT user_id FROM auth_user_groups WHERE group_id = %s))']
     96            params = [settings.COMMENTS_BANNED_USERS_GROUP]
     97            qs = qs.extra(where=where, params=params)
    9498
    9599        return qs
    96100
  • tests/regressiontests/comment_tests/tests/templatetag_tests.py

    diff --git a/tests/regressiontests/comment_tests/tests/templatetag_tests.py b/tests/regressiontests/comment_tests/tests/templatetag_tests.py
    index 0ead6c2..cd3eb02 100644
    a b class CommentTemplateTagTests(CommentTestCase):  
    6464        self.assertEqual(out, "")
    6565        self.assertEqual(list(ctx["cl"]), [c2])
    6666
     67    def testGetCommentListBanned(self, tag=None):
     68        from django.conf import settings
     69        from django.contrib.auth.models import User, Group
     70       
     71        # Stow.
     72        old_banned_group = getattr(settings, 'COMMENTS_BANNED_USERS_GROUP', None)
     73        c1, c2, c3, c4 = self.createSomeComments()
     74       
     75        try:
     76            banned_group = Group.objects.get(pk=old_banned_group)
     77        except Group.DoesNotExist:
     78            banned_group = Group.objects.create(
     79                name="Banned commentors"
     80            )
     81       
     82        settings.COMMENTS_BANNED_USERS_GROUP = banned_group.pk
     83       
     84        # Ensure it looks right pre-ban.
     85        t = "{% load comments %}" + (tag or "{% get_comment_list for comment_tests.article 1 as cl %}")
     86        ctx, out = self.render(t)
     87        self.assertEqual(out, "")
     88        self.assertEqual(list(ctx["cl"]), [c1, c3])
     89       
     90        # Ban the user.
     91        user = User.objects.get(username='frank_nobody')
     92        user.groups.add(banned_group)
     93       
     94        t = "{% load comments %}" + (tag or "{% get_comment_list for comment_tests.article 1 as cl %}")
     95        ctx, out = self.render(t)
     96        self.assertEqual(out, "")
     97        # The banned user's comment is no longer included.
     98        self.assertEqual(list(ctx["cl"]), [c1])
     99       
     100        # Restore.
     101        if old_banned_group:
     102            settings.COMMENTS_BANNED_USERS_GROUP = old_banned_group
     103        else:
     104            del(settings.COMMENTS_BANNED_USERS_GROUP)
     105
    67106    def testGetCommentListFromLiteral(self):
    68107        self.testGetCommentList("{% get_comment_list for comment_tests.author 1 as cl %}")
    69108
Back to Top