def voter_create(voter_device_id): results = is_voter_device_id_valid(voter_device_id) if not results['success']: return HttpResponse(json.dumps(results['json_data']), content_type='application/json') voter_id = 0 # Make sure a voter record hasn't already been created for this existing_voter_id = fetch_voter_id_from_voter_device_link(voter_device_id) if existing_voter_id: json_data = { 'status': "VOTER_ALREADY_EXISTS", 'success': False, 'voter_device_id': voter_device_id, } return HttpResponse(json.dumps(json_data), content_type='application/json') # Create a new voter and return the id voter_manager = VoterManager() results = voter_manager.create_voter() if results['voter_created']: voter = results['voter'] # Now save the voter_device_link voter_device_link_manager = VoterDeviceLinkManager() results = voter_device_link_manager.save_new_voter_device_link( voter_device_id, voter.id) if results['voter_device_link_created']: voter_device_link = results['voter_device_link'] voter_id_found = True if voter_device_link.voter_id > 0 else False if voter_id_found: voter_id = voter_device_link.voter_id if voter_id: json_data = { 'status': "VOTER_CREATED", 'success': False, 'voter_device_id': voter_device_id, 'voter_id': voter_id, # We may want to remove this after initial testing } return HttpResponse(json.dumps(json_data), content_type='application/json') else: json_data = { 'status': "VOTER_NOT_CREATED", 'success': False, 'voter_device_id': voter_device_id, } return HttpResponse(json.dumps(json_data), content_type='application/json')
def my_ballot_view(request): generate_voter_device_id_if_needed = True voter_device_id = get_voter_device_id(request, generate_voter_device_id_if_needed) voter_id_found = False voter_device_link_manager = VoterDeviceLinkManager() results = voter_device_link_manager.retrieve_voter_device_link_from_voter_device_id( voter_device_id) if results['voter_device_link_found']: voter_device_link = results['voter_device_link'] voter_id_found = True if voter_device_link.voter_id > 0 else False # If existing voter not found, create a new voter if not voter_id_found: # Create a new voter and return the id voter_manager = VoterManager() results = voter_manager.create_voter() if results['voter_created']: voter = results['voter'] # Now save the voter_device_link results = voter_device_link_manager.save_new_voter_device_link( voter_device_id, voter.id) if results['voter_device_link_created']: voter_device_link = results['voter_device_link'] voter_id_found = True if voter_device_link.voter_id > 0 else False if not voter_id_found: print "Voter ID not found, nor generated. This should not be possible. (Cookies may be turned off?)" ballot_item_list = [] else: ballot_item_manager = BallotItemManager() results = ballot_item_manager.retrieve_all_ballot_items_for_voter( voter_device_link.voter_id) ballot_item_list = results['ballot_item_list'] template_values = { 'ballot_item_list': ballot_item_list, } response = render(request, 'ux_oak/my_ballot.html', template_values) set_voter_device_id(request, response, voter_device_id) return response
def logout_user(request): logout(request) info_message = "You are now signed out." messages.add_message(request, messages.INFO, info_message) messages_on_stage = get_messages(request) template_values = { 'request': request, 'next': '/admin/', 'messages_on_stage': messages_on_stage, } response = render(request, 'registration/login_user.html', template_values) # Find current voter_api_device_id voter_api_device_id = get_voter_api_device_id(request) delete_voter_api_device_id_cookie(response) # Now delete voter_api_device_id from database voter_device_link_manager = VoterDeviceLinkManager() voter_device_link_manager.delete_voter_device_link(voter_api_device_id) return response
def positions_count_for_all_ballot_items_for_api( voter_device_id, google_civic_election_id=0, show_positions_this_voter_follows=True): """ We want to return a JSON file with the a list of the support and oppose counts from the orgs, friends and public figures the voter follows """ # Get voter_id from the voter_device_id so we can know whose stars to retrieve results = is_voter_device_id_valid(voter_device_id) if not results['success']: json_data = { 'status': "VALID_VOTER_DEVICE_ID_MISSING-COUNT_FOR_ALL_BALLOT_ITEMS", 'success': False, 'google_civic_election_id': google_civic_election_id, 'ballot_item_list': [], } return json_data voter_id = fetch_voter_id_from_voter_device_link(voter_device_id) if not positive_value_exists(voter_id): json_data = { 'status': "VALID_VOTER_ID_MISSING-COUNT_FOR_ALL_BALLOT_ITEMS", 'success': False, 'google_civic_election_id': google_civic_election_id, 'ballot_item_list': [], } return json_data if not positive_value_exists(google_civic_election_id): # We must have an election id to proceed -- otherwise we don't know what ballot items to work with voter_device_link_manager = VoterDeviceLinkManager() voter_device_link_results = voter_device_link_manager.retrieve_voter_device_link( voter_device_id) if voter_device_link_results['voter_device_link_found']: voter_device_link = voter_device_link_results['voter_device_link'] if positive_value_exists( voter_device_link.google_civic_election_id): google_civic_election_id = voter_device_link.google_civic_election_id if not positive_value_exists(google_civic_election_id): voter_address_manager = VoterAddressManager() voter_address_results = voter_address_manager.retrieve_address( 0, voter_id) if voter_address_results['voter_address_found']: voter_address = voter_address_results['voter_address'] if positive_value_exists( voter_address.google_civic_election_id): google_civic_election_id = voter_address.google_civic_election_id google_civic_election_id = convert_to_int(google_civic_election_id) if not positive_value_exists(google_civic_election_id): json_data = { 'status': "GOOGLE_CIVIC_ELECTION_ID_MISSING-COUNT_FOR_ALL_BALLOT_ITEMS", 'success': False, 'google_civic_election_id': google_civic_election_id, 'ballot_item_list': [], } return json_data position_list_manager = PositionListManager() candidate_list_object = CandidateCampaignList() follow_organization_list_manager = FollowOrganizationList() organizations_followed_by_voter = \ follow_organization_list_manager.retrieve_follow_organization_by_voter_id_simple_id_array(voter_id) # Get a list of all candidates and measures from this election (in the active election) ballot_item_results = voter_ballot_items_retrieve_for_one_election_for_api( voter_device_id, voter_id, google_civic_election_id) ballot_item_list = ballot_item_results['ballot_item_list'] # The list where we capture results ballot_item_list_results = [] # ballot_item_list is populated with contest_office and contest_measure entries for one_ballot_item in ballot_item_list: # Retrieve all positions for each ballot item if one_ballot_item['kind_of_ballot_item'] == OFFICE: results = candidate_list_object.retrieve_all_candidates_for_office( 0, one_ballot_item['we_vote_id']) success = results['success'] candidate_list = results['candidate_list'] if success: for candidate in candidate_list: # Loop through all candidates under this office support_positions_list_for_one_ballot_item = \ position_list_manager.retrieve_all_positions_for_candidate_campaign( 0, candidate.we_vote_id, SUPPORT) oppose_positions_list_for_one_ballot_item = \ position_list_manager.retrieve_all_positions_for_candidate_campaign( 0, candidate.we_vote_id, OPPOSE) finalize_results = finalize_support_and_oppose_positions_count( voter_id, show_positions_this_voter_follows, organizations_followed_by_voter, support_positions_list_for_one_ballot_item, oppose_positions_list_for_one_ballot_item) one_ballot_item_results = { 'ballot_item_we_vote_id': candidate.we_vote_id, 'support_count': finalize_results['support_positions_count'], 'oppose_count': finalize_results['oppose_positions_count'], } ballot_item_list_results.append(one_ballot_item_results) elif one_ballot_item['kind_of_ballot_item'] == MEASURE: support_positions_list_for_one_ballot_item = \ position_list_manager.retrieve_all_positions_for_contest_measure(0, one_ballot_item['we_vote_id'], SUPPORT) oppose_positions_list_for_one_ballot_item = \ position_list_manager.retrieve_all_positions_for_contest_measure(0, one_ballot_item['we_vote_id'], OPPOSE) finalize_results = finalize_support_and_oppose_positions_count( voter_id, show_positions_this_voter_follows, organizations_followed_by_voter, support_positions_list_for_one_ballot_item, oppose_positions_list_for_one_ballot_item) one_ballot_item_results = { 'ballot_item_we_vote_id': one_ballot_item['we_vote_id'], 'support_count': finalize_results['support_positions_count'], 'oppose_count': finalize_results['oppose_positions_count'], } ballot_item_list_results.append(one_ballot_item_results) else: # Skip the rest of this loop continue json_data = { 'success': True, 'status': "POSITIONS_COUNT_FOR_ALL_BALLOT_ITEMS", 'google_civic_election_id': google_civic_election_id, 'ballot_item_list': ballot_item_list_results, } return json_data
def login_user(request): """ This method is called when you login from the /login/ form :param request: :return: """ voter_api_device_id = get_voter_api_device_id( request) # We look in the cookies for voter_api_device_id store_new_voter_api_device_id_in_cookie = False voter_signed_in = False voter_manager = VoterManager() voter_device_link_manager = VoterDeviceLinkManager() results = voter_manager.retrieve_voter_from_voter_device_id( voter_api_device_id) if results['voter_found']: voter_on_stage = results['voter'] voter_on_stage_id = voter_on_stage.id # Just because a We Vote voter is found doesn't mean they are authenticated for Django else: voter_on_stage_id = 0 info_message = '' error_message = '' username = '' # Does Django think user is already signed in? if request.user.is_authenticated(): # If so, make sure user and voter_on_stage are the same. if request.user.id != voter_on_stage_id: # Delete the prior voter_api_device_id from database voter_device_link_manager.delete_voter_device_link( voter_api_device_id) # Create a new voter_api_device_id and voter_device_link voter_api_device_id = generate_voter_device_id() results = voter_device_link_manager.save_new_voter_device_link( voter_api_device_id, request.user.id) store_new_voter_api_device_id_in_cookie = results[ 'voter_device_link_created'] voter_on_stage = request.user voter_on_stage_id = voter_on_stage.id elif request.POST: username = request.POST.get('username') password = request.POST.get('password') user = authenticate(username=username, password=password) if user is not None: if user.is_active: login(request, user) info_message = "You're successfully logged in!" # Delete the prior voter_api_device_id from database voter_device_link_manager.delete_voter_device_link( voter_api_device_id) # Create a new voter_api_device_id and voter_device_link voter_api_device_id = generate_voter_device_id() results = voter_device_link_manager.save_new_voter_device_link( voter_api_device_id, user.id) store_new_voter_api_device_id_in_cookie = results[ 'voter_device_link_created'] else: error_message = "Your account is not active, please contact the site admin." if user.id != voter_on_stage_id: # Eventually we want to merge voter_on_stage into user account pass else: error_message = "Your username and/or password were incorrect." elif not positive_value_exists(voter_on_stage_id): # If here, delete the prior voter_api_device_id from database voter_device_link_manager.delete_voter_device_link(voter_api_device_id) # We then need to set a voter_api_device_id cookie and create a new voter (even though not signed in) 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'] # Does Django think user is signed in? if request.user.is_authenticated(): voter_signed_in = True else: info_message = "Please log in below..." if positive_value_exists(error_message): messages.add_message(request, messages.ERROR, error_message) if positive_value_exists(info_message): messages.add_message(request, messages.INFO, info_message) messages_on_stage = get_messages(request) template_values = { 'request': request, 'username': username, 'next': next, 'voter_signed_in': voter_signed_in, 'messages_on_stage': messages_on_stage, } response = render(request, 'registration/login_user.html', template_values) # We want to store the voter_api_device_id cookie if it is new if positive_value_exists(voter_api_device_id) and positive_value_exists( store_new_voter_api_device_id_in_cookie): set_voter_api_device_id(request, response, voter_api_device_id) return response
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, 'ballot_item_list': [], 'google_civic_election_id': google_civic_election_id, 'text_for_map_search': '', 'substituted_address_nearby': '', 'ballot_caveat': '', 'is_from_substituted_address': False, 'is_from_test_ballot': False, } return error_json_data voter_device_link = voter_device_link_results['voter_device_link'] voter_id = voter_device_link.voter_id if not positive_value_exists(voter_id): status += " " + "VALID_VOTER_ID_MISSING" error_json_data = { 'status': status, 'success': False, 'voter_device_id': voter_device_id, 'ballot_found': False, 'ballot_item_list': [], 'google_civic_election_id': google_civic_election_id, 'text_for_map_search': '', 'substituted_address_nearby': '', 'ballot_caveat': '', 'is_from_substituted_address': False, 'is_from_test_ballot': False, } return error_json_data voter_address_manager = VoterAddressManager() voter_address_id = 0 address_type = BALLOT_ADDRESS voter_address_results = voter_address_manager.retrieve_address( voter_address_id, voter_id, address_type) status += " " + voter_address_results['status'] if not positive_value_exists( voter_address_results['voter_address_has_value']): error_json_data = { 'status': status, 'success': voter_address_results['success'], 'voter_device_id': voter_device_id, 'ballot_found': False, 'ballot_item_list': [], 'google_civic_election_id': 0, 'text_for_map_search': '', 'substituted_address_nearby': '', 'ballot_caveat': '', 'is_from_substituted_address': False, 'is_from_test_ballot': False, } return error_json_data voter_address = voter_address_results['voter_address'] results = choose_election_and_prepare_ballot_data( voter_device_link, google_civic_election_id, voter_address) status += " " + results['status'] if not results['voter_ballot_saved_found']: if positive_value_exists(voter_address.text_for_map_search): ballot_caveat = "We could not find a ballot near '{text_for_map_search}'.".format( text_for_map_search=voter_address.text_for_map_search) else: ballot_caveat = "Please save your address so we can find your ballot." error_json_data = { 'status': status, 'success': True, 'voter_device_id': voter_device_id, 'ballot_found': False, 'ballot_item_list': [], 'google_civic_election_id': 0, 'text_for_map_search': voter_address.text_for_map_search, 'substituted_address_nearby': '', 'ballot_caveat': ballot_caveat, 'is_from_substituted_address': False, 'is_from_test_ballot': False, } return error_json_data google_civic_election_id = results['google_civic_election_id'] voter_ballot_saved = results['voter_ballot_saved'] # Update voter_device_link if voter_device_link.google_civic_election_id != google_civic_election_id: voter_device_link_manager.update_voter_device_link_with_election_id( voter_device_link, google_civic_election_id) # Update voter_address to include matching google_civic_election_id and voter_ballot_saved entry if positive_value_exists(google_civic_election_id): voter_address.google_civic_election_id = google_civic_election_id voter_address_manager.update_existing_voter_address_object( voter_address) # Get and return the ballot_item_list results = voter_ballot_items_retrieve_for_one_election_for_api( voter_device_id, voter_id, google_civic_election_id) if not positive_value_exists(voter_ballot_saved.election_description_text) \ or not positive_value_exists(voter_ballot_saved.election_date_text()): try: election_manager = ElectionManager() election_results = election_manager.retrieve_election( google_civic_election_id) if election_results['election_found']: election = election_results['election'] if not positive_value_exists( voter_ballot_saved.election_description_text): voter_ballot_saved.election_description_text = election.election_name if not positive_value_exists( voter_ballot_saved.election_date_text()): voter_ballot_saved.election_date = \ datetime.strptime(election.election_day_text, "%Y-%m-%d").date() voter_ballot_saved.save() except Exception as e: status += "Failed to update election_name" status += " " + results['status'] json_data = { 'status': status, 'success': True, 'voter_device_id': voter_device_id, 'ballot_found': True, 'ballot_item_list': results['ballot_item_list'], 'google_civic_election_id': google_civic_election_id, 'election_name': voter_ballot_saved.election_description_text, 'election_date': voter_ballot_saved.election_date_text(), 'text_for_map_search': voter_ballot_saved.original_text_for_map_search, 'substituted_address_nearby': voter_ballot_saved.substituted_address_nearby, 'ballot_caveat': voter_ballot_saved.ballot_caveat(), 'is_from_substituted_address': voter_ballot_saved.is_from_substituted_address, 'is_from_test_ballot': voter_ballot_saved.is_from_test_ballot, } return json_data status += " " + "NO_VOTER_BALLOT_SAVED_FOUND" error_json_data = { 'status': status, 'success': True, 'voter_device_id': voter_device_id, 'ballot_found': False, 'ballot_item_list': [], 'google_civic_election_id': 0, 'text_for_map_search': '', 'substituted_address_nearby': '', 'ballot_caveat': '', 'is_from_substituted_address': False, 'is_from_test_ballot': False, } return error_json_data
def figure_out_google_civic_election_id_voter_is_watching(voter_device_id): status = '' # We zero out this value since we will never have this coming in for this function google_civic_election_id = 0 # 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: " + voter_device_link_results[ 'status'] results = { 'status': status, 'success': False, 'voter_device_id': voter_device_id, 'voter_device_link_found': False, 'voter_address_object_found': False, 'voter_ballot_saved_found': False, 'google_civic_election_id': 0, } return results voter_device_link = voter_device_link_results['voter_device_link'] voter_id = voter_device_link.voter_id if not positive_value_exists(voter_id): status += " " + "VALID_VOTER_ID_MISSING" results = { 'status': status, 'success': False, 'voter_device_id': voter_device_id, 'voter_device_link_found': False, 'voter_address_object_found': False, 'voter_ballot_saved_found': False, 'google_civic_election_id': 0, } return results voter_address_manager = VoterAddressManager() voter_address_id = 0 address_type = BALLOT_ADDRESS voter_address_results = voter_address_manager.retrieve_address( voter_address_id, voter_id, address_type) status += " " + voter_address_results['status'] if not positive_value_exists( voter_address_results['voter_address_has_value']): # If there isn't an address with a value, then there won't be a voter_ballot_saved_found results = { 'status': status, 'success': True, 'voter_device_id': voter_device_id, 'voter_device_link_found': False, 'voter_address_object_found': voter_address_results['voter_address_found'], 'voter_ballot_saved_found': False, 'google_civic_election_id': 0, } return results voter_address = voter_address_results['voter_address'] # This routine finds a ballot saved for this voter choose_election_results = choose_election_from_existing_data( voter_device_link, google_civic_election_id, voter_address) status += " " + choose_election_results['status'] results = { 'status': status, 'success': choose_election_results['success'], 'voter_device_id': voter_device_id, 'voter_device_link_found': True, 'voter_address_object_found': voter_address_results['voter_address_found'], 'voter_ballot_saved_found': choose_election_results['voter_ballot_saved_found'], 'google_civic_election_id': choose_election_results['google_civic_election_id'], } return results
def twitter_sign_in_request_voter_info_for_api(voter_device_id, return_url, switch_accounts_if_needed=True): """ twitterSignInRequestVoterInfo When here, the incoming voter_device_id should already be authenticated :param voter_device_id: :param return_url: Where to return the browser when sign in process is complete :param switch_accounts_if_needed: :return: """ twitter_handle = '' twitter_handle_found = False tweepy_user_object = None twitter_user_object_found = False voter_info_retrieved = False switch_accounts = False # Get voter_id from the voter_device_id results = is_voter_device_id_valid(voter_device_id) if not results['success']: results = { 'success': False, 'status': "VALID_VOTER_DEVICE_ID_MISSING", 'voter_device_id': voter_device_id, 'twitter_handle': twitter_handle, 'twitter_handle_found': twitter_handle_found, 'voter_info_retrieved': voter_info_retrieved, 'switch_accounts': switch_accounts, 'return_url': return_url, } return results voter_manager = VoterManager() results = voter_manager.retrieve_voter_from_voter_device_id( voter_device_id) if not positive_value_exists(results['voter_found']): results = { 'status': "VALID_VOTER_MISSING", 'success': False, 'voter_device_id': voter_device_id, 'twitter_handle': twitter_handle, 'twitter_handle_found': twitter_handle_found, 'voter_info_retrieved': voter_info_retrieved, 'switch_accounts': switch_accounts, 'return_url': return_url, } return results voter = results['voter'] auth = tweepy.OAuthHandler(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET) auth.set_access_token(voter.twitter_access_token, voter.twitter_access_secret) api = tweepy.API(auth) try: tweepy_user_object = api.me() twitter_json = tweepy_user_object._json success = True status = 'TWITTER_SIGN_IN_REQUEST_VOTER_INFO_SUCCESSFUL' twitter_handle = tweepy_user_object.screen_name twitter_handle_found = True twitter_user_object_found = True except tweepy.RateLimitError: success = False status = 'TWITTER_SIGN_IN_REQUEST_VOTER_INFO_RATE_LIMIT_ERROR' except tweepy.error.TweepError as error_instance: success = False status = 'TWITTER_SIGN_IN_REQUEST_VOTER_INFO_TWEEPY_ERROR: ' error_tuple = error_instance.args for error_dict in error_tuple: for one_error in error_dict: status += '[' + one_error['message'] + '] ' if twitter_user_object_found: # We need to deal with these cases # 1) Does account already exist? results = voter_manager.retrieve_voter_by_twitter_id( tweepy_user_object.id) if results['voter_found'] and switch_accounts_if_needed: voter_found_with_twitter_id = results['voter'] switch_accounts = True # Relink this voter_device_id to the original account voter_device_manager = VoterDeviceLinkManager() voter_device_link_results = voter_device_manager.retrieve_voter_device_link( voter_device_id) voter_device_link = voter_device_link_results['voter_device_link'] update_voter_device_link_results = voter_device_manager.update_voter_device_link( voter_device_link, voter_found_with_twitter_id) if update_voter_device_link_results['voter_device_link_updated']: # Transfer access token and secret voter_found_with_twitter_id.twitter_access_token = voter.twitter_access_token voter_found_with_twitter_id.twitter_access_secret = voter.twitter_access_secret voter_found_with_twitter_id.save() status += "TWITTER_SIGN_IN-ALREADY_LINKED_TO_OTHER_ACCOUNT-TRANSFERRED " success = True save_user_results = voter_manager.save_twitter_user_values( voter_found_with_twitter_id, tweepy_user_object) if save_user_results['success']: voter_info_retrieved = True status += save_user_results['status'] else: status = "TWITTER_SIGN_IN-ALREADY_LINKED_TO_OTHER_ACCOUNT-COULD_NOT_TRANSFER " success = False # 2) If account doesn't exist for this person, save else: save_user_results = voter_manager.save_twitter_user_values( voter, tweepy_user_object) if save_user_results['success']: voter_info_retrieved = True results = { 'status': status, 'success': success, 'voter_device_id': voter_device_id, 'twitter_handle': twitter_handle, 'twitter_handle_found': twitter_handle_found, 'voter_info_retrieved': voter_info_retrieved, 'switch_accounts': switch_accounts, 'return_url': return_url, } return results
def facebook_sign_in_for_api(voter_device_id, facebook_id=None, facebook_email=None): # facebookSignIn """ :param voter_device_id: :return: """ status = "" success = False # Get voter_id from the voter_device_id results = is_voter_device_id_valid(voter_device_id) if not results['success']: results = { 'success': False, 'status': "VALID_VOTER_DEVICE_ID_MISSING", 'voter_device_id': voter_device_id, 'facebook_id': facebook_id, 'facebook_email': facebook_email, } return results voter_manager = VoterManager() results = voter_manager.retrieve_voter_from_voter_device_id( voter_device_id) if not positive_value_exists(results['voter_found']): results = { 'success': False, 'status': "VALID_VOTER_MISSING", 'voter_device_id': voter_device_id, 'facebook_id': facebook_id, 'facebook_email': facebook_email, } return results voter = results['voter'] results_from_facebook_id = voter_manager.retrieve_voter_by_facebook_id( facebook_id) if positive_value_exists(results_from_facebook_id['voter_found']): voter_found_with_facebook_id = results_from_facebook_id['voter'] if voter_found_with_facebook_id.id == voter.id: # If here, the owner of the facebook_id is already the current primary voter status += "FACEBOOK_SIGN_IN-ALREADY_LINKED_TO_THIS_FACEBOOK_ACCOUNT " success = True # Only save if the email is different than what is saved if positive_value_exists(facebook_email) or facebook_email == '': results = voter_manager.save_facebook_user_values( voter, facebook_id, facebook_email) status += results['status'] success = results['success'] else: # If here, we need to merge accounts TODO # ...but for now we are simply going to switch to the earlier account and abandon # the newer account if positive_value_exists(facebook_email) or facebook_email == '': results = voter_manager.save_facebook_user_values( voter_found_with_facebook_id, facebook_id, facebook_email) status += results['status'] + ", " success = results['success'] # Relink this voter_device_id to the original account voter_device_manager = VoterDeviceLinkManager() voter_device_link_results = voter_device_manager.retrieve_voter_device_link( voter_device_id) voter_device_link = voter_device_link_results['voter_device_link'] update_voter_device_link_results = voter_device_manager.update_voter_device_link( voter_device_link, voter_found_with_facebook_id) if update_voter_device_link_results['voter_device_link_updated']: status += "FACEBOOK_SIGN_IN-ALREADY_LINKED_TO_OTHER_ACCOUNT-TRANSFERRED " success = True else: status = "FACEBOOK_SIGN_IN-ALREADY_LINKED_TO_OTHER_ACCOUNT-COULD_NOT_TRANSFER " success = False else: # An existing account linked to this facebook account was not found results = voter_manager.save_facebook_user_values( voter, facebook_id, facebook_email) status = results['status'] success = results['success'] if success: results = { 'success': True, 'status': status, 'voter_device_id': voter_device_id, 'facebook_id': facebook_id, 'facebook_email': facebook_email, } else: results = { 'success': False, 'status': status, 'voter_device_id': voter_device_id, 'facebook_id': facebook_id, 'facebook_email': facebook_email, } return results