from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.views.generic.base import TemplateView
from django.views.generic.edit import FormView
from django.views.generic.detail import SingleObjectMixin
from django.views.decorators.cache import never_cache
from django.core.urlresolvers import reverse_lazy

from notorhot.models import Competition, Candidate
from notorhot.forms import VoteForm
from notorhot.utils import NeverCacheMixin, ExecutableQuerysetMixin


class CompetitionView(NeverCacheMixin, TemplateView):
    template_name = 'notorhot/competition.html'
    insufficient_data_template_name = 'notorhot/insufficient_data.html'
    http_method_names = ['get',]
    
    def get_context_data(self):
        context = super(CompetitionView, self).get_context_data()
        
        try:
            competition = Competition.objects.generate()
        except Candidate.DoesNotExist:
            # Not enough data.  Try again later
            self.template_name = self.insufficient_data_template_name
            return context
        
        context.update({
            'competition': competition,
        })        
        return context


class VoteView(NeverCacheMixin, ExecutableQuerysetMixin, SingleObjectMixin, 
        FormView):
    template_name = 'notorhot/already_voted.html'
    http_method_names = ['post',]
    form_class = VoteForm
    queryset_executable = Competition.votable.all
    success_url = reverse_lazy('notorhot_competition')
    
    def dispatch(self, *args, **kwargs):
        self.object = self.get_object()
        return super(VoteView, self).dispatch(*args, **kwargs)
        
    def form_valid(self, form):
        form.save()
        return super(VoteView, self).form_valid(form)
    
    def get_form_kwargs(self):        
        kwargs = super(VoteView, self).get_form_kwargs()
        kwargs.update({'competition': self.object})
        return kwargs
@@ -723,24 +723,377 @@
 get',%5D%0A    %0A
+    def get_previous_vote(self):%0A        previous = None%0A        previous_pk = self.request.session.get('last_vote_pk')%0A        %0A        if previous_pk is not None:%0A            try:%0A                previous = Competition.objects.get(pk=previous_pk)%0A            except Competition.DoesNotExist:%0A                pass%0A        %0A        return previous%0A    %0A
     def get_
@@ -1104,32 +1104,32 @@
 ext_data(self):%0A
-
         context 
@@ -1515,16 +1515,71 @@
 tition,%0A
+            'previous_vote': self.get_previous_vote(),%0A
         
@@ -2115,16 +2115,16 @@
  form):%0A
-
         
@@ -2131,24 +2131,86 @@
 form.save()%0A
+        self.request.session%5B'last_vote_pk'%5D = self.object.pk%0A
         retu

Enable retrieval of previous vote for optional display on next competition