from election.models import ElectionManager from exception.models import handle_exception from import_export_google_civic.controllers import voter_ballot_items_retrieve_from_google_civic_for_api import json from measure.models import ContestMeasureList from office.models import ContestOfficeListManager from polling_location.models import PollingLocationManager import requests from voter.models import BALLOT_ADDRESS, VoterAddressManager, \ VoterDeviceLinkManager import wevote_functions.admin from wevote_functions.functions import convert_to_int, positive_value_exists logger = wevote_functions.admin.get_logger(__name__) GOOGLE_CIVIC_API_KEY = get_environment_variable("GOOGLE_CIVIC_API_KEY") WE_VOTE_API_KEY = get_environment_variable("WE_VOTE_API_KEY") BALLOT_ITEMS_SYNC_URL = get_environment_variable("BALLOT_ITEMS_SYNC_URL") BALLOT_RETURNED_SYNC_URL = get_environment_variable("BALLOT_RETURNED_SYNC_URL") def ballot_items_import_from_master_server(request, google_civic_election_id): """ Get the json data, and either create new entries or update existing :return: """ # Request json file from We Vote servers messages.add_message(request, messages.INFO, "Loading Ballot Items from We Vote Master servers") logger.info("Loading Ballot Items from We Vote Master servers") request = requests.get(
from .models import QuickInfo, QuickInfoManager, QuickInfoMasterManager from ballot.models import OFFICE, CANDIDATE, POLITICIAN, MEASURE from candidate.models import CandidateCampaignManager from config.base import get_environment_variable from django.contrib import messages from django.http import HttpResponse from exception.models import handle_record_not_found_exception, handle_record_not_saved_exception from organization.models import OrganizationManager import json from voter.models import fetch_voter_id_from_voter_device_link, VoterManager import wevote_functions.admin from wevote_functions.functions import convert_to_int, is_voter_device_id_valid, positive_value_exists logger = wevote_functions.admin.get_logger(__name__) WE_VOTE_API_KEY = get_environment_variable("WE_VOTE_API_KEY") QUICK_INFO_URL = get_environment_variable("QUICK_INFO_URL") def quick_info_save_for_api( # TODO to be converted voter_device_id, quick_info_id, quick_info_we_vote_id, organization_we_vote_id, public_figure_we_vote_id, voter_we_vote_id, google_civic_election_id, ballot_item_display_name, office_we_vote_id, candidate_we_vote_id, measure_we_vote_id, stance, statement_text, statement_html, more_info_url): quick_info_id = convert_to_int(quick_info_id) quick_info_we_vote_id = quick_info_we_vote_id.strip().lower() existing_unique_identifier_found = positive_value_exists(quick_info_id) \ or positive_value_exists(quick_info_we_vote_id) new_unique_identifier_found = positive_value_exists(organization_we_vote_id) \
from config.base import get_environment_variable from django.contrib import messages from django.http import HttpResponse from exception.models import handle_record_not_found_exception, \ handle_record_not_saved_exception from follow.models import FollowOrganizationManager, FollowOrganizationList, FOLLOW_IGNORE, FOLLOWING, STOP_FOLLOWING import json from organization.models import Organization from voter.models import fetch_voter_id_from_voter_device_link from voter_guide.models import VoterGuide, VoterGuideManager import wevote_functions.admin from wevote_functions.functions import convert_to_int, positive_value_exists logger = wevote_functions.admin.get_logger(__name__) WE_VOTE_API_KEY = get_environment_variable("WE_VOTE_API_KEY") ORGANIZATIONS_URL = get_environment_variable("ORGANIZATIONS_URL") def organization_follow_all(voter_device_id, organization_id, organization_we_vote_id, follow_kind=FOLLOWING): if not positive_value_exists(voter_device_id): json_data = { 'status': 'VALID_VOTER_DEVICE_ID_MISSING', 'success': False, 'voter_device_id': voter_device_id, 'organization_id': organization_id, } return HttpResponse(json.dumps(json_data), content_type='application/json') voter_id = fetch_voter_id_from_voter_device_link(voter_device_id) if not positive_value_exists(voter_id):
from candidate.models import CandidateCampaignManager from config.base import get_environment_variable from django.contrib import messages from django.http import HttpResponse from exception.models import handle_record_not_found_exception, handle_record_not_saved_exception from follow.models import FollowOrganizationList from organization.models import OrganizationManager import json from voter.models import fetch_voter_id_from_voter_device_link, VoterManager from voter_guide.models import ORGANIZATION, PUBLIC_FIGURE, VOTER, UNKNOWN_VOTER_GUIDE import wevote_functions.admin from wevote_functions.functions import convert_to_int, is_voter_device_id_valid, positive_value_exists logger = wevote_functions.admin.get_logger(__name__) WE_VOTE_API_KEY = get_environment_variable("WE_VOTE_API_KEY") POSITIONS_URL = get_environment_variable("POSITIONS_URL") # We retrieve from only one of the two possible variables def position_retrieve_for_api(position_id, position_we_vote_id, voter_device_id): position_id = convert_to_int(position_id) position_we_vote_id = position_we_vote_id.strip() # TODO for certain positions (voter positions), we need to restrict the retrieve based on voter_device_id / voter_id if voter_device_id: pass we_vote_id = position_we_vote_id.strip() if not positive_value_exists(position_id) and not positive_value_exists(position_we_vote_id): json_data = {
# import_export_twitter/views.py # Brought to you by We Vote. Be good. # -*- coding: UTF-8 -*- # See also WeVoteServer/twitter/views.py for routines that manage internal twitter data from config.base import get_environment_variable from django.http import HttpResponseRedirect import tweepy from voter.models import VoterManager from wevote_functions.functions import positive_value_exists TWITTER_CONSUMER_KEY = get_environment_variable("TWITTER_CONSUMER_KEY") TWITTER_CONSUMER_SECRET = get_environment_variable("TWITTER_CONSUMER_SECRET") def process_sign_in_response_view(request): oauth_token = request.GET.get('oauth_token', '') oauth_verifier = request.GET.get('oauth_verifier', '') if not positive_value_exists(oauth_token) or not positive_value_exists(oauth_verifier): # Redirect back to ReactJS so we can display failure message return HttpResponseRedirect('http://localhost:3001/twitter/missing_variables') # TODO Convert to env variable voter_manager = VoterManager() # Look in the Voter table for a matching request_token, placed by the API endpoint twitterSignInStart results = voter_manager.retrieve_voter_by_twitter_request_token(oauth_token) if not results['voter_found']: # Redirect back to ReactJS so we can display failure message if the token wasn't found return HttpResponseRedirect('http://localhost:3001/twitter/token_missing') # TODO Convert to env variable
voter_ballot_items_retrieve_doc, voter_ballot_items_retrieve_from_google_civic_doc, voter_count_doc, \ voter_create_doc, voter_guide_possibility_retrieve_doc, voter_guide_possibility_save_doc, \ voter_guides_followed_retrieve_doc, \ voter_guides_to_follow_retrieve_doc, voter_location_retrieve_from_ip_doc, voter_photo_save_doc, \ voter_position_like_off_save_doc, voter_position_like_on_save_doc, voter_position_like_status_retrieve_doc, \ voter_position_comment_save_doc, voter_position_retrieve_doc, \ voter_opposing_save_doc, voter_retrieve_doc, voter_sign_out_doc, voter_star_off_save_doc, voter_star_on_save_doc, \ voter_star_status_retrieve_doc, voter_stop_opposing_save_doc, \ voter_stop_supporting_save_doc, voter_supporting_save_doc from config.base import get_environment_variable from django.contrib.messages import get_messages from django.shortcuts import render from voter.models import voter_setup from wevote_functions.functions import get_voter_api_device_id, set_voter_api_device_id, positive_value_exists WE_VOTE_SERVER_ROOT_URL = get_environment_variable("WE_VOTE_SERVER_ROOT_URL") def apis_index_doc_view(request): """ Show a list of available APIs """ # Create a voter_device_id and voter in the database if one doesn't exist yet results = voter_setup(request) voter_api_device_id = results['voter_api_device_id'] store_new_voter_api_device_id_in_cookie = results['store_new_voter_api_device_id_in_cookie'] messages_on_stage = get_messages(request) template_values = { 'next': next, 'messages_on_stage': messages_on_stage,
voter_ballot_items_retrieve_doc, voter_ballot_items_retrieve_from_google_civic_doc, voter_count_doc, \ voter_create_doc, voter_guide_possibility_retrieve_doc, voter_guide_possibility_save_doc, \ voter_guides_followed_retrieve_doc, voter_guides_sync_out_doc, voter_guides_to_follow_retrieve_doc, \ voter_location_retrieve_from_ip_doc, voter_photo_save_doc, \ voter_position_like_off_save_doc, voter_position_like_on_save_doc, voter_position_like_status_retrieve_doc, \ voter_position_comment_save_doc, voter_position_retrieve_doc, voter_position_visibility_save_doc, \ voter_opposing_save_doc, voter_retrieve_doc, voter_sign_out_doc, voter_star_off_save_doc, voter_star_on_save_doc, \ voter_star_status_retrieve_doc, voter_stop_opposing_save_doc, \ voter_stop_supporting_save_doc, voter_supporting_save_doc, voter_update_doc from config.base import get_environment_variable from django.contrib.messages import get_messages from django.shortcuts import render from voter.models import voter_setup from wevote_functions.functions import get_voter_api_device_id, set_voter_api_device_id, positive_value_exists WE_VOTE_SERVER_ROOT_URL = get_environment_variable("WE_VOTE_SERVER_ROOT_URL") def apis_index_doc_view(request): """ Show a list of available APIs """ # Create a voter_device_id and voter in the database if one doesn't exist yet results = voter_setup(request) voter_api_device_id = results['voter_api_device_id'] store_new_voter_api_device_id_in_cookie = results[ 'store_new_voter_api_device_id_in_cookie'] messages_on_stage = get_messages(request) template_values = { 'next': next,
# Brought to you by We Vote. Be good. # -*- coding: UTF-8 -*- from .models import PollingLocationListManager, PollingLocationManager from config.base import get_environment_variable from django.contrib import messages import glob import json import requests import wevote_functions.admin from wevote_functions.functions import positive_value_exists import xml.etree.ElementTree as MyElementTree logger = wevote_functions.admin.get_logger(__name__) WE_VOTE_API_KEY = get_environment_variable("WE_VOTE_API_KEY") POLLING_LOCATIONS_SYNC_URL = get_environment_variable( "POLLING_LOCATIONS_SYNC_URL") def polling_locations_import_from_master_server(request, state_code): """ Get the json data, and either create new entries or update existing :return: """ # Request json file from We Vote servers messages.add_message( request, messages.INFO, "Loading Polling Locations from We Vote Master servers") logger.info("Loading Polling Locations from We Vote Master servers") request = requests.get(
# Brought to you by We Vote. Be good. # -*- coding: UTF-8 -*- from .models import PollingLocationListManager, PollingLocationManager from config.base import get_environment_variable from django.contrib import messages import glob import json import requests import wevote_functions.admin from wevote_functions.functions import positive_value_exists import xml.etree.ElementTree as MyElementTree logger = wevote_functions.admin.get_logger(__name__) WE_VOTE_API_KEY = get_environment_variable("WE_VOTE_API_KEY") POLLING_LOCATIONS_SYNC_URL = get_environment_variable("POLLING_LOCATIONS_SYNC_URL") def polling_locations_import_from_master_server(request, state_code): """ Get the json data, and either create new entries or update existing :return: """ # Request json file from We Vote servers messages.add_message(request, messages.INFO, "Loading Polling Locations from We Vote Master servers") logger.info("Loading Polling Locations from We Vote Master servers") request = requests.get(POLLING_LOCATIONS_SYNC_URL, params={ "key": WE_VOTE_API_KEY, # This comes from an environment variable "format": 'json', "state": state_code,
# election/controllers.py # Brought to you by We Vote. Be good. # -*- coding: UTF-8 -*- from .models import Election, ElectionManager from config.base import get_environment_variable from import_export_google_civic.controllers import retrieve_from_google_civic_api_election_query, \ store_results_from_google_civic_api_election_query import json import requests import wevote_functions.admin from wevote_functions.functions import positive_value_exists logger = wevote_functions.admin.get_logger(__name__) WE_VOTE_API_KEY = get_environment_variable("WE_VOTE_API_KEY") ELECTIONS_SYNC_URL = get_environment_variable("ELECTIONS_SYNC_URL") def election_remote_retrieve(): retrieve_results = retrieve_from_google_civic_api_election_query() if not retrieve_results['success']: results = {'success': False, 'status': retrieve_results['status']} return results else: structured_json = retrieve_results['structured_json'] results = store_results_from_google_civic_api_election_query( structured_json) return results
from django.contrib import messages from django.http import HttpResponse from follow.models import FollowOrganizationList from itertools import chain import json from organization.models import OrganizationManager from position.models import ANY_STANCE, PositionEntered, PositionEnteredManager, PositionListManager import requests from voter.models import fetch_voter_id_from_voter_device_link, VoterManager from voter_guide.models import VoterGuideListManager, VoterGuideManager, VoterGuidePossibilityManager import wevote_functions.admin from wevote_functions.functions import is_voter_device_id_valid, positive_value_exists logger = wevote_functions.admin.get_logger(__name__) WE_VOTE_API_KEY = get_environment_variable("WE_VOTE_API_KEY") VOTER_GUIDES_SYNC_URL = get_environment_variable("VOTER_GUIDES_SYNC_URL") def voter_guides_import_from_master_server(request, google_civic_election_id): """ Get the json data, and either create new entries or update existing :return: """ # Request json file from We Vote servers messages.add_message(request, messages.INFO, "Loading Voter Guides from We Vote Master servers") logger.info("Loading Voter Guides from We Vote Master servers") request = requests.get( VOTER_GUIDES_SYNC_URL, params={
from django.core.urlresolvers import reverse from django.http import HttpResponseRedirect from django.shortcuts import render from election.models import Election from election.controllers import elections_import_from_sample_file from import_export_google_civic.models import GoogleCivicApiCounterManager from import_export_vote_smart.models import VoteSmartApiCounterManager from office.controllers import offices_import_from_sample_file from organization.controllers import organizations_import_from_sample_file from polling_location.controllers import import_and_save_all_polling_locations_data from position.controllers import positions_import_from_sample_file from voter.models import Voter, VoterDeviceLinkManager, VoterManager, voter_has_authority, voter_setup from wevote_functions.functions import delete_voter_api_device_id_cookie, generate_voter_device_id, \ get_voter_api_device_id, positive_value_exists, set_voter_api_device_id, STATE_CODE_MAP BALLOT_ITEMS_SYNC_URL = get_environment_variable("BALLOT_ITEMS_SYNC_URL") BALLOT_RETURNED_SYNC_URL = get_environment_variable("BALLOT_RETURNED_SYNC_URL") ELECTIONS_SYNC_URL = get_environment_variable("ELECTIONS_SYNC_URL") ORGANIZATIONS_SYNC_URL = get_environment_variable("ORGANIZATIONS_SYNC_URL") OFFICES_SYNC_URL = get_environment_variable("OFFICES_SYNC_URL") CANDIDATES_SYNC_URL = get_environment_variable("CANDIDATES_SYNC_URL") MEASURES_SYNC_URL = get_environment_variable("MEASURES_SYNC_URL") POLLING_LOCATIONS_SYNC_URL = get_environment_variable("POLLING_LOCATIONS_SYNC_URL") POSITIONS_SYNC_URL = get_environment_variable("POSITIONS_SYNC_URL") VOTER_GUIDES_SYNC_URL = get_environment_variable("VOTER_GUIDES_SYNC_URL") @login_required def admin_home_view(request): authority_required = {'verified_volunteer'} # admin, verified_volunteer if not voter_has_authority(request, authority_required):
from .models import BallotItemListManager, CANDIDATE, copy_existing_ballot_items_from_stored_ballot, OFFICE, MEASURE, \ VoterBallotSaved, VoterBallotSavedManager from candidate.models import CandidateCampaignList from config.base import get_environment_variable from exception.models import handle_exception from import_export_google_civic.controllers import voter_ballot_items_retrieve_from_google_civic_for_api from measure.models import ContestMeasureList from office.models import ContestOfficeList from voter.models import BALLOT_ADDRESS, VoterAddressManager, \ VoterDeviceLinkManager import wevote_functions.admin from wevote_functions.functions import convert_to_int, positive_value_exists logger = wevote_functions.admin.get_logger(__name__) GOOGLE_CIVIC_API_KEY = get_environment_variable("GOOGLE_CIVIC_API_KEY") def voter_ballot_items_retrieve_for_api(voter_device_id, google_civic_election_id): status = '' # We retrieve voter_device_link voter_device_link_manager = VoterDeviceLinkManager() voter_device_link_results = voter_device_link_manager.retrieve_voter_device_link( voter_device_id) if not voter_device_link_results['voter_device_link_found']: status += "VALID_VOTER_DEVICE_ID_MISSING " error_json_data = { 'status': status, 'success': False,
# Brought to you by We Vote. Be good. # -*- coding: UTF-8 -*- from .models import ContestOfficeListManager, ContestOfficeManager from ballot.models import OFFICE from config.base import get_environment_variable from django.contrib import messages from django.http import HttpResponse import json import requests import wevote_functions.admin from wevote_functions.functions import positive_value_exists logger = wevote_functions.admin.get_logger(__name__) WE_VOTE_API_KEY = get_environment_variable("WE_VOTE_API_KEY") OFFICES_SYNC_URL = get_environment_variable("OFFICES_SYNC_URL") def offices_import_from_sample_file(): """ Get the json data, and either create new entries or update existing :return: """ with open("office/import_data/contest_office_sample.json") as json_data: structured_json = json.load(json_data) return offices_import_from_structured_json(structured_json) def offices_import_from_master_server(request, google_civic_election_id=''):
# Brought to you by We Vote. Be good. # -*- coding: UTF-8 -*- from .models import ContestMeasureList, ContestMeasureManager from ballot.models import MEASURE from config.base import get_environment_variable from django.contrib import messages from django.http import HttpResponse import json import requests import wevote_functions.admin from wevote_functions.functions import positive_value_exists logger = wevote_functions.admin.get_logger(__name__) WE_VOTE_API_KEY = get_environment_variable("WE_VOTE_API_KEY") MEASURES_SYNC_URL = get_environment_variable("MEASURES_SYNC_URL") def measure_retrieve_for_api(measure_id, measure_we_vote_id): # measureRetrieve """ Used by the api :param measure_id: :param measure_we_vote_id: :return: """ # NOTE: Office retrieve is independent of *who* wants to see the data. Office retrieve never triggers # a ballot data lookup from Google Civic, like voterBallotItemsFromGoogleCivic does if not positive_value_exists(measure_id) and not positive_value_exists(
# search/controllers.py # Brought to you by We Vote. Be good. # -*- coding: UTF-8 -*- from config.base import get_environment_variable from elasticsearch import Elasticsearch from voter.models import fetch_voter_id_from_voter_device_link import wevote_functions.admin from wevote_functions.functions import is_voter_device_id_valid, positive_value_exists logger = wevote_functions.admin.get_logger(__name__) ELASTIC_SEARCH_CONNECTION_STRING = get_environment_variable("ELASTIC_SEARCH_CONNECTION_STRING") def search_all_for_api(text_from_search_field, voter_device_id): """ :param text_from_search_field: :param voter_device_id: :return: """ if not positive_value_exists(text_from_search_field): results = { 'status': 'TEXT_FROM_SEARCH_FIELD_MISSING', 'success': True, 'text_from_search_field': text_from_search_field, 'voter_device_id': voter_device_id, 'search_results_found': False, 'search_results': [], } return results
# https://developers.google.com/resources/api-libraries/documentation/civicinfo/v2/python/latest/civicinfo_v2.elections.html # -*- coding: UTF-8 -*- import json import requests from election.models import Election, ElectionManager from exception.models import handle_record_not_found_exception from import_export_google_civic.models import GoogleCivicContestOffice, GoogleCivicBallotItemManager, \ GoogleCivicCandidateCampaign, GoogleCivicContestReferendum, GoogleCivicElection from config.base import get_environment_variable from wevote_functions.models import logger, value_exists # TODO DALE is it ok to import logger like above? the following line was saying it couldn't find admin.get_logger # logger = wevote_functions.admin.get_logger(__name__) GOOGLE_CIVIC_API_KEY = get_environment_variable("GOOGLE_CIVIC_API_KEY") ELECTION_QUERY_URL = get_environment_variable("ELECTION_QUERY_URL") VOTER_INFO_URL = get_environment_variable("VOTER_INFO_URL") VOTER_INFO_JSON_FILE = get_environment_variable("VOTER_INFO_JSON_FILE") # See import_data/election_query_sample.json def retrieve_from_google_civic_api_election_query(): # Request json file from Google servers logger.info("Loading json data from Google servers, API call electionQuery") request = requests.get(ELECTION_QUERY_URL, params={ "key": GOOGLE_CIVIC_API_KEY, # This comes from an environment variable }) return json.loads(request.text)
# https://developers.google.com/resources/api-libraries/documentation/civicinfo/v2/python/latest/civicinfo_v2.elections.html # -*- coding: UTF-8 -*- import json import requests from election.models import Election, ElectionManager from exception.models import handle_record_not_found_exception from import_export_google_civic.models import GoogleCivicContestOffice, GoogleCivicBallotItemManager, \ GoogleCivicCandidateCampaign, GoogleCivicContestReferendum, GoogleCivicElection from config.base import get_environment_variable from wevote_functions.models import logger, value_exists # TODO DALE is it ok to import logger like above? the following line was saying it couldn't find admin.get_logger # logger = wevote_functions.admin.get_logger(__name__) GOOGLE_CIVIC_API_KEY = get_environment_variable("GOOGLE_CIVIC_API_KEY") ELECTION_QUERY_URL = get_environment_variable("ELECTION_QUERY_URL") VOTER_INFO_URL = get_environment_variable("VOTER_INFO_URL") VOTER_INFO_JSON_FILE = get_environment_variable("VOTER_INFO_JSON_FILE") # See import_data/election_query_sample.json def retrieve_from_google_civic_api_election_query(): # Request json file from Google servers logger.info( "Loading json data from Google servers, API call electionQuery") request = requests.get( ELECTION_QUERY_URL, params={ "key": GOOGLE_CIVIC_API_KEY, # This comes from an environment variable
# import_export_twitter/views.py # Brought to you by We Vote. Be good. # -*- coding: UTF-8 -*- # See also WeVoteServer/twitter/views.py for routines that manage internal twitter data from config.base import get_environment_variable from django.http import HttpResponseRedirect import tweepy from voter.models import VoterManager from wevote_functions.functions import positive_value_exists TWITTER_CONSUMER_KEY = get_environment_variable("TWITTER_CONSUMER_KEY") TWITTER_CONSUMER_SECRET = get_environment_variable("TWITTER_CONSUMER_SECRET") def process_sign_in_response_view(request): oauth_token = request.GET.get('oauth_token', '') oauth_verifier = request.GET.get('oauth_verifier', '') if not positive_value_exists(oauth_token) or not positive_value_exists( oauth_verifier): # Redirect back to ReactJS so we can display failure message return HttpResponseRedirect( 'http://localhost:3001/twitter/missing_variables' ) # TODO Convert to env variable voter_manager = VoterManager() # Look in the Voter table for a matching request_token, placed by the API endpoint twitterSignInStart results = voter_manager.retrieve_voter_by_twitter_request_token( oauth_token)
# measure/controllers.py # Brought to you by We Vote. Be good. # -*- coding: UTF-8 -*- from .models import ContestMeasureManager from ballot.models import MEASURE from config.base import get_environment_variable from django.http import HttpResponse import json import wevote_functions.admin from wevote_functions.functions import positive_value_exists logger = wevote_functions.admin.get_logger(__name__) WE_VOTE_API_KEY = get_environment_variable("WE_VOTE_API_KEY") # CANDIDATE_CAMPAIGNS_URL = get_environment_variable("CANDIDATE_CAMPAIGNS_URL") def measure_retrieve_for_api(measure_id, measure_we_vote_id): """ Used by the api :param measure_id: :param measure_we_vote_id: :return: """ # NOTE: Office retrieve is independent of *who* wants to see the data. Office retrieve never triggers # a ballot data lookup from Google Civic, like voterBallotItemsFromGoogleCivic does if not positive_value_exists(measure_id) and not positive_value_exists( measure_we_vote_id): status = 'VALID_MEASURE_ID_AND_MEASURE_WE_VOTE_ID_MISSING'
from django.contrib import messages from exception.models import handle_exception from import_export_google_civic.controllers import voter_ballot_items_retrieve_from_google_civic_for_api import json from measure.models import ContestMeasureList from office.models import ContestOfficeListManager from polling_location.models import PollingLocationManager import requests from voter.models import BALLOT_ADDRESS, VoterAddressManager, \ VoterDeviceLinkManager import wevote_functions.admin from wevote_functions.functions import convert_to_int, positive_value_exists logger = wevote_functions.admin.get_logger(__name__) GOOGLE_CIVIC_API_KEY = get_environment_variable("GOOGLE_CIVIC_API_KEY") WE_VOTE_API_KEY = get_environment_variable("WE_VOTE_API_KEY") BALLOT_ITEMS_SYNC_URL = get_environment_variable("BALLOT_ITEMS_SYNC_URL") BALLOT_RETURNED_SYNC_URL = get_environment_variable("BALLOT_RETURNED_SYNC_URL") def ballot_items_import_from_master_server(request, google_civic_election_id): """ Get the json data, and either create new entries or update existing :return: """ # Request json file from We Vote servers messages.add_message(request, messages.INFO, "Loading Ballot Items from We Vote Master servers") logger.info("Loading Ballot Items from We Vote Master servers") request = requests.get(BALLOT_ITEMS_SYNC_URL, params={ "key": WE_VOTE_API_KEY, # This comes from an environment variable
from django.contrib import messages from django.http import HttpResponse from follow.models import FollowOrganizationList from itertools import chain import json from organization.models import OrganizationManager from position.models import ANY_STANCE, PositionEntered, PositionEnteredManager, PositionListManager import requests from voter.models import fetch_voter_id_from_voter_device_link, VoterManager from voter_guide.models import VoterGuideListManager, VoterGuideManager, VoterGuidePossibilityManager import wevote_functions.admin from wevote_functions.functions import is_voter_device_id_valid, positive_value_exists logger = wevote_functions.admin.get_logger(__name__) WE_VOTE_API_KEY = get_environment_variable("WE_VOTE_API_KEY") VOTER_GUIDES_SYNC_URL = get_environment_variable("VOTER_GUIDES_SYNC_URL") def voter_guides_import_from_master_server(request, google_civic_election_id): """ Get the json data, and either create new entries or update existing :return: """ # Request json file from We Vote servers messages.add_message(request, messages.INFO, "Loading Voter Guides from We Vote Master servers") logger.info("Loading Voter Guides from We Vote Master servers") request = requests.get(VOTER_GUIDES_SYNC_URL, params={ "key": WE_VOTE_API_KEY, # This comes from an environment variable "format": 'json', "google_civic_election_id": google_civic_election_id,
VoteSmartSpecialInterestGroup, VoteSmartSpecialInterestGroupManager, vote_smart_special_interest_group_filter, \ vote_smart_special_interest_group_list_filter, \ VoteSmartState, vote_smart_state_filter from .votesmart_local import votesmart, VotesmartApiError from candidate.models import CandidateCampaignManager from config.base import get_environment_variable import copy from exception.models import handle_record_found_more_than_one_exception from position.models import PositionEnteredManager, PERCENT_RATING import requests import wevote_functions.admin from wevote_functions.functions import convert_to_int, positive_value_exists logger = wevote_functions.admin.get_logger(__name__) VOTE_SMART_API_KEY = get_environment_variable("VOTE_SMART_API_KEY") VOTE_SMART_API_URL = get_environment_variable("VOTE_SMART_API_URL") votesmart.apikey = VOTE_SMART_API_KEY def retrieve_and_match_candidate_from_vote_smart(we_vote_candidate, force_retrieve=False): status = "" vote_smart_candidate_just_retrieved = False # Has this candidate already been linked to a Vote Smart candidate? if positive_value_exists( we_vote_candidate.vote_smart_id) and not force_retrieve: vote_smart_candidate_id = we_vote_candidate.vote_smart_id status += 'VOTE_SMART_CANDIDATE_ID_PREVIOUSLY_RETRIEVED '
from config.base import get_environment_variable from django.contrib import messages from django.http import HttpResponse from exception.models import handle_exception from import_export_vote_smart.controllers import retrieve_and_match_candidate_from_vote_smart, \ retrieve_candidate_photo_from_vote_smart import json from office.models import ContestOfficeManager from politician.models import PoliticianManager import requests import wevote_functions.admin from wevote_functions.functions import positive_value_exists logger = wevote_functions.admin.get_logger(__name__) WE_VOTE_API_KEY = get_environment_variable("WE_VOTE_API_KEY") CANDIDATES_SYNC_URL = get_environment_variable("CANDIDATES_SYNC_URL") def candidates_import_from_sample_file(): """ Get the json data, and either create new entries or update existing :return: """ # Load saved json from local file logger.info("Loading CandidateCampaigns from local file") with open("candidate/import_data/candidate_campaigns_sample.json") as json_data: structured_json = json.load(json_data) return candidates_import_from_structured_json(structured_json)
from .models import QuickInfo, QuickInfoManager, QuickInfoMasterManager from ballot.models import OFFICE, CANDIDATE, POLITICIAN, MEASURE from candidate.models import CandidateCampaignManager from config.base import get_environment_variable from django.contrib import messages from django.http import HttpResponse from exception.models import handle_record_not_found_exception, handle_record_not_saved_exception from organization.models import OrganizationManager import json from voter.models import fetch_voter_id_from_voter_device_link, VoterManager import wevote_functions.admin from wevote_functions.functions import convert_to_int, is_voter_device_id_valid, positive_value_exists logger = wevote_functions.admin.get_logger(__name__) WE_VOTE_API_KEY = get_environment_variable("WE_VOTE_API_KEY") QUICK_INFO_URL = get_environment_variable("QUICK_INFO_URL") def quick_info_save_for_api( # TODO to be converted voter_device_id, quick_info_id, quick_info_we_vote_id, organization_we_vote_id, public_figure_we_vote_id, voter_we_vote_id, google_civic_election_id, ballot_item_display_name, office_we_vote_id, candidate_we_vote_id, measure_we_vote_id, stance, statement_text,
from config.base import get_environment_variable from django.contrib import messages from django.http import HttpResponse from exception.models import handle_exception from import_export_vote_smart.controllers import retrieve_and_match_candidate_from_vote_smart, \ retrieve_candidate_photo_from_vote_smart import json from office.models import ContestOfficeManager from politician.models import PoliticianManager import requests import wevote_functions.admin from wevote_functions.functions import positive_value_exists logger = wevote_functions.admin.get_logger(__name__) WE_VOTE_API_KEY = get_environment_variable("WE_VOTE_API_KEY") CANDIDATES_SYNC_URL = get_environment_variable("CANDIDATES_SYNC_URL") def candidates_import_from_sample_file(): """ Get the json data, and either create new entries or update existing :return: """ # Load saved json from local file logger.info("Loading CandidateCampaigns from local file") with open("candidate/import_data/candidate_campaigns_sample.json" ) as json_data: structured_json = json.load(json_data)
# Brought to you by We Vote. Be good. # -*- coding: UTF-8 -*- from .models import CandidateCampaignList, CandidateCampaignManager from config.base import get_environment_variable from django.contrib import messages from django.http import HttpResponse from exception.models import handle_exception import json from office.models import ContestOfficeManager import wevote_functions.admin from wevote_functions.models import positive_value_exists logger = wevote_functions.admin.get_logger(__name__) WE_VOTE_API_KEY = get_environment_variable("WE_VOTE_API_KEY") CANDIDATE_CAMPAIGNS_URL = get_environment_variable("CANDIDATE_CAMPAIGNS_URL") def candidates_import_from_sample_file(request=None, load_from_uri=False): """ Get the json data, and either create new entries or update existing :return: """ # if load_from_uri: # # Request json file from We Vote servers # messages.add_message(request, messages.INFO, "Loading CandidateCampaign IDs from We Vote Master servers") # request = requests.get(CANDIDATE_CAMPAIGNS_URL, params={ # "key": WE_VOTE_API_KEY, # This comes from an environment variable # }) # structured_json = json.loads(request.text)
# office/controllers.py # Brought to you by We Vote. Be good. # -*- coding: UTF-8 -*- from .models import ContestOfficeManager from config.base import get_environment_variable from django.contrib import messages from exception.models import handle_record_not_found_exception, handle_record_not_saved_exception import json import requests import wevote_functions.admin from wevote_functions.models import positive_value_exists logger = wevote_functions.admin.get_logger(__name__) WE_VOTE_API_KEY = get_environment_variable("WE_VOTE_API_KEY") # CANDIDATE_CAMPAIGNS_URL = get_environment_variable("CANDIDATE_CAMPAIGNS_URL") def offices_import_from_sample_file(request=None, load_from_uri=False): # TODO FINISH BUILDING/TESTING THIS """ Get the json data, and either create new entries or update existing :return: """ # if load_from_uri: # # Request json file from We Vote servers # messages.add_message(request, messages.INFO, "Loading ContestOffice IDs from We Vote Master servers") # request = requests.get(CANDIDATE_CAMPAIGNS_URL, params={ # "key": WE_VOTE_API_KEY, # This comes from an environment variable # }) # structured_json = json.loads(request.text)
from candidate.models import CandidateCampaignManager, CandidateCampaignListManager from config.base import get_environment_variable from organization.controllers import update_social_media_statistics_in_other_tables from organization.models import Organization, OrganizationManager import re from socket import timeout import tweepy import urllib.request from voter.models import VoterDeviceLinkManager, VoterManager import wevote_functions.admin from wevote_functions.functions import convert_to_int, extract_twitter_handle_from_text_string, \ is_voter_device_id_valid, positive_value_exists logger = wevote_functions.admin.get_logger(__name__) WE_VOTE_SERVER_ROOT_URL = get_environment_variable("WE_VOTE_SERVER_ROOT_URL") RE_FACEBOOK = r'//www\.facebook\.com/(?:#!/)?(\w+)' # RE_FACEBOOK = r'/(?:https?:\/\/)?(?:www\.)?facebook\.com\/(?:(?:\w)*#!\/)?(?:pages\/)?(?:[\w\-]*\/)*?(\/)?([^/?]*)/' FACEBOOK_BLACKLIST = ['group', 'group.php', 'None'] # NOTE: Scraping a website for the Facebook handle is more complicated than Twitter. There must be an existing # solution available? My attempt turned off for now. # Only pays attention to https://twitter.com or http://twitter.com and ignores www.twitter.com RE_TWITTER = r'//twitter\.com/(?:#!/)?(\w+)' TWITTER_BLACKLIST = [ 'home', 'https', 'intent', 'none', 'search', 'share', 'twitterapi' ] TWITTER_CONSUMER_KEY = get_environment_variable("TWITTER_CONSUMER_KEY") TWITTER_CONSUMER_SECRET = get_environment_variable("TWITTER_CONSUMER_SECRET") TWITTER_ACCESS_TOKEN = get_environment_variable("TWITTER_ACCESS_TOKEN")
# Brought to you by We Vote. Be good. # -*- coding: UTF-8 -*- from .models import ContestMeasureList, ContestMeasureManager from ballot.models import MEASURE from config.base import get_environment_variable from django.contrib import messages from django.http import HttpResponse import json import requests import wevote_functions.admin from wevote_functions.functions import positive_value_exists logger = wevote_functions.admin.get_logger(__name__) WE_VOTE_API_KEY = get_environment_variable("WE_VOTE_API_KEY") MEASURES_SYNC_URL = get_environment_variable("MEASURES_SYNC_URL") def measure_retrieve_for_api(measure_id, measure_we_vote_id): # measureRetrieve """ Used by the api :param measure_id: :param measure_we_vote_id: :return: """ # NOTE: Office retrieve is independent of *who* wants to see the data. Office retrieve never triggers # a ballot data lookup from Google Civic, like voterBallotItemsFromGoogleCivic does if not positive_value_exists(measure_id) and not positive_value_exists(measure_we_vote_id): status = "VALID_MEASURE_ID_AND_MEASURE_WE_VOTE_ID_MISSING"
from config.base import get_environment_variable from organization.controllers import update_social_media_statistics_in_other_tables from organization.models import Organization, OrganizationManager import re from socket import timeout import tweepy import urllib.request from voter.models import VoterDeviceLinkManager, VoterManager import wevote_functions.admin from wevote_functions.functions import convert_to_int, extract_twitter_handle_from_text_string, \ is_voter_device_id_valid, positive_value_exists logger = wevote_functions.admin.get_logger(__name__) WE_VOTE_SERVER_ROOT_URL = get_environment_variable("WE_VOTE_SERVER_ROOT_URL") RE_FACEBOOK = r'//www\.facebook\.com/(?:#!/)?(\w+)' # RE_FACEBOOK = r'/(?:https?:\/\/)?(?:www\.)?facebook\.com\/(?:(?:\w)*#!\/)?(?:pages\/)?(?:[\w\-]*\/)*?(\/)?([^/?]*)/' FACEBOOK_BLACKLIST = ['group', 'group.php', 'None'] # NOTE: Scraping a website for the Facebook handle is more complicated than Twitter. There must be an existing # solution available? My attempt turned off for now. # Only pays attention to https://twitter.com or http://twitter.com and ignores www.twitter.com RE_TWITTER = r'//twitter\.com/(?:#!/)?(\w+)' TWITTER_BLACKLIST = ['home', 'https', 'intent', 'none', 'search', 'share', 'twitterapi'] TWITTER_CONSUMER_KEY = get_environment_variable("TWITTER_CONSUMER_KEY") TWITTER_CONSUMER_SECRET = get_environment_variable("TWITTER_CONSUMER_SECRET") TWITTER_ACCESS_TOKEN = get_environment_variable("TWITTER_ACCESS_TOKEN") TWITTER_ACCESS_TOKEN_SECRET = get_environment_variable("TWITTER_ACCESS_TOKEN_SECRET")
from config.base import get_environment_variable from django.contrib import messages from django.http import HttpResponse from exception.models import handle_record_not_found_exception, \ handle_record_not_saved_exception from follow.models import FollowOrganizationManager, FollowOrganizationList, FOLLOW_IGNORE, FOLLOWING, STOP_FOLLOWING import json from organization.models import Organization from voter.models import fetch_voter_id_from_voter_device_link from voter_guide.models import VoterGuide, VoterGuideManager import wevote_functions.admin from wevote_functions.functions import convert_to_int, positive_value_exists logger = wevote_functions.admin.get_logger(__name__) WE_VOTE_API_KEY = get_environment_variable("WE_VOTE_API_KEY") ORGANIZATIONS_URL = get_environment_variable("ORGANIZATIONS_URL") def organization_follow_all(voter_device_id, organization_id, organization_we_vote_id, follow_kind=FOLLOWING): if not positive_value_exists(voter_device_id): json_data = { 'status': 'VALID_VOTER_DEVICE_ID_MISSING', 'success': False, 'voter_device_id': voter_device_id, 'organization_id': organization_id, } return HttpResponse(json.dumps(json_data),
from django.core.urlresolvers import reverse from django.http import HttpResponseRedirect from django.shortcuts import render from election.models import Election from election.controllers import elections_import_from_sample_file from import_export_google_civic.models import GoogleCivicApiCounterManager from import_export_vote_smart.models import VoteSmartApiCounterManager from office.controllers import offices_import_from_sample_file from organization.controllers import organizations_import_from_sample_file from polling_location.controllers import import_and_save_all_polling_locations_data from position.controllers import positions_import_from_sample_file from voter.models import Voter, VoterDeviceLinkManager, VoterManager, voter_has_authority, voter_setup from wevote_functions.functions import convert_to_int, delete_voter_api_device_id_cookie, generate_voter_device_id, \ get_voter_api_device_id, positive_value_exists, set_voter_api_device_id, STATE_CODE_MAP BALLOT_ITEMS_SYNC_URL = get_environment_variable("BALLOT_ITEMS_SYNC_URL") BALLOT_RETURNED_SYNC_URL = get_environment_variable("BALLOT_RETURNED_SYNC_URL") ELECTIONS_SYNC_URL = get_environment_variable("ELECTIONS_SYNC_URL") ORGANIZATIONS_SYNC_URL = get_environment_variable("ORGANIZATIONS_SYNC_URL") OFFICES_SYNC_URL = get_environment_variable("OFFICES_SYNC_URL") CANDIDATES_SYNC_URL = get_environment_variable("CANDIDATES_SYNC_URL") MEASURES_SYNC_URL = get_environment_variable("MEASURES_SYNC_URL") POLLING_LOCATIONS_SYNC_URL = get_environment_variable( "POLLING_LOCATIONS_SYNC_URL") POSITIONS_SYNC_URL = get_environment_variable("POSITIONS_SYNC_URL") VOTER_GUIDES_SYNC_URL = get_environment_variable("VOTER_GUIDES_SYNC_URL") @login_required def admin_home_view(request): authority_required = {'verified_volunteer'} # admin, verified_volunteer
# election/controllers.py # Brought to you by We Vote. Be good. # -*- coding: UTF-8 -*- from .models import Election, ElectionManager from config.base import get_environment_variable from import_export_google_civic.controllers import retrieve_from_google_civic_api_election_query, \ store_results_from_google_civic_api_election_query import json import requests import wevote_functions.admin from wevote_functions.functions import positive_value_exists logger = wevote_functions.admin.get_logger(__name__) WE_VOTE_API_KEY = get_environment_variable("WE_VOTE_API_KEY") ELECTIONS_SYNC_URL = get_environment_variable("ELECTIONS_SYNC_URL") def election_remote_retrieve(): retrieve_results = retrieve_from_google_civic_api_election_query() if not retrieve_results['success']: results = { 'success': False, 'status': retrieve_results['status'] } return results else: structured_json = retrieve_results['structured_json']
VoteSmartRatingOneCandidate, vote_smart_rating_one_candidate_filter, \ VoteSmartSpecialInterestGroup, VoteSmartSpecialInterestGroupManager, vote_smart_special_interest_group_filter, \ vote_smart_special_interest_group_list_filter, \ VoteSmartState, vote_smart_state_filter from .votesmart_local import votesmart, VotesmartApiError from candidate.models import CandidateCampaignManager from config.base import get_environment_variable import copy from position.models import PositionEnteredManager, PERCENT_RATING import requests import wevote_functions.admin from wevote_functions.functions import convert_to_int, positive_value_exists logger = wevote_functions.admin.get_logger(__name__) VOTE_SMART_API_KEY = get_environment_variable("VOTE_SMART_API_KEY") VOTE_SMART_API_URL = get_environment_variable("VOTE_SMART_API_URL") votesmart.apikey = VOTE_SMART_API_KEY def retrieve_and_match_candidate_from_vote_smart(we_vote_candidate, force_retrieve=False): status = "" # Has this candidate already been linked to a Vote Smart candidate? if positive_value_exists(we_vote_candidate.vote_smart_id) and not force_retrieve: vote_smart_candidate_id = we_vote_candidate.vote_smart_id status += 'VOTE_SMART_CANDIDATE_ID_PREVIOUSLY_RETRIEVED ' results = { 'success': True, 'status': status, 'message_type': 'INFO',
from .models import BallotItemListManager, CANDIDATE, copy_existing_ballot_items_from_stored_ballot, OFFICE, MEASURE, \ VoterBallotSaved, VoterBallotSavedManager from candidate.models import CandidateCampaignList from config.base import get_environment_variable from exception.models import handle_exception from import_export_google_civic.controllers import voter_ballot_items_retrieve_from_google_civic_for_api from measure.models import ContestMeasureList from office.models import ContestOfficeList from voter.models import BALLOT_ADDRESS, VoterAddressManager, \ VoterDeviceLinkManager import wevote_functions.admin from wevote_functions.functions import convert_to_int, positive_value_exists logger = wevote_functions.admin.get_logger(__name__) GOOGLE_CIVIC_API_KEY = get_environment_variable("GOOGLE_CIVIC_API_KEY") def voter_ballot_items_retrieve_for_api(voter_device_id, google_civic_election_id): status = '' # We retrieve voter_device_link voter_device_link_manager = VoterDeviceLinkManager() voter_device_link_results = voter_device_link_manager.retrieve_voter_device_link(voter_device_id) if not voter_device_link_results['voter_device_link_found']: status += "VALID_VOTER_DEVICE_ID_MISSING " error_json_data = { 'status': status, 'success': False, 'voter_device_id': voter_device_id, 'ballot_found': False,
from ballot.models import CANDIDATE from config.base import get_environment_variable from django.contrib import messages from django.http import HttpResponse from exception.models import handle_exception from import_export_vote_smart.controllers import retrieve_and_match_candidate_from_vote_smart, \ retrieve_candidate_photo_from_vote_smart from import_export_vote_smart.models import VoteSmartCandidateManager import json from office.models import ContestOfficeManager import wevote_functions.admin from wevote_functions.functions import positive_value_exists logger = wevote_functions.admin.get_logger(__name__) WE_VOTE_API_KEY = get_environment_variable("WE_VOTE_API_KEY") CANDIDATE_CAMPAIGNS_URL = get_environment_variable("CANDIDATE_CAMPAIGNS_URL") def candidates_import_from_sample_file(request=None, load_from_uri=False): """ Get the json data, and either create new entries or update existing :return: """ # if load_from_uri: # # Request json file from We Vote servers # messages.add_message(request, messages.INFO, "Loading CandidateCampaign IDs from We Vote Master servers") # request = requests.get(CANDIDATE_CAMPAIGNS_URL, params={ # "key": WE_VOTE_API_KEY, # This comes from an environment variable # }) # structured_json = json.loads(request.text)