예제 #1
0
    def load_2002_file(self, mapping, contest):
        headers = [
            'office',
            'fill',
            'jurisdiction',
            'last',
            'middle',
            'first',
            'party',
            'winner',
            'vote_type',
            'votes',
            'fill2'
        ]

        # Store result instances for bulk loading
        results = []
        with open(join(self.cache_dir, mapping['generated_filename']), 'rU') as csvfile:
            reader = unicodecsv.DictReader(csvfile, fieldnames = headers, delimiter='|', encoding='latin-1')
            reporting_level = 'county'
            #TODO: replace jurisidctions with datasource.mappings
            #jurisdictions = self.jurisdiction_mappings(('ocd','fips','urlname','name'))
            for row in reader:
                if row['jurisdiction'].strip() == 'Baltimore City':
                    jurisdiction = [x for x in jurisdictions if x['name'] == "Baltimore City"][0]
                else:
                    jurisdiction = [x for x in jurisdictions if x['name']+" County" == row['jurisdiction'].strip()][0]
                if row['winner'] == '1':
                    winner = True
                else:
                    winner = False
                if row['last'].strip() == 'zz998':
                    name = row['last'].strip()
                    candidate = Candidate(state=self.state.upper(), family_name=name)
                    write_in=True
                else:
                    cand_name = self.combine_name_parts([row['first'], row['middle'], row['last']])
                    name = self.parse_name(cand_name)
                    write_in=False
                    candidate = Candidate(state=self.state.upper(), given_name=name.first, additional_name=name.middle, family_name=name.last, suffix=name.suffix, name=name.full_name)

                result_kwargs = {
                    'ocd_id': ocd_id + "/precinct:" + str(row['Election District']) + "-" + str(row['Election Precinct']),
                    'jurisdiction': jurisdiction,
                    'raw_office': row['Office Name'] + ' ' + row['Office District'],
                    'reporting_level': reporting_level,
                    'candidate': candidate,
                    'party': row['Party'],
                    'write_in': write_in,
                    'total_votes': total_votes,
                    'vote_breakdowns': vote_breakdowns
                }

                result = Result(**result_kwargs)
                #self.load_county_2002(row, jurisdiction['ocd'], jurisdiction['name'], candidate, reporting_level, write_in, winner)))
        Result.insert(results)
예제 #2
0
def validate_obama_primary_candidacy_2012():
    """Should only be one Obama primary candidacy for 2012"""
    elec_id= 'md-2012-04-03-primary'
    kwargs = {
        'election_id': elec_id,
        'contest_slug': 'president-d',
        'slug': 'barack-obama',
    }
    try:
        cand = Candidate.objects.get(**kwargs)
        print "PASS: 1 obama primary candidacy found for 2012: %s" % "-".join(cand.key)
    except Candidate.DoesNotExist:
        raise Candidate.DoesNotExist("zero obama primary candidacies found for 2012")
    except Candidate.MultipleObjectsReturned as e:
        raise Candidate.MultipleObjectsReturned("multiple obama primary candidacies found for 2012: %s" %  e)
예제 #3
0
    def __call__(self):
        candidates = []
        seen = set()

        for rr in self.get_rawresults():
            key = (rr.election_id, rr.contest_slug, rr.candidate_slug)
            if key not in seen:
                fields = self.get_candidate_fields(rr)
                fields['contest'] = self.get_contest(rr)
                if "other" in fields['full_name'].lower():
                    if fields['full_name'] == "Other Write-Ins":
                        fields['flags'] = [
                            'aggregate',
                        ]
                    else:
                        # As far as I can tell the value should always be
                        # "Other Write-Ins", but output a warning to let us
                        # know about some cases we may be missing.
                        logging.warn("'other' found in candidate name field"
                                     "value: '%s'" % rr.full_name)
                candidate = Candidate(**fields)
                candidates.append(candidate)
                seen.add(key)

        Candidate.objects.insert(candidates, load_bulk=False)
        print "Created %d candidates." % len(candidates)
예제 #4
0
 def load_non2002_file(self, mapping, contest):
     with open(join(self.cache_dir, mapping['generated_filename']), 'rU') as csvfile:
         reader = unicodecsv.DictReader(csvfile, encoding='latin-1')
         if 'state_legislative' in mapping['generated_filename']:
             reporting_level = 'state_legislative'
             districts = [j for j in reader.fieldnames if j not in ['Vote Type','Vote Type (FOR=1,AGAINST=2)', 'County', 'Candidate Name', 'Party', 'Office Name', 'Office District', 'Winner', 'Write-In?']]
         elif 'precinct' in mapping['generated_filename']:
             reporting_level = 'precinct'
             jurisdiction = mapping['name']
         else:
             reporting_level = 'county'
             jurisdiction = mapping['name']
         for row in reader:
             if not row['Office Name'].strip() in ['President - Vice Pres', 'U.S. Senator', 'U.S. Congress', 'Governor / Lt. Governor', 'Comptroller', 'Attorney General', 'State Senator', 'House of Delegates']:
                 continue
             # parse candidate - we get full names
             name = self.parse_name(row['Candidate Name'])
             # if office is president, then skip state in lookup, otherwise use
             if row['Office Name'] == 'President - Vice Pres':
                 candidate = Candidate(state='US', given_name=name.first, additional_name=name.middle, family_name=name.last, suffix=name.suffix, name=name.full_name)
             else:
                 candidate = Candidate(state=self.state.upper(), given_name=name.first, additional_name=name.middle, family_name=name.last, suffix=name.suffix, name=name.full_name)
             # sometimes write-in field not present
             try:
                 if row['Write-In?'] == 'Y':
                     write_in = True
                 else:
                     write_in = False
             except KeyError:
                 write_in = None
             if row['Winner'] == 'Y':
                 winner = True
             else:
                 winner = False
             if reporting_level == 'state_legislative':
                 contest.update(push_all__results=(self.load_state_legislative(row, districts, candidate, reporting_level, write_in, winner)))
             elif reporting_level == 'county':
                 contest.update(push__results=(self.load_county(row, mapping['ocd_id'], jurisdiction, candidate, reporting_level, write_in, winner)))
             elif reporting_level == 'precinct':
                 contest.update(push__results=(self.load_precinct(row, mapping['ocd_id'], jurisdiction, candidate, reporting_level, write_in, winner)))
    def test_get_list_filter_by_level(self):
        level = 'precinct'

        # Make sure there's at least one result at this level
        if Result.objects(reporting_level=level).count() == 0:
            candidate = Candidate.objects()[0]
            contest = candidate.contest
            ResultFactory(candidate=candidate, contest=contest,
                reporting_level=level)

        data = self.roller.get_list(state="MD", reporting_level=level)
        self.assertEqual(len(data),
            Result.objects(state="MD", reporting_level=level).count())
예제 #6
0
    def test_get_list_filter_by_level(self):
        level = 'precinct'

        # Make sure there's at least one result at this level
        if Result.objects(reporting_level=level).count() == 0:
            candidate = Candidate.objects()[0]
            contest = candidate.contest
            ResultFactory(candidate=candidate,
                          contest=contest,
                          reporting_level=level)

        data = self.roller.get_list(state="MD", reporting_level=level)
        self.assertEqual(
            len(data),
            Result.objects(state="MD", reporting_level=level).count())
예제 #7
0
    def __call__(self):
        candidates = []
        seen = set()

        for rr in self.get_raw_results():
            key = (rr.election_id, rr.contest_slug, rr.candidate_slug)
            if key not in seen:
                fields = self.get_candidate_fields(rr)
                if not fields['full_name']:
                    quit(fields)
                fields['contest'] = self.get_contest(rr)
                #print fields
                candidate = Candidate(**fields)
                candidates.append(candidate)
                seen.add(key)

        Candidate.objects.insert(candidates, load_bulk=False)
        logger.info("Created {} candidates.".format(len(candidates)))
예제 #8
0
    def load_2000_primary_file(self, mapping, contest):
        candidates = {}
        results = []
        with open(join(self.cache_dir, mapping['generated_filename']), 'rU') as csvfile:
            lines = csvfile.readlines()
            #TODO: use Datasource.mappings instead of jurisdiction_mappings
            #import ipdb;ipdb.set_trace()
            #jurisdictions = self.jurisdiction_mappings(('ocd','fips','urlname','name'))
            #jurisdictions = self.datasource.mappings()
            for line in lines[0:377]:
                if line.strip() == '':
                    continue # skip blank lines
                else: # determine if this is a row with an office
                    cols = line.strip().split(',')
                    if cols[0][1:29] == 'President and Vice President':
                        office_name = 'President - Vice Pres'
                        if 'Democratic' in cols[0]:
                            party = 'DEM'
                        else:
                            party = 'REP'
                        continue
                    elif cols[0][1:13] == 'U.S. Senator':
                        office_name = 'U.S. Senator'
                        if 'Democratic' in cols[0]:
                            party = 'DEM'
                        else:
                            party = 'REP'
                        continue
                    elif cols[0][1:27] == 'Representative in Congress':
                        district = int(cols[0][71:73])
                        office_name = 'U.S. Congress ' + str(district)
                        if 'Democratic' in cols[0]:
                            party = 'DEM'
                        else:
                            party = 'REP'
                        continue
                    # skip offices we don't want
                    elif cols[0][1:21] == 'Judge of the Circuit':
                        continue
                    elif cols[0][1:31] == 'Female Delegates and Alternate':
                        continue
                    elif cols[0][1:29] == 'Male Delegates and Alternate':
                        continue
                    elif cols[0][1:28] == 'Delegates to the Republican':
                        continue
                    elif cols[0] == '""':
                        candidates = [x.replace('"','').strip() for x in cols if x.replace('"','') != '']
                        winner = [i for i, j in enumerate(candidates) if 'Winner' in j][0] # index of winning candidate
                        candidates[winner] = candidates[winner].split(' Winner')[0]
                        # handle name_parse and Uncommitted candidate
                    else: # county results
                        result = [x.replace('"','').strip() for x in cols if x != '']
                        #juris = [j for j in jurisdictions if j['name'] == result[0].strip()][0]
                        juris = [j for j in self.datasource.mappings() if j['name'] == result[0].strip()][0]
                        cand_results = zip(candidates, result[1:])
                        for cand, votes in cand_results:
                            name = self.parse_name(cand)
                            #cand_kwargs = {
                                #'given_name': name.first,
                                #'additional_name': name.middle,
                                #'family_name': name.last,
                                #'suffix': name.suffix,
                                #'name': name.full_name
                            #}
                            if office_name == 'President - Vice Pres':
                                cand_kwargs['state'] = 'US'
                            else:
                                cand_kwargs['state'] = self.state.upper()

                            #TODO Get or create candidate
                            candidate = Candidate(**cand_kwargs)

                            result_kwargs = {
                                'candidate': candidate,
                                'ocd_id': juris['ocd'],
                                'jurisdiction': juris['name'],
                                'raw_office': office_name,
                                'reporting_level': 'county',
                                'party': party,
                                'write_in': False,
                                'total_votes': int(votes),
                                'vote_breakdowns': {}
                            }
                            result = Result(**result_kwargs)
                            results.append(result)