Exemplo n.º 1
0
    def get_areas_context(self, area_ids):
        
        all_area_names = []
        area_context = []

        for area_id in area_ids:

            area_extra = get_object_or_404(
                AreaExtra.objects \
                    .select_related('base', 'type') \
                    .prefetch_related('base__posts'),
                base__identifier=area_id,
                base__posts__extra__elections__current=True
            )

            area = area_extra.base

            for post in area.posts.all():
                all_area_names.append(post.label)
                post_extra = post.extra
                election = post_extra.elections.get(current=True)
                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,
                    party_list=election.party_lists_in_use,
                    max_people=election.default_party_list_members_to_show
                )
                area_context.append({
                    'election': election.slug,
                    'election_data': election,
                    'post_data': {
                        'id': post.extra.slug,
                        'label': post.label,
                    },
                    'candidates_locked': 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,
                    ),
                })

        return all_area_names, area_context
Exemplo n.º 2
0
    def get_context_data(self, **kwargs):
        context = super(StPaulAreasView, self).get_context_data(**kwargs)

        all_area_names = set()
        context["posts"] = []

        for area_id in self.area_ids:
            ocd_division = area_id.replace(",", "/")
            # FIXME: there's quite a bit of repetition from
            # candidates/views/areas.py; do some DRY refactoring:
            area_extra = get_object_or_404(
                AreaExtra.objects.select_related("base", "type").prefetch_related("base__posts"),
                base__identifier=ocd_division,
                base__posts__extra__elections__current=True,
            )
            area = area_extra.base
            all_area_names.add(area.name)
            for post in area.posts.all():
                post_extra = post.extra
                election = post_extra.elections.get(current=True)
                locked = post_extra.candidates_locked
                extra_qs = MembershipExtra.objects.select_related("election")
                current_candidacies, created = 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,
                    party_list=election.party_lists_in_use,
                    max_people=election.default_party_list_members_to_show,
                )
                context["posts"].append(
                    {
                        "election": election.slug,
                        "election_data": election,
                        "post_data": {"id": post.extra.slug, "label": post.label},
                        "candidates_locked": 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,
                        ),
                    }
                )

        context["all_area_names"] = " — ".join(all_area_names)
        context["suppress_official_documents"] = True

        return context
Exemplo n.º 3
0
 def get_context_data(self, **kwargs):
     context = super(AreasView, self).get_context_data(**kwargs)
     all_area_names = set()
     context['posts'] = []
     for area_type, area_id in self.types_and_areas:
         # Show candidates from the current elections:
         for election_data in Election.objects.current().by_date():
             area_generation = election_data.area_generation
             if area_type in [area.name for area in election_data.area_types.all()]:
                 area_tuple = (area_type, area_generation)
                 post_id = AREA_POST_DATA.get_post_id(election_data.slug, area_type, area_id)
                 post_data = get_post_cached(self.api, post_id)['result']
                 area_name = AREA_DATA.areas_by_id[area_tuple][area_id]['name']
                 all_area_names.add(area_name)
                 locked = post_data.get('candidates_locked', False)
                 current_candidates, _ = get_people_from_memberships(
                     election_data,
                     post_data['memberships']
                 )
                 # The 'memberships' data can be huge; when you
                 # have Django Debug Toolbar active this causes
                 # page loading to be incredibly slow; it's not
                 # needed any longer from this point on, so remove
                 # it from the data that goes into the context.
                 del post_data['memberships']
                 current_candidates = group_people_by_party(
                     election_data.slug,
                     current_candidates,
                     party_list=election_data.party_lists_in_use,
                     max_people=election_data.default_party_list_members_to_show
                 )
                 context['posts'].append({
                     'election': election_data.slug,
                     'election_data': election_data,
                     'post_data': post_data,
                     'candidates_locked': locked,
                     'candidate_list_edits_allowed':
                     get_edits_allowed(self.request.user, locked),
                     'candidates': current_candidates,
                     'add_candidate_form': NewPersonForm(
                         election=election_data.slug,
                         initial={
                             ('constituency_' + election_data.slug): post_id,
                             ('standing_' + election_data.slug): 'standing',
                         },
                         hidden_post_widget=True,
                     ),
                 })
     context['all_area_names'] = u' — '.join(all_area_names)
     context['suppress_official_documents'] = True
     return context
Exemplo n.º 4
0
    def get_context_data(self, **kwargs):
        context = super(PoliticiansView, self).get_context_data(**kwargs)
        areaId = self.type_and_area[1]
        parentArea = pmodels.Area.objects.get(id=areaId)

        context['internal_areas_count'] = pmodels.Area.objects.filter(parent_id=areaId).count()
        context['area_name'] = parentArea.name
        context['area_id'] = areaId
        context['redirect_after_login'] = reverse('create-politician-view', kwargs={'area_id': areaId})
        context['politician_list_edits_allowed'] = get_edits_allowed(self.request.user, False)
        context['internal_areas_url'] = '/politicians-areas/' + kwargs['type_and_area_ids'] + '/' + slugify(parentArea.name)
        context['bread_crumb'] = self.breadCrumb
        context['memberships'] = self.__getAreaMemberships(areaId)
        return context
Exemplo n.º 5
0
 def get_context_data(self, **kwargs):
     context = super(AreasView, self).get_context_data(**kwargs)
     all_area_names = set()
     context['posts'] = []
     for mapit_type, area_id in self.types_and_areas:
         # Show candidates from the current elections:
         for election, election_data in settings.ELECTIONS_CURRENT:
             mapit_generation = election_data['mapit_generation']
             if mapit_type in election_data['mapit_types']:
                 mapit_tuple = (mapit_type, mapit_generation)
                 post_id = AREA_POST_DATA.get_post_id(
                     election, mapit_type, area_id)
                 post_data = get_post_cached(self.api, post_id)['result']
                 area_name = MAPIT_DATA.areas_by_id[mapit_tuple][area_id][
                     'name']
                 all_area_names.add(area_name)
                 locked = post_data.get('candidates_locked', False)
                 current_candidates, _ = get_people_from_memberships(
                     election_data,
                     post_data['memberships'],
                 )
                 context['posts'].append({
                     'election':
                     election,
                     'election_data':
                     election_data,
                     'post_data':
                     post_data,
                     'candidates_locked':
                     locked,
                     'candidate_list_edits_allowed':
                     get_edits_allowed(self.request.user, locked),
                     'candidates':
                     current_candidates,
                     'add_candidate_form':
                     NewPersonForm(
                         election=election,
                         initial={
                             ('constituency_' + election): post_id,
                             ('standing_' + election): 'standing',
                         },
                         hidden_post_widget=True,
                     ),
                 })
     context['all_area_names'] = u' — '.join(all_area_names)
     context['suppress_official_documents'] = True
     return context
Exemplo n.º 6
0
 def get_context_data(self, **kwargs):
     context = super(AreasView, self).get_context_data(**kwargs)
     all_area_names = set()
     context['posts'] = []
     for mapit_type, area_id in self.types_and_areas:
         # Show candidates from the current elections:
         for election, election_data in settings.ELECTIONS_CURRENT:
             mapit_generation = election_data['mapit_generation']
             if mapit_type in election_data['mapit_types']:
                 mapit_tuple = (mapit_type, mapit_generation)
                 post_id = AREA_POST_DATA.get_post_id(election, mapit_type, area_id)
                 post_data = get_post_cached(self.api, post_id)['result']
                 area_name = MAPIT_DATA.areas_by_id[mapit_tuple][area_id]['name']
                 all_area_names.add(area_name)
                 locked = post_data.get('candidates_locked', False)
                 current_candidates, _ = get_people_from_memberships(
                     election_data,
                     post_data['memberships'],
                 )
                 context['posts'].append({
                     'election': election,
                     'election_data': election_data,
                     'post_data': post_data,
                     'candidates_locked': locked,
                     'candidate_list_edits_allowed':
                     get_edits_allowed(self.request.user, locked),
                     'candidates': current_candidates,
                     'add_candidate_form': NewPersonForm(
                         election=election,
                         initial={
                             ('constituency_' + election): post_id,
                             ('standing_' + election): 'standing',
                         },
                         hidden_post_widget=True,
                     ),
                 })
     context['all_area_names'] = u' — '.join(all_area_names)
     context['suppress_official_documents'] = True
     return context
Exemplo n.º 7
0
    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
Exemplo n.º 8
0
    def get_context_data(self, **kwargs):
        context = super(StPaulAreasView, self).get_context_data(**kwargs)

        all_area_names = set()
        context['posts'] = []

        for area_id in self.area_ids:
            ocd_division = area_id.replace(',', '/')
            # FIXME: there's quite a bit of repetition from
            # candidates/views/areas.py; do some DRY refactoring:
            area_extra = get_object_or_404(
                AreaExtra.objects \
                    .select_related('base', 'type') \
                    .prefetch_related('base__posts'),
                base__identifier=ocd_division,
                base__posts__extra__elections__current=True
            )
            area = area_extra.base
            all_area_names.add(area.name)
            for post in area.posts.all():
                post_extra = post.extra
                election = post_extra.elections.get(current=True)
                locked = post_extra.candidates_locked
                extra_qs = MembershipExtra.objects.select_related('election')
                current_candidacies, created = 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,
                )
                context['posts'].append({
                    'election': election.slug,
                    'election_data': election,
                    'post_data': {
                        'id': post.extra.slug,
                        'label': post.label,
                    },
                    'candidates_locked': 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,
                    ),
                })

        context['all_area_names'] = ' — '.join(all_area_names)
        context['suppress_official_documents'] = True

        return context
Exemplo n.º 9
0
    def get_context_data(self, **kwargs):
        context = super(StPaulAreasView, self).get_context_data(**kwargs)

        all_area_names = set()
        context['posts'] = []

        for area_id in self.area_ids:
            ocd_division = area_id.replace(',', '/')
            # FIXME: there's quite a bit of repetition from
            # candidates/views/areas.py; do some DRY refactoring:
            area_extra = get_object_or_404(
                AreaExtra.objects \
                    .select_related('base', 'type') \
                    .prefetch_related('base__posts'),
                base__identifier=ocd_division,
                base__posts__extra__elections__current=True
            )
            area = area_extra.base
            all_area_names.add(area.name)
            for post in area.posts.all():
                post_extra = post.extra
                election = post_extra.elections.get(current=True)
                locked = post_extra.candidates_locked
                extra_qs = MembershipExtra.objects.select_related('election')
                current_candidacies, created = 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,
                    party_list=election.party_lists_in_use,
                    max_people=election.default_party_list_members_to_show)
                context['posts'].append({
                    'election':
                    election.slug,
                    'election_data':
                    election,
                    'post_data': {
                        'id': post.extra.slug,
                        'label': post.label,
                    },
                    'candidates_locked':
                    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,
                    ),
                })

        context['all_area_names'] = u' — '.join(all_area_names)
        context['suppress_official_documents'] = True

        return context
Exemplo n.º 10
0
    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

            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())
                    elected_candidacies, unelected_candidacies = split_by_elected(
                        election,
                        current_candidacies,
                    )
                    elected_candidacies = group_candidates_by_party(
                        election,
                        elected_candidacies,
                    )
                    unelected_candidacies = group_candidates_by_party(
                        election,
                        unelected_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),
                        'has_elected': \
                            len(elected_candidacies['parties_and_people']) > 0,
                        'elected': elected_candidacies,
                        'unelected': unelected_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
Exemplo n.º 11
0
 def get_context_data(self, **kwargs):
     context = super(AreasView, self).get_context_data(**kwargs)
     all_area_names = set()
     context['posts'] = []
     for area_type_code, area_id in self.types_and_areas:
         # FIXME: check that we're doing enough prefetch_related
         # and select_related
         area_extra = get_object_or_404(
             AreaExtra.objects \
                 .select_related('base', 'type') \
                 .prefetch_related('base__posts'),
             base__identifier=area_id,
         )
         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,
                 party_list=election.party_lists_in_use,
                 max_people=election.default_party_list_members_to_show
             )
             context['posts'].append({
                 'election': election.slug,
                 'election_data': election,
                 'post_data': {
                     'id': post.extra.slug,
                     'label': post.label,
                 },
                 'candidates_locked': 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,
                 ),
             })
     context['all_area_names'] = u' — '.join(all_area_names)
     context['suppress_official_documents'] = True
     return context