def test_register_raw(self):
        mock_transform = Mock(return_value=None)
        mock_transform.__name__ = 'mock_transform'

        registry.register("XX", mock_transform, raw=True)
        transform = registry.get("XX", "mock_transform", raw=True)
        transform()
        mock_transform.assert_called_once_with()
Beispiel #2
0
    def test_register_raw(self):
        mock_transform = Mock(return_value=None)
        mock_transform.__name__ = 'mock_transform'

        registry.register("XX", mock_transform, raw=True)
        transform = registry.get("XX", "mock_transform", raw=True)
        transform()
        mock_transform.assert_called_once_with()
Beispiel #3
0
    def test_register_with_validators(self):
        mock_transform = Mock(return_value=None)
        mock_transform.__name__ = 'mock_transform'
        mock_validator1 = Mock(return_value=None)
        mock_validator1.__name__ = 'mock_validator1'
        mock_validator2 = Mock(return_value=None)
        mock_validator2.__name__ = 'mock_validator2'

        validators = [mock_validator1, mock_validator2]

        registry.register("XX", mock_transform, validators)

        transform = registry.get("XX", "mock_transform")
        self.assertEqual(transform.validators, validators)

        transform()
        mock_transform.assert_called_once_with()
    def test_register_with_validators(self):
        mock_transform = Mock(return_value=None)
        mock_transform.__name__ = 'mock_transform'
        mock_validator1 = Mock(return_value=None)
        mock_validator1.__name__ = 'mock_validator1'
        mock_validator2 = Mock(return_value=None)
        mock_validator2.__name__ = 'mock_validator2'

        validators = [mock_validator1, mock_validator2]

        registry.register("XX", mock_transform, validators) 

        transform = registry.get("XX", "mock_transform")
        self.assertEqual(list(transform.validators.values()), validators)

        transform()
        mock_transform.assert_called_once_with()
Beispiel #5
0
        """
        Get the BallotMeasure model for a RawResult
        """
        fields = self.get_ballot_measure_fields(raw_result)
        fields.update(extra)
        del fields['source']
        try:
            bm = BallotMeasure.objects.get(**fields)
        except BallotMeasureesNotExist:
            raise
        # self._candidate_cache[key] = candidate 
        return bm


    def _create_results(self, results):
        """
        Create the Result objects in the database.
        """
        Result.objects.no_cache().insert(results, load_bulk=False)
        print "Created %d results." % len(results)

    def reverse(self):
        old_results = self.get_results()
        print "\tDeleting %d previously loaded results" % old_results.count() 
        old_results.delete()


registry.register('il', CreateContestsTransform)
registry.register('il', CreateBallotChoicesTransform)
registry.register('il', CreateResultsTransform)
Beispiel #6
0
                created.
                Default is the raw result's jurisdiction field.
            reporting_level: the reporting level to reflect in the OCD ID.
                Default is raw_result.reporting_level. Specifying this
                argument is useful if you want to use a RawResult's
                jurisdiction, but override the reporting level.

        """
        if reporting_level is None:
            reporting_level = raw_result.reporting_level

        if jurisdiction is None:
            jurisdiction = raw_result.jurisdiction

        juris_ocd = ocd_type_id(jurisdiction)

        if reporting_level == "county":
            # TODO: Should jurisdiction/ocd_id be different for Baltimore City?
            return "ocd-division/country:us/state:md/county:%s" % juris_ocd
        elif reporting_level == "state_legislative":
            return "ocd-division/country:us/state:md/sldl:%s" % juris_ocd
        elif reporting_level == "precinct":
            county_ocd_id = "/".join(raw_result.ocd_id.split('/')[:-1])
            return "%s/precinct:%s" % (county_ocd_id, juris_ocd)
        else:
            return None


registry.register('wa', CreateContestsTransform)
registry.register('wa', CreateCandidatesTransform)
registry.register('wa', CreateResultsTransform)
Beispiel #7
0
from openelex.base.transform import registry
from openelex.models import Candidate

def parse_names_after_2002():
    cands = Candidate.objects.filter(
        (Q(election_id__not__contains="2000") | Q(election_id__not__contains="2002")),
        state='MD'
    )
    #Loop through candidates, perform name parse and update
    for cand in cands:
        # Skip Other write-ins
        if 'other' in cand.slug:
            continue
        name = HumanName(cand.raw_full_name)
        cand.given_name = name.first
        cand.family_name = name.last
        cand.additional_name = name.middle
        cand.suffix = name.suffix
        cand.save()

#def standardize_office_and_district():
#    pass

#def clean_vote_counts():
    #pass

registry.register('md', parse_names_after_2002)
#registry.register('md', standardize_office_and_district)
#registry.register('md', clean_vote_counts)
Beispiel #8
0
    """
    Add a note explaining that precinct-level results only contain election
    night totals.
    """
    note = "Value of votes field contains only election night vote totals."
    Result.objects.filter(reporting_level='precinct').update(set__notes=note)


# TODO: When should we create a Person

#def standardize_office_and_district():
#    pass

#def clean_vote_counts():
#pass

registry.register('md', CreateContestsTransform)
registry.register('md', CreateCandidatesTransform)
registry.register('md', CreateResultsTransform)
registry.register('md', CreateDistrictResultsTransform)
registry.register('md', Create2000PrimaryCongressCountyResultsTransform)
registry.register('md', NormalizePrecinctTransform,
                  [validate_precinct_names_normalized])
registry.register('md', RemoveBaltimoreCityComptroller,
                  [validate_no_baltimore_city_comptroller])
registry.register('md', CombineUncommittedPresStateLegislativeResults,
                  [validate_uncommitted_primary_state_legislative_results])
registry.register('md', add_precinct_result_note)
#registry.register('md', standardize_office_and_district)
#registry.register('md', clean_vote_counts)
Beispiel #9
0
            raw_result.candidate_slug)
        try:
            return self._candidate_cache[key]
        except KeyError:
            fields = self.get_candidate_fields(raw_result)
            fields.update(extra)
            del fields['source']
            try:
                candidate = Candidate.objects.get(**fields)
            except Candidate.DoesNotExist:
                raise
            self._candidate_cache[key] = candidate 
            return candidate

    def _create_results(self, results):
        """
        Create the Result objects in the database.
        """
        Result.objects.insert(results, load_bulk=False)
        print "Created %d results." % len(results)

    def reverse(self):
        old_results = self.get_results()
        print "\tDeleting %d previously loaded results" % old_results.count() 
        old_results.delete()


registry.register('il', CreateContestsTransform)
registry.register('il', CreateCandidatesTransform)
registry.register('il', CreateResultsTransform)
Beispiel #10
0
from openelex.base.transform import Transform, registry
from openelex.models import RawResult

class FixVanBurenTransform(Transform):
    """
    s/VanBuren/Van Buren in RawResults from the source file
    20001107__ia__general__state_senate__county.csv
    """
    name = 'fix_van_buren'

    def __call__(self):
        results = RawResult.objects.filter(
            source="20001107__ia__general__state_senate__county.csv",
            jurisdiction="VanBuren")
        msg = "Changing 'VanBuren' to 'Van Buren' in {} raw results.".format(
            results.count())
        print(msg)
        results.update(set__jurisdiction="Van Buren",
            set__ocd_id="ocd-division/country:us/state:ia/county:van_buren")

    def reverse(self):
        results = RawResult.objects.filter(
            source="20001107__ia__general__state_senate__county.csv",
            jurisdiction="Van Buren")
        msg = "Reverting 'Van Buren' to 'VanBuren' in {} raw results".format(
            results.count())
        print(msg)
        results.update(set__jurisdiction="VanBuren", set__ocd_id="")
           
registry.register('ia', FixVanBurenTransform, raw=True)
    """
    Add a note explaining that precinct-level results only contain election
    night totals.
    """
    note = "Value of votes field contains only election night vote totals."
    Result.objects.filter(reporting_level='precinct').update(set__notes=note)


# TODO: When should we create a Person

#def standardize_office_and_district():
#    pass

#def clean_vote_counts():
    #pass

registry.register('md', CreateContestsTransform)
registry.register('md', CreateCandidatesTransform)
registry.register('md', CreateResultsTransform)
registry.register('md', CreateDistrictResultsTransform)
registry.register('md', Create2000PrimaryCongressCountyResultsTransform)
registry.register('md', NormalizePrecinctTransform,
    [validate_precinct_names_normalized])
registry.register('md', RemoveBaltimoreCityComptroller,
    [validate_no_baltimore_city_comptroller])
registry.register('md', CombineUncommittedPresStateLegislativeResults,
    [validate_uncommitted_primary_state_legislative_results])
registry.register('md', add_precinct_result_note)
#registry.register('md', standardize_office_and_district)
#registry.register('md', clean_vote_counts)
from openelex.base.transform import Transform, registry
from openelex.models import RawResult

class FixVanBurenTransform(Transform):
    """
    s/VanBuren/Van Buren in RawResults from the source file
    20001107__ia__general__state_senate__county.csv
    """
    name = 'fix_van_buren'

    def __call__(self):
        results = RawResult.objects.filter(
            source="20001107__ia__general__state_senate__county.csv",
            jurisdiction="VanBuren")
        msg = "Changing 'VanBuren' to 'Van Buren' in {} raw results.".format(
            results.count())
        print(msg)
        results.update(set__jurisdiction="Van Buren",
            set__ocd_id="ocd-division/country:us/state:ia/county:van_buren")

    def reverse(self):
        results = RawResult.objects.filter(
            source="20001107__ia__general__state_senate__county.csv",
            jurisdiction="Van Buren")
        msg = "Reverting 'Van Buren' to 'VanBuren' in {} raw results".format(
            results.count())
        print(msg)
        results.update(set__jurisdiction="VanBuren", set__ocd_id="")
           
registry.register('ia', FixVanBurenTransform, raw=True)
Beispiel #13
0
from openelex.base.transform import registry
from openelex.models import Candidate


def parse_names_after_2002():
    cands = Candidate.objects.filter((Q(election_id__not__contains="2000")
                                      | Q(election_id__not__contains="2002")),
                                     state='MD')
    #Loop through candidates, perform name parse and update
    for cand in cands:
        # Skip Other write-ins
        if 'other' in cand.slug:
            continue
        name = HumanName(cand.raw_full_name)
        cand.given_name = name.first
        cand.family_name = name.last
        cand.additional_name = name.middle
        cand.suffix = name.suffix
        cand.save()


#def standardize_office_and_district():
#    pass

#def clean_vote_counts():
#pass

registry.register('md', parse_names_after_2002)
#registry.register('md', standardize_office_and_district)
#registry.register('md', clean_vote_counts)