from django.http import HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.template import RequestContext
from django.shortcuts import render, render_to_response, get_object_or_404
from django.views.generic.base import View
from django.views.generic import ListView
from .models import Quiz
from .forms import QuizForm

class QuizHome(ListView):
    #model = Quiz
    queryset = Quiz.live.all()

    #def get_context_data(self, **kwargs):
    #    context = super(QuizHome, self).get_context_data(**kwargs)
    #    context['myvar'] = 'my variable goes here'
    #    return context

class QuizBasicDetail(View):
    greeting = 'Hola'

    def get(self, request, *args, **kwargs):
        if 'slug' in self.kwargs:
            slug = self.kwargs['slug']
            object = Quiz.objects.get(slug=slug, status = Quiz.LIVE_STATUS)

        return render(request, 
                      'vodkamartiniquiz/quiz_detail.html',
                      {
                        'object': object,
                        'slug': slug,
                        'greeting': self.greeting,
                      },
                      )

class QuizCreate(View):
    form_class = QuizForm
    initial = {'title': 'Quiz X', 'body': 'This is all about X'}
    template_name = 'vodkamartiniquiz/quiz_form.html'

    def get(self, request, *args, **kwargs):
        form = self.form_class(initial=self.initial)
        return render(request, self.template_name, {'form': form})

    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST)
        if form.is_valid():
            # process form cleaned_data
            quiz = form.save()
            print quiz
            #return HttpResponseRedirect(object.get_absolute_url())
            return HttpResponseRedirect(reverse('vodkamartiniquiz_quiz_home'))
        return render(request, self.template_name, {'form': form})

@@ -97,16 +97,66 @@
 reverse%0A
+from django.core.urlresolvers import reverse_lazy%0A
 from dja
@@ -346,24 +346,71 @@
 rt ListView%0A
+from django.views.generic.edit import FormView%0A
 from .models
@@ -451,16 +451,17 @@
 izForm%0A%0A
+%0A
 class Qu
@@ -1259,16 +1259,17 @@
      )%0A%0A
+#
 class Qu
@@ -1280,24 +1280,25 @@
 eate(View):%0A
+#
     form_cla
@@ -1311,16 +1311,17 @@
 uizForm%0A
+#
     init
@@ -1377,16 +1377,17 @@
 out X'%7D%0A
+#
     temp
@@ -1432,17 +1432,19 @@
 m.html'%0A
-%0A
+#%0A#
     def 
@@ -1472,32 +1472,33 @@
 rgs, **kwargs):%0A
+#
         form = s
@@ -1534,16 +1534,17 @@
 nitial)%0A
+#
         
@@ -1602,17 +1602,19 @@
  form%7D)%0A
-%0A
+#%0A#
     def 
@@ -1643,32 +1643,33 @@
 rgs, **kwargs):%0A
+#
         form = s
@@ -1697,16 +1697,17 @@
 t.POST)%0A
+#
         
@@ -1726,16 +1726,17 @@
 alid():%0A
+#
         
@@ -1767,16 +1767,17 @@
 ed_data%0A
+#
         
@@ -1799,16 +1799,17 @@
 .save()%0A
+#
         
@@ -1819,24 +1819,25 @@
  print quiz%0A
+#
             
@@ -1892,16 +1892,17 @@
 _url())%0A
+#
         
@@ -1972,16 +1972,17 @@
 home'))%0A
+#
         
@@ -2020,28 +2020,454 @@
 plate_name, %7B'form': form%7D)%0A
+%0Aclass QuizCreate(FormView):%0A    form_class = QuizForm%0A    initial = %7B'title': 'Quiz X', 'body': 'This is all about X'%7D%0A    template_name = 'vodkamartiniquiz/quiz_form.html'%0A    success_url = reverse_lazy('vodkamartiniquiz_quiz_home') # use reverse_lazy because URLconf is not processed yet%0A%0A    def form_valid(self, form):%0A        quiz = form.save()%0A        print quiz%0A        return super(QuizCreate, self).form_valid(form)%0A

use generic class base view FormView