def test_create_post__election_from_ballot_id_dict(self):
        self.assertEqual(every_election.Post.objects.all().count(), 0)
        self.assertEqual(every_election.PostExtra.objects.all().count(), 0)
        self.assertEqual(every_election.YNRElection.objects.all().count(), 0)
        election = self.ee_importer.election_tree[self.child_id]

        parent = self.ee_importer.get_parent(self.child_id)
        election.get_or_create_post_election(parent=parent)

        self.assertTrue(election.post_created)
        with self.assertNumQueries(0):
            election.get_or_create_post_election(parent=parent)
            election.get_or_create_post()
        self.assertFalse(election.post_created)

        self.assertEqual(every_election.Post.objects.all().count(), 1)
        self.assertEqual(every_election.PostExtra.objects.all().count(), 1)

        self.assertEqual(
            election.party_set_object.name,
            "Great Britain"
        )
        self.assertEqual(
            election.post_object.label,
            "Alperton"
        )
        self.assertEqual(check_constraints(), [])
예제 #2
0
 def test_create_many_elections_and_posts(self):
     for ballot_id, election_dict in self.ee_importer.ballot_ids.items():
         parent = self.ee_importer.get_parent(ballot_id)
         election_dict.get_or_create_post_election(parent=parent)
     self.assertEqual(every_election.PostExtra.objects.all().count(), 189)
     self.assertEqual(every_election.YNRElection.objects.all().count(), 10)
     self.assertEqual(check_constraints(), [])
예제 #3
0
    def handle(self, *args, **options):

        # Mark all elections as not current, any that are current will
        # be (re)set later
        Election.objects.update(current=False)

        url = "{}api/elections?group_type=organisation&current=1".format(
            self.EE_BASE_URL)
        while url:
            req = requests.get(url)
            data = req.json()
            self.process_results(data['results'])
            url = data.get('next')

        url = "{}api/elections?group_type=election".format(
            self.EE_BASE_URL)
        while url:
            req = requests.get(url)
            data = req.json()
            self.process_parl_results(data['results'])
            url = data.get('next')

        errors = check_constraints()
        if errors:
            for error in errors:
                print(error)
            msg = "The import broke the constraints listed above; rolling " \
                  "back the transaction..."
            raise Exception(msg)
    def handle(self, *args, **options):
        # TODO Consider filtering on future or current elections?
        url = "{}api/elections?group_type=organisation&current=1".format(
            self.EE_BASE_URL)
        while url:
            req = requests.get(url)
            data = req.json()
            self.process_results(data['results'])
            url = data.get('next')

        url = "{}api/elections?group_type=election".format(
            self.EE_BASE_URL)
        while url:
            req = requests.get(url)
            data = req.json()
            self.process_parl_results(data['results'])
            url = data.get('next')

        errors = check_constraints()
        if errors:
            for error in errors:
                print(error)
            msg = "The import broke the constraints listed above; rolling " \
                  "back the transaction..."
            raise Exception(msg)
예제 #5
0
    def test_create_from_all_elections(self, mock_requests):
        mock_requests.get.side_effect = fake_requests_for_every_election
        query_args = {
            'poll_open_date': '2019-01-17',
            'current': 'True',
        }
        self.ee_importer = every_election.EveryElectionImporter(query_args)
        self.ee_importer.build_election_tree()

        for ballot_id, election_dict in self.ee_importer.ballot_ids.items():
            parent = self.ee_importer.get_parent(ballot_id)
            election_dict.get_or_create_post_election(parent=parent)
        self.assertEqual(every_election.PostExtra.objects.all().count(), 11)
        self.assertEqual(every_election.YNRElection.objects.all().count(), 10)
        self.assertEqual(check_constraints(), [])
예제 #6
0
    def handle(self, *args, **options):

        # Make sure the PartySet exists
        party_set, created = PartySet.objects.update_or_create(
            slug=PARTY_SET_SLUG, defaults={'name': PARTY_SET_NAME})

        for election in ELECTIONS:
            print('Importing candidates for election: {}'.format(
                election['CANDIDATES_FILE']))
            self.import_candidates_for_election(election, party_set)

        errors = check_constraints()
        if errors:
            for e in errors:
                print(e)
            raise Exception('Constraint errors detected. Aborting.')
    def handle(self, *args, **options):

        # Make sure the PartySet exists
        party_set, created = PartySet.objects.update_or_create(
            slug=PARTY_SET_SLUG,
            defaults={
                'name': PARTY_SET_NAME
            }
        )

        for election in ELECTIONS:
            print('Importing candidates for election: {}'.format(election['CANDIDATES_FILE']))
            self.import_candidates_for_election(election, party_set)

        errors = check_constraints()
        if errors:
            for e in errors:
                print(e)
            raise Exception('Constraint errors detected. Aborting.')
예제 #8
0
    def handle(self, *args, **options):

        # Get all elections from EveryElection
        ee_importer = EveryElectionImporter()
        ee_importer.build_election_tree()

        with transaction.atomic():
            # Mark all elections as not current, any that are current will
            # be (re)set later
            Election.objects.update(current=False)
            for ballot_id, election_dict in ee_importer.ballot_ids.items():
                parent = ee_importer.get_parent(ballot_id)
                election_dict.get_or_create_post_election(parent=parent)

            # Check for errors
            errors = check_constraints()
            if errors:
                for error in errors:
                    print(error)
                msg = "The import broke the constraints listed above; " \
                      "rolling back the transaction..."
                raise Exception(msg)
    def handle(self, *args, **options):
        # TODO Consider filtering on future or current elections?
        url = "{}api/elections?group_type=organisation".format(
            self.EE_BASE_URL)
        while url:
            req = requests.get(url)
            data = req.json()
            self.process_results(data['results'])
            url = data.get('next')
        url = "{}api/elections?group_type=election".format(self.EE_BASE_URL)
        while url:
            req = requests.get(url)
            data = req.json()
            self.process_parl_results(data['results'])
            url = data.get('next')

        errors = check_constraints()
        if errors:
            for error in errors:
                print(error)
            msg = "The import broke the constraints listed above; rolling " \
                  "back the transaction..."
            raise Exception(msg)
    def handle(self, *args, **options):

        # Make sure the PartySet exists
        party_set, created = PartySet.objects.update_or_create(
            slug=PARTY_SET_SLUG, defaults={'name': PARTY_SET_NAME})

        for election_metadata in self.get_all_elections():

            # Set up the election
            election_data = {
                'slug': election_metadata['ELECTION_SLUG'],
                'for_post_role': election_metadata['POST_ROLE'],
                'name': election_metadata['ELECTION_NAME'],
                'organization_name': election_metadata['ORG_NAME'],
                'organization_slug': election_metadata['ORG_SLUG'],
                'party_lists_in_use': False,
            }

            org = self.get_or_create_organization(
                election_data['organization_slug'],
                election_data['organization_name'],
            )

            del election_data['organization_name']
            del election_data['organization_slug']
            election_data['organization'] = org

            election_slug = election_data.pop('slug')
            election_data.update(CONSISTENT_ELECTION_DATA)
            election, created = Election.objects.update_or_create(
                slug=election_slug,
                defaults=election_data,
            )

            # Create the AreaType for the country
            # DIS is the Kenya MapIt type for County/District
            area_type, created = AreaType.objects.get_or_create(
                name=election_metadata['AREA_TYPE_FOR_POSTS'],
                defaults={'source': 'MapIt'},
            )

            # Tie the AreaType to the election
            election.area_types.add(area_type)

            code_column, name_column, area_id_prefix, classification = {
                'CTR': (None, None, 'country:', 'Country'),
                'DIS': ('County Code', 'County Name', 'county:', 'County'),
                'CON': ('Constituency Code', 'Constituency Name',
                        'constituency:', 'Constituency'),
                'WRD': ('Ward Code', 'Ward Name', 'ward:', 'Ward')
            }[election_metadata['AREA_TYPE_FOR_POSTS']]

            if election_metadata['POST_ROLE'] == 'President':
                areas_for_posts = {
                    '1': {
                        'id': '1',
                        'name': 'Kenya',
                    }
                }
            else:
                areas_for_posts = self.areas_from_csv(
                    election_metadata['CANDIDATES_FILE'],
                    code_column, name_column,
                    election_metadata.get('COUNTY_ID_RESTRICTION'))

            # By now areas_for_posts should contain one of each area.

            # Common stuff
            organization = election.organization
            post_role = election.for_post_role

            # For each area, make sure the area exists and make sure a post exists.
            for id, area in areas_for_posts.iteritems():

                area_object = self.get_or_create_area(
                    identifier=area_id_prefix + area['id'],
                    name=area['name'],
                    classification=classification,
                    area_type=area_type)

                post_label = election_metadata['POST_LABEL_PREFIX'] + area[
                    'name']
                post_slug = election_metadata['POST_SLUG_PREFIX'] + '-' + area[
                    'id']

                self.get_or_create_post(slug=post_slug,
                                        label=post_label,
                                        organization=organization,
                                        area=area_object,
                                        role=post_role,
                                        election=election,
                                        party_set=party_set)

            errors = check_constraints()
            if errors:
                for e in errors:
                    print(e)
                raise Exception('Constraint errors detected. Aborting.')