Skip to content
This repository has been archived by the owner on Dec 22, 2021. It is now read-only.

Commit

Permalink
article feeds handle tag intersection queries
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanpitts committed Oct 28, 2012
1 parent 957879e commit 7a082c9
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions source/base/feeds.py
Expand Up @@ -11,27 +11,31 @@ class ArticleFeed(Feed):
def get_object(self, request, *args, **kwargs):
self.section = kwargs.get('section', None)
self.category = kwargs.get('category', None)
self.tag_slug = kwargs.get('tag_slug', None)
if self.tag_slug:
self.tag = get_object_or_404(Tag, slug=self.tag_slug)
self.tags = None
self.tag_slugs = kwargs.get('tag_slugs', None)
self.tag_slug_list = []
if self.tag_slugs:
self.tag_slug_list = self.tag_slugs.split('+')
# need to fail if any item in slug list references nonexistent tag
self.tags = [get_object_or_404(Tag, slug=tag_slug) for tag_slug in self.tag_slug_list]
return ''

def title(self, obj):
if self.section:
return "Source: %s" % SECTION_MAP[self.section]['name']
elif self.category:
return "Source: Articles in the category %s" % CATEGORY_MAP[self.category]['name']
elif self.tag_slug:
return "Source: Articles tagged with '%s'" % self.tag.name
elif self.tag_slugs:
return "Source: Articles tagged with '%s'" % "+".join([tag.name for tag in self.tags])
return "Source"

def link(self, obj):
if self.section:
return reverse('article_list_by_section', kwargs={'section': self.section})
elif self.category:
return reverse('article_list_by_category', kwargs={'category': self.category})
elif self.tag_slug:
return reverse('article_list_by_tag', kwargs={'tag_slug': self.tag_slug})
elif self.tag_slugs:
return reverse('article_list_by_tag', kwargs={'tag_slugs': self.tag_slugs})
return reverse('homepage')

def description(self, obj):
Expand All @@ -40,8 +44,8 @@ def description(self, obj):
identifier = "in the %s section" % SECTION_MAP[self.section]['name']
elif self.category:
identifier = "in the %s category" % CATEGORY_MAP[self.category]['name']
elif self.tag_slug:
identifier = "tagged with '%s'" % self.tag.name
elif self.tag_slugs:
identifier = "tagged with '%s'" % "+".join([tag.name for tag in self.tags])
return "Recent articles %s" % identifier

def item_title(self, item):
Expand All @@ -56,8 +60,9 @@ def items(self, obj):
queryset = queryset.filter(article_type__in=SECTION_MAP[self.section]['article_types'])
elif self.category:
queryset = queryset.filter(article_type=self.category)
elif self.tag_slug:
queryset = queryset.filter(tags__slug=self.tag_slug)
elif self.tag_slugs:
for tag_slug in self.tag_slug_list:
queryset = queryset.filter(tags__slug=tag_slug)
return queryset[:20]

class CodeFeed(Feed):
Expand Down

0 comments on commit 7a082c9

Please sign in to comment.