Ticket #16524: comments-add-queryset-methods.diff

File comments-add-queryset-methods.diff, 3.6 KB (added by Mathieu Pillard, 13 years ago)
  • managers.py

     
    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)
    12 
     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
     28       
    1329    def for_model(self, model):
    1430        """
    1531        QuerySet for all comments for a particular model (either an instance or
    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)
     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)
     64   
  • templatetags/comments.py

     
    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)
     82        return qs.not_in_moderation()
    9183
    92         return qs
    93 
    9484    def get_target_ctype_pk(self, context):
    9585        if self.object_expr:
    9686            try:
Back to Top