from django.contrib.syndication.views import Feed
from django.core.urlresolvers import reverse
from django.shortcuts import get_object_or_404

from source.articles.models import Article
from source.articles.views import CATEGORY_MAP, SECTION_MAP
from source.code.models import Code
from taggit.models import Tag

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)
        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
        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})
        return reverse('homepage')

    def description(self, obj):
        identifier = 'from Source'
        if self.section:
            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
        return "Recent articles %s" % identifier

    def item_title(self, item):
        return item.title

    def item_description(self, item):
        return item.summary
        
    def items(self, obj):
        queryset = Article.live_objects.all()
        if self.section:
            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)
        return queryset[:20]

class CodeFeed(Feed):
    def get_object(self, request, *args, **kwargs):
        self.tag_slug = kwargs.get('tag_slug', None)
        if self.tag_slug:
            self.tag = get_object_or_404(Tag, slug=self.tag_slug)
        return ''

    def title(self, obj):
        identifier = ""
        if self.tag_slug:
            identifier = " tagged with '%s'" % self.tag.name
        return "Source: Code%s" % identifier

    def link(self, obj):
        if self.tag_slug:
            return reverse('code_list_by_tag', kwargs={'tag_slug': self.tag_slug})
        return reverse('code_list')

    def description(self, obj):
        identifier = " from Source"
        if self.tag_slug:
            identifier = " tagged with '%s'" % self.tag.name
        return "Recent code index pages%s" % identifier

    def item_title(self, item):
        return item.name

    def item_description(self, item):
        return item.description

    def items(self, obj):
        queryset = Code.live_objects.order_by('-created')
        if self.tag_slug:
            queryset = queryset.filter(tags__slug=self.tag_slug)
        return queryset[:20]


@@ -2462,32 +2462,57 @@
 rgs, **kwargs):%0A
+        self.tags = None%0A
         self.tag
@@ -2508,32 +2508,33 @@
    self.tag_slug
+s
  = kwargs.get('t
@@ -2532,32 +2532,33 @@
 gs.get('tag_slug
+s
 ', None)%0A       
@@ -2542,32 +2542,64 @@
 g_slugs', None)%0A
+        self.tag_slug_list = %5B%5D%0A
         if self.
@@ -2598,32 +2598,33 @@
 if self.tag_slug
+s
 :%0A            se
@@ -2621,35 +2621,175 @@
         self.tag
- = 
+_slug_list = self.tag_slugs.split('+')%0A            # need to fail if any item in slug list references nonexistent tag%0A            self.tags = %5B
 get_object_or_40
@@ -2792,32 +2792,58 @@
 r_404(Tag, slug=
+tag_slug) for tag_slug in 
 self.tag_slug)%0A 
@@ -2831,33 +2831,38 @@
 in self.tag_slug
-)
+_list%5D
 %0A        return 
@@ -2926,37 +2926,33 @@
      if self.tag
-_slug
+s
 :%0A            id
@@ -2967,37 +2967,32 @@
  = %22 tagged 
-with 
 '%25s'%22 %25 
 self.tag.nam
@@ -2975,37 +2975,65 @@
 ged '%25s'%22 %25 
-self.tag.name
+%22+%22.join(%5Btag.name for tag in self.tags%5D)
 %0A        ret
@@ -3108,32 +3108,33 @@
 if self.tag_slug
+s
 :%0A            re
@@ -3175,32 +3175,33 @@
 wargs=%7B'tag_slug
+s
 ': self.tag_slug
@@ -3192,32 +3192,33 @@
 ': self.tag_slug
+s
 %7D)%0A        retur
@@ -3325,32 +3325,33 @@
 if self.tag_slug
+s
 :%0A            id
@@ -3366,37 +3366,32 @@
  = %22 tagged 
-with 
 '%25s'%22 %25 
 self.tag.nam
@@ -3374,37 +3374,65 @@
 ged '%25s'%22 %25 
-self.tag.name
+%22+%22.join(%5Btag.name for tag in self.tags%5D)
 %0A        ret
@@ -3690,34 +3690,47 @@
 eated')%0A        
-i
 f
+or tag_slug in
  self.tag_slug:%0A
@@ -3719,32 +3719,37 @@
 in self.tag_slug
+_list
 :%0A            qu
@@ -3776,37 +3776,32 @@
 lter(tags__slug=
-self.
 tag_slug)%0A      

handle tag intersection queries for code feed