<commit_before># -*- coding: utf-8 -*-

from __future__ import unicode_literals

import re

from django.db.models import Prefetch
from django.core.urlresolvers import reverse
from django.http import Http404, HttpResponseBadRequest
from django.views.generic import TemplateView
from django.utils.text import slugify
from django.utils.translation import ugettext as _
from django.shortcuts import get_object_or_404

from candidates.models import AreaExtra, MembershipExtra
from candidates.models.auth import get_edits_allowed

from elections.models import AreaType, Election

from ..forms import NewPersonForm, ToggleLockForm
from .helpers import (
    split_candidacies, group_candidates_by_party,
    get_person_form_fields
)

class AreasView(TemplateView):
    template_name = 'candidates/areas.html'

    def get(self, request, *args, **kwargs):
        self.types_and_areas = []
        for type_and_area in kwargs['type_and_area_ids'].split(','):
            m = re.search(r'^([A-Z0-9]+?)-([-a-zA-Z0-9\:_]+)$', type_and_area)
            if not m:
                message = _("Malformed type and area: '{0}'")
                return HttpResponseBadRequest(message.format(type_and_area))
            self.types_and_areas.append(m.groups())
        response = super(AreasView, self).get(request, *args, **kwargs)
        return response

    def get_context_data(self, **kwargs):
        context = super(AreasView, self).get_context_data(**kwargs)
        all_area_names = set()
        context['posts'] = []
        any_area_found = False
        no_data_areas = []
        for area_type_code, area_id in self.types_and_areas:
            try:
                area_extra = AreaExtra.objects \
                    .select_related('base', 'type') \
                    .prefetch_related('base__posts') \
                    .get(base__identifier=area_id)
                any_area_found = True
            except AreaExtra.DoesNotExist:
                continue

            if area_type_code == "NODATA":
                no_data_areas.append(area_extra)
                continue

            area = area_extra.base
            all_area_names.add(area.name)
            for post in area.posts.all():
                post_extra = post.extra
                try:
                    election = post_extra.elections.get(current=True)
                except Election.DoesNotExist:
                    continue
                locked = post_extra.candidates_locked
                extra_qs = MembershipExtra.objects.select_related('election')
                current_candidacies, _ = split_candidacies(
                    election,
                    post.memberships.prefetch_related(
                        Prefetch('extra', queryset=extra_qs)
                    ).select_related(
                        'person', 'person__extra', 'on_behalf_of',
                        'on_behalf_of__extra', 'organization'
                    ).all()
                )
                current_candidacies = group_candidates_by_party(
                    election,
                    current_candidacies,
                )
                post_context = {
                    'election': election.slug,
                    'election_data': election,
                    'post_data': {
                        'id': post.extra.slug,
                        'label': post.label,
                    },
                    'candidates_locked': locked,
                    'lock_form': ToggleLockForm(
                        initial={
                            'post_id': post.extra.slug,
                            'lock': not locked
                        },
                    ),
                    'candidate_list_edits_allowed':
                    get_edits_allowed(self.request.user, locked),
                    'candidacies': current_candidacies,
                    'add_candidate_form': NewPersonForm(
                        election=election.slug,
                        initial={
                            ('constituency_' + election.slug): post_extra.slug,
                            ('standing_' + election.slug): 'standing',
                        },
                        hidden_post_widget=True,
                    ),
                }

                post_context = get_person_form_fields(
                    post_context,
                    post_context['add_candidate_form']
                )

                context['posts'].append(post_context)

        context['no_data_areas'] = no_data_areas

        if not any_area_found:
            raise Http404("No such areas found")
        context['all_area_names'] = ' — '.join(all_area_names)
        context['suppress_official_documents'] = True
        return context

class AreasOfTypeView(TemplateView):
    template_name = 'candidates/areas-of-type.html'

    def get_context_data(self, **kwargs):
        context = super(AreasOfTypeView, self).get_context_data(**kwargs)
        requested_area_type = kwargs['area_type']
        all_area_tuples = set(
            (area_type.name, election_data.area_generation)
            for election_data in Election.objects.current().by_date()
            for area_type in election_data.area_types.all()
            if area_type.name == requested_area_type
        )
        if not all_area_tuples:
            raise Http404(_("Area '{0}' not found").format(requested_area_type))
        if len(all_area_tuples) > 1:
            message = _("Multiple Area generations for type {area_type} found")
            raise Exception(message.format(area_type=requested_area_type))
        prefetch_qs = AreaExtra.objects.select_related('base').order_by('base__name')
        area_type = get_object_or_404(<commit_msg>Workaround for multiple areas with the same identifier existing<commit_after># -*- coding: utf-8 -*-

from __future__ import unicode_literals

import re

from django.db.models import Prefetch
from django.core.urlresolvers import reverse
from django.http import Http404, HttpResponseBadRequest
from django.views.generic import TemplateView
from django.utils.text import slugify
from django.utils.translation import ugettext as _
from django.shortcuts import get_object_or_404

from candidates.models import AreaExtra, MembershipExtra
from candidates.models.auth import get_edits_allowed

from elections.models import AreaType, Election

from ..forms import NewPersonForm, ToggleLockForm
from .helpers import (
    split_candidacies, group_candidates_by_party,
    get_person_form_fields
)

class AreasView(TemplateView):
    template_name = 'candidates/areas.html'

    def get(self, request, *args, **kwargs):
        self.types_and_areas = []
        for type_and_area in kwargs['type_and_area_ids'].split(','):
            m = re.search(r'^([A-Z0-9]+?)-([-a-zA-Z0-9\:_]+)$', type_and_area)
            if not m:
                message = _("Malformed type and area: '{0}'")
                return HttpResponseBadRequest(message.format(type_and_area))
            self.types_and_areas.append(m.groups())
        response = super(AreasView, self).get(request, *args, **kwargs)
        return response

    def get_context_data(self, **kwargs):
        context = super(AreasView, self).get_context_data(**kwargs)
        all_area_names = set()
        context['posts'] = []
        any_area_found = False
        no_data_areas = []
        posts_seen = set()
        for area_type_code, area_id in self.types_and_areas:
            area_extras = AreaExtra.objects \
                .select_related('base', 'type') \
                .prefetch_related('base__posts') \
                .filter(base__identifier=area_id)
            if area_extras.exists():
                any_area_found = True
            else:
                continue

            if area_type_code == "NODATA":
                no_data_areas.append(area_extras.first())
                continue

            for area_extra in area_extras:
                area = area_extra.base
                all_area_names.add(area.name)
                for post in area.posts.all():
                    post_extra = post.extra
                    try:
                        election = post_extra.elections.get(current=True)
                    except Election.DoesNotExist:
                        continue
                    if post.id in posts_seen:
                        continue
                    else:
                        posts_seen.add(post.id)
                    locked = post_extra.candidates_locked
                    extra_qs = MembershipExtra.objects.select_related('election')
                    current_candidacies, _ = split_candidacies(
                        election,
                        post.memberships.prefetch_related(
                            Prefetch('extra', queryset=extra_qs)
                        ).select_related(
                            'person', 'person__extra', 'on_behalf_of',
                            'on_behalf_of__extra', 'organization'
                        ).all()
                    )
                    current_candidacies = group_candidates_by_party(
                        election,
                        current_candidacies,
                    )
                    post_context = {
                        'election': election.slug,
                        'election_data': election,
                        'post_data': {
                            'id': post.extra.slug,
                            'label': post.label,
                        },
                        'candidates_locked': locked,
                        'lock_form': ToggleLockForm(
                            initial={
                                'post_id': post.extra.slug,
                                'lock': not locked
                            },
                        ),
                        'candidate_list_edits_allowed':
                        get_edits_allowed(self.request.user, locked),
                        'candidacies': current_candidacies,
                        'add_candidate_form': NewPersonForm(
                            election=election.slug,
                            initial={
                                ('constituency_' + election.slug): post_extra.slug,
                                ('standing_' + election.slug): 'standing',
                            },
                            hidden_post_widget=True,
                        ),
                    }

                    post_context = get_person_form_fields(
                        post_context,
                        post_context['add_candidate_form']
                    )

                    context['posts'].append(post_context)

        context['no_data_areas'] = no_data_areas

        if not any_area_found:
            raise Http404("No such areas found")
        context['all_area_names'] = ' — '.join(all_area_names)
        context['suppress_official_documents'] = True
        return context

class AreasOfTypeView(TemplateView):
    template_name = 'candidates/areas-of-type.html'

    def get_context_data(self, **kwargs):
        context = super(AreasOfTypeView, self).get_context_data(**kwargs)
        requested_area_type = kwargs['area_type']
        all_area_tuples = set(
            (area_type.name, election_data.area_generation)
            for election_data in Election.objects.current().by_date()
            for area_type in election_data.area_types.all()
            if area_type.name == requested_area_type
        )
        if not all_area_tuples:
            raise Http404(_("Area '{0}' not found").format(requested_area_type))
        if len(all_area_tuples) > 1:
            message = _("Multiple Area generations for type {area_type} found")
            raise Exception(message.format(area_type=requested_area_type))
        prefetch_qs = AreaExtra.objects.select_related('base').order_by('base__name')
        area_type = get_object_or_404(
{'fraction_non_alphanumeric': 0.08666499036124382, 'fraction_numerical': 0.0038555024725504985, 'mean_word_length': 1.6474373197248724, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, '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': '1173420', 'n_tokens_mistral': 3233, 'n_tokens_neox': 3052, 'n_words': 580}