from django.views.generic import TemplateView
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse

from election_snooper.models import SnoopedElection
from election_snooper.forms import ReviewElectionForm


class SnoopedElectionView(TemplateView):
    template_name = "election_snooper/snooped_election_list.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        queryset = SnoopedElection.objects.all().order_by('date_seen')

        if 'status' in self.request.GET:
            queryset = queryset.filter(status=self.request.GET['status'])

        if 'pk' in self.request.GET:
            queryset = queryset.filter(pk=self.request.GET['pk'])

        object_list = []
        for item in queryset:
            object_list.append(
                ReviewElectionForm(instance=item, prefix=item.pk)
            )
        context['object_list'] = object_list
        return context

    def post(self, request, *args, **kwargs):
        instance = SnoopedElection.objects.get(pk=request.POST.get('pk'))
        form = ReviewElectionForm(
            request.POST, instance=instance, prefix=instance.pk)
        if form.is_valid():
            form.save()
        # TODO: if there's an error it's not processed yet
        return HttpResponseRedirect(reverse('snooped_election_view'))


from django.views.generic import TemplateView
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse

from election_snooper.models import SnoopedElection
from election_snooper.forms import ReviewElectionForm


class SnoopedElectionView(TemplateView):
    template_name = "election_snooper/snooped_election_list.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        queryset = SnoopedElection.objects.all().order_by('-date_seen', 'id')

        if 'status' in self.request.GET:
            queryset = queryset.filter(status=self.request.GET['status'])

        if 'pk' in self.request.GET:
            queryset = queryset.filter(pk=self.request.GET['pk'])

        object_list = []
        for item in queryset:
            object_list.append(
                ReviewElectionForm(instance=item, prefix=item.pk)
            )
        context['object_list'] = object_list
        return context

    def post(self, request, *args, **kwargs):
        instance = SnoopedElection.objects.get(pk=request.POST.get('pk'))
        form = ReviewElectionForm(
            request.POST, instance=instance, prefix=instance.pk)
        if form.is_valid():
            form.save()
        # TODO: if there's an error it's not processed yet
        return HttpResponseRedirect(reverse('snooped_election_view'))


Order radar view more sensibly
Order radar view more sensibly

1. Sort with by date_seen DESC so new stuff is at the top
   and old stuff is at the bottom
2. Sort by id as secondary criteria so order is consistent
   otherwise the order changes a bit every time we make an edit
