Commits

Andrés Reyes Monge committed d098016

Ahora el RSS filtra por tags
Fix en el listado de posts al usar las tags
Fix en la template del listado de posts

Comments (0)

Files changed (4)

 # -*- coding: utf-8 -*-
 
 from django.contrib.syndication.views import Feed
+from django.shortcuts import get_object_or_404
+from tagging.models import Tag, TaggedItem
 from blog.models import Post
 from django.utils.feedgenerator import Atom1Feed
 
 class RssSiteNewsFeed(Feed):
     title = "armonge"
     link = "/blog/"
-    description = "Updates on changes and additions to chicagocrime.org."
+    description = ""
+
+    def items(self, obj=None):
+        queryset = Post.published.all().order_by('-date_published')
+
+        if obj is not None:
+            queryset = TaggedItem.objects.get_by_model(queryset, obj)
+
+        return queryset[:5]
+
+    def get_object(self, request, tag=None, *args, **kwargs):
+        if tag is not None:
+            return get_object_or_404(Tag, name=tag)
+        super(RssSiteNewsFeed, self).get_object(request, *args, **kwargs)
 
-    def items(self):
-        return Post.published.all().order_by('-date_published')[:5]
 
 class AtomSiteNewsFeed(RssSiteNewsFeed):
     feed_type = Atom1Feed

blog/templates/blog/post_list.html

         <div class="row post">
             <div class="span2">
                 <span class="date"><i class="icon-calendar"></i> {{ post.date_published.date }}</span> <br>
-                <span class="tags">
-                    <i class="icon-tag"></i>
-                    {% for tag in post.tags %}
-                        <a href="{% url blog_post_list_by_tag tag %}">{{ tag }}</a>{% if not forloop.last %}, {% endif %}
-                    {% endfor %}
-                </span> <br>
+                {% if post.tags %}
+                    <span class="tags">
+                        <i class="icon-tag"></i>
+                        {% for tag in post.tags %}
+                            <a href="{% url blog_post_list_by_tag tag %}">{{ tag }}</a>{% if not forloop.last %}, {% endif %}
+                        {% endfor %}
+                    </span> <br>
+                {% endif %}
                 <span class="comments"><i class="icon-comment"></i>
                     <a href="{{ post.get_absolute_url}}#disqus_thread" data-disqus-identifier="{{ post.id }}">Comentarios</a>
                     {% disqus_num_replies %}
 
 urlpatterns = patterns('blog.views',
     (r'^rss/$', RssSiteNewsFeed()),
+    (r'^rss/(?P<tag>[\w-]+)/$', RssSiteNewsFeed()),
     (r'^atom/$', AtomSiteNewsFeed()),
     url(r'^(?P<pk>\d+)/(?P<slug>[\w-]+)/$', PostDetailView.as_view(), name='blog_post_detail'),
     url(r'^(?P<tag>[\w-]+)/$', PostListView.as_view(), name='blog_post_list_by_tag'),
-    url(r'$',PostListView.as_view(), name='blog_post_list'),
+    url(r'^$',PostListView.as_view(), name='blog_post_list'),
 )
+from django.http import Http404
 from django.views.generic import ListView, DetailView
+from tagging.models import TaggedItem
 from blog.models import Post
 
 class PostDetailView(DetailView):
     model = Post
     queryset = Post.published.all()
 
+    def get_queryset(self):
+        queryset = super(PostListView, self).get_queryset()
+
+        if self.kwargs.get('tag'):
+            queryset = TaggedItem.objects.get_by_model(queryset, self.kwargs['tag'])
+            if not queryset:
+                raise Http404
+
+        return queryset