Example #1
0
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 = results['voter']

    # Fetch the access token
    try:
        # Set up a tweepy auth handler
        auth = tweepy.OAuthHandler(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET)
        auth.request_token = {'oauth_token': voter.twitter_request_token,
                              'oauth_token_secret': voter.twitter_request_secret}
        auth.get_access_token(oauth_verifier)

        if not positive_value_exists(auth.access_token) or not positive_value_exists(auth.access_token_secret):
            # Redirect back with error
            return HttpResponseRedirect('http://localhost:3001/twitter/access_token_missing')  # TODO Convert to env var

        voter.twitter_access_token = auth.access_token
        voter.twitter_access_secret = auth.access_token_secret
        voter.save()

        # Next use the access_token and access_secret to retrieve Twitter user info
        api = tweepy.API(auth)
        tweepy_user_object = api.me()

        voter_manager.save_twitter_user_values(voter, tweepy_user_object)

    except tweepy.RateLimitError:
        success = False
        status = 'TWITTER_RATE_LIMIT_ERROR'
    except tweepy.error.TweepError as error_instance:
        success = False
        status = ''
        error_tuple = error_instance.args
        for error_dict in error_tuple:
            for one_error in error_dict:
                status += '[' + one_error['message'] + '] '

    # Redirect back, success
    return HttpResponseRedirect('http://localhost:3001/ballot')  # TODO Convert to env var
Example #2
0
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
Example #3
0
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 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 = results['voter']

    # Fetch the access token
    try:
        # Set up a tweepy auth handler
        auth = tweepy.OAuthHandler(TWITTER_CONSUMER_KEY,
                                   TWITTER_CONSUMER_SECRET)
        auth.request_token = {
            'oauth_token': voter.twitter_request_token,
            'oauth_token_secret': voter.twitter_request_secret
        }
        auth.get_access_token(oauth_verifier)

        if not positive_value_exists(
                auth.access_token) or not positive_value_exists(
                    auth.access_token_secret):
            # Redirect back with error
            return HttpResponseRedirect(
                'http://localhost:3001/twitter/access_token_missing'
            )  # TODO Convert to env var

        voter.twitter_access_token = auth.access_token
        voter.twitter_access_secret = auth.access_token_secret
        voter.save()

        # Next use the access_token and access_secret to retrieve Twitter user info
        api = tweepy.API(auth)
        tweepy_user_object = api.me()

        voter_manager.save_twitter_user_values(voter, tweepy_user_object)

    except tweepy.RateLimitError:
        success = False
        status = 'TWITTER_RATE_LIMIT_ERROR'
    except tweepy.error.TweepError as error_instance:
        success = False
        status = ''
        error_tuple = error_instance.args
        for error_dict in error_tuple:
            for one_error in error_dict:
                status += '[' + one_error['message'] + '] '

    # Redirect back, success
    return HttpResponseRedirect(
        'http://localhost:3001/ballot')  # TODO Convert to env var