Ticket #16524: 16524.diff

File 16524.diff, 4.6 KB (added by Justin Lilly, 13 years ago)
  • django/contrib/comments/managers.py

    diff --git a/django/contrib/comments/managers.py b/django/contrib/comments/managers.py
    index 499feee..45c81c3 100644
    a b  
    11from django.db import models
     2from django.conf import settings
    23from django.contrib.contenttypes.models import ContentType
    34from django.utils.encoding import force_unicode
    45
    5 class CommentManager(models.Manager):
    6 
     6class CommentQuerySet(models.query.QuerySet):
    77    def in_moderation(self):
    88        """
    99        QuerySet for all comments currently in the moderation queue.
    1010        """
    11         return self.get_query_set().filter(is_public=False, is_removed=False)
     11        return self.filter(is_public=False, is_removed=False)
     12
     13    def not_in_moderation(self):
     14        """
     15        QuerySet for all comments currently not in the moderation queue, i.e.,
     16        all the comments that are publicly viewable.
     17        """
     18        # The is_public and is_removed fields are implementation details of the
     19        # built-in comment model's spam filtering system, so they might not
     20        # be present on a custom comment model subclass. If they exist, we
     21        # should filter on them.
     22        field_names = [f.name for f in self.model._meta.fields]
     23        if 'is_public' in field_names:
     24            qs = self.filter(is_public=True)
     25        if getattr(settings, 'COMMENTS_HIDE_REMOVED', True) and 'is_removed' in field_names:
     26            qs = qs.filter(is_removed=False)
     27        return qs
    1228
    1329    def for_model(self, model):
    1430        """
    class CommentManager(models.Manager):  
    1632        a class).
    1733        """
    1834        ct = ContentType.objects.get_for_model(model)
    19         qs = self.get_query_set().filter(content_type=ct)
     35        qs = self.filter(content_type=ct)
    2036        if isinstance(model, models.Model):
    2137            qs = qs.filter(object_pk=force_unicode(model._get_pk_val()))
    2238        return qs
     39
     40class CommentManager(models.Manager):
     41
     42    def get_query_set(self):
     43        return CommentQuerySet(self.model, using=self._db)
     44
     45    def in_moderation(self):
     46        """
     47        QuerySet for all comments currently in the moderation queue.
     48        """
     49        return self.get_query_set().in_moderation()
     50
     51    def not_in_moderation(self):
     52        """
     53        QuerySet for all comments currently not in the moderation queue, i.e.,
     54        all the comments that are publicly viewable.
     55        """
     56        return self.get_query_set().not_in_moderation()
     57
     58    def for_model(self, model):
     59        """
     60        QuerySet for all comments for a particular model (either an instance or
     61        a class).
     62        """
     63        return self.get_query_set().for_model(model)
  • django/contrib/comments/templatetags/comments.py

    diff --git a/django/contrib/comments/templatetags/comments.py b/django/contrib/comments/templatetags/comments.py
    index ce1825e..0aded1c 100644
    a b class BaseCommentNode(template.Node):  
    7979            site__pk     = settings.SITE_ID,
    8080        )
    8181
    82         # The is_public and is_removed fields are implementation details of the
    83         # built-in comment model's spam filtering system, so they might not
    84         # be present on a custom comment model subclass. If they exist, we
    85         # should filter on them.
    86         field_names = [f.name for f in self.comment_model._meta.fields]
    87         if 'is_public' in field_names:
    88             qs = qs.filter(is_public=True)
    89         if getattr(settings, 'COMMENTS_HIDE_REMOVED', True) and 'is_removed' in field_names:
    90             qs = qs.filter(is_removed=False)
    91 
    92         return qs
     82        return qs.not_in_moderation()
    9383
    9484    def get_target_ctype_pk(self, context):
    9585        if self.object_expr:
  • tests/regressiontests/comment_tests/tests/model_tests.py

    diff --git a/tests/regressiontests/comment_tests/tests/model_tests.py b/tests/regressiontests/comment_tests/tests/model_tests.py
    index 2cbf66f..36be29b 100644
    a b class CommentManagerTests(CommentTestCase):  
    3030        moderated_comments = list(Comment.objects.in_moderation().order_by("id"))
    3131        self.assertEqual(moderated_comments, [c1, c2])
    3232
     33    def testGetCommentsForModel(self):
     34        c1, c2, c3, c4 = self.createSomeComments()
     35        comments_for_model = list(Comment.objects.for_model(Article).order_by("id"))
     36        self.assertEqual(comments_for_model, [c1, c3])
     37
    3338    def testRemovedCommentsNotInModeration(self):
    3439        """Removed comments are not considered in moderation"""
    3540        c1, c2, c3, c4 = self.createSomeComments()
Back to Top