Commits

Max Peterson committed cc96c92

Converted categories to CBVs; Fixed (app) urls

Comments (0)

Files changed (3)

articles/modules/category/extensions/category.py

 from django.db.models import Q
 from django.utils.translation import ugettext_lazy as _
 from django.conf.urls.defaults import patterns, url
+from feincms.content.application import models as app_models
 
 def register(cls, admin_cls):
     cls.add_to_class('category', models.ForeignKey('articles.Category', verbose_name=_('category')))
 
     @classmethod
     def get_urlpatterns(cls):
-        return patterns('articles.modules.category.views',
-                url(r'^(?P<category_url>[a-z0-9_/-]+/)(?P<slug>[a-z0-9_-]+)/$', 'article_detail', name="article_detail"),
-                url(r'^(?P<category_url>[a-z0-9_/-]+/)$', 'article_category', name='article_category'),
-                url(r'^$', 'article_category', name='article_index'),
+        from articles.modules.category import views
+        return patterns('',
+                url(r'^(?P<category_url>[a-z0-9_/-]+/)(?P<slug>[a-z0-9_-]+)/$', views.CategoryArticleDetail.as_view(), name="article_detail"),
+                url(r'^(?P<category_url>[a-z0-9_/-]+/)$', views.CategoryArticleList.as_view(), name='article_category'),
+                url(r'^$', views.CategoryArticleList.as_view(), name='article_index'),
        ) 
     cls.get_urlpatterns = get_urlpatterns
         
 
     def get_absolute_url(self):
-        return ('article_detail', (), {
+        return ('article_detail', 'articles.urls', (), {
                 'category_url': self.category.local_url,
                 'slug': self.slug,
                 })
-    cls.get_absolute_url = models.permalink(get_absolute_url)
+    cls.get_absolute_url = app_models.permalink(get_absolute_url)
 
     # TODO: What happened to active_filters??
     #def active_filter(queryset, user=None):

articles/modules/category/models.py

 from denorm import denormalized, depend_on_related
 from incuna.db.models import AutoSlugField
 from articles.models import Article
+from feincms.content.application import models as app_models
 
+# TODO: Use feincms.utils.managers.ActiveAwareContentManagerMixin
 class CategoryManager(models.Manager):
 
     def active_query(self, user=None):
     def __unicode__(self):
         return self.name
 
-    @models.permalink
+    @app_models.permalink
     def get_absolute_url(self):
-        return ('article_category', (self.local_url,))
+        return ('article_category', 'articles.urls', (self.local_url,))
 
 mptt.register(Category)
 

articles/modules/category/views.py

-from django.template import RequestContext
-from django.shortcuts import get_object_or_404, render_to_response
-from articles.views import ArticleDetail
+from django.shortcuts import get_object_or_404
+from articles.views import ArticleDetail, ArticleList
 from articles.models import Article
 from models import Category
 #from tagging.models import Tag, TaggedItem
     template_name = "articles/category_article_detail.html"
     def get_queryset(self):
         return super(CategoryArticleDetail, self).get_queryset().filter(category__local_url=self.kwargs['category_url'])
-article_detail = CategoryArticleDetail.as_view()
 
-def article_category(request, category_url=None, extra_context=None):
-    context = RequestContext(request)
 
-    articles = Article.objects.active()
-    if category_url is not None:
-        category = get_object_or_404(Category.objects.active(user=request.user), local_url=category_url)
-        if getattr(settings, 'ARTICLE_SHOW_DESCENDANTS', False):
-            articles = articles.filter(category__in=category.get_descendants(include_self=True)).order_by(category.order_by)
-        else:
-            articles = articles.filter(category=category).order_by(category.order_by)
-    else:
-        if getattr(settings, 'ARTICLE_SHOW_FIRST_CATEGORY', False):
-            # Redirect to the first category
-            try:
-                return HttpResponseRedirect(Category.objects.active(user=request.user)[0].get_absolute_url())
-            except IndexError, e:
-                pass
-        category = None
-
-    #tag = None
-    #if request.GET and 'tag' in request.GET:
-    #    try:
-    #        tag = Tag.objects.get(name=request.GET['tag'])
-    #    except Tag.DoesNotExist:
-    #        pass
-    #    else:
-    #        articles = TaggedItem.objects.get_union_by_model(articles, [tag])
-
-    if extra_context is not None:
-        contect.update(extra_context)
+class CategoryArticleList(ArticleList):
+    template_name = "articles/category_article_list.html"
+    def get_context_data(self, **kwargs):
+        context = super(CategoryArticleList, self).get_context_data(**kwargs)
 
-    context.update({
-        'object_list': articles,
-        #'tag': tag,
-        'category': category,
-    })
-    
-    return render_to_response('articles/category_article_list.html', context)
+        category = None
+        if 'category_url' in self.kwargs:
+            # TODO: This is done TWICE
+            category = get_object_or_404(Category.objects.active(), local_url=self.kwargs['category_url'])
+        
+        context['category'] = category
+
+        return context
+        
+    def get_queryset(self):
 
+        articles = super(CategoryArticleList, self).get_queryset()
+        #.filter(category__local_url=self.kwargs['category_url'])
+        if 'category_url' in self.kwargs:
+            # TODO: This is done TWICE
+            category = get_object_or_404(Category.objects.active(), local_url=self.kwargs['category_url'])
 
+            if getattr(settings, 'ARTICLE_SHOW_DESCENDANTS', False):
+                articles = articles.filter(category__in=category.get_descendants(include_self=True)).order_by(category.order_by)
+            else:
+                articles = articles.filter(category=category).order_by(category.order_by)
+        else:
+            if getattr(settings, 'ARTICLE_SHOW_FIRST_CATEGORY', False):
+                # Redirect to the first category
+                try:
+                    return HttpResponseRedirect(Category.objects.active()[0].get_absolute_url())
+                except IndexError, e:
+                    pass
+            category = None
+
+        return articles