def test_results_for_upcoming_elections(self, mock_requests):
        self._setup_data()

        mock_requests.get.side_effect = fake_requests_for_every_election
        response = self.app.get("/upcoming-elections/?postcode=SE24+0AG")
        output = response.json
        self.assertEqual(len(output), 2)

        self.maxDiff = None
        expected = [
            {
                "organization": "London Assembly",
                "election_date": text_type(self.future_date.isoformat()),
                "election_id": "gla.c.2016-05-05",
                "election_name":
                "2016 London Assembly Election (Constituencies)",
                "post_name": "Assembly Member for Lambeth and Southwark",
                "post_slug": "lambeth-and-southwark",
            },
            {
                "organization": "London Assembly",
                "election_date": text_type(self.future_date.isoformat()),
                "election_id": "gla.a.2016-05-05",
                "election_name": "2016 London Assembly Election (Additional)",
                "post_name": "Assembly Member",
                "post_slug": "london",
            },
        ]

        self.assertEqual(expected, output)
예제 #2
0
    def __init__(self, dest=None, count=1, port=None, index=None, host=None):
        SearchCommand.__init__(self, run_in_preview=False, logger_name="ping_search_command")

        self.dest = None

        if dest is not None:
            self.dest = text_type(dest)

        # Use the host argument if someone provided that instead (since that is the old argument)
        elif self.dest is None and host is not None:
            self.dest = text_type(host)

        self.index = index

        try:
            self.count = int(count)
        except ValueError:
            raise ValueError('The count parameter must be an integer')

        if port is not None:
            try:
                self.port = int(port)
            except ValueError:
                raise ValueError('The port parameter must be an integer')

            if self.port < 1 or self.port > 65535:
                raise ValueError('The port parameter must be an integer from 1 to 65535')

        else:
            self.port = None

        self.logger.info("Ping running")
예제 #3
0
    def test_results_for_upcoming_elections(self, mock_requests):
        self._setup_data()

        mock_requests.get.side_effect = fake_requests_for_every_election
        response = self.app.get('/upcoming-elections/?postcode=SE24+0AG')
        output = response.json
        self.assertEqual(len(output), 2)

        self.maxDiff = None
        expected = [
            {
                'organization': 'London Assembly',
                'election_date': text_type(self.future_date.isoformat()),
                'election_id': 'gla.c.2016-05-05',
                'election_name':
                '2016 London Assembly Election (Constituencies)',
                'post_name': 'Assembly Member for Lambeth and Southwark',
                'post_slug': 'lambeth-and-southwark',
            },
            {
                'organization': 'London Assembly',
                'election_date': text_type(self.future_date.isoformat()),
                'election_id': 'gla.a.2016-05-05',
                'election_name': '2016 London Assembly Election (Additional)',
                'post_name': 'Assembly Member',
                'post_slug': 'london',
            },
        ]

        self.assertEqual(expected, output)
예제 #4
0
 def test_reports_top_page_json(self):
     response = self.app.get('/numbers/?format=json')
     data = json.loads(response.body.decode('utf-8'))
     self.assertEqual(
         data,
         [
             {
                 "current": True,
                 "dates": {
                     text_type(self.election.election_date.isoformat()): [
                         {
                             "elections": [
                                 {
                                     "html_id": "2015",
                                     "id": "2015",
                                     "name": "2015 General Election",
                                     "total": 18
                                 }
                             ],
                             "role": "Member of Parliament"
                         }
                     ],
                     text_type(self.local_election.election_date.isoformat()): [
                         {
                             "elections": [
                                 {
                                     "html_id": "local-maidstone-2016-05-05",
                                     "id": "local.maidstone.2016-05-05",
                                     "name": "Maidstone local election",
                                     "total": 0,
                                 }
                             ],
                             "role": "Local Councillor",
                         },
                     ],
                 }
             },
             {
                 "current": False,
                 "dates": {
                     text_type(self.earlier_election.election_date.isoformat()): [
                         {
                             "elections": [
                                 {
                                     "html_id": "2010",
                                     "id": "2010",
                                     "name": "2010 General Election",
                                     "total": 2
                                 }
                             ],
                             "role": "Member of Parliament"
                         }
                     ]
                 }
             }
         ]
     )
예제 #5
0
def _handle_nopinyin_char(chars, errors='default'):
    """处理没有拼音的字符"""
    if callable_check(errors):
        return errors(chars)

    if errors == 'default':
        return chars
    elif errors == 'ignore':
        return None
    elif errors == 'replace':
        if len(chars) > 1:
            return ''.join(text_type('%x' % ord(x)) for x in chars)
        else:
            return text_type('%x' % ord(chars))
예제 #6
0
    def test_unicode(self):
        doc = OfficialDocument(
            post=self.post.base,
            source_url="http://example.com/",
        )

        self.assertEqual(text_type(doc), "65808 (http://example.com/)")
예제 #7
0
    def get(self, request, *args, **kwargs):
        results = {}
        qs = Election.objects.filter(current=True).order_by("id")
        if request.GET.get("future"):
            qs = qs.future()
        for election in qs:
            results[election.slug] = {
                "election_date":
                text_type(election.election_date),
                "name":
                election.name,
                "url":
                reverse(
                    "election-detail",
                    kwargs={
                        "version": "v0.9",
                        "slug": election.slug
                    },
                ),
            }

        res = HttpResponse(json.dumps(results),
                           content_type="application/json")
        res["Expires"] = date.today() + timedelta(days=7)
        return res
예제 #8
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:
                ballots = get_ballots_from_coords(coords)
            else:
                ballots = get_ballots_from_postcode(postcode)
        except Exception as e:
            return self._error(str(e))

        results = []
        ballots = ballots.select_related(
            "post__organization", "election"
        ).order_by("-ballot_paper_id")
        for ballot in ballots:
            candidates = []
            for membership in (
                ballot.membership_set.filter(ballot__election=ballot.election)
                .prefetch_related(
                    Prefetch(
                        "person__memberships",
                        Membership.objects.select_related(
                            "party", "post", "ballot__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(ballot.election.election_date),
                "election_name": ballot.election.name,
                "election_id": ballot.election.slug,
                "post": {
                    "post_name": ballot.post.label,
                    "post_slug": ballot.post.slug,
                    "post_candidates": None,
                },
                "organization": ballot.post.organization.name,
                "candidates": candidates,
            }

            results.append(election)

        return Response(results)
예제 #9
0
 def add_item_elements(self, handler, item):
     super(ResultEventsAtomFeedGenerator, self). \
         add_item_elements(handler, item)
     keys = [
         'retraction',
         'election_slug',
         'election_name',
         'election_date',
         'post_id',
         'winner_person_id',
         'winner_person_name',
         'winner_party_id',
         'winner_party_name',
         'user_id',
         'post_name',
         'information_source',
     ]
     for k in [
         'image_url',
         'parlparse_id',
     ]:
         if item[k]:
             keys.append(k)
     for k in keys:
         handler.addQuickElement(k, text_type(item[k]))
예제 #10
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:
            areas = fetch_area_ids(postcode=postcode, coords=coords)
        except Exception as e:
            errors = {
                'error': e.message
            }

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

        area_ids = [area[1] for area in areas]

        posts = Post.objects.filter(
            area__identifier__in=area_ids,
        ).select_related(
            'area', 'area__extra__type', 'organization'
        ).prefetch_related(
            'extra__elections'
        )

        results = []
        for post in posts.all():
            try:
                election = post.extra.elections.get(
                    current=True,
                    election_date__gte=date.today()
                )
            except:
                continue
            if election:
                results.append({
                    'post_name': post.label,
                    'post_slug': post.extra.slug,
                    'organization': post.organization.name,
                    'area': {
                        'type': post.area.extra.type.name,
                        'name': post.area.name,
                        'identifier': post.area.identifier,
                    },
                    'election_date': text_type(election.election_date),
                    'election_name': election.name,
                    'election_id': election.slug
                })

        return HttpResponse(
            json.dumps(results), content_type='application/json'
        )
예제 #11
0
    def test_unicode(self):
        doc = OfficialDocument(
            post=self.post.base,
            source_url="http://example.com/",
        )

        self.assertEqual(text_type(doc), "65808 (http://example.com/)")
예제 #12
0
    def test_unicode(self):
        doc = OfficialDocument(ballot=self.ballot,
                               source_url="http://example.com/")

        self.assertEqual(
            text_type(doc),
            "parl.dulwich-and-west-norwood.2015-05-07 (http://example.com/)",
        )
예제 #13
0
 def test_reports_top_page_json(self):
     response = self.app.get('/numbers/?format=json')
     data = json.loads(response.body.decode('utf-8'))
     self.assertEqual(data, [{
         "current": True,
         "dates": {
             text_type(self.election.election_date.isoformat()): [{
                 "elections": [{
                     "html_id":
                     "2015",
                     "id":
                     "2015",
                     "name":
                     "2015 General Election",
                     "prior_elections": [{
                         "name": "2010 General Election",
                         "new_candidates": 16,
                         "percentage": 900.0,
                         "standing_again": 2,
                         "standing_again_different_party": 1,
                         "standing_again_same_party": 1
                     }],
                     "total":
                     18
                 }],
                 "role":
                 "Member of Parliament"
             }]
         }
     }, {
         "current": False,
         "dates": {
             text_type(self.earlier_election.election_date.isoformat()): [{
                 "elections": [{
                     "html_id": "2010",
                     "id": "2010",
                     "name": "2010 General Election",
                     "total": 2
                 }],
                 "role":
                 "Member of Parliament"
             }]
         }
     }])
예제 #14
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:
            areas = fetch_area_ids(postcode=postcode, coords=coords)
        except Exception as e:
            errors = {'error': e.message}

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

        area_ids = [area[1] for area in areas]

        posts = Post.objects.filter(
            area__identifier__in=area_ids, ).select_related(
                'area', 'area__extra__type',
                'organization').prefetch_related('extra__elections')

        results = []
        for post in posts.all():
            try:
                election = post.extra.elections.get(
                    current=True, election_date__gte=date.today())
            except:
                continue
            if election:
                results.append({
                    'post_name':
                    post.label,
                    'post_slug':
                    post.extra.slug,
                    'organization':
                    post.organization.name,
                    'area': {
                        'type': post.area.extra.type.name,
                        'name': post.area.name,
                        'identifier': post.area.identifier,
                    },
                    'election_date':
                    text_type(election.election_date),
                    'election_name':
                    election.name,
                    'election_id':
                    election.slug
                })

        return HttpResponse(json.dumps(results),
                            content_type='application/json')
예제 #15
0
 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
예제 #16
0
 def clean_postcode(self):
     postcode = self.cleaned_data['postcode']
     try:
         # Go to MapIt to 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_areas_from_postcode(postcode)
     except BaseMapItException as e:
         raise ValidationError(text_type(e))
     return postcode
    def test_results_for_upcoming_elections(self, mock_requests):
        self._setup_data()

        mock_requests.get.side_effect = fake_requests_for_every_election
        response = self.app.get('/upcoming-elections/?postcode=SE24+0AG')

        output = response.json
        self.assertEqual(len(output), 2)

        self.maxDiff = None
        expected = [
            {
                'organization': 'London Assembly',
                'election_date': text_type(self.future_date.isoformat()),
                'election_id': 'gb-gla-2016-05-05-c',
                'election_name':
                '2016 London Assembly Election (Constituencies)',
                'post_name': 'Assembly Member for Lambeth and Southwark',
                'post_slug': 'gss:E32000010',
                'area': {
                    'identifier': 'gss:E32000010',
                    'type': 'LAC',
                    'name': 'Dulwich and West Norwood'
                }
            },
            {
                'organization': 'London Assembly',
                'election_date': text_type(self.future_date.isoformat()),
                'election_id': 'gb-gla-2016-05-05-a',
                'election_name': '2016 London Assembly Election (Additional)',
                'post_name': 'Assembly Member',
                'post_slug': 'unit_id:41441',
                'area': {
                    'identifier': 'unit_id:41441',
                    'type': 'GLA',
                    'name': 'Greater London Authority'
                }
            },
        ]

        self.assertEqual(expected, output)
예제 #18
0
    def get(self, request, *args, **kwargs):
        results = {}
        for election in Election.objects.filter(current=True).order_by('id'):
            results[election.slug] = {
                'election_date': text_type(election.election_date),
                'name': election.name,
                'url': reverse('election-detail', kwargs={
                    'version': 'v0.9',
                    'slug': election.slug})
            }

        res = HttpResponse(
            json.dumps(results), content_type='application/json',
            )
        res['Expires'] = date.today() + timedelta(days=7)
        return res
예제 #19
0
    def get(self, request, *args, **kwargs):
        results = {}
        for election in Election.objects.filter(current=True).order_by('id'):
            results[election.slug] = {
                'election_date': text_type(election.election_date),
                'name': election.name,
                'url': reverse('election-detail', kwargs={
                    'version': 'v0.9',
                    'slug': election.slug})
            }

        res = HttpResponse(
            json.dumps(results), content_type='application/json',
            )
        res['Expires'] = date.today() + timedelta(days=7)
        return res
예제 #20
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:
            posts = fetch_posts_for_area(postcode=postcode, coords=coords)
        except Exception as e:
            errors = {
                'error': e.message
            }

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

        results = []
        for post in posts:
            for election in post.extra.elections.all():
                if not handle_election(election, request, only_upcoming=True):
                    continue
                results.append({
                    'post_name': post.label,
                    'post_slug': post.extra.slug,
                    'organization': post.organization.name,
                    'area': {
                        'type': post.area.extra.type.name,
                        'name': post.area.name,
                        'identifier': post.area.identifier,
                    },
                    'election_date': text_type(election.election_date),
                    'election_name': election.name,
                    'election_id': election.slug
                })

        return HttpResponse(
            json.dumps(results), content_type='application/json'
        )
예제 #21
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:
            areas = fetch_area_ids(postcode=postcode, coords=coords)
        except Exception as e:
            errors = {"error": e.message}

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

        area_ids = [area[1] for area in areas]

        posts = (
            Post.objects.filter(area__identifier__in=area_ids)
            .select_related("area", "area__extra__type", "organization")
            .prefetch_related("extra__elections")
        )

        results = []
        for post in posts.all():
            election = post.extra.elections.get(current=True, election_date__gte=date.today())
            if election:
                results.append(
                    {
                        "post_name": post.label,
                        "organization": post.organization.name,
                        "area": {
                            "type": post.area.extra.type.name,
                            "name": post.area.name,
                            "identifier": post.area.identifier,
                        },
                        "election_date": text_type(election.election_date),
                        "election_name": election.name,
                    }
                )

        return HttpResponse(json.dumps(results), content_type="application/json")
예제 #22
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')
예제 #23
0
def parse(ping_output):
    """
    Parse `ping_output` string into a dictionary containing the following
    fields:

        `host`: *string*; the target hostname that was pinged
        `sent`: *int*; the number of ping request packets sent
        `received`: *int*; the number of ping reply packets received
        `packet_loss`: *int*; the percentage of  packet loss
        `minping`: *float*; the minimum (fastest) round trip ping request/reply
                    time in milliseconds
        `avgping`: *float*; the average round trip ping time in milliseconds
        `maxping`: *float*; the maximum (slowest) round trip ping time in
                    milliseconds
        `jitter`: *float*; the standard deviation between round trip ping times
                    in milliseconds
    """

    if not isinstance(ping_output, text_type):
        ping_output = text_type(ping_output)

    host = _get_match_groups(ping_output, host_matcher)[0]
    sent, received, packet_loss = _get_match_groups(ping_output, [rslt_matcher, rslt_matcher_windows], ['sent', 'received', 'packet_loss'])

    try:
        minping, avgping, maxping, jitter = _get_match_groups(ping_output,
                                                              [minmax_matcher, minmax_matcher_windows],
                                                              ['min', 'avg', 'max', 'jitter'])
    except PingParseException:
        minping = avgping = maxping = jitter = 'NA'

    d = collections.OrderedDict()
    d['host'] = host
    d['sent'] = sent
    d['received'] = received
    d['packet_loss'] = packet_loss
    d['min_ping'] = minping
    d['avg_ping'] = avgping
    d['max_ping'] = maxping
    d['jitter'] = jitter

    return d
예제 #24
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")
예제 #25
0
 def add_item_elements(self, handler, item):
     super().add_item_elements(handler, item)
     keys = [
         "retraction",
         "election_slug",
         "election_name",
         "election_date",
         "post_id",
         "winner_person_id",
         "winner_person_name",
         "winner_party_id",
         "winner_party_name",
         "user_id",
         "post_name",
         "information_source",
     ]
     for k in ["image_url", "parlparse_id"]:
         if item[k]:
             keys.append(k)
     for k in keys:
         handler.addQuickElement(k, text_type(item[k]))
예제 #26
0
 def add_item_elements(self, handler, item):
     super(ResultEventsAtomFeedGenerator, self). \
         add_item_elements(handler, item)
     keys = [
         'post_id',
         'winner_person_id',
         'winner_person_name',
         'winner_party_id',
         'winner_party_name',
         'user_id',
         'post_name',
         'information_source',
     ]
     for k in [
         'image_url_template',
         'parlparse_id',
     ]:
         if item[k]:
             keys.append(k)
     for k in keys:
         handler.addQuickElement(k, text_type(item[k]))
    def test_results_for_upcoming_elections(self, mock_requests):
        one_day = timedelta(days=1)
        future_date = date.today() + one_day
        london_assembly = ParliamentaryChamberExtraFactory.create(
            slug='london-assembly', base__name='London Assembly'
        )
        lac_area_type = AreaTypeFactory.create(name='LAC')
        gla_area_type = AreaTypeFactory.create(name='GLA')
        area_extra_lac = AreaExtraFactory.create(
            base__identifier='gss:E32000010',
            base__name="Dulwich and West Norwood",
            type=lac_area_type,
        )
        area_extra_gla = AreaExtraFactory.create(
            base__identifier='unit_id:41441',
            base__name='Greater London Authority',
            type=gla_area_type,
        )
        election_lac = ElectionFactory.create(
            slug='gb-gla-2016-05-05-c',
            organization=london_assembly.base,
            name='2016 London Assembly Election (Constituencies)',
            election_date=future_date.isoformat(),
            area_types=(lac_area_type,),
        )
        election_gla = ElectionFactory.create(
            slug='gb-gla-2016-05-05-a',
            organization=london_assembly.base,
            name='2016 London Assembly Election (Additional)',
            election_date=future_date.isoformat(),
            area_types=(gla_area_type,),
        )
        PostExtraFactory.create(
            elections=(election_lac,),
            base__area=area_extra_lac.base,
            base__organization=london_assembly.base,
            slug='gss:E32000010',
            base__label='Assembly Member for Lambeth and Southwark',
        )
        PostExtraFactory.create(
            elections=(election_gla,),
            base__area=area_extra_gla.base,
            base__organization=london_assembly.base,
            slug='unit_id:41441',
            base__label='Assembly Member',
        )

        mock_requests.get.side_effect = fake_requests_for_mapit
        response = self.app.get('/upcoming-elections/?postcode=SE24+0AG')

        output = response.json
        self.assertEqual(len(output), 2)

        self.maxDiff = None
        expected = [
            {
                'organization': 'London Assembly',
                'election_date': text_type(future_date.isoformat()),
                'election_name': '2016 London Assembly Election (Constituencies)',
                'post_name': 'Assembly Member for Lambeth and Southwark',
                'area': {
                    'identifier': 'gss:E32000010',
                    'type': 'LAC',
                    'name': 'Dulwich and West Norwood'
                }
            },
            {
                'organization': 'London Assembly',
                'election_date': text_type(future_date.isoformat()),
                'election_name': '2016 London Assembly Election (Additional)',
                'post_name': 'Assembly Member',
                'area': {
                    'identifier': 'unit_id:41441',
                    'type': 'GLA',
                    'name': 'Greater London Authority'
                }
            },
        ]

        self.assertEqual(expected, output)
예제 #28
0
    def test_results_for_upcoming_elections(self, mock_requests):
        one_day = timedelta(days=1)
        future_date = date.today() + one_day
        london_assembly = ParliamentaryChamberExtraFactory.create(
            slug='london-assembly', base__name='London Assembly'
        )
        lac_area_type = AreaTypeFactory.create(name='LAC')
        gla_area_type = AreaTypeFactory.create(name='GLA')
        area_extra_lac = AreaExtraFactory.create(
            base__identifier='gss:E32000010',
            base__name="Dulwich and West Norwood",
            type=lac_area_type,
        )
        area_extra_gla = AreaExtraFactory.create(
            base__identifier='unit_id:41441',
            base__name='Greater London Authority',
            type=gla_area_type,
        )
        election_lac = ElectionFactory.create(
            slug='gb-gla-2016-05-05-c',
            organization=london_assembly.base,
            name='2016 London Assembly Election (Constituencies)',
            election_date=future_date.isoformat(),
            area_types=(lac_area_type,),
        )
        election_gla = ElectionFactory.create(
            slug='gb-gla-2016-05-05-a',
            organization=london_assembly.base,
            name='2016 London Assembly Election (Additional)',
            election_date=future_date.isoformat(),
            area_types=(gla_area_type,),
        )
        PostExtraFactory.create(
            elections=(election_lac,),
            base__area=area_extra_lac.base,
            base__organization=london_assembly.base,
            slug='gss:E32000010',
            base__label='Assembly Member for Lambeth and Southwark',
        )
        PostExtraFactory.create(
            elections=(election_gla,),
            base__area=area_extra_gla.base,
            base__organization=london_assembly.base,
            slug='unit_id:41441',
            base__label='Assembly Member',
        )

        mock_requests.get.side_effect = fake_requests_for_mapit
        response = self.app.get('/upcoming-elections/?postcode=SE24+0AG')

        output = response.json
        self.assertEqual(len(output), 2)

        self.maxDiff = None
        expected = [
            {
                'organization': 'London Assembly',
                'election_date': text_type(future_date.isoformat()),
                'election_name': '2016 London Assembly Election (Constituencies)',
                'post_name': 'Assembly Member for Lambeth and Southwark',
                'area': {
                    'identifier': 'gss:E32000010',
                    'type': 'LAC',
                    'name': 'Dulwich and West Norwood'
                }
            },
            {
                'organization': 'London Assembly',
                'election_date': text_type(future_date.isoformat()),
                'election_name': '2016 London Assembly Election (Additional)',
                'post_name': 'Assembly Member',
                'area': {
                    'identifier': 'unit_id:41441',
                    'type': 'GLA',
                    'name': 'Greater London Authority'
                }
            },
        ]

        self.assertEqual(expected, output)
예제 #29
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)
예제 #30
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")
        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"),
                    ),
                    Prefetch(
                        "person__images",
                        PersonImage.objects.select_related("uploading_user"),
                    ),
                    "person__other_names",
                    "person__contact_details",
                    "person__links",
                    "person__identifiers",
                    "person__extra_field_values",
            ).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)
예제 #31
0
def py_scanstring(s,
                  end,
                  encoding=None,
                  strict=True,
                  _b=BACKSLASH,
                  _m=STRINGCHUNK.match,
                  _join=u('').join,
                  _PY3=PY3,
                  _maxunicode=sys.maxunicode):
    """Scan the string s for a JSON string. End is the index of the
    character in s after the quote that started the JSON string.
    Unescapes all valid JSON string escape sequences and raises ValueError
    on attempt to decode an invalid string. If strict is False then literal
    control characters are allowed in the string.

    Returns a tuple of the decoded string and the index of the character in s
    after the end quote."""
    if encoding is None:
        encoding = DEFAULT_ENCODING
    chunks = []
    _append = chunks.append
    begin = end - 1
    while 1:
        chunk = _m(s, end)
        if chunk is None:
            raise JSONDecodeError("Unterminated string starting at", s, begin)
        end = chunk.end()
        content, terminator = chunk.groups()
        # Content is contains zero or more unescaped string characters
        if content:
            if not _PY3 and not isinstance(content, text_type):
                content = text_type(content, encoding)
            _append(content)
        # Terminator is the end of string, a literal control character,
        # or a backslash denoting that an escape sequence follows
        if terminator == '"':
            break
        elif terminator != '\\':
            if strict:
                msg = "Invalid control character %r at"
                raise JSONDecodeError(msg, s, end)
            else:
                _append(terminator)
                continue
        try:
            esc = s[end]
        except IndexError:
            raise JSONDecodeError("Unterminated string starting at", s, begin)
        # If not a unicode escape sequence, must be in the lookup table
        if esc != 'u':
            try:
                char = _b[esc]
            except KeyError:
                msg = "Invalid \\X escape sequence %r"
                raise JSONDecodeError(msg, s, end)
            end += 1
        else:
            # Unicode escape sequence
            msg = "Invalid \\uXXXX escape sequence"
            esc = s[end + 1:end + 5]
            escX = esc[1:2]
            if len(esc) != 4 or escX == 'x' or escX == 'X':
                raise JSONDecodeError(msg, s, end - 1)
            try:
                uni = int(esc, 16)
            except ValueError:
                raise JSONDecodeError(msg, s, end - 1)
            end += 5
            # Check for surrogate pair on UCS-4 systems
            # Note that this will join high/low surrogate pairs
            # but will also pass unpaired surrogates through
            if (_maxunicode > 65535 and uni & 0xfc00 == 0xd800
                    and s[end:end + 2] == '\\u'):
                esc2 = s[end + 2:end + 6]
                escX = esc2[1:2]
                if len(esc2) == 4 and not (escX == 'x' or escX == 'X'):
                    try:
                        uni2 = int(esc2, 16)
                    except ValueError:
                        raise JSONDecodeError(msg, s, end)
                    if uni2 & 0xfc00 == 0xdc00:
                        uni = 0x10000 + (((uni - 0xd800) << 10) |
                                         (uni2 - 0xdc00))
                        end += 6
            char = unichr(uni)
        # Append the unescaped character
        _append(char)
    return _join(chunks), end
예제 #32
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:
            posts = fetch_posts_for_area(postcode=postcode, coords=coords)
        except Exception as e:
            return self._error(e.message)

        election_kwargs = {
            'current': True,
        }
        if 'all_elections' in request.GET:
            del election_kwargs['current']
        if 'date_gte' in request.GET:
            if request.GET['date_gte'] == "today":
                election_kwargs['election_date__gte'] = date.today()
            else:
                election_kwargs['election_date__gte'] = request.GET['date_gte']

        results = []
        for post in posts.all():
            for election in post.extra.elections.filter(**election_kwargs):
                candidates = []
                for membership in post.memberships.filter(
                        extra__election=election,
                        role=election.candidate_membership_role) \
                        .prefetch_related(
                            Prefetch(
                                'person__memberships',
                                Membership.objects.select_related(
                                    'on_behalf_of__extra',
                                    'organization__extra',
                                    'post__extra',
                                    'extra__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(election.election_date),
                    'election_name': election.name,
                    'election_id': election.slug,
                    'post': {
                        'post_name': post.label,
                        'post_slug': post.extra.slug,
                        'post_candidates': None
                    },
                    'area': {
                        'type': post.area.extra.type.name,
                        'name': post.area.name,
                        'identifier': post.area.identifier,
                    },
                    'organization': post.organization.name,
                    'candidates': candidates,
                }

                results.append(election)

        return Response(results)
    def test_results_for_candidates_for_postcode(self, mock_requests):
        self._setup_data()

        person = PersonFactory.create(id=2009, name="Tessa Jowell")

        MembershipFactory.create(
            person=person,
            post=self.post,
            party=self.labour_party,
            ballot=self.election_gla.ballot_set.get(post=self.post),
        )
        membership_pk = person.memberships.first().pk

        self.maxDiff = None

        mock_requests.get.side_effect = fake_requests_for_every_election
        response = self.app.get(
            "/api/v0.9/candidates_for_postcode/?postcode=SE24+0AG")

        output = response.json
        self.assertEqual(len(output), 2)
        expected = [
            {
                "candidates": [],
                "election_date": text_type(self.future_date.isoformat()),
                "election_id": "gla.c.2016-05-05",
                "election_name":
                "2016 London Assembly Election (Constituencies)",
                "organization": "London Assembly",
                "post": {
                    "post_candidates": None,
                    "post_name": "Assembly Member for Lambeth and Southwark",
                    "post_slug": "lambeth-and-southwark",
                },
            },
            {
                "candidates": [{
                    "birth_date":
                    "",
                    "contact_details": [],
                    "death_date":
                    "",
                    "email":
                    None,
                    "extra_fields": [{
                        "favourite_biscuits": ""
                    }],
                    "gender":
                    "",
                    "honorific_prefix":
                    "",
                    "honorific_suffix":
                    "",
                    "id":
                    2009,
                    "identifiers": [],
                    "images": [],
                    "links": [],
                    "memberships": [{
                        "elected":
                        None,
                        "election": {
                            "id":
                            "gla.a.2016-05-05",
                            "name":
                            "2016 London Assembly Election (Additional)",
                            "url":
                            "http://testserver/api/v0.9/elections/gla.a.2016-05-05/",
                        },
                        "end_date":
                        None,
                        "id":
                        membership_pk,
                        "label":
                        "",
                        "on_behalf_of": {
                            "id": "party:53",
                            "name": "Labour Party",
                        },
                        "party_list_position":
                        None,
                        "person": {
                            "id": 2009,
                            "name": "Tessa Jowell",
                            "url": "http://testserver/api/v0.9/persons/2009/",
                        },
                        "post": {
                            "id": "assembly-member",
                            "label": "Assembly Member",
                            "slug": "london",
                            "url": "http://testserver/api/v0.9/posts/london/",
                        },
                        "role":
                        "Candidate",
                        "start_date":
                        None,
                        "url":
                        "http://testserver/api/v0.9/memberships/{}/".format(
                            membership_pk),
                    }],
                    "name":
                    "Tessa Jowell",
                    "other_names": [],
                    "sort_name":
                    "",
                    "thumbnail":
                    None,
                    "url":
                    "http://testserver/api/v0.9/persons/2009/",
                }],
                "election_date":
                text_type(self.future_date.isoformat()),
                "election_id":
                "gla.a.2016-05-05",
                "election_name":
                "2016 London Assembly Election (Additional)",
                "organization":
                "London Assembly",
                "post": {
                    "post_candidates": None,
                    "post_name": "Assembly Member",
                    "post_slug": "london",
                },
            },
        ]

        self.assertEqual(expected, output)
예제 #34
0
def validate_ip(host):
    if not isinstance(host, text_type):
        host = text_type(host)

    return ip_network(host)
예제 #35
0
def py_scanstring(s, end, encoding=None, strict=True,
        _b=BACKSLASH, _m=STRINGCHUNK.match, _join=u('').join,
        _PY3=PY3, _maxunicode=sys.maxunicode):
    """Scan the string s for a JSON string. End is the index of the
    character in s after the quote that started the JSON string.
    Unescapes all valid JSON string escape sequences and raises ValueError
    on attempt to decode an invalid string. If strict is False then literal
    control characters are allowed in the string.

    Returns a tuple of the decoded string and the index of the character in s
    after the end quote."""
    if encoding is None:
        encoding = DEFAULT_ENCODING
    chunks = []
    _append = chunks.append
    begin = end - 1
    while 1:
        chunk = _m(s, end)
        if chunk is None:
            raise JSONDecodeError(
                "Unterminated string starting at", s, begin)
        end = chunk.end()
        content, terminator = chunk.groups()
        # Content is contains zero or more unescaped string characters
        if content:
            if not _PY3 and not isinstance(content, text_type):
                content = text_type(content, encoding)
            _append(content)
        # Terminator is the end of string, a literal control character,
        # or a backslash denoting that an escape sequence follows
        if terminator == '"':
            break
        elif terminator != '\\':
            if strict:
                msg = "Invalid control character %r at"
                raise JSONDecodeError(msg, s, end)
            else:
                _append(terminator)
                continue
        try:
            esc = s[end]
        except IndexError:
            raise JSONDecodeError(
                "Unterminated string starting at", s, begin)
        # If not a unicode escape sequence, must be in the lookup table
        if esc != 'u':
            try:
                char = _b[esc]
            except KeyError:
                msg = "Invalid \\X escape sequence %r"
                raise JSONDecodeError(msg, s, end)
            end += 1
        else:
            # Unicode escape sequence
            msg = "Invalid \\uXXXX escape sequence"
            esc = s[end + 1:end + 5]
            escX = esc[1:2]
            if len(esc) != 4 or escX == 'x' or escX == 'X':
                raise JSONDecodeError(msg, s, end - 1)
            try:
                uni = int(esc, 16)
            except ValueError:
                raise JSONDecodeError(msg, s, end - 1)
            end += 5
            # Check for surrogate pair on UCS-4 systems
            # Note that this will join high/low surrogate pairs
            # but will also pass unpaired surrogates through
            if (_maxunicode > 65535 and
                uni & 0xfc00 == 0xd800 and
                s[end:end + 2] == '\\u'):
                esc2 = s[end + 2:end + 6]
                escX = esc2[1:2]
                if len(esc2) == 4 and not (escX == 'x' or escX == 'X'):
                    try:
                        uni2 = int(esc2, 16)
                    except ValueError:
                        raise JSONDecodeError(msg, s, end)
                    if uni2 & 0xfc00 == 0xdc00:
                        uni = 0x10000 + (((uni - 0xd800) << 10) |
                                         (uni2 - 0xdc00))
                        end += 6
            char = unichr(uni)
        # Append the unescaped character
        _append(char)
    return _join(chunks), end
예제 #36
0
    def test_results_for_candidates_for_postcode(self, mock_requests):
        self._setup_data()

        person_extra = PersonExtraFactory.create(base__id='2009',
                                                 base__name='Tessa Jowell')

        CandidacyExtraFactory.create(
            election=self.election_gla,
            base__person=person_extra.base,
            base__post=self.post_extra.base,
            base__on_behalf_of=self.labour_party_extra.base,
            post_election=self.election_gla.postextraelection_set.get(
                postextra=self.post_extra))
        membership_pk = person_extra.base.memberships.first().pk

        self.maxDiff = None

        mock_requests.get.side_effect = fake_requests_for_every_election
        response = self.app.get(
            '/api/v0.9/candidates_for_postcode/?postcode=SE24+0AG')

        output = response.json
        self.assertEqual(len(output), 2)
        expected = [{
            u'candidates': [],
            u'election_date': text_type(self.future_date.isoformat()),
            u'election_id': u'gla.c.2016-05-05',
            u'election_name':
            u'2016 London Assembly Election (Constituencies)',
            u'organization': u'London Assembly',
            u'post': {
                u'post_candidates': None,
                u'post_name': u'Assembly Member for Lambeth and Southwark',
                u'post_slug': u'lambeth-and-southwark'
            }
        }, {
            u'candidates': [{
                u'birth_date':
                u'',
                u'contact_details': [],
                u'death_date':
                u'',
                u'email':
                None,
                u'extra_fields': [],
                u'gender':
                u'',
                u'honorific_prefix':
                u'',
                u'honorific_suffix':
                u'',
                u'id':
                2009,
                u'identifiers': [],
                u'images': [],
                u'links': [],
                u'memberships': [{
                    u'elected':
                    None,
                    u'election': {
                        u'id':
                        u'gla.a.2016-05-05',
                        u'name':
                        u'2016 London Assembly Election (Additional)',
                        u'url':
                        u'http://localhost:80/api/v0.9/elections/gla.a.2016-05-05/'
                    },
                    u'end_date':
                    None,
                    u'id':
                    membership_pk,
                    u'label':
                    u'',
                    u'on_behalf_of': {
                        u'id':
                        u'party:53',
                        u'name':
                        u'Labour Party',
                        u'url':
                        u'http://localhost:80/api/v0.9/organizations/party:53/'
                    },
                    u'organization':
                    None,
                    u'party_list_position':
                    None,
                    u'person': {
                        u'id': 2009,
                        u'name': u'Tessa Jowell',
                        u'url': u'http://localhost:80/api/v0.9/persons/2009/'
                    },
                    u'post': {
                        u'id': u'london',
                        u'label': u'Assembly Member',
                        u'slug': u'london',
                        u'url': u'http://localhost:80/api/v0.9/posts/london/'
                    },
                    u'role':
                    u'Candidate',
                    u'start_date':
                    None,
                    u'url':
                    u'http://localhost:80/api/v0.9/memberships/{}/'.format(
                        membership_pk)
                }],
                u'name':
                u'Tessa Jowell',
                u'other_names': [],
                u'sort_name':
                u'',
                u'thumbnail':
                None,
                u'url':
                u'http://localhost:80/api/v0.9/persons/2009/',
            }],
            u'election_date':
            text_type(self.future_date.isoformat()),
            u'election_id':
            u'gla.a.2016-05-05',
            u'election_name':
            u'2016 London Assembly Election (Additional)',
            u'organization':
            u'London Assembly',
            u'post': {
                u'post_candidates': None,
                u'post_name': u'Assembly Member',
                u'post_slug': u'london'
            }
        }]

        self.assertEqual(expected, output)