# Create your views here. article_detail, article_category
from django.template import RequestContext
from django.shortcuts import get_object_or_404, render_to_response
from models import Article, Category
from tagging.models import Tag, TaggedItem


def article_detail(request, category_url, slug, extra_context=None):

    context = RequestContext(request)

    article = get_object_or_404(Article.objects.active(), category__local_url=category_url, slug=slug)

    tags = Tag.objects.usage_for_queryset(Article.objects.filter(category__in=article.category.get_descendants(include_self=True)))

    if extra_context is not None:
        contect.update(extra_context)

    context.update({
        'object': article,
        'category' : article.category,
        'tags': tags,
    })
    
    return render_to_response('articles/article_detail.html', context)


def article_category(request, category_url=None, extra_context=None):
    context = RequestContext(request)
    
    if category_url is not None:
        category = get_object_or_404(Category, local_url=category_url)
        articles = Article.objects.active().filter(category=category)
    else:
        articles = Article.objects.active()
        category = None

    tags = Tag.objects.usage_for_queryset(articles)
    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)

    context.update({
        'object_list': articles,
        'tags': tags,
        'tag': tag,
        'category': category,
    })
    
    return render_to_response('articles/article_list.html', context)


@@ -242,16 +242,94 @@
 gedItem%0A
+from django.conf import settings%0Afrom django.http import HttpResponseRedirect%0A
 %0A%0Adef ar
@@ -1224,32 +1224,307 @@
 gory)%0A    else:%0A
+        if getattr(settings, 'ARTICLE_SHOW_FIRST_CATEGORY', False):%0A            # Redirect to the first category%0A            try:%0A                return HttpResponseRedirect(Category.objects.all()%5B0%5D.get_absolute_url())%0A            except IndexError, e:%0A                pass%0A
         articles
@@ -1542,21 +1542,19 @@
 objects.
-activ
+non
 e()%0A    

Change this view - use the ARTICLE_SHOW_FIRST_CATEGORY setting, now works in a way which will not affect other projects which use this app