<commit_before>import time
import calendar
import datetime
import os
import random
import json
from urlparse import urlsplit, urlunsplit

from django.core.urlresolvers import reverse
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.decorators.cache import cache_control, never_cache
from django.views.generic import ListView, DetailView, TemplateView
from django.views.generic.detail import SingleObjectMixin
from django.core.cache import cache
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist

from speeches.models import Speaker

from pombola.core import models
from pombola.slug_helpers.views import SlugRedirectMixin, get_slug_redirect


class HomeView(TemplateView):

    template_name = 'home.html'

    def get_context_data(self, **kwargs):

        context = super(HomeView, self).get_context_data(**kwargs)

        before, after = (self.request.GET.get(k) for k in ('before', 'after'))
        current_slug = before or after

        context['featured_person'] = \
            models.Person.objects.get_next_featured(current_slug,
                                                    want_previous=before)

        # For the election homepage produce a list of all the featured people.
        # Shuffle it each time to avoid any bias.
        context['featured_persons'] = list(models.Person.objects.get_featured())
        random.shuffle(context['featured_persons'])

        return context

class HelpApiView(TemplateView):

    template_name = 'core/help_api.html'

    def get_context_data(self, **kwargs):
        context = super(HelpApiView, self).get_context_data(**kwargs)
        # Generate the links to the PopIt API endpoint and the
        # prettier web interface:
        split_url = list(urlsplit(settings.POPIT_API_URL))
        context['popit_api_url'] = urlunsplit(split_url)
        # Remove the path and any query string:
        for i in range(2, len(split_url)):
            split_url[i] = ''
        context['popit_url'] = urlunsplit(split_url)
        # Set the URL path to the nightly JSON exports:
        context['dump_base_path'] = settings.MEDIA_URL + 'popolo_json/'
        return context


class OrganisationList(ListView):
    model = models.Organisation


class SkipHidden(object):

    def get_queryset(self):
        qs = super(SkipHidden, self).get_queryset()
        # Only display person pages for hidden people if the user is
        # logged in and a superuser:
        if self.request.user.is_superuser:
            return qs
        return qs.filter(hidden=False)


class CommentArchiveMixin(object):
    """This checks whether the current page has a matching Disqus
       comment thread in the local /data/disqus.json file, which is a frozen
       copy of the output of an non-admin call to the Disqus API at
       https://disqus.com/api/3.0/forums/listPosts.json?forum=<DISQUS_SHORTNAME>&order=desc&related=thread&limit=100"""

    def check_for_archive_link(self, path):
        if settings.COUNTRY_APP == None:
            return

        try:
            archive_file = os.path.abspath(
                os.path.join(
                    os.path.dirname(os.path.realpath(__file__)),
                    os.pardir,
                    settings.COUNTRY_APP,
                    'data/disqus.json',
                    )
                )
            with open(archive_file) as f:
                archives = json.load(f)
        except IOError:
            return
        for archive in archives['response']:
            disqus_thread_link = archive['thread']['link']
            if urlsplit(disqus_thread_link).path == path:
                return disqus_thread_link

    def get_context_data(self, **kwargs):
        context = super(CommentArchiveMixin, self).get_context_data(**kwargs)
        if settings.FACEBOOK_APP_ID:
            context['archive_link'] = self.check_for_archive_link(self.request.path)
        return context


class SubSlugRedirectMixin(SlugRedirectMixin):
    """This customization of SlugRedirectMixin understands sub pages"""

    def object_to_detail_url_pattern_name(self, o):
        url_pattern_name = o.__class__.__name__.lower()
        try:
            url_pattern_name += '_' + self.sub_page
        except AttributeError:
            pass
        return url_pattern_name

    def redirect_to(self, correct_object):
        pattern = self.object_to_detail_url_pattern_name(correct_object)
        url = reverse(pattern, args=[correct_object.slug])
        return redirect(url)


class BasePersonDetailView(SkipHidden, DetailView):
    model = models.Person

    def get_context_data(self, **kwargs):
        context = super(BasePersonDetailView, self).get_context_data(**kwargs)
        if settings.ENABLED_FEATURES['hansard']:
            hansard_count = self.object.hansard_entries.count()
            if hansard_count:
                context['appearances_sub_link_text'] = \
                    'Appearances ({number_of_appearances})'.format(
                        number_of_appearances=hansard_count
                    )
            else:
                context['appearances_sub_link_text'] = 'Appearances'
        return context

    pass


class PersonDetail(SlugRedirectMixin, BasePersonDetailView):
    pass


class PersonDetailSub(SubSlugRedirectMixin, BasePersonDetailView):
    sub_page = None

    def get_template_names(self):
        return ["core/person_%s.html" % self.sub_page]

class PersonSpeakerMappingsMixin(object):
    def pombola_person_to_sayit_speaker(self, person):
        try:
            expected_popit_id = 'core_person:{0}'.format(person.id)
            return Speaker.objects.get(person__popit_id=expected_popit_id)<commit_msg>Make pombola_person_to_sayit_speaker use the django-popolo Identifier<commit_after># This is needed to avoid an error that there's "no module named
# models" when importing Identifier from popolo.models.
# This was caused because the import mechanism tries the popolo module
# in this same package first.  By forcing only absolute imports, this
# will get the django-popolo package's popolo package instead.
from __future__ import absolute_import

import time
import calendar
import datetime
import os
import random
import json
from urlparse import urlsplit, urlunsplit

from django.core.urlresolvers import reverse
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.decorators.cache import cache_control, never_cache
from django.views.generic import ListView, DetailView, TemplateView
from django.views.generic.detail import SingleObjectMixin
from django.core.cache import cache
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist

from popolo.models import Identifier

from pombola.core import models
from pombola.slug_helpers.views import SlugRedirectMixin, get_slug_redirect


class HomeView(TemplateView):

    template_name = 'home.html'

    def get_context_data(self, **kwargs):

        context = super(HomeView, self).get_context_data(**kwargs)

        before, after = (self.request.GET.get(k) for k in ('before', 'after'))
        current_slug = before or after

        context['featured_person'] = \
            models.Person.objects.get_next_featured(current_slug,
                                                    want_previous=before)

        # For the election homepage produce a list of all the featured people.
        # Shuffle it each time to avoid any bias.
        context['featured_persons'] = list(models.Person.objects.get_featured())
        random.shuffle(context['featured_persons'])

        return context

class HelpApiView(TemplateView):

    template_name = 'core/help_api.html'

    def get_context_data(self, **kwargs):
        context = super(HelpApiView, self).get_context_data(**kwargs)
        # Generate the links to the PopIt API endpoint and the
        # prettier web interface:
        split_url = list(urlsplit(settings.POPIT_API_URL))
        context['popit_api_url'] = urlunsplit(split_url)
        # Remove the path and any query string:
        for i in range(2, len(split_url)):
            split_url[i] = ''
        context['popit_url'] = urlunsplit(split_url)
        # Set the URL path to the nightly JSON exports:
        context['dump_base_path'] = settings.MEDIA_URL + 'popolo_json/'
        return context


class OrganisationList(ListView):
    model = models.Organisation


class SkipHidden(object):

    def get_queryset(self):
        qs = super(SkipHidden, self).get_queryset()
        # Only display person pages for hidden people if the user is
        # logged in and a superuser:
        if self.request.user.is_superuser:
            return qs
        return qs.filter(hidden=False)


class CommentArchiveMixin(object):
    """This checks whether the current page has a matching Disqus
       comment thread in the local /data/disqus.json file, which is a frozen
       copy of the output of an non-admin call to the Disqus API at
       https://disqus.com/api/3.0/forums/listPosts.json?forum=<DISQUS_SHORTNAME>&order=desc&related=thread&limit=100"""

    def check_for_archive_link(self, path):
        if settings.COUNTRY_APP == None:
            return

        try:
            archive_file = os.path.abspath(
                os.path.join(
                    os.path.dirname(os.path.realpath(__file__)),
                    os.pardir,
                    settings.COUNTRY_APP,
                    'data/disqus.json',
                    )
                )
            with open(archive_file) as f:
                archives = json.load(f)
        except IOError:
            return
        for archive in archives['response']:
            disqus_thread_link = archive['thread']['link']
            if urlsplit(disqus_thread_link).path == path:
                return disqus_thread_link

    def get_context_data(self, **kwargs):
        context = super(CommentArchiveMixin, self).get_context_data(**kwargs)
        if settings.FACEBOOK_APP_ID:
            context['archive_link'] = self.check_for_archive_link(self.request.path)
        return context


class SubSlugRedirectMixin(SlugRedirectMixin):
    """This customization of SlugRedirectMixin understands sub pages"""

    def object_to_detail_url_pattern_name(self, o):
        url_pattern_name = o.__class__.__name__.lower()
        try:
            url_pattern_name += '_' + self.sub_page
        except AttributeError:
            pass
        return url_pattern_name

    def redirect_to(self, correct_object):
        pattern = self.object_to_detail_url_pattern_name(correct_object)
        url = reverse(pattern, args=[correct_object.slug])
        return redirect(url)


class BasePersonDetailView(SkipHidden, DetailView):
    model = models.Person

    def get_context_data(self, **kwargs):
        context = super(BasePersonDetailView, self).get_context_data(**kwargs)
        if settings.ENABLED_FEATURES['hansard']:
            hansard_count = self.object.hansard_entries.count()
            if hansard_count:
                context['appearances_sub_link_text'] = \
                    'Appearances ({number_of_appearances})'.format(
                        number_of_appearances=hansard_count
                    )
            else:
                context['appearances_sub_link_text'] = 'Appearances'
        return context

    pass


class PersonDetail(SlugRedirectMixin, BasePersonDetailView):
    pass


class PersonDetailSub(SubSlugRedirectMixin, BasePersonDetailView):
    sub_page = None

    def get_template_names(self):
        return ["core/person_%s.html" % self.sub_page]

class PersonSpeakerMappingsMixin(object):
    def pombola_person_to_sayit_speaker(self, person):
        try:
            expected_popit_id = 'core_person:{0}'.format(person.id)
            i = Identifier.objects.get(
                scheme='PopIt ID',
                identifier=expected_popit_id
            )
            return i.content_object.speaker
{'fraction_non_alphanumeric': 0.080360770293329, 'fraction_numerical': 0.0016250914113918908, 'mean_word_length': 2.7964219617520047, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4899035', 'n_tokens_mistral': 3533, 'n_tokens_neox': 3430, 'n_words': 840}