Ticket #8729: 8729.diff

File 8729.diff, 4.0 KB (added by Kyle Fox, 16 years ago)

Allows filtering of django.contrib.comments feed described in #8729

  • contrib/comments/feeds.py

     
    22from django.contrib.syndication.feeds import Feed
    33from django.contrib.sites.models import Site
    44from django.contrib import comments
     5from django.contrib.contenttypes.models import ContentType
     6from django.db.models import Model
    57
    68class LatestCommentFeed(Feed):
    79    """Feed of latest comments on the current site."""
    810
    9     def title(self):
     11    def title(self, obj):
    1012        if not hasattr(self, '_site'):
    1113            self._site = Site.objects.get_current()
    12         return u"%s comments" % self._site.name
     14        if isinstance(obj, type):
     15            # Feed is filtered according to a ContentType (Model class).
     16            subtitle =  u' on all %s' % unicode(obj._meta.verbose_name_plural)
     17        elif isinstance(obj, Model):
     18            # Feed is filtered according to a model instance.
     19            subtitle =  u' on "%s"' % obj
     20        elif isinstance(obj, type(None)):
     21            # Feed is not filtered.
     22            subtitle = u''
     23        return u"%s comments%s" % (self._site.name, subtitle)
    1324
    1425    def link(self):
    1526        if not hasattr(self, '_site'):
     
    2132            self._site = Site.objects.get_current()
    2233        return u"Latest comments on %s" % self._site.name
    2334
    24     def items(self):
    25         qs = comments.get_model().objects.filter(
    26             site__pk = settings.SITE_ID,
    27             is_public = True,
    28             is_removed = False,
    29         )
     35    def items(self, obj):
     36        filters = {
     37            'site__pk': settings.SITE_ID,
     38            'is_public': True,
     39            'is_removed': False,
     40        }
     41        if obj is None:
     42            qs = comments.get_model().objects.filter(**filters)
     43        else:
     44            # Filter comments according to `obj`, which is
     45            # either a Model class or instance.
     46            qs = comments.get_model().objects.for_model(obj).filter(**filters)
    3047        if getattr(settings, 'COMMENTS_BANNED_USERS_GROUP', None):
    3148            where = ['user_id NOT IN (SELECT user_id FROM auth_users_group WHERE group_id = %s)']
    3249            params = [settings.COMMENTS_BANNED_USERS_GROUP]
     
    3451        return qs[:40]
    3552       
    3653    def item_pubdate(self, item):
    37         return item.submit_date
    38  No newline at end of file
     54        return item.submit_date
     55
     56    def get_object(self, bits):
     57        """
     58        Allows the comment feed to be filtered according to
     59        a particular ContentType or object based on URL bits.
     60       
     61        Feed URLs will have a scheme similar to this, depending on your URLCONF:
     62            /feeds/comments/  (All comments)
     63            /feeds/comments/<app>.<model>/  (Comments for a particular ContentType)
     64            /feeds/comments/<app>.<model>/<pk>/  (Comments for a specific object)
     65        """
     66        comment_model = comments.get_model()
     67        if len(bits) == 0:
     68            # No extra parameters were provided (default feed).
     69            return None
     70        if len(bits)  >= 1:
     71            try:
     72                # First, determine the ContentType.
     73                app_name, model_name = bits[0].split('.')
     74                content_type = ContentType.objects.get(app_label=app_name, name=model_name)
     75            except ValueError:
     76                # Parameters were not in proper format, or were not a ContentType.
     77                raise comment_model.DoesNotExist
     78            if len(bits) == 1:
     79                # User wants a feed for an entire ContentType.
     80                return content_type.model_class()
     81            elif len(bits) == 2:
     82                try:
     83                    # User wants a feed for a specfic instance of ContentType.
     84                    return content_type.get_object_for_this_type(pk=bits[1])
     85                except:
     86                    raise comment_model.DoesNotExist
     87        # Anything else is invalid.
     88        raise comment_model.DoesNotExist
Back to Top