<commit_before>from django.shortcuts import render

from django.views.generic import ListView, TemplateView

from .models import CachedCount

class ReportsHomeView(TemplateView):
    template_name = "reports.html"

    def get_context_data(self, **kwargs):
        context = super(ReportsHomeView, self).get_context_data(**kwargs)
        for object_id in (
                'candidates_2015',
                'candidates_2010',
                'new_candidates',
                'standing_again',
                'standing_again_same_party',
                'standing_again_different_party',
        ):
            try:
                context[object_id] = CachedCount.objects.get(
                    object_id=object_id).count
            except CachedCount.DoesNotExist:
                pass
        context['percent_of_2010'] = \
            (100 * float(context['candidates_2015'])) / \
            context['candidates_2010']
        return context

class PartyCountsView(ListView):
    template_name = "party_counts.html"
    queryset = CachedCount.objects.filter(count_type='party')

class ConstituencyCountsView(ListView):
    template_name = "constituency_counts.html"
    queryset = CachedCount.objects.filter(count_type='constituency')<commit_msg>Cope with some cached counts not being present yet<commit_after>from django.shortcuts import render

from django.views.generic import ListView, TemplateView

from .models import CachedCount

class ReportsHomeView(TemplateView):
    template_name = "reports.html"

    def get_context_data(self, **kwargs):
        context = super(ReportsHomeView, self).get_context_data(**kwargs)
        for object_id in (
                'candidates_2015',
                'candidates_2010',
                'new_candidates',
                'standing_again',
                'standing_again_same_party',
                'standing_again_different_party',
        ):
            try:
                context[object_id] = CachedCount.objects.get(
                    object_id=object_id).count
            except CachedCount.DoesNotExist:
                pass
        if context.get('candidates_2010') and 'candidates_2015' in context:
            context['percent_of_2010'] = \
                (100 * float(context['candidates_2015'])) / \
                context['candidates_2010']
        return context

class PartyCountsView(ListView):
    template_name = "party_counts.html"
    queryset = CachedCount.objects.filter(count_type='party')

class ConstituencyCountsView(ListView):
    template_name = "constituency_counts.html"
    queryset = CachedCount.objects.filter(count_type='constituency')
{'fraction_non_alphanumeric': 0.0894278135657446, 'fraction_numerical': 0.02046229632436529, 'mean_word_length': 2.53887399463807, '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': '5793781', 'n_tokens_mistral': 796, 'n_tokens_neox': 717, 'n_words': 127}