<commit_before>import re
import urllib2
import time
import calendar

from django.db.models import Count
from django.db.models import Q
from django.http import HttpResponse
from django.shortcuts  import render_to_response, get_object_or_404, redirect
from django.template   import RequestContext
from django.views.generic.list_detail import object_detail, object_list
from django.views.decorators.cache import cache_control, never_cache
from django.views.generic import ListView, DetailView
from django.core.cache import cache
from django.conf import settings
from django.http import Http404

from mzalendo.core import models
from mzalendo.helpers import geocode

def home(request):
    """Homepage"""
    current_slug = False
    if request.GET.get('after'):
        current_slug = request.GET.get('after')
    elif request.GET.get('before'):
        current_slug = request.GET.get('before')
    featured_person = models.Person.objects.get_next_featured(current_slug, request.GET.get('before'))
    return render_to_response(
        'home.html',
        {
          'featured_person': featured_person,
        },
        context_instance=RequestContext(request)
    )

def organisation_list(request):
    return object_list(
        request,
        queryset=models.Organisation.objects.all(),
    )

def person(request, slug):
    return object_detail(
        request,
        queryset = models.Person.objects,
        slug     = slug,
    )

class PlaceDetailView(DetailView):
    model = models.Place

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(PlaceDetailView, self).get_context_data(**kwargs)
        context['place_type_count'] = models.Place.objects.filter(kind=self.object.kind).count()
        return context


def place_kind(request, slug=None):

    if slug and slug != 'all':
        kind = get_object_or_404(
            models.PlaceKind,
            slug=slug
        )
        queryset = kind.place_set.all()
    else:
        kind = None
        queryset = models.Place.objects.all()

    return object_list(
        request,
        queryset = queryset,
        extra_context = {
            'kind':       kind,
            'all_kinds':  models.PlaceKind.objects.all(),
        },
    )


def position(request, slug):
    title = get_object_or_404(
        models.PositionTitle,
        slug=slug
    )
    
    positions = title.position_set.all().currently_active().order_by('place')
    
    place_slug = request.GET.get('place_slug')
    if place_slug:
        positions = positions.filter(
            Q(place__slug=place_slug) | Q(place__parent_place__slug=place_slug)
        )
    
    # see if we should show the grid
    view = request.GET.get('view', 'list')

    if view == 'grid':
        template = 'core/position_detail_grid.html'
        places   = [] # not relevant to this view
    else:
        template = 'core/position_detail.html'

        # This is an expensive query. Alternative is to have some sort of config that
        # links job titles to relevant place kinds - eg MP :: constituency. Even that
        # would fail for some types of position though.
        child_places = [x.place for x in positions.distinct('place').order_by('place__name')]
        
        # Extract all the parent places too
        parent_places = [x.parent_place for x in child_places]
        parent_places = sorted( set( parent_places ), key=lambda item: item.name ) 
        
        # combine the places into a single list for the search drop down
        places = []
        places.extend(parent_places)
        places.extend(child_places)


    return render_to_response(
        template,
        {
            'object':     title,
            'positions':  positions,
            'places':     places,
            'place_slug': place_slug,
        },
        context_instance=RequestContext(request)
    )


def organisation(request, slug):
    org = get_object_or_404(
        models.Organisation,
        slug=slug
    )
<commit_msg>Fix an error when .parent_place is None<commit_after>import re
import urllib2
import time
import calendar

from django.db.models import Count
from django.db.models import Q
from django.http import HttpResponse
from django.shortcuts  import render_to_response, get_object_or_404, redirect
from django.template   import RequestContext
from django.views.generic.list_detail import object_detail, object_list
from django.views.decorators.cache import cache_control, never_cache
from django.views.generic import ListView, DetailView
from django.core.cache import cache
from django.conf import settings
from django.http import Http404

from mzalendo.core import models
from mzalendo.helpers import geocode

def home(request):
    """Homepage"""
    current_slug = False
    if request.GET.get('after'):
        current_slug = request.GET.get('after')
    elif request.GET.get('before'):
        current_slug = request.GET.get('before')
    featured_person = models.Person.objects.get_next_featured(current_slug, request.GET.get('before'))
    return render_to_response(
        'home.html',
        {
          'featured_person': featured_person,
        },
        context_instance=RequestContext(request)
    )

def organisation_list(request):
    return object_list(
        request,
        queryset=models.Organisation.objects.all(),
    )

def person(request, slug):
    return object_detail(
        request,
        queryset = models.Person.objects,
        slug     = slug,
    )

class PlaceDetailView(DetailView):
    model = models.Place

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(PlaceDetailView, self).get_context_data(**kwargs)
        context['place_type_count'] = models.Place.objects.filter(kind=self.object.kind).count()
        return context


def place_kind(request, slug=None):

    if slug and slug != 'all':
        kind = get_object_or_404(
            models.PlaceKind,
            slug=slug
        )
        queryset = kind.place_set.all()
    else:
        kind = None
        queryset = models.Place.objects.all()

    return object_list(
        request,
        queryset = queryset,
        extra_context = {
            'kind':       kind,
            'all_kinds':  models.PlaceKind.objects.all(),
        },
    )


def position(request, slug):
    title = get_object_or_404(
        models.PositionTitle,
        slug=slug
    )
    
    positions = title.position_set.all().currently_active().order_by('place')
    
    place_slug = request.GET.get('place_slug')
    if place_slug:
        positions = positions.filter(
            Q(place__slug=place_slug) | Q(place__parent_place__slug=place_slug)
        )
    
    # see if we should show the grid
    view = request.GET.get('view', 'list')

    if view == 'grid':
        template = 'core/position_detail_grid.html'
        places   = [] # not relevant to this view
    else:
        template = 'core/position_detail.html'

        # This is an expensive query. Alternative is to have some sort of config that
        # links job titles to relevant place kinds - eg MP :: constituency. Even that
        # would fail for some types of position though.
        child_places = [x.place for x in positions.distinct('place').order_by('place__name')]
        
        # Extract all the parent places too
        parent_places = [x.parent_place for x in child_places if x.parent_place]
        parent_places = sorted( set( parent_places ), key=lambda item: item.name ) 
        
        # combine the places into a single list for the search drop down
        places = []
        places.extend(parent_places)
        places.extend(child_places)


    return render_to_response(
        template,
        {
            'object':     title,
            'positions':  positions,
            'places':     places,
            'place_slug': place_slug,
        },
        context_instance=RequestContext(request)
    )


def organisation(request, slug):
    org = get_object_or_404(
        models.Organisation,
        slug=slug
    )

{'fraction_non_alphanumeric': 0.0898159509202454, 'fraction_numerical': 0.00392638036809816, 'mean_word_length': 2.5828571428571427, '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': '5411498', 'n_tokens_mistral': 2358, 'n_tokens_neox': 2307, 'n_words': 586}