Beispiel #1
0
    def _prep_county_result(self, row):
        kwargs = self._base_kwargs(row)
        vote_brkdown_fields = [
            ('election_day', 'Election Night Votes'),
            ('absentee', 'Absentees Votes'),
            ('provisional', 'Provisional Votes'),
            ('second_absentee', '2nd Absentees Votes'),
        ]
        vote_breakdowns = {}
        for field, key in vote_brkdown_fields:
            try:
                vote_breakdowns[field] = self._votes(row[key].strip())
            except KeyError:
                pass
        kwargs.update({
            'reporting_level': 'county',
            'jurisdiction': self.mapping['name'],
            'ocd_id': self.mapping['ocd_id'],
            'party': row['Party'].strip(),
            'votes': self._votes(row['Total Votes']),
            'vote_breakdowns': vote_breakdowns,
        })
        if (kwargs['office'] not in self.district_offices
                and kwargs['district'] != ''):
            kwargs['reporting_level'] = 'congressional_district_by_county'
            kwargs['reporting_district'] = kwargs['district']
            del kwargs['district']

        return RawResult(**kwargs)
Beispiel #2
0
    def load(self):
        headers = [
            'county', 'office', 'district', 'candidate', 'incumbent', 'party',
            'votes', 'pct'
        ]
        self._common_kwargs = self._build_common_election_kwargs()
        self._common_kwargs['reporting_level'] = 'county'
        # Store result instances for bulk loading
        results = []

        with self._file_handle as csvfile:
            reader = unicodecsv.DictReader(csvfile,
                                           fieldnames=headers,
                                           encoding='latin-1')
            for row in reader:
                if self._skip_row(row):
                    continue
                rr_kwargs = self._common_kwargs.copy()
                rr_kwargs.update(self._build_contest_kwargs(row))
                rr_kwargs.update(self._build_candidate_kwargs(row))
                jurisdiction = row['county'].strip()
                rr_kwargs.update({
                    'jurisdiction':
                    jurisdiction,
                    'parent_jurisdiction':
                    "Texas",
                    'ocd_id':
                    "{}/county:{}".format(self.mapping['ocd_id'],
                                          ocd_type_id(jurisdiction)),
                    'votes':
                    self._votes(row['votes'])
                })
                results.append(RawResult(**rr_kwargs))
        RawResult.objects.insert(results)
Beispiel #3
0
 def _prep_state_leg_results(self, row):
     kwargs = self._base_kwargs(row)
     kwargs.update({
         'reporting_level': 'state_legislative',
         'winner': row['Winner'].strip(),
         'write_in': self._writein(row),
         'party': row['Party'].strip(),
     })
     try:
         kwargs['write_in'] = row['Write-In?'].strip(
         )  # at the contest-level
     except KeyError as e:
         pass
     results = []
     for field, val in row.items():
         clean_field = field.strip()
         # Legislative fields prefixed with LEGS
         if not clean_field.startswith('LEGS'):
             continue
         kwargs.update({
             'jurisdiction':
             clean_field,
             # Remove the "LEGS " from the ocd_id.  This is a somewhat
             # transformy action, but do it here in order to make the OCD IDs
             # as usable as possible when we bake out raw results
             'ocd_id':
             "{}/sldl:{}".format(
                 self._get_state_ocd_id(),
                 ocd_type_id(clean_field.replace("LEGS ", ""))),
             'votes':
             self._votes(val),
         })
         results.append(RawResult(**kwargs))
     return results
Beispiel #4
0
    def load(self):
        headers = [
            'county', 'office', 'district', 'party', 'candidate', 'votes',
            'winner', 'write-in', 'notes'
        ]
        self._common_kwargs = self._build_common_election_kwargs()
        self._common_kwargs['reporting_level'] = 'county'
        # Store result instances for bulk loading
        results = []

        with self._file_handle as csvfile:
            reader = unicodecsv.DictReader(csvfile,
                                           fieldnames=headers,
                                           encoding='latin-1')
            for row in reader:
                if self._skip_row(row):
                    continue
                rr_kwargs = self._common_kwargs.copy()
                rr_kwargs.update(self._build_contest_kwargs(row))
                rr_kwargs.update(self._build_candidate_kwargs(row))
                if row['county'].strip() == '':
                    rr_kwargs.update({
                        'contest_winner': row['winner'].strip(),
                        'write_in': row['write-in'],
                        'notes': row['notes']
                    })
                results.append(RawResult(**rr_kwargs))
        RawResult.objects.insert(results)
Beispiel #5
0
    def load(self):
        headers = [
            'office', 'district', 'candidate', 'incumbent', 'party', 'votes',
            'pct'
        ]
        self._common_kwargs = self._build_common_election_kwargs()
        self._common_kwargs['reporting_level'] = 'county'
        # Store result instances for bulk loading
        results = []

        with self._file_handle as csvfile:
            reader = unicodecsv.DictReader(csvfile, fieldnames=headers)
            for row in reader:
                if self._skip_row(row):
                    continue
                rr_kwargs = self._common_kwargs.copy()
                rr_kwargs.update(self._build_contest_kwargs(row))
                rr_kwargs.update(self._build_candidate_kwargs(row))
                rr_kwargs.update({
                    'jurisdiction': "Texas",
                    'ocd_id': "ocd-division/country:us/state:tx",
                    'votes': self._votes(row['votes'])
                })
                results.append(RawResult(**rr_kwargs))
        RawResult.objects.insert(results)
Beispiel #6
0
    def _prep_state_leg_results(self, row):
        kwargs = self._base_kwargs(row)
        kwargs.update({
            'reporting_level': 'state_legislative',
            'winner': row['Winner'].strip(),
            'write_in': self._writein(row),
            'party': row['Party'].strip(),
        })
        try:
            kwargs['write_in'] = row['Write-In?'].strip(
            )  # at the contest-level
        except KeyError as e:
            pass
        results = []
        for field, val in list(row.items()):
            clean_field = field.strip()
            # Legislative fields prefixed with LEGS
            if not clean_field.startswith('LEGS'):
                continue

            kwargs.update({
                'jurisdiction':
                clean_field,
                'ocd_id':
                "{}/sldl:{}".format(self._get_state_ocd_id(),
                                    ocd_type_id(clean_field)),
                'votes':
                self._votes(val),
            })
            results.append(RawResult(**kwargs))
        return results
Beispiel #7
0
 def _prep_precinct_result(self, row):
     kwargs = self._common_kwargs.copy()
     kwargs.update(self._build_contest_kwargs(row))
     kwargs.update(self._build_candidate_kwargs(row))
     precinct = str(row['precinct_id'] + ' ' +
                    row['polling_location']).strip()
     county_ocd_id = [
         c for c in self.datasource._jurisdictions()
         if c['county'].upper() == row['county_name'].upper() + ' COUNTY'
     ][0]['ocd_id']
     kwargs.update({
         'reporting_level':
         'precinct',
         'jurisdiction':
         precinct,
         'parent_jurisdiction':
         row['county_name'],
         'ocd_id':
         "{}/precinct:{}".format(county_ocd_id,
                                 ocd_type_id(str(row['precinct_id']))),
         'party':
         row['party'].strip(),
         'votes':
         self._votes(row['votes'])
     })
     return RawResult(**kwargs)
Beispiel #8
0
    def load(self):
        self._common_kwargs = self._build_common_election_kwargs()
        self._common_kwargs['reporting_level'] = 'county'
        # Store result instances for bulk loading
        results = []

        p = clarify.Parser()

        p.parse(self._file_handle)
        for result in p.results:
            if self._skip_row(result.contest):
                continue
            if row['county'].strip() == '':
                total_votes = int(row['votes'].strip())
            else:
                rr_kwargs = self._common_kwargs.copy()
                rr_kwargs.update(self._build_contest_kwargs(row))
                rr_kwargs.update(self._build_candidate_kwargs(row))
                jurisdiction = row['county'].strip()
                rr_kwargs.update({
                    'jurisdiction': jurisdiction,
                    'ocd_id': "{}/county:{}".format(self.mapping['ocd_id'],
                        ocd_type_id(jurisdiction)),
                    'office': row['office'].strip(),
                    'district': row['district'].strip(),
                    'votes': int(row['votes'].strip())
                })
                results.append(RawResult(**rr_kwargs))
        RawResult.objects.insert(results)
Beispiel #9
0
    def load(self):
        # use first row as headers, not pre-canned list
        # need to use OCD_ID from jurisdiction in mapping
        self._common_kwargs = self._build_common_election_kwargs()
        self._common_kwargs['reporting_level'] = 'county'
        # Store result instances for bulk loading
        results = []

        with self._file_handle as csvfile:
            reader = unicodecsv.DictReader(csvfile, encoding='latin-1')
            for row in reader:
                if self._skip_row(row):
                    continue
                rr_kwargs = self._common_kwargs.copy()
                rr_kwargs.update(self._build_contest_kwargs(row))
                rr_kwargs.update(self._build_candidate_kwargs(row))
                jurisdiction = self.mapping['name']
                if row['party'] and row['party'] != ' ':
                    rr_kwargs['party'] = row['party'].strip()
                rr_kwargs.update({
                    'jurisdiction':
                    jurisdiction,
                    'ocd_id':
                    self.mapping['ocd_id'],
                    'votes':
                    int(row['votes'].replace(',', '').strip())
                })
                results.append(RawResult(**rr_kwargs))
        RawResult.objects.insert(results)
Beispiel #10
0
    def load(self):
        self._common_kwargs = self._build_common_election_kwargs()
        self._common_kwargs['reporting_level'] = 'precinct'
        # Store result instances for bulk loading
        results = []

        with self._file_handle as csvfile:
            reader = unicodecsv.DictReader(csvfile)
            next(reader, None)
            for row in reader:
                if self._skip_row(row):
                    continue
                if row['votes'] == 'X':
                    continue
                rr_kwargs = self._common_kwargs.copy()
                rr_kwargs['primary_party'] = row['party'].strip()
                rr_kwargs.update(self._build_contest_kwargs(row))
                rr_kwargs.update(self._build_candidate_kwargs(row))
                jurisdiction = row['precinct'].strip()
                county_ocd_id = [c for c in self.datasource._jurisdictions() if c['county'].strip().upper() == row['county'].strip().upper()][0]['ocd_id']
                rr_kwargs.update({
                    'party': row['party'].strip(),
                    'jurisdiction': jurisdiction,
                    'parent_jurisdiction': row['county'],
                    'ocd_id': "{}/precinct:{}".format(county_ocd_id, ocd_type_id(jurisdiction)),
                    'office': row['office'].strip(),
                    'district': row['district'].strip(),
                    'votes': int(float(row['votes']))
                })
                results.append(RawResult(**rr_kwargs))
        RawResult.objects.insert(results)
Beispiel #11
0
    def load(self):
        headers = [
            'office', 'district', 'jurisdiction', 'family_name',
            'additional_name', 'given_name', 'party', 'winner', 'vote_type',
            'votes', 'fill2'
        ]
        self._common_kwargs = self._build_common_election_kwargs()
        self._common_kwargs['reporting_level'] = 'county'
        # Store result instances for bulk loading
        results = []

        with self._file_handle as csvfile:
            reader = unicodecsv.DictReader(csvfile,
                                           fieldnames=headers,
                                           delimiter='|',
                                           encoding='latin-1')
            for row in reader:
                if self._skip_row(row):
                    continue

                rr_kwargs = self._common_kwargs.copy()
                if rr_kwargs['primary_type'] == 'closed':
                    rr_kwargs['primary_party'] = row['party'].strip()
                rr_kwargs.update(self._build_contest_kwargs(row))
                rr_kwargs.update(self._build_candidate_kwargs(row))
                rr_kwargs.update({
                    'party': row['party'].strip(),
                    'jurisdiction': row['jurisdiction'].strip(),
                    'office': row['office'].strip(),
                    'district': row['district'].strip(),
                    'votes': int(row['votes'].strip()),
                })
                results.append(RawResult(**rr_kwargs))
        RawResult.objects.insert(results)
Beispiel #12
0
    def load(self):
        # use first row as headers, not pre-canned list
        # need to use OCD_ID from jurisdiction in mapping
        self._common_kwargs = self._build_common_election_kwargs()
        self._common_kwargs['reporting_level'] = 'precinct'
        # Store result instances for bulk loading
        results = []

        with self._file_handle as csvfile:
            reader = unicodecsv.DictReader(csvfile, encoding='latin-1', fieldnames=("Jurisdiction", "Precinct", "office", "candidate", "Votes"))
            next(reader, None)
            next(reader, None)
            next(reader, None)
            next(reader, None)
            for row in reader:
                if self._skip_row(row):
                    continue
                rr_kwargs = self._common_kwargs.copy()
                rr_kwargs.update(self._build_contest_kwargs(row))
                rr_kwargs.update(self._build_candidate_kwargs(row))
                ocd_id = [c for c in self.datasource._jurisdictions() if c['jurisdiction'] == row['Jurisdiction']][0]['ocd_id']
                jurisdiction = row['Jurisdiction'].strip()
                if row['Votes'].strip() == '*':
                    votes = 'N/A'
                else:
                    votes = int(row['Votes'].replace(',','').strip())
                rr_kwargs.update({
                    'jurisdiction': jurisdiction,
                    'ocd_id': "{}/precinct:{}".format(self.mapping['ocd_id'], ocd_type_id(row['Precinct'])),
                    'votes': votes
                })
                results.append(RawResult(**rr_kwargs))
        RawResult.objects.insert(results)
Beispiel #13
0
 def _prep_precinct_result(self, row):
     kwargs = self._base_kwargs(row)
     kwargs.update(self._build_contest_kwargs(row))
     kwargs.update(self._build_candidate_kwargs(row))
     precinct = str(row['precinct'])
     county_ocd_id = [
         c for c in self.datasource._jurisdictions()
         if c['county'].upper() == row['county'].upper()
     ][0]['ocd_id']
     kwargs.update({
         'reporting_level':
         'precinct',
         'jurisdiction':
         precinct,
         'parent_jurisdiction':
         row['county'],
         'ocd_id':
         "{}/precinct:{}".format(county_ocd_id, ocd_type_id(precinct)),
         'party':
         row['party'].strip(),
         'votes':
         self._votes(row['total_votes']),
         'vote_breakdowns':
         self._breakdowns(row, kwargs)
     })
     return RawResult(**kwargs)
Beispiel #14
0
 def _prep_precinct_result(self, row):
     kwargs = self._base_kwargs(row)
     vote_breakdowns = {
         'election_night_total': self._votes(row['Election Night Votes'])
     }
     precinct = "%s-%s" % (row['Election District'],
                           row['Election Precinct'].strip())
     kwargs.update({
         'reporting_level':
         'precinct',
         'jurisdiction':
         precinct,
         'ocd_id':
         "{}/precinct:{}".format(self._get_state_ocd_id(),
                                 ocd_type_id(precinct)),
         'party':
         row['Party'].strip(),
         'votes':
         self._votes(row['Election Night Votes']),
         'winner':
         row['Winner'],
         'write_in':
         self._writein(row),
         'vote_breakdowns':
         vote_breakdowns,
     })
     return RawResult(**kwargs)
 def _prep_precinct_result(self, row, office, district, primary_party,
                           candidate, county, votes):
     kwargs = self._base_kwargs(row, office, district, candidate)
     county_ocd_id = [
         c for c in self.datasource._jurisdictions()
         if c['county'].upper() == county.upper()
     ][0]['ocd_id']
     precinct = str(row[0]).strip()
     if precinct.upper() == 'TOTALS':
         jurisdiction = None
         ocd_id = county_ocd_id
     else:
         jurisdiction = precinct
         ocd_id = "{}/precinct:{}".format(county_ocd_id,
                                          ocd_type_id(precinct))
     kwargs.update({
         'reporting_level': 'precinct',
         'jurisdiction': jurisdiction,
         'parent_jurisdiction': county,
         'ocd_id': ocd_id,
         'primary_party': primary_party,
         'votes': self._votes(votes)
     })
     if primary_party:
         kwargs.update({'party': primary_party})
     return RawResult(**kwargs)
Beispiel #16
0
    def load(self):
        headers = [
            'candidate',
            'office',
            'district',
            'party',
            'county',
            'votes',
            'winner'
        ]
        self._common_kwargs = self._build_common_election_kwargs()
        self._common_kwargs['reporting_level'] = 'county'
        # Store result instances for bulk loading
        results = []

        with self._file_handle as csvfile:
            reader = unicodecsv.DictReader(csvfile, fieldnames=headers)
            for row in reader:
                if row['votes'] == 'votes':
                    continue
                if row['county'].strip() == '':
                    total_votes = int(row['votes'].strip())
                else:
                    rr_kwargs = self._common_kwargs.copy()
                    rr_kwargs.update(self._build_contest_kwargs(row))
                    rr_kwargs.update(self._build_candidate_kwargs(row))
                    rr_kwargs.update({
                        'party': row['party'].strip(),
                        'jurisdiction': row['county'].strip(),
                        'votes': int(row['votes'].strip()),
                        'winner': row['winner'].strip(),
                        'ocd_id': self.mapping['ocd_id'],
                    })
                    results.append(RawResult(**rr_kwargs))
        RawResult.objects.insert(results)
Beispiel #17
0
    def load(self):
        headers = [
            'CandidateUid',
            'FirstName',
            'MiddleName',
            'LastName',
            'Suffix',
            'TOTAL_VOTES',
            'Party',
            'WriteInVote',
            'LocalityUid',
            'LocalityCode',
            'LocalityName',
            'PrecinctUid',
            'PrecinctName',
            'DistrictUid',
            'DistrictType',
            'DistrictName',
            'OfficeUid',
            'OfficeTitle',
            'ElectionUid',
            'ElectionType',
            'ElectionDate',
            'ElectionName'
        ]

        self._common_kwargs = self._build_common_election_kwargs()
        self._common_kwargs['reporting_level'] = 'precinct'
        # Store result instances for bulk loading
        results = BulkInsertBuffer(RawResult)

        with self._file_handle as csvfile:
            reader = unicodecsv.DictReader(csvfile, fieldnames = headers, encoding='latin-1')
            for row in reader:
                if self._skip_row(row):
                    continue
                rr_kwargs = self._common_kwargs.copy()
                if 'primary' in self.mapping['election']:
                    rr_kwargs['primary_party'] = row['Party'].strip()
                rr_kwargs.update(self._build_contest_kwargs(row))
                rr_kwargs.update(self._build_candidate_kwargs(row))
                rr_kwargs.update(self._build_write_in_kwargs(row))
                rr_kwargs.update(self._build_total_votes(row))
                parent_jurisdiction = [c for c in self.datasource._jurisdictions() if int(c['fips']) == int(row['LocalityCode'])][0]
                if row['PrecinctUid'].strip() == '':
                    ocd_id = parent_jurisdiction['ocd_id']
                else:
                    ocd_id = "{}/precinct:{}".format(parent_jurisdiction['ocd_id'], ocd_type_id(str(row['PrecinctName'])))
                rr_kwargs.update({
                    'party': row['Party'].strip(),
                    'jurisdiction': str(row['PrecinctName']),
                    'parent_jurisdiction': parent_jurisdiction['name'],
                    'ocd_id': ocd_id
                })
                results.append(RawResult(**rr_kwargs))

        results.flush()
Beispiel #18
0
 def _prep_county_result(self, row, office, district, candidate, county, votes):
     kwargs = self._base_kwargs(row, office, district, candidate)
     county_ocd_id = [c for c in self.datasource._jurisdictions() if c['county'].upper() == county.upper()][0]['ocd_id']
     kwargs.update({
         'reporting_level': 'county',
         'jurisdiction': county,
         'ocd_id': county_ocd_id,
         'party': candidate[1],
         'votes': self._votes(votes)
     })
     return RawResult(**kwargs)
Beispiel #19
0
 def _prep_county_result(self, row):
     kwargs = self._base_kwargs(row)
     county_ocd_id = [c for c in self.datasource._jurisdictions() if c['county'].upper() == row['county'].upper()][0]['ocd_id']
     kwargs.update({
         'reporting_level': 'county',
         'jurisdiction': row['county'],
         'ocd_id': county_ocd_id,
         'party': row['party'].strip(),
         'votes': self._votes(row['total_votes'])
     })
     return RawResult(**kwargs)
Beispiel #20
0
 def _prep_result(self, row):
     """
     Creates a RawResult model instance for a row of data.
     """
     # Copy fields that are common to this source file
     result_kwargs = self._common_kwargs.copy()
     # Extract remaining fields from the row of data
     result_kwargs.update(self._build_contest_kwargs(row))
     result_kwargs.update(self._build_candidate_kwargs(row))
     result_kwargs.update(self._build_result_kwargs(row))
     return RawResult(**result_kwargs)
Beispiel #21
0
    def load(self):
        print((str(datetime.now()), "load begin"))
        results = []
        self._common_kwargs = self._build_common_election_kwargs()

        self._common_kwargs['reporting_level'] = 'precinct' if self.mapping['isPrecinct'] else ''
        with self._file_handle as csvfile:
            reader = unicodecsv.reader(csvfile, delimiter=',')
            readerData = list(reader)
            candListRow = readerData[0]
            if not self._isValidHeaderRow(candListRow):
                print(("Error: Header not valid: ", candListRow))
                return []
            partyAffil = readerData[1]
            kwargs = self._build_common_election_kwargs()
            contest_kwargs = self._build_contest_kwargs(kwargs['primary_type'])
            for row in readerData[2:]:
                if self._skip_row(row):
                    continue
                cityLocation = row[0]
                wardLocation = row[1]
                precinctLocation = row[2]

                jurisdiction_kwargs = {}
                if self.mapping['isPrecinct']:
                    jurisdiction_kwargs = self._generatePrecinctJurisdiction(cityLocation, precinctLocation)
                else:
                    jurisdiction_kwargs = self._generateParrishJurisdiction(cityLocation)

                for colInd, res in enumerate(row):
                    if colInd < 3:
                        continue
                    if candListRow[colInd] == "Total Votes Cast":
                        continue
                    if candListRow[colInd] == "No Nomination":
                        continue
                    votes_kwargs = {'votes' : int(res.replace(',', ''))}
                    candidate_kwargs = {}
                    if colInd < len(partyAffil):
                        candidate_kwargs = self._build_candidate_kwargs(candListRow[colInd], partyAffil[colInd])
                        # print (cityLocation, precinctLocation, candListRow[colInd], partyAffil[colInd], res, kwargs)
                    else:
                        candidate_kwargs = self._build_candidate_kwargs(candListRow[colInd], "")
                    curResult = {}
                    curResult.update(kwargs)
                    curResult.update(contest_kwargs)
                    curResult.update(jurisdiction_kwargs)
                    curResult.update(candidate_kwargs)
                    curResult.update(votes_kwargs)
                    results.append(RawResult(**curResult))

        # Store result instances for bulk loading
        if len(results) > 0:
            RawResult.objects.insert(results, { 'writeConcern': {'w':0, 'j':False}, 'ordered': False })
Beispiel #22
0
 def _prep_county_result(self, row):
     kwargs = self._base_kwargs(row)
     county_ocd_id = [c for c in self.datasource._jurisdictions() if c['county'] == row['CountyName']][0]['ocd_id']
     kwargs.update({
         'reporting_level': 'county',
         'jurisdiction': row['CountyName'],
         'ocd_id': county_ocd_id,
         'party': row['PartyName'].strip(),
         'votes': self._votes(row['Votes']),
         'vote_breakdowns': {},
     })
     return RawResult(**kwargs)
Beispiel #23
0
    def load(self):

        self._common_kwargs = self._build_common_election_kwargs()
        self._common_kwargs['reporting_level'] = 'county'
        results = []

        with self._file_handle as csvfile:
            district_flag = 0
            reader = unicodecsv.DictReader(csvfile,
                                           encoding='latin-1',
                                           delimiter=',')
            self.header = [x.replace('"', '') for x in reader.fieldnames]
            self.contest_index = normalize_contest(self.header)
            for row in reader:
                if self._skip_row(row):
                    continue
                else:
                    rr_kwargs = self._common_kwargs.copy()
                    rr_kwargs['primary_party'] = row['Party'].strip()
                    rr_kwargs.update(self._build_contest_kwargs(row))
                    rr_kwargs.update(self._build_candidate_kwargs(row))
                    rr_kwargs.update({
                        'party':
                        row['Party'].strip(),
                        'votes':
                        int(row['Votes'].strip()),
                        'ocd_id':
                        "{}".format(self._get_ocd_id(
                            rr_kwargs['jurisdiction'])),
                    })
                    try:
                        rr_kwargs.update({
                            'district':
                            normalize_district(self.header,
                                               row[self.contest_index], row)
                        })
                    except KeyError:
                        district_flag = 1
                    results.append(RawResult(**rr_kwargs))
            if 0 is not district_flag:
                logger.info('Some rows did not contain district info.')
        """
        Many county files *only* have local races, such as schoolboard or
        fire chief races. Since openstates does not want these results,
        the entire files end up being skipped. To clarify the error message,
        we print our own if RawResult tries to insert nothing into mongodb

        """

        try:
            RawResult.objects.insert(results)
        except errors.InvalidOperation:
            logger.error('\tNo raw results loaded')
Beispiel #24
0
 def _prep_precinct_result(self, row, office, district, candidate, county, votes):
     kwargs = self._base_kwargs(row, office, district, candidate)
     precinct = str(row[1]).strip()
     county_ocd_id = [c for c in self.datasource._jurisdictions() if c['county'].upper() == county.upper()][0]['ocd_id']
     kwargs.update({
         'reporting_level': 'precinct',
         'jurisdiction': precinct,
         'parent_jurisdiction': county,
         'ocd_id': "{}/precinct:{}".format(county_ocd_id, ocd_type_id(precinct)),
         'party': candidate[1],
         'votes': self._votes(votes)
     })
     return RawResult(**kwargs)
Beispiel #25
0
 def _prep_county_result(self, row):
     kwargs = self._base_kwargs(row)
     kwargs.update(self._build_contest_kwargs(row))
     kwargs.update(self._build_candidate_kwargs(row))
     county_ocd_id = [c for c in self.datasource._jurisdictions() if c['county'].upper() == row['County'].upper()][0]['ocd_id']
     kwargs.update({
         'reporting_level': 'county',
         'jurisdiction': row['County'],
         'ocd_id': county_ocd_id,
         'party': row['Choice Party'].strip(),
         'votes_type': row['Precinct'],
         'votes': self._votes(row['Total Votes'])
     })
     return RawResult(**kwargs)
Beispiel #26
0
    def load(self):
        headers = [
            'year', 'election', 'office', 'party', 'district', 'candidate',
            'county', 'votes', 'winner'
        ]
        self._common_kwargs = self._build_common_election_kwargs()
        self._common_kwargs['reporting_level'] = 'county'
        # Store result instances for bulk loading
        results = []

        with self._file_handle as csvfile:
            reader = unicodecsv.DictReader(csvfile,
                                           fieldnames=headers,
                                           encoding='latin-1')
            for row in reader:
                if self._skip_row(row):
                    continue
                if row['county'].strip() == 'Totals':
                    total_votes = int(row['votes'].strip())
                    contest_winner = row['winner'].strip()
                else:
                    rr_kwargs = self._common_kwargs.copy()
                    rr_kwargs['primary_party'] = row['party'].strip()
                    rr_kwargs.update(self._build_contest_kwargs(row))
                    rr_kwargs.update(self._build_candidate_kwargs(row))
                    jurisdiction = row['county'].strip()
                    rr_kwargs.update({
                        'party':
                        row['party'].strip(),
                        'jurisdiction':
                        jurisdiction,
                        'ocd_id':
                        "{}/county:{}".format(self.mapping['ocd_id'],
                                              ocd_type_id(jurisdiction)),
                        'office':
                        row['office'].strip(),
                        'district':
                        row['district'].strip(),
                        'votes':
                        int(row['votes'].strip()),
                        'winner':
                        row['winner'].strip(),
                        'total_votes':
                        total_votes,
                        'contest_winner':
                        contest_winner
                    })
                    results.append(RawResult(**rr_kwargs))
        RawResult.objects.insert(results)
Beispiel #27
0
 def _prep_precinct_result(self, precinct, county, candidate, office, party, votes):
     # each precinct has multiple candidate totals, plus write-ins, over and under votes
     kwargs = self._base_kwargs(candidate, office, party)
     if party:
         kwargs.update({'primary_party': party})
     kwargs.update({
         'reporting_level': 'precinct',
         'jurisdiction': precinct,
         'parent_jurisdiction': county,
         'ocd_id': "{}/precinct:{}".format(self.mapping['ocd_id'],
             ocd_type_id(precinct)),
         'votes': votes,
         'vote_breakdowns': {},
     })
     return RawResult(**kwargs)
Beispiel #28
0
 def _prep_county_result(self, row):
     kwargs = self._base_kwargs(row)
     county_ocd_id = [c for c in self.datasource._jurisdictions() if c['county'].upper() == row['county'].upper()][0]['ocd_id']
     if row['precinct'] == 'absentee/provisional':
         votes_type = 'absentee_provisional'
     else:
         votes_type = None
     kwargs.update({
         'reporting_level': 'county',
         'jurisdiction': row['county'],
         'ocd_id': county_ocd_id,
         'party': row['party'].strip(),
         'votes': self._votes(row['total_votes']),
         'votes_type': votes_type,
     })
     return RawResult(**kwargs)
Beispiel #29
0
 def _prep_precinct_result(self, row):
     kwargs = self._base_kwargs(row)
     precinct = "%s-%s" % (row['Election District'], row['Election Precinct'].strip())
     ocd_id = "{}/precinct:{}".format(self.mapping['ocd_id'],
         ocd_type_id(precinct))
     kwargs.update({
         'reporting_level': 'precinct',
         'jurisdiction': precinct,
         'ocd_id': ocd_id,
         'party': row['Party'].strip(),
         'votes': self._votes(row['Election Night Votes']),
         'votes_type': 'election_day',
         'winner': row['Winner'],
         'write_in': self._writein(row),
     })
     return RawResult(**kwargs)
Beispiel #30
0
    def load(self):
        self._common_kwargs = self._build_common_election_kwargs()
        self._common_kwargs['reporting_level'] = 'precinct'

        # Store result instances for bulk loading
        results = []
        num_skipped = 0
        with self._file_handle as csvfile:
            reader = unicodecsv.DictReader(csvfile)
            for row in reader:
                if self._skip_row(row):
                    num_skipped += 1
                    continue

                rr_kwargs = self._common_kwargs.copy()
                rr_kwargs.update(self._build_contest_kwargs(row))
                rr_kwargs.update(self._build_candidate_kwargs(row))
                # The 'votes' column gets screwed up a lot, so handle it
                # by additionally printing debug information.
                try:
                    rr_kwargs.update({'votes': int(row['votes'])})
                except ValueError as e:
                    print('Bad votes in row {}'.format(row))
                    raise e

                county = row['county'].strip()
                county_ocd_id = self._get_county_ocd_id(county)
                precinct = row['precinct'].strip()
                if precinct:
                  precinct_ocd_id = "{}/precinct:{}".format(
                      county_ocd_id, ocd_type_id(precinct)),
                  rr_kwargs.update({
                      'ocd_id': precinct_ocd_id,
                      'jurisdiction': precinct,
                      'parent_jurisdiction': county,
                  })
                else:
                  rr_kwargs.update({
                      'ocd_id': county_ocd_id,
                      'jurisdiction': county,
                      'parent_jurisdiction': 'ocd-division/country:us/state:in',
                  })
                results.append(RawResult(**rr_kwargs))

        print('\tInserting {} results (skipped {} rows)'.format(len(results),
                                                                num_skipped))
        RawResult.objects.insert(results)