def clean_q(self):
     postcode = self.cleaned_data["q"]
     try:
         # Check if this postcode is valid and
         # contained in a constituency. (If it's valid then the
         # result is cached, so this doesn't cause a double lookup.)
         get_post_elections_from_postcode(postcode)
     except (UnknownGeoException, BadPostcodeException) as e:
         raise ValidationError(text_type(e))
     return postcode
Exemple #2
0
    def list(self, request, *args, **kwargs):
        postcode = request.GET.get("postcode", None)
        coords = request.GET.get("coords", None)

        # TODO: postcode may not make sense everywhere
        errors = None
        if not postcode and not coords:
            return self._error("Postcode or Co-ordinates required")

        try:
            if coords:
                pees = get_post_elections_from_coords(coords)
            else:
                pees = get_post_elections_from_postcode(postcode)
        except Exception as e:
            return self._error(e.message)

        results = []
        pees = pees.select_related("post__organization",
                                   "election").order_by("-ballot_paper_id")
        for pee in pees:
            candidates = []
            for membership in (pee.membership_set.filter(
                    post_election__election=pee.election,
                    role=pee.election.candidate_membership_role,
            ).prefetch_related(
                    Prefetch(
                        "person__memberships",
                        Membership.objects.select_related(
                            "party", "post", "post_election__election"),
                    ),
                    "person__images",
                    "person__other_names",
            ).select_related("person")):
                candidates.append(
                    serializers.NoVersionPersonSerializer(
                        instance=membership.person,
                        context={
                            "request": request
                        },
                        read_only=True,
                    ).data)
            election = {
                "election_date": text_type(pee.election.election_date),
                "election_name": pee.election.name,
                "election_id": pee.election.slug,
                "post": {
                    "post_name": pee.post.label,
                    "post_slug": pee.post.slug,
                    "post_candidates": None,
                },
                "organization": pee.post.organization.name,
                "candidates": candidates,
            }

            results.append(election)

        return Response(results)
Exemple #3
0
    def get(self, request, *args, **kwargs):
        postcode = request.GET.get("postcode", None)
        coords = request.GET.get("coords", None)

        # TODO: postcode may not make sense everywhere
        errors = None
        if not postcode and not coords:
            errors = {"error": "Postcode or Co-ordinates required"}

        try:
            if coords:
                pees = get_post_elections_from_coords(coords)
            else:
                pees = get_post_elections_from_postcode(postcode)
        except Exception as e:
            errors = {"error": e.message}

        if errors:
            return HttpResponse(json.dumps(errors),
                                status=400,
                                content_type="application/json")

        results = []
        pees = pees.select_related("post", "election")
        for pee in pees:
            results.append({
                "post_name":
                pee.post.label,
                "post_slug":
                pee.post.slug,
                "organization":
                pee.post.organization.name,
                "election_date":
                text_type(pee.election.election_date),
                "election_name":
                pee.election.name,
                "election_id":
                pee.election.slug,
            })

        return HttpResponse(json.dumps(results),
                            content_type="application/json")
Exemple #4
0
    def get(self, request, *args, **kwargs):
        postcode = request.GET.get('postcode', None)
        coords = request.GET.get('coords', None)

        # TODO: postcode may not make sense everywhere
        errors = None
        if not postcode and not coords:
            errors = {'error': 'Postcode or Co-ordinates required'}

        try:
            if coords:
                pees = get_post_elections_from_coords(coords)
            else:
                pees = get_post_elections_from_postcode(postcode)
        except Exception as e:
            errors = {'error': e.message}

        if errors:
            return HttpResponse(json.dumps(errors),
                                status=400,
                                content_type='application/json')

        results = []
        pees = pees.select_related('postextra__base', 'election')
        for pee in pees:
            results.append({
                'post_name':
                pee.postextra.base.label,
                'post_slug':
                pee.postextra.slug,
                'organization':
                pee.postextra.base.organization.name,
                'election_date':
                text_type(pee.election.election_date),
                'election_name':
                pee.election.name,
                'election_id':
                pee.election.slug
            })

        return HttpResponse(json.dumps(results),
                            content_type='application/json')
Exemple #5
0
 def get_context_data(self, **kwargs):
     context = super().get_context_data(**kwargs)
     context["pees"] = get_post_elections_from_postcode(kwargs["postcode"])
     return context
Exemple #6
0
 def get_context_data(self, **kwargs):
     context = super(PostcodeView, self).get_context_data(**kwargs)
     context['pees'] = get_post_elections_from_postcode(kwargs['postcode'])
     return context
Exemple #7
0
    def list(self, request, *args, **kwargs):
        postcode = request.GET.get('postcode', None)
        coords = request.GET.get('coords', None)

        # TODO: postcode may not make sense everywhere
        errors = None
        if not postcode and not coords:
            return self._error('Postcode or Co-ordinates required')

        try:
            if coords:
                pees = get_post_elections_from_coords(coords)
            else:
                pees = get_post_elections_from_postcode(postcode)
        except Exception as e:
            return self._error(e.message)

        results = []
        pees = pees.select_related(
            'postextra__base__organization',
            'election'
        )
        for pee in pees:
            candidates = []
            for membership in pee.membership_set.filter(
                    post_election__election=pee.election,
                    role=pee.election.candidate_membership_role) \
                    .prefetch_related(
                        Prefetch(
                            'person__memberships',
                            Membership.objects.select_related(
                                'on_behalf_of__extra',
                                'organization__extra',
                                'post__extra',
                                'post_election__election',
                            )
                        ),
                        Prefetch(
                            'person__extra__images',
                            Image.objects.select_related('extra__uploading_user')
                        ),
                        'person__other_names',
                        'person__contact_details',
                        'person__links',
                        'person__identifiers',
                        'person__extra_field_values',
                    ) \
                    .select_related('person__extra'):
                candidates.append(
                    serializers.NoVersionPersonSerializer(
                        instance=membership.person,
                        context={
                            'request': request,
                        },
                        read_only=True,
                    ).data
                )
            election = {
                'election_date': text_type(pee.election.election_date),
                'election_name': pee.election.name,
                'election_id': pee.election.slug,
                'post': {
                    'post_name': pee.postextra.base.label,
                    'post_slug': pee.postextra.slug,
                    'post_candidates': None
                },
                'organization': pee.postextra.base.organization.name,
                'candidates': candidates,

            }

            results.append(election)

        return Response(results)