<commit_before>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))
        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)
        if form.is_valid():
            form.save()
        # TODO: if there's an error it's not processed yet
        return HttpResponseRedirect(reverse('snooped_election_view'))
<commit_msg>Set form prefixes<commit_after>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'))

{'fraction_non_alphanumeric': 0.09952433223563849, 'fraction_numerical': 0.0, 'mean_word_length': 3.3259493670886076, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '404777', 'n_tokens_mistral': 788, 'n_tokens_neox': 768, 'n_words': 160}