import logging
from django.conf import settings
from django.core.urlresolvers import reverse
from django.utils.functional import lazy
from django.conf.urls.defaults import *
from django.template import loader, RequestContext
from django.views.generic import DetailView, ListView 
from django.views.generic.edit import CreateView, UpdateView, DeleteView

from multiblogs.models import Blog, Post
from multiblogs.forms import PostForm

WITHOUT_SETS= getattr(settings, 'MULTIBLOGS_WITHOUT_SETS', False)

if not WITHOUT_SETS:
    from multiblogs.models import BlogSet
    class BlogSetView(DetailView):
        model=BlogSet
        queryset=BlogSet.published_objects.all()
        template_name_field = 'template_name'
    

class BlogDetailView(DetailView):
    model=Blog
    template_name_field = 'template_name'

    def get_queryset(self):
        if not WITHOUT_SETS:
            queryset=Blog.published_objects.filter(blog_set__slug=self.kwargs['blog_set_slug'])
        else:
            queryset=Blog.published_objects.all()
        return queryset

class PostYearListView(ListView):
    model=Post
    template_name_field = 'blog__template_name'

    def get_queryset(self):
        if not WITHOUT_SETS:
            queryset=Post.published_objects.all().filter(blog__blog_set__slug=self.kwargs['blog_set_slug'], blog__slug=self.kwargs['blog_slug'], publish_date__year=self.kwargs['year'])
        else:
            queryset=Post.published_objects.all().filter(blog__slug=self.kwargs['slug'], publish_date__year=self.kwargs['year'])
        return queryset

class PostDetailView(DetailView):
    model=Post
    template_name_field = 'blog__template_name'

    def get_queryset(self):
        if not WITHOUT_SETS:
            queryset=Post.published_objects.all().filter(blog__blog_set__slug=self.kwargs['blog_set_slug'], blog__slug=self.kwargs['blog_slug'], publish_date__year=self.kwargs['year'])
        else:
            queryset=Post.published_objects.all().filter(blog__slug=self.kwargs['slug'], publish_date__year=self.kwargs['year'])
        return queryset

class PostCreateView(CreateView):
    form_class=PostForm
    template_name='multiblogs/post_create.html'

    def get_context_data(self, **kwargs):
        context = super(PostCreateView, self).get_context_data(**kwargs)
        context['blog'] = Blog.published_objects.get(slug=self.kwargs['slug'])
        return context

class PostUpdateView(UpdateView):
    form_class=PostForm
    template_name='multiblogs/post_update.html'
    queryset=Post.objects.all()

    def get_context_data(self, **kwargs):
        context = super(PostUpdateView, self).get_context_data(**kwargs)
        context['blog'] = Blog.published_objects.get(slug=self.kwargs['blog_slug'])
        return context

class PostDeleteView(DeleteView):
    model=Post
    template_name='multiblogs/post_delete.html'
    success_url='/blogs/' 

