from datetime import datetime
from django.http import Http404
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.views.generic import (
    ArchiveIndexView,
    YearArchiveView,
    MonthArchiveView,
)
from tinyblog.models import Post


def post(request, year, month, slug):
    post = get_object_or_404(Post, created__year=year, created__month=month,
                             slug=slug)

    if post.created > datetime.now():
        if not request.user.is_staff:
            raise Http404

    return render_to_response('tinyblog/post.html',
                              {'post': post},
                              context_instance=RequestContext(request))


class TinyBlogIndexView(ArchiveIndexView):
    date_field = 'created'

    def get_queryset(self):
        return Post.published_objects.all()
index_view = TinyBlogIndexView.as_view()


class TinyBlogYearView(YearArchiveView):
    date_field = 'created'
    make_object_list = True

    def get_queryset(self):
        return Post.published_objects.all()
year_view = TinyBlogYearView.as_view()


class TinyBlogMonthView(MonthArchiveView):
    date_field = 'created'
    month_format = '%m'

    def get_queryset(self):
        return Post.published_objects.all()
month_view = TinyBlogMonthView.as_view()

from datetime import datetime
from django.http import Http404
from django.shortcuts import get_object_or_404
from django.views.generic import (
    ArchiveIndexView,
    YearArchiveView,
    MonthArchiveView,
    DetailView,
)
from tinyblog.models import Post


class TinyBlogPostView(DetailView):
    template_name = 'tinyblog/post.html'

    def get_object(self):
        post = get_object_or_404(
            Post,
            created__year=int(self.kwargs['year']),
            created__month=int(self.kwargs['month']),
            slug=self.kwargs['slug']
        )

        if post.created > datetime.now():
            if not self.request.user.is_staff:
                raise Http404
        return post
post = TinyBlogPostView.as_view()


class TinyBlogIndexView(ArchiveIndexView):
    date_field = 'created'

    def get_queryset(self):
        return Post.published_objects.all()
index_view = TinyBlogIndexView.as_view()


class TinyBlogYearView(YearArchiveView):
    date_field = 'created'
    make_object_list = True

    def get_queryset(self):
        return Post.published_objects.all()
year_view = TinyBlogYearView.as_view()


class TinyBlogMonthView(MonthArchiveView):
    date_field = 'created'
    month_format = '%m'

    def get_queryset(self):
        return Post.published_objects.all()
month_view = TinyBlogMonthView.as_view()

Switch the main post detail view to a CBV
Switch the main post detail view to a CBV

  from datetime import datetime
  from django.http import Http404
- from django.shortcuts import render_to_response, get_object_or_404
+ from django.shortcuts import get_object_or_404
- from django.template import RequestContext
  from django.views.generic import (
      ArchiveIndexView,
      YearArchiveView,
      MonthArchiveView,
+     DetailView,
  )
  from tinyblog.models import Post
  
  
+ class TinyBlogPostView(DetailView):
+     template_name = 'tinyblog/post.html'
- def post(request, year, month, slug):
-     post = get_object_or_404(Post, created__year=year, created__month=month,
-                              slug=slug)
  
-     if post.created > datetime.now():
-         if not request.user.is_staff:
-             raise Http404
+     def get_object(self):
+         post = get_object_or_404(
+             Post,
+             created__year=int(self.kwargs['year']),
+             created__month=int(self.kwargs['month']),
+             slug=self.kwargs['slug']
+         )
  
-     return render_to_response('tinyblog/post.html',
-                               {'post': post},
-                               context_instance=RequestContext(request))
+         if post.created > datetime.now():
+             if not self.request.user.is_staff:
+                 raise Http404
+         return post
+ post = TinyBlogPostView.as_view()
  
  
  class TinyBlogIndexView(ArchiveIndexView):
      date_field = 'created'
  
      def get_queryset(self):
          return Post.published_objects.all()
  index_view = TinyBlogIndexView.as_view()
  
  
  class TinyBlogYearView(YearArchiveView):
      date_field = 'created'
      make_object_list = True
  
      def get_queryset(self):
          return Post.published_objects.all()
  year_view = TinyBlogYearView.as_view()
  
  
  class TinyBlogMonthView(MonthArchiveView):
      date_field = 'created'
      month_format = '%m'
  
      def get_queryset(self):
          return Post.published_objects.all()
  month_view = TinyBlogMonthView.as_view()
  
Switch the main post detail view to a CBV
## Code Before:
from datetime import datetime
from django.http import Http404
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.views.generic import (
    ArchiveIndexView,
    YearArchiveView,
    MonthArchiveView,
)
from tinyblog.models import Post


def post(request, year, month, slug):
    post = get_object_or_404(Post, created__year=year, created__month=month,
                             slug=slug)

    if post.created > datetime.now():
        if not request.user.is_staff:
            raise Http404

    return render_to_response('tinyblog/post.html',
                              {'post': post},
                              context_instance=RequestContext(request))


class TinyBlogIndexView(ArchiveIndexView):
    date_field = 'created'

    def get_queryset(self):
        return Post.published_objects.all()
index_view = TinyBlogIndexView.as_view()


class TinyBlogYearView(YearArchiveView):
    date_field = 'created'
    make_object_list = True

    def get_queryset(self):
        return Post.published_objects.all()
year_view = TinyBlogYearView.as_view()


class TinyBlogMonthView(MonthArchiveView):
    date_field = 'created'
    month_format = '%m'

    def get_queryset(self):
        return Post.published_objects.all()
month_view = TinyBlogMonthView.as_view()

## Instruction:
Switch the main post detail view to a CBV
## Code After:
from datetime import datetime
from django.http import Http404
from django.shortcuts import get_object_or_404
from django.views.generic import (
    ArchiveIndexView,
    YearArchiveView,
    MonthArchiveView,
    DetailView,
)
from tinyblog.models import Post


class TinyBlogPostView(DetailView):
    template_name = 'tinyblog/post.html'

    def get_object(self):
        post = get_object_or_404(
            Post,
            created__year=int(self.kwargs['year']),
            created__month=int(self.kwargs['month']),
            slug=self.kwargs['slug']
        )

        if post.created > datetime.now():
            if not self.request.user.is_staff:
                raise Http404
        return post
post = TinyBlogPostView.as_view()


class TinyBlogIndexView(ArchiveIndexView):
    date_field = 'created'

    def get_queryset(self):
        return Post.published_objects.all()
index_view = TinyBlogIndexView.as_view()


class TinyBlogYearView(YearArchiveView):
    date_field = 'created'
    make_object_list = True

    def get_queryset(self):
        return Post.published_objects.all()
year_view = TinyBlogYearView.as_view()


class TinyBlogMonthView(MonthArchiveView):
    date_field = 'created'
    month_format = '%m'

    def get_queryset(self):
        return Post.published_objects.all()
month_view = TinyBlogMonthView.as_view()
