def process_contest_referendum_from_structured_json( one_contest_referendum_structured_json, google_civic_election_id, ocd_division_id, local_ballot_order, state_code, voter_id, polling_location_we_vote_id): """ "referendumTitle": "Proposition 45", "referendumSubtitle": "Healthcare Insurance. Rate Changes. Initiative Statute.", "referendumUrl": "http://vig.cdn.sos.ca.gov/2014/general/en/pdf/proposition-45-title-summary-analysis.pdf", "district" <= this is an array """ referendum_title = one_contest_referendum_structured_json['referendumTitle'] if \ 'referendumTitle' in one_contest_referendum_structured_json else '' referendum_subtitle = one_contest_referendum_structured_json['referendumSubtitle'] if \ 'referendumSubtitle' in one_contest_referendum_structured_json else '' referendum_url = one_contest_referendum_structured_json['referendumUrl'] if \ 'referendumUrl' in one_contest_referendum_structured_json else '' referendum_text = one_contest_referendum_structured_json['referendumText'] if \ 'referendumText' in one_contest_referendum_structured_json else '' # These following fields exist for both candidates and referendum results = process_contest_common_fields_from_structured_json(one_contest_referendum_structured_json) google_ballot_placement = results['ballot_placement'] # A number specifying the position of this contest # on the voter's ballot. primary_party = results['primary_party'] # If this is a partisan election, the name of the party it is for. district_name = results['district_name'] # The name of the district. district_scope = results['district_scope'] # The geographic scope of this district. If unspecified the # district's geography is not known. One of: national, statewide, congressional, stateUpper, stateLower, # countywide, judicial, schoolBoard, cityWide, township, countyCouncil, cityCouncil, ward, special district_id = results['district_id'] # Note that all of the information saved here is independent of a particular voter we_vote_id = '' if google_civic_election_id and (district_id or district_name) and referendum_title: update_contest_measure_values = { # Values we search against 'google_civic_election_id': google_civic_election_id, 'state_code': state_code.lower(), # Not required for cases of federal offices 'district_id': district_id, 'district_name': district_name, 'measure_title': referendum_title, # The rest of the values 'measure_subtitle': referendum_subtitle, 'measure_url': referendum_url, 'measure_text': referendum_text, 'ocd_division_id': ocd_division_id, 'primary_party': primary_party, 'district_scope': district_scope, } contest_measure_manager = ContestMeasureManager() update_or_create_contest_measure_results = contest_measure_manager.update_or_create_contest_measure( we_vote_id, google_civic_election_id, district_id, district_name, referendum_title, state_code, update_contest_measure_values) # , create_contest_measure_values else: update_or_create_contest_measure_results = { 'success': False, 'saved': 0, 'updated': 0, 'not_processed': 1, } if update_or_create_contest_measure_results['success']: contest_measure = update_or_create_contest_measure_results['contest_measure'] contest_measure_id = contest_measure.id contest_measure_we_vote_id = contest_measure.we_vote_id ballot_item_display_name = contest_measure.measure_title measure_subtitle = contest_measure.measure_subtitle ballot_item_manager = BallotItemManager() # If a voter_id was passed in, save an entry for this office for the voter's ballot if positive_value_exists(voter_id) and positive_value_exists(google_civic_election_id) \ and positive_value_exists(contest_measure_id): contest_office_id = 0 contest_office_we_vote_id = '' ballot_item_manager.update_or_create_ballot_item_for_voter( voter_id, google_civic_election_id, google_ballot_placement, ballot_item_display_name, measure_subtitle, local_ballot_order, contest_office_id, contest_office_we_vote_id, contest_measure_id, contest_measure_we_vote_id) if positive_value_exists(polling_location_we_vote_id) and positive_value_exists(google_civic_election_id) \ and positive_value_exists(contest_measure_id): contest_office_id = 0 contest_office_we_vote_id = '' ballot_item_manager.update_or_create_ballot_item_for_polling_location( polling_location_we_vote_id, google_civic_election_id, google_ballot_placement, ballot_item_display_name, measure_subtitle, local_ballot_order, contest_office_id, contest_office_we_vote_id, contest_measure_id, contest_measure_we_vote_id) return update_or_create_contest_measure_results
def process_contest_referendum_from_structured_json( one_contest_referendum_structured_json, google_civic_election_id, ocd_division_id, local_ballot_order, state_code, voter_id, polling_location_we_vote_id): """ "referendumTitle": "Proposition 45", "referendumSubtitle": "Healthcare Insurance. Rate Changes. Initiative Statute.", "referendumUrl": "http://vig.cdn.sos.ca.gov/2014/general/en/pdf/proposition-45-title-summary-analysis.pdf", "district" <= this is an array """ referendum_title = one_contest_referendum_structured_json['referendumTitle'] if \ 'referendumTitle' in one_contest_referendum_structured_json else '' referendum_subtitle = one_contest_referendum_structured_json['referendumSubtitle'] if \ 'referendumSubtitle' in one_contest_referendum_structured_json else '' referendum_url = one_contest_referendum_structured_json['referendumUrl'] if \ 'referendumUrl' in one_contest_referendum_structured_json else '' referendum_text = one_contest_referendum_structured_json['referendumText'] if \ 'referendumText' in one_contest_referendum_structured_json else '' # These following fields exist for both candidates and referendum results = process_contest_common_fields_from_structured_json( one_contest_referendum_structured_json) google_ballot_placement = results[ 'ballot_placement'] # A number specifying the position of this contest # on the voter's ballot. primary_party = results[ 'primary_party'] # If this is a partisan election, the name of the party it is for. district_name = results['district_name'] # The name of the district. district_scope = results[ 'district_scope'] # The geographic scope of this district. If unspecified the # district's geography is not known. One of: national, statewide, congressional, stateUpper, stateLower, # countywide, judicial, schoolBoard, cityWide, township, countyCouncil, cityCouncil, ward, special district_id = results['district_id'] # Note that all of the information saved here is independent of a particular voter we_vote_id = '' if google_civic_election_id and (district_id or district_name) and referendum_title: update_contest_measure_values = { # Values we search against 'google_civic_election_id': google_civic_election_id, 'state_code': state_code.lower(), # Not required for cases of federal offices 'district_id': district_id, 'district_name': district_name, 'measure_title': referendum_title, # The rest of the values 'measure_subtitle': referendum_subtitle, 'measure_url': referendum_url, 'measure_text': referendum_text, 'ocd_division_id': ocd_division_id, 'primary_party': primary_party, 'district_scope': district_scope, } contest_measure_manager = ContestMeasureManager() update_or_create_contest_measure_results = contest_measure_manager.update_or_create_contest_measure( we_vote_id, google_civic_election_id, district_id, district_name, referendum_title, state_code, update_contest_measure_values) # , create_contest_measure_values else: update_or_create_contest_measure_results = { 'success': False, 'saved': 0, 'updated': 0, 'not_processed': 1, } if update_or_create_contest_measure_results['success']: contest_measure = update_or_create_contest_measure_results[ 'contest_measure'] contest_measure_id = contest_measure.id contest_measure_we_vote_id = contest_measure.we_vote_id ballot_item_display_name = contest_measure.measure_title measure_subtitle = contest_measure.measure_subtitle ballot_item_manager = BallotItemManager() # If a voter_id was passed in, save an entry for this office for the voter's ballot if positive_value_exists(voter_id) and positive_value_exists(google_civic_election_id) \ and positive_value_exists(contest_measure_id): contest_office_id = 0 contest_office_we_vote_id = '' ballot_item_manager.update_or_create_ballot_item_for_voter( voter_id, google_civic_election_id, google_ballot_placement, ballot_item_display_name, measure_subtitle, local_ballot_order, contest_office_id, contest_office_we_vote_id, contest_measure_id, contest_measure_we_vote_id) if positive_value_exists(polling_location_we_vote_id) and positive_value_exists(google_civic_election_id) \ and positive_value_exists(contest_measure_id): contest_office_id = 0 contest_office_we_vote_id = '' ballot_item_manager.update_or_create_ballot_item_for_polling_location( polling_location_we_vote_id, google_civic_election_id, google_ballot_placement, ballot_item_display_name, measure_subtitle, local_ballot_order, contest_office_id, contest_office_we_vote_id, contest_measure_id, contest_measure_we_vote_id) return update_or_create_contest_measure_results
def process_contest_office_from_structured_json( one_contest_office_structured_json, google_civic_election_id, ocd_division_id, local_ballot_order, state_code, voter_id, polling_location_we_vote_id): logger.debug("General contest_type") # Protect against the case where this is NOT an office if 'candidates' not in one_contest_office_structured_json: is_not_office = True else: is_not_office = False if is_not_office: update_or_create_contest_office_results = { 'success': False, 'saved': 0, 'updated': 0, 'not_processed': 1, } return update_or_create_contest_office_results office_name = one_contest_office_structured_json['office'] # The number of candidates that a voter may vote for in this contest. if 'numberVotingFor' in one_contest_office_structured_json: number_voting_for = one_contest_office_structured_json['numberVotingFor'] else: number_voting_for = 1 # The number of candidates that will be elected to office in this contest. if 'numberElected' in one_contest_office_structured_json: number_elected = one_contest_office_structured_json['numberElected'] else: number_elected = 1 # These are several fields that are shared in common between offices and measures results = process_contest_common_fields_from_structured_json(one_contest_office_structured_json) # ballot_placement: A number specifying the position of this contest on the voter's ballot. google_ballot_placement = results['ballot_placement'] primary_party = results['primary_party'] # If this is a partisan election, the name of the party it is for. # district_scope: The geographic scope of this district. If unspecified the # district's geography is not known. One of: national, statewide, congressional, stateUpper, stateLower, # countywide, judicial, schoolBoard, cityWide, township, countyCouncil, cityCouncil, ward, special district_scope = results['district_scope'] district_id = results['district_id'] district_name = results['district_name'] # The name of the district. # electorate_specifications: A description of any additional eligibility requirements for voting in this contest. electorate_specifications = results['electorate_specifications'] # special: "Yes" or "No" depending on whether this a contest being held outside the normal election cycle. special = results['special'] # We want to convert this from an array to three fields for the same table # levels: string, A list of office levels to filter by. Only offices that serve at least one of these levels # will be returned. Divisions that don't contain a matching office will not be returned. (repeated) # Allowed values # administrativeArea1 - # administrativeArea2 - # country - # international - # locality - # regional - # special - # subLocality1 - # subLocality2 - # The levels of government of the office for this contest. There may be more than one in cases where a # jurisdiction effectively acts at two different levels of government; for example, the mayor of the # District of Columbia acts at "locality" level, but also effectively at both "administrative-area-2" # and "administrative-area-1". level_structured_json = \ one_contest_office_structured_json['level'] if 'level' in one_contest_office_structured_json else '' contest_level = [] for one_level in level_structured_json: contest_level.append(one_level) if 0 in contest_level: contest_level0 = contest_level[0] else: contest_level0 = '' if 1 in contest_level: contest_level1 = contest_level[1] else: contest_level1 = '' if 2 in contest_level: contest_level2 = contest_level[2] else: contest_level2 = '' # roles: string, A list of office roles to filter by. Only offices fulfilling one of these roles will be returned. # Divisions that don't contain a matching office will not be returned. (repeated) # Allowed values # deputyHeadOfGovernment - # executiveCouncil - # governmentOfficer - # headOfGovernment - # headOfState - # highestCourtJudge - # judge - # legislatorLowerBody - # legislatorUpperBody - # schoolBoard - # specialPurposeOfficer - # roles_structured_json = \ # one_contest_office_structured_json['roles'] if 'roles' in one_contest_office_structured_json else '' # for one_role in roles_structured_json: # Figure out how we are going to use level info candidates_structured_json = \ one_contest_office_structured_json['candidates'] if 'candidates' in one_contest_office_structured_json else '' we_vote_id = '' # Note that all of the information saved here is independent of a particular voter if google_civic_election_id and (district_id or district_name) and office_name: updated_contest_office_values = { # Values we search against 'google_civic_election_id': google_civic_election_id, 'state_code': state_code.lower(), # Not required for cases of federal offices 'district_id': district_id, 'district_name': district_name, 'office_name': office_name, # The rest of the values 'ocd_division_id': ocd_division_id, 'number_voting_for': number_voting_for, 'number_elected': number_elected, 'contest_level0': contest_level0, 'contest_level1': contest_level1, 'contest_level2': contest_level2, 'primary_party': primary_party, 'district_scope': district_scope, 'electorate_specifications': electorate_specifications, 'special': special, } contest_office_manager = ContestOfficeManager() update_or_create_contest_office_results = contest_office_manager.update_or_create_contest_office( we_vote_id, google_civic_election_id, district_id, district_name, office_name, state_code, updated_contest_office_values) else: update_or_create_contest_office_results = { 'success': False, 'saved': 0, 'updated': 0, 'not_processed': 1, } if update_or_create_contest_office_results['success']: contest_office = update_or_create_contest_office_results['contest_office'] contest_office_id = contest_office.id contest_office_we_vote_id = contest_office.we_vote_id ballot_item_display_name = contest_office.office_name else: contest_office_id = 0 contest_office_we_vote_id = '' ballot_item_display_name = '' # If a voter_id was passed in, save an entry for this office for the voter's ballot if positive_value_exists(voter_id) and positive_value_exists(google_civic_election_id) \ and positive_value_exists(contest_office_id): ballot_item_manager = BallotItemManager() measure_subtitle = "" ballot_item_manager.update_or_create_ballot_item_for_voter( voter_id, google_civic_election_id, google_ballot_placement, ballot_item_display_name, measure_subtitle, local_ballot_order, contest_office_id, contest_office_we_vote_id) # We leave off these and rely on default empty values: contest_measure_id, contest_measure_we_vote_id # If this is a polling location, we want to save the ballot information for it so we can use it as reference # for nearby voters (when we don't have their full address) if positive_value_exists(polling_location_we_vote_id) and positive_value_exists(google_civic_election_id) \ and positive_value_exists(contest_office_id): ballot_item_manager = BallotItemManager() measure_subtitle = "" ballot_item_manager.update_or_create_ballot_item_for_polling_location( polling_location_we_vote_id, google_civic_election_id, google_ballot_placement, ballot_item_display_name, measure_subtitle, local_ballot_order, contest_office_id, contest_office_we_vote_id) # We leave off these and rely on default empty values: contest_measure_id, contest_measure_we_vote_id # Note: We do not need to connect the candidates with the voter here for a ballot item process_candidates_from_structured_json( candidates_structured_json, google_civic_election_id, ocd_division_id, state_code, contest_office_id, contest_office_we_vote_id) return update_or_create_contest_office_results
def process_contest_office_from_structured_json( one_contest_office_structured_json, google_civic_election_id, ocd_division_id, local_ballot_order, state_code, voter_id, polling_location_we_vote_id): logger.debug("General contest_type") # Protect against the case where this is NOT an office if 'candidates' not in one_contest_office_structured_json: is_not_office = True else: is_not_office = False if is_not_office: update_or_create_contest_office_results = { 'success': False, 'saved': 0, 'updated': 0, 'not_processed': 1, } return update_or_create_contest_office_results office_name = one_contest_office_structured_json['office'] # The number of candidates that a voter may vote for in this contest. if 'numberVotingFor' in one_contest_office_structured_json: number_voting_for = one_contest_office_structured_json[ 'numberVotingFor'] else: number_voting_for = 1 # The number of candidates that will be elected to office in this contest. if 'numberElected' in one_contest_office_structured_json: number_elected = one_contest_office_structured_json['numberElected'] else: number_elected = 1 # These are several fields that are shared in common between offices and measures results = process_contest_common_fields_from_structured_json( one_contest_office_structured_json) # ballot_placement: A number specifying the position of this contest on the voter's ballot. google_ballot_placement = results['ballot_placement'] primary_party = results[ 'primary_party'] # If this is a partisan election, the name of the party it is for. # district_scope: The geographic scope of this district. If unspecified the # district's geography is not known. One of: national, statewide, congressional, stateUpper, stateLower, # countywide, judicial, schoolBoard, cityWide, township, countyCouncil, cityCouncil, ward, special district_scope = results['district_scope'] district_id = results['district_id'] district_name = results['district_name'] # The name of the district. # electorate_specifications: A description of any additional eligibility requirements for voting in this contest. electorate_specifications = results['electorate_specifications'] # special: "Yes" or "No" depending on whether this a contest being held outside the normal election cycle. special = results['special'] # We want to convert this from an array to three fields for the same table # levels: string, A list of office levels to filter by. Only offices that serve at least one of these levels # will be returned. Divisions that don't contain a matching office will not be returned. (repeated) # Allowed values # administrativeArea1 - # administrativeArea2 - # country - # international - # locality - # regional - # special - # subLocality1 - # subLocality2 - # The levels of government of the office for this contest. There may be more than one in cases where a # jurisdiction effectively acts at two different levels of government; for example, the mayor of the # District of Columbia acts at "locality" level, but also effectively at both "administrative-area-2" # and "administrative-area-1". level_structured_json = \ one_contest_office_structured_json['level'] if 'level' in one_contest_office_structured_json else '' contest_level = [] for one_level in level_structured_json: contest_level.append(one_level) if 0 in contest_level: contest_level0 = contest_level[0] else: contest_level0 = '' if 1 in contest_level: contest_level1 = contest_level[1] else: contest_level1 = '' if 2 in contest_level: contest_level2 = contest_level[2] else: contest_level2 = '' # roles: string, A list of office roles to filter by. Only offices fulfilling one of these roles will be returned. # Divisions that don't contain a matching office will not be returned. (repeated) # Allowed values # deputyHeadOfGovernment - # executiveCouncil - # governmentOfficer - # headOfGovernment - # headOfState - # highestCourtJudge - # judge - # legislatorLowerBody - # legislatorUpperBody - # schoolBoard - # specialPurposeOfficer - # roles_structured_json = \ # one_contest_office_structured_json['roles'] if 'roles' in one_contest_office_structured_json else '' # for one_role in roles_structured_json: # Figure out how we are going to use level info candidates_structured_json = \ one_contest_office_structured_json['candidates'] if 'candidates' in one_contest_office_structured_json else '' we_vote_id = '' # Note that all of the information saved here is independent of a particular voter if google_civic_election_id and (district_id or district_name) and office_name: updated_contest_office_values = { # Values we search against 'google_civic_election_id': google_civic_election_id, 'state_code': state_code.lower(), # Not required for cases of federal offices 'district_id': district_id, 'district_name': district_name, 'office_name': office_name, # The rest of the values 'ocd_division_id': ocd_division_id, 'number_voting_for': number_voting_for, 'number_elected': number_elected, 'contest_level0': contest_level0, 'contest_level1': contest_level1, 'contest_level2': contest_level2, 'primary_party': primary_party, 'district_scope': district_scope, 'electorate_specifications': electorate_specifications, 'special': special, } contest_office_manager = ContestOfficeManager() update_or_create_contest_office_results = contest_office_manager.update_or_create_contest_office( we_vote_id, google_civic_election_id, district_id, district_name, office_name, state_code, updated_contest_office_values) else: update_or_create_contest_office_results = { 'success': False, 'saved': 0, 'updated': 0, 'not_processed': 1, } if update_or_create_contest_office_results['success']: contest_office = update_or_create_contest_office_results[ 'contest_office'] contest_office_id = contest_office.id contest_office_we_vote_id = contest_office.we_vote_id ballot_item_display_name = contest_office.office_name else: contest_office_id = 0 contest_office_we_vote_id = '' ballot_item_display_name = '' # If a voter_id was passed in, save an entry for this office for the voter's ballot if positive_value_exists(voter_id) and positive_value_exists(google_civic_election_id) \ and positive_value_exists(contest_office_id): ballot_item_manager = BallotItemManager() measure_subtitle = "" ballot_item_manager.update_or_create_ballot_item_for_voter( voter_id, google_civic_election_id, google_ballot_placement, ballot_item_display_name, measure_subtitle, local_ballot_order, contest_office_id, contest_office_we_vote_id) # We leave off these and rely on default empty values: contest_measure_id, contest_measure_we_vote_id # If this is a polling location, we want to save the ballot information for it so we can use it as reference # for nearby voters (when we don't have their full address) if positive_value_exists(polling_location_we_vote_id) and positive_value_exists(google_civic_election_id) \ and positive_value_exists(contest_office_id): ballot_item_manager = BallotItemManager() measure_subtitle = "" ballot_item_manager.update_or_create_ballot_item_for_polling_location( polling_location_we_vote_id, google_civic_election_id, google_ballot_placement, ballot_item_display_name, measure_subtitle, local_ballot_order, contest_office_id, contest_office_we_vote_id) # We leave off these and rely on default empty values: contest_measure_id, contest_measure_we_vote_id # Note: We do not need to connect the candidates with the voter here for a ballot item process_candidates_from_structured_json(candidates_structured_json, google_civic_election_id, ocd_division_id, state_code, contest_office_id, contest_office_we_vote_id) return update_or_create_contest_office_results