<commit_before>from django.contrib import messages
from django.core.urlresolvers import reverse
from django.views.generic import FormView

from djofx.forms import CategoriseTransactionForm
from djofx import models
from djofx.views.base import PageTitleMixin, UserRequiredMixin


class CategoriseTransactionView(PageTitleMixin, UserRequiredMixin, FormView):
    form_class = CategoriseTransactionForm
    template_name = "djofx/categorise.html"
    page_title = "Categorise payment"

    def get_transaction(self):
        if not hasattr(self, 'transaction_'):
            if 'pk' in self.kwargs:
                self.transaction_ = models.Transaction.objects.get(
                    account__owner=self.request.user,
                    pk=self.kwargs['pk']
                )
            else:
                self.transaction_ = models.Transaction.objects.filter(
                    account__owner=self.request.user,
                    category_verified=False
                ).order_by('?')[0]
        return self.transaction_

    def get_context_data(self, **kwargs):
        ctx = super(CategoriseTransactionView, self).get_context_data(**kwargs)
        ctx['transaction'] = self.get_transaction()
        return ctx

    def get_initial(self):
        transaction = self.get_transaction()

        next_url = None

        if 'keepreferrer' in self.request.GET:
            next_url = self.request.META['HTTP_REFERER']

        return {<commit_msg>Ensure the categorise view correctly handles errors<commit_after>from django.contrib import messages
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect, Http404
from django.views.generic import FormView

from djofx.forms import CategoriseTransactionForm
from djofx import models
from djofx.views.base import PageTitleMixin, UserRequiredMixin


class CategoriseTransactionView(PageTitleMixin, UserRequiredMixin, FormView):
    form_class = CategoriseTransactionForm
    template_name = "djofx/categorise.html"
    page_title = "Categorise payment"

    def dispatch(self, request, *args, **kwargs):
        try:
            self.get_transaction()
        except IndexError:
            messages.info(
                self.request,
                'No transactions need categorising.'
            )
            return HttpResponseRedirect(reverse('djofx_home'))
        except models.Transaction.DoesNotExist:
            raise Http404("Transaction not found.")

        return super(CategoriseTransactionView, self).dispatch(
            request,
            *args,
            **kwargs
        )

    def get_transaction(self):
        if not hasattr(self, 'transaction_'):
            if 'pk' in self.kwargs:
                self.transaction_ = models.Transaction.objects.get(
                    account__owner=self.request.user,
                    pk=self.kwargs['pk']
                )
            else:
                self.transaction_ = models.Transaction.objects.filter(
                    account__owner=self.request.user,
                    category_verified=False
                ).order_by('?')[0]
        return self.transaction_

    def get_context_data(self, **kwargs):
        ctx = super(CategoriseTransactionView, self).get_context_data(**kwargs)
        ctx['transaction'] = self.get_transaction()
        return ctx

    def get_initial(self):
        transaction = self.get_transaction()

        next_url = None

        if 'keepreferrer' in self.request.GET:
            next_url = self.request.META['HTTP_REFERER']

        return {
{'fraction_non_alphanumeric': 0.08237278605566489, 'fraction_numerical': 0.002249086308687096, 'mean_word_length': 2.401529636711281, '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': '5905857', 'n_tokens_mistral': 969, 'n_tokens_neox': 939, 'n_words': 202}