Beispiel #1
0
def voter_star_status_retrieve_for_api(voter_device_id, office_id,
                                       office_we_vote_id, candidate_id,
                                       candidate_we_vote_id, measure_id,
                                       measure_we_vote_id):
    # Get voter_id from the voter_device_id so we can know who is doing the starring
    results = is_voter_device_id_valid(voter_device_id)
    if not results['success']:
        json_data = {
            'status': 'VALID_VOTER_DEVICE_ID_MISSING',
            'success': False,
            'voter_device_id': voter_device_id,
            'is_starred': False,
            'office_id': convert_to_int(office_id),
            'candidate_id': convert_to_int(candidate_id),
            'measure_id': convert_to_int(measure_id),
            'ballot_item_id': 0,
            'ballot_item_we_vote_id': '',
            'kind_of_ballot_item': '',
        }
        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):
        json_data = {
            'status': "VALID_VOTER_ID_MISSING",
            'success': False,
            'voter_device_id': voter_device_id,
            'is_starred': False,
            'office_id': convert_to_int(office_id),
            'candidate_id': convert_to_int(candidate_id),
            'measure_id': convert_to_int(measure_id),
            'ballot_item_id': 0,
            'ballot_item_we_vote_id': '',
            'kind_of_ballot_item': '',
        }
        return HttpResponse(json.dumps(json_data),
                            content_type='application/json')

    star_item_manager = StarItemManager()
    if positive_value_exists(office_id) or positive_value_exists(
            office_we_vote_id):
        contest_office_manager = ContestOfficeManager()
        # Since we can take in either office_id or office_we_vote_id, we need to retrieve the value we don't have
        if positive_value_exists(office_id):
            office_we_vote_id = contest_office_manager.fetch_contest_office_we_vote_id_from_id(
                office_id)
        elif positive_value_exists(office_we_vote_id):
            office_id = contest_office_manager.fetch_contest_office_id_from_we_vote_id(
                office_we_vote_id)

        # Zero out the unused values
        star_item_id = 0
        candidate_campaign_id = 0
        contest_measure_id = 0
        results = star_item_manager.retrieve_star_item(star_item_id, voter_id,
                                                       office_id,
                                                       candidate_campaign_id,
                                                       contest_measure_id)
        status = results['status']
        success = results['success']
        is_starred = results['is_starred']

        json_data = {
            'status': status,
            'success': success,
            'voter_device_id': voter_device_id,
            'is_starred': is_starred,
            'ballot_item_id': convert_to_int(office_id),
            'ballot_item_we_vote_id': office_we_vote_id,
            'kind_of_ballot_item': OFFICE,
            'office_id': convert_to_int(office_id),
            'candidate_id': convert_to_int(candidate_id),
            'measure_id': convert_to_int(measure_id),
        }
        return HttpResponse(json.dumps(json_data),
                            content_type='application/json')
    elif positive_value_exists(candidate_id) or positive_value_exists(
            candidate_we_vote_id):
        candidate_campaign_manager = CandidateCampaignManager()
        # Since we can take in either candidate_id or candidate_we_vote_id, we need to retrieve the value we don't have
        if positive_value_exists(candidate_id):
            candidate_we_vote_id = candidate_campaign_manager.fetch_candidate_campaign_we_vote_id_from_id(
                candidate_id)
        elif positive_value_exists(candidate_we_vote_id):
            candidate_id = candidate_campaign_manager.fetch_candidate_campaign_id_from_we_vote_id(
                candidate_we_vote_id)

        # Zero out the unused values
        star_item_id = 0
        contest_office_id = 0
        contest_measure_id = 0
        results = star_item_manager.retrieve_star_item(star_item_id, voter_id,
                                                       contest_office_id,
                                                       candidate_id,
                                                       contest_measure_id)
        status = results['status']
        success = results['success']
        is_starred = results['is_starred']

        json_data = {
            'status': status,
            'success': success,
            'voter_device_id': voter_device_id,
            'is_starred': is_starred,
            'ballot_item_id': convert_to_int(candidate_id),
            'ballot_item_we_vote_id': candidate_we_vote_id,
            'kind_of_ballot_item': CANDIDATE,
            'office_id': convert_to_int(office_id),
            'candidate_id': convert_to_int(candidate_id),
            'measure_id': convert_to_int(measure_id),
        }
        return HttpResponse(json.dumps(json_data),
                            content_type='application/json')
    elif positive_value_exists(measure_id) or positive_value_exists(
            measure_we_vote_id):
        contest_measure_manager = ContestMeasureManager()
        # Since we can take in either measure_id or measure_we_vote_id, we need to retrieve the value we don't have
        if positive_value_exists(measure_id):
            measure_we_vote_id = contest_measure_manager.fetch_contest_measure_we_vote_id_from_id(
                measure_id)
        elif positive_value_exists(measure_we_vote_id):
            measure_id = contest_measure_manager.fetch_contest_measure_id_from_we_vote_id(
                measure_we_vote_id)

        # Zero out the unused values
        star_item_id = 0
        contest_office_id = 0
        candidate_campaign_id = 0
        results = star_item_manager.retrieve_star_item(star_item_id, voter_id,
                                                       contest_office_id,
                                                       candidate_campaign_id,
                                                       measure_id)
        status = results['status']
        success = results['success']
        is_starred = results['is_starred']

        json_data = {
            'status': status,
            'success': success,
            'voter_device_id': voter_device_id,
            'is_starred': is_starred,
            'ballot_item_id': convert_to_int(measure_id),
            'ballot_item_we_vote_id': measure_we_vote_id,
            'kind_of_ballot_item': MEASURE,
            'office_id': convert_to_int(office_id),
            'candidate_id': convert_to_int(candidate_id),
            'measure_id': convert_to_int(measure_id),
        }
        return HttpResponse(json.dumps(json_data),
                            content_type='application/json')
    else:
        status = 'UNABLE_TO_SAVE-OFFICE_ID_AND_CANDIDATE_ID_AND_MEASURE_ID_MISSING'
        success = False
        is_starred = False

    json_data = {
        'status': status,
        'success': success,
        'voter_device_id': voter_device_id,
        'is_starred': is_starred,
        'office_id': convert_to_int(office_id),
        'candidate_id': convert_to_int(candidate_id),
        'measure_id': convert_to_int(measure_id),
        'ballot_item_id': 0,
        'ballot_item_we_vote_id': '',
        'kind_of_ballot_item': '',
    }
    return HttpResponse(json.dumps(json_data), content_type='application/json')
Beispiel #2
0
def voter_star_status_retrieve_for_api(voter_device_id,
                                       office_id, office_we_vote_id,
                                       candidate_id, candidate_we_vote_id,
                                       measure_id, measure_we_vote_id):
    # Get voter_id from the voter_device_id so we can know who is doing the starring
    results = is_voter_device_id_valid(voter_device_id)
    if not results['success']:
        json_data = {
            'status':                   'VALID_VOTER_DEVICE_ID_MISSING',
            'success':                  False,
            'voter_device_id':          voter_device_id,
            'is_starred':               False,
            'office_id':                convert_to_int(office_id),
            'candidate_id':             convert_to_int(candidate_id),
            'measure_id':               convert_to_int(measure_id),
            'ballot_item_id':           0,
            'ballot_item_we_vote_id':   '',
            'kind_of_ballot_item':      '',
        }
        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):
        json_data = {
            'status':                   "VALID_VOTER_ID_MISSING",
            'success':                  False,
            'voter_device_id':          voter_device_id,
            'is_starred':               False,
            'office_id':                convert_to_int(office_id),
            'candidate_id':             convert_to_int(candidate_id),
            'measure_id':               convert_to_int(measure_id),
            'ballot_item_id':           0,
            'ballot_item_we_vote_id':   '',
            'kind_of_ballot_item':      '',
        }
        return HttpResponse(json.dumps(json_data), content_type='application/json')

    star_item_manager = StarItemManager()
    if positive_value_exists(office_id) or positive_value_exists(office_we_vote_id):
        contest_office_manager = ContestOfficeManager()
        # Since we can take in either office_id or office_we_vote_id, we need to retrieve the value we don't have
        if positive_value_exists(office_id):
            office_we_vote_id = contest_office_manager.fetch_contest_office_we_vote_id_from_id(office_id)
        elif positive_value_exists(office_we_vote_id):
            office_id = contest_office_manager.fetch_contest_office_id_from_we_vote_id(office_we_vote_id)

        # Zero out the unused values
        star_item_id = 0
        candidate_campaign_id = 0
        contest_measure_id = 0
        results = star_item_manager.retrieve_star_item(star_item_id, voter_id, office_id,
                                                       candidate_campaign_id, contest_measure_id)
        status = results['status']
        success = results['success']
        is_starred = results['is_starred']

        json_data = {
            'status':                   status,
            'success':                  success,
            'voter_device_id':          voter_device_id,
            'is_starred':               is_starred,
            'ballot_item_id':           convert_to_int(office_id),
            'ballot_item_we_vote_id':   office_we_vote_id,
            'kind_of_ballot_item':      OFFICE,
            'office_id':                convert_to_int(office_id),
            'candidate_id':             convert_to_int(candidate_id),
            'measure_id':               convert_to_int(measure_id),
        }
        return HttpResponse(json.dumps(json_data), content_type='application/json')
    elif positive_value_exists(candidate_id) or positive_value_exists(candidate_we_vote_id):
        candidate_campaign_manager = CandidateCampaignManager()
        # Since we can take in either candidate_id or candidate_we_vote_id, we need to retrieve the value we don't have
        if positive_value_exists(candidate_id):
            candidate_we_vote_id = candidate_campaign_manager.fetch_candidate_campaign_we_vote_id_from_id(candidate_id)
        elif positive_value_exists(candidate_we_vote_id):
            candidate_id = candidate_campaign_manager.fetch_candidate_campaign_id_from_we_vote_id(candidate_we_vote_id)

        # Zero out the unused values
        star_item_id = 0
        contest_office_id = 0
        contest_measure_id = 0
        results = star_item_manager.retrieve_star_item(star_item_id, voter_id, contest_office_id, candidate_id,
                                                       contest_measure_id)
        status = results['status']
        success = results['success']
        is_starred = results['is_starred']

        json_data = {
            'status':                   status,
            'success':                  success,
            'voter_device_id':          voter_device_id,
            'is_starred':               is_starred,
            'ballot_item_id':           convert_to_int(candidate_id),
            'ballot_item_we_vote_id':   candidate_we_vote_id,
            'kind_of_ballot_item':      CANDIDATE,
            'office_id':                convert_to_int(office_id),
            'candidate_id':             convert_to_int(candidate_id),
            'measure_id':               convert_to_int(measure_id),
        }
        return HttpResponse(json.dumps(json_data), content_type='application/json')
    elif positive_value_exists(measure_id) or positive_value_exists(measure_we_vote_id):
        contest_measure_manager = ContestMeasureManager()
        # Since we can take in either measure_id or measure_we_vote_id, we need to retrieve the value we don't have
        if positive_value_exists(measure_id):
            measure_we_vote_id = contest_measure_manager.fetch_contest_measure_we_vote_id_from_id(measure_id)
        elif positive_value_exists(measure_we_vote_id):
            measure_id = contest_measure_manager.fetch_contest_measure_id_from_we_vote_id(measure_we_vote_id)

        # Zero out the unused values
        star_item_id = 0
        contest_office_id = 0
        candidate_campaign_id = 0
        results = star_item_manager.retrieve_star_item(star_item_id, voter_id, contest_office_id, candidate_campaign_id,
                                                       measure_id)
        status = results['status']
        success = results['success']
        is_starred = results['is_starred']

        json_data = {
            'status':                   status,
            'success':                  success,
            'voter_device_id':          voter_device_id,
            'is_starred':               is_starred,
            'ballot_item_id':           convert_to_int(measure_id),
            'ballot_item_we_vote_id':   measure_we_vote_id,
            'kind_of_ballot_item':      MEASURE,
            'office_id':                convert_to_int(office_id),
            'candidate_id':             convert_to_int(candidate_id),
            'measure_id':               convert_to_int(measure_id),
        }
        return HttpResponse(json.dumps(json_data), content_type='application/json')
    else:
        status = 'UNABLE_TO_SAVE-OFFICE_ID_AND_CANDIDATE_ID_AND_MEASURE_ID_MISSING'
        success = False
        is_starred = False

    json_data = {
        'status':                   status,
        'success':                  success,
        'voter_device_id':          voter_device_id,
        'is_starred':               is_starred,
        'office_id':                convert_to_int(office_id),
        'candidate_id':             convert_to_int(candidate_id),
        'measure_id':               convert_to_int(measure_id),
        'ballot_item_id':           0,
        'ballot_item_we_vote_id':   '',
        'kind_of_ballot_item':      '',
    }
    return HttpResponse(json.dumps(json_data), content_type='application/json')
Beispiel #3
0
def candidates_import_from_structured_json(structured_json):
    candidate_campaign_manager = CandidateCampaignManager()
    candidates_saved = 0
    candidates_updated = 0
    candidates_not_processed = 0
    for one_candidate in structured_json:
        candidate_name = one_candidate[
            'candidate_name'] if 'candidate_name' in one_candidate else ''
        we_vote_id = one_candidate[
            'we_vote_id'] if 'we_vote_id' in one_candidate else ''
        google_civic_election_id = \
            one_candidate['google_civic_election_id'] if 'google_civic_election_id' in one_candidate else ''
        ocd_division_id = one_candidate[
            'ocd_division_id'] if 'ocd_division_id' in one_candidate else ''
        contest_office_we_vote_id = \
            one_candidate['contest_office_we_vote_id'] if 'contest_office_we_vote_id' in one_candidate else ''

        # This routine imports from another We Vote server, so a contest_office_id doesn't come from import
        # Look up contest_office in this local database.
        # If we don't find a contest_office by we_vote_id, then we know the contest_office hasn't been imported
        # from another server yet, so we fail out.
        contest_office_manager = ContestOfficeManager()
        contest_office_id = contest_office_manager.fetch_contest_office_id_from_we_vote_id(
            contest_office_we_vote_id)

        if positive_value_exists(candidate_name) and positive_value_exists(google_civic_election_id) \
                and positive_value_exists(we_vote_id) and positive_value_exists(contest_office_id):
            proceed_to_update_or_create = True
        else:
            proceed_to_update_or_create = False
        if proceed_to_update_or_create:
            updated_candidate_campaign_values = {
                # Values we search against
                'google_civic_election_id':
                google_civic_election_id,
                'ocd_division_id':
                ocd_division_id,
                'contest_office_we_vote_id':
                contest_office_we_vote_id,
                'candidate_name':
                candidate_name,
                # The rest of the values
                'we_vote_id':
                we_vote_id,
                'maplight_id':
                one_candidate['maplight_id']
                if 'maplight_id' in one_candidate else None,
                'vote_smart_id':
                one_candidate['vote_smart_id']
                if 'vote_smart_id' in one_candidate else None,
                'contest_office_id':
                contest_office_id,  # Retrieved from above
                'politician_we_vote_id':
                one_candidate['politician_we_vote_id']
                if 'politician_we_vote_id' in one_candidate else '',
                'state_code':
                one_candidate['state_code']
                if 'state_code' in one_candidate else '',
                'party':
                one_candidate['party'] if 'party' in one_candidate else '',
                'order_on_ballot':
                one_candidate['order_on_ballot']
                if 'order_on_ballot' in one_candidate else 0,
                'candidate_url':
                one_candidate['candidate_url']
                if 'candidate_url' in one_candidate else '',
                'photo_url':
                one_candidate['photo_url']
                if 'photo_url' in one_candidate else '',
                'photo_url_from_maplight':
                one_candidate['photo_url_from_maplight']
                if 'photo_url_from_maplight' in one_candidate else '',
                'photo_url_from_vote_smart':
                one_candidate['photo_url_from_vote_smart']
                if 'photo_url_from_vote_smart' in one_candidate else '',
                'facebook_url':
                one_candidate['facebook_url']
                if 'facebook_url' in one_candidate else '',
                'twitter_url':
                one_candidate['twitter_url']
                if 'twitter_url' in one_candidate else '',
                'google_plus_url':
                one_candidate['google_plus_url']
                if 'google_plus_url' in one_candidate else '',
                'youtube_url':
                one_candidate['youtube_url']
                if 'youtube_url' in one_candidate else '',
                'google_civic_candidate_name':
                one_candidate['google_civic_candidate_name']
                if 'google_civic_candidate_name' in one_candidate else '',
                'candidate_email':
                one_candidate['candidate_email']
                if 'candidate_email' in one_candidate else '',
                'candidate_phone':
                one_candidate['candidate_phone']
                if 'candidate_phone' in one_candidate else '',
                'twitter_user_id':
                one_candidate['twitter_user_id']
                if 'twitter_user_id' in one_candidate else '',
                'candidate_twitter_handle':
                one_candidate['candidate_twitter_handle']
                if 'candidate_twitter_handle' in one_candidate else '',
                'twitter_name':
                one_candidate['twitter_name']
                if 'twitter_name' in one_candidate else '',
                'twitter_location':
                one_candidate['twitter_location']
                if 'twitter_location' in one_candidate else '',
                'twitter_followers_count':
                one_candidate['twitter_followers_count']
                if 'twitter_followers_count' in one_candidate else '',
                'twitter_profile_image_url_https':
                one_candidate['twitter_profile_image_url_https']
                if 'twitter_profile_image_url_https' in one_candidate else '',
                'twitter_description':
                one_candidate['twitter_description']
                if 'twitter_description' in one_candidate else '',
                'wikipedia_page_id':
                one_candidate['wikipedia_page_id']
                if 'wikipedia_page_id' in one_candidate else '',
                'wikipedia_page_title':
                one_candidate['wikipedia_page_title']
                if 'wikipedia_page_title' in one_candidate else '',
                'wikipedia_photo_url':
                one_candidate['wikipedia_photo_url']
                if 'wikipedia_photo_url' in one_candidate else '',
                'ballotpedia_page_title':
                one_candidate['ballotpedia_page_title']
                if 'ballotpedia_page_title' in one_candidate else '',
                'ballotpedia_photo_url':
                one_candidate['ballotpedia_photo_url']
                if 'ballotpedia_photo_url' in one_candidate else '',
                'ballot_guide_official_statement':
                one_candidate['ballot_guide_official_statement']
                if 'ballot_guide_official_statement' in one_candidate else '',
            }
            results = candidate_campaign_manager.update_or_create_candidate_campaign(
                we_vote_id, google_civic_election_id, ocd_division_id,
                contest_office_id, contest_office_we_vote_id, candidate_name,
                updated_candidate_campaign_values)
        else:
            candidates_not_processed += 1
            results = {
                'success': False,
                'status': 'Required value missing, cannot update or create'
            }

        if results['success']:
            if results['new_candidate_created']:
                candidates_saved += 1
            else:
                candidates_updated += 1

    candidates_results = {
        'success': True,
        'status': "CANDIDATES_IMPORT_PROCESS_COMPLETE",
        'saved': candidates_saved,
        'updated': candidates_updated,
        'not_processed': candidates_not_processed,
    }
    return candidates_results
def candidates_import_from_structured_json(structured_json):
    candidate_campaign_manager = CandidateCampaignManager()
    candidates_saved = 0
    candidates_updated = 0
    candidates_not_processed = 0
    for one_candidate in structured_json:
        candidate_name = one_candidate['candidate_name'] if 'candidate_name' in one_candidate else ''
        we_vote_id = one_candidate['we_vote_id'] if 'we_vote_id' in one_candidate else ''
        google_civic_election_id = \
            one_candidate['google_civic_election_id'] if 'google_civic_election_id' in one_candidate else ''
        ocd_division_id = one_candidate['ocd_division_id'] if 'ocd_division_id' in one_candidate else ''
        contest_office_we_vote_id = \
            one_candidate['contest_office_we_vote_id'] if 'contest_office_we_vote_id' in one_candidate else ''

        # This routine imports from another We Vote server, so a contest_office_id doesn't come from import
        # Look up contest_office in this local database.
        # If we don't find a contest_office by we_vote_id, then we know the contest_office hasn't been imported
        # from another server yet, so we fail out.
        contest_office_manager = ContestOfficeManager()
        contest_office_id = contest_office_manager.fetch_contest_office_id_from_we_vote_id(
            contest_office_we_vote_id)

        if positive_value_exists(candidate_name) and positive_value_exists(google_civic_election_id) \
                and positive_value_exists(we_vote_id) and positive_value_exists(contest_office_id):
            proceed_to_update_or_create = True
        else:
            proceed_to_update_or_create = False
        if proceed_to_update_or_create:
            updated_candidate_campaign_values = {
                # Values we search against
                'google_civic_election_id': google_civic_election_id,
                'ocd_division_id': ocd_division_id,
                'contest_office_we_vote_id': contest_office_we_vote_id,
                'candidate_name': candidate_name,
                # The rest of the values
                'we_vote_id': we_vote_id,
                'maplight_id': one_candidate['maplight_id'] if 'maplight_id' in one_candidate else None,
                'vote_smart_id': one_candidate['vote_smart_id'] if 'vote_smart_id' in one_candidate else None,
                'contest_office_id': contest_office_id,  # Retrieved from above
                'politician_we_vote_id':
                    one_candidate['politician_we_vote_id'] if 'politician_we_vote_id' in one_candidate else '',
                'state_code': one_candidate['state_code'] if 'state_code' in one_candidate else '',
                'party': one_candidate['party'] if 'party' in one_candidate else '',
                'order_on_ballot': one_candidate['order_on_ballot'] if 'order_on_ballot' in one_candidate else 0,
                'candidate_url': one_candidate['candidate_url'] if 'candidate_url' in one_candidate else '',
                'photo_url': one_candidate['photo_url'] if 'photo_url' in one_candidate else '',
                'photo_url_from_maplight':
                    one_candidate['photo_url_from_maplight'] if 'photo_url_from_maplight' in one_candidate else '',
                'photo_url_from_vote_smart':
                    one_candidate['photo_url_from_vote_smart'] if 'photo_url_from_vote_smart' in one_candidate else '',
                'facebook_url': one_candidate['facebook_url'] if 'facebook_url' in one_candidate else '',
                'twitter_url': one_candidate['twitter_url'] if 'twitter_url' in one_candidate else '',
                'google_plus_url': one_candidate['google_plus_url'] if 'google_plus_url' in one_candidate else '',
                'youtube_url': one_candidate['youtube_url'] if 'youtube_url' in one_candidate else '',
                'google_civic_candidate_name':
                    one_candidate['google_civic_candidate_name']
                    if 'google_civic_candidate_name' in one_candidate else '',
                'candidate_email': one_candidate['candidate_email'] if 'candidate_email' in one_candidate else '',
                'candidate_phone': one_candidate['candidate_phone'] if 'candidate_phone' in one_candidate else '',
                'twitter_user_id': one_candidate['twitter_user_id'] if 'twitter_user_id' in one_candidate else '',
                'candidate_twitter_handle': one_candidate['candidate_twitter_handle']
                    if 'candidate_twitter_handle' in one_candidate else '',
                'twitter_name': one_candidate['twitter_name'] if 'twitter_name' in one_candidate else '',
                'twitter_location': one_candidate['twitter_location'] if 'twitter_location' in one_candidate else '',
                'twitter_followers_count': one_candidate['twitter_followers_count']
                    if 'twitter_followers_count' in one_candidate else '',
                'twitter_profile_image_url_https': one_candidate['twitter_profile_image_url_https']
                    if 'twitter_profile_image_url_https' in one_candidate else '',
                'twitter_description': one_candidate['twitter_description']
                    if 'twitter_description' in one_candidate else '',
                'wikipedia_page_id': one_candidate['wikipedia_page_id']
                    if 'wikipedia_page_id' in one_candidate else '',
                'wikipedia_page_title': one_candidate['wikipedia_page_title']
                    if 'wikipedia_page_title' in one_candidate else '',
                'wikipedia_photo_url': one_candidate['wikipedia_photo_url']
                    if 'wikipedia_photo_url' in one_candidate else '',
                'ballotpedia_page_title': one_candidate['ballotpedia_page_title']
                    if 'ballotpedia_page_title' in one_candidate else '',
                'ballotpedia_photo_url': one_candidate['ballotpedia_photo_url']
                    if 'ballotpedia_photo_url' in one_candidate else '',
                'ballot_guide_official_statement': one_candidate['ballot_guide_official_statement']
                    if 'ballot_guide_official_statement' in one_candidate else '',
            }
            results = candidate_campaign_manager.update_or_create_candidate_campaign(
                we_vote_id, google_civic_election_id, ocd_division_id,
                contest_office_id, contest_office_we_vote_id,
                candidate_name, updated_candidate_campaign_values)
        else:
            candidates_not_processed += 1
            results = {
                'success': False,
                'status': 'Required value missing, cannot update or create'
            }

        if results['success']:
            if results['new_candidate_created']:
                candidates_saved += 1
            else:
                candidates_updated += 1

    candidates_results = {
        'success':          True,
        'status':           "CANDIDATES_IMPORT_PROCESS_COMPLETE",
        'saved':            candidates_saved,
        'updated':          candidates_updated,
        'not_processed':    candidates_not_processed,
    }
    return candidates_results
Beispiel #5
0
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)
    # else:
    # Load saved json from local file

    # messages.add_message(request, messages.INFO, "Loading CandidateCampaigns from local file")

    with open("candidate/import_data/candidate_campaigns_sample.json") as json_data:
        structured_json = json.load(json_data)

    candidate_campaign_manager = CandidateCampaignManager()
    candidates_saved = 0
    candidates_updated = 0
    candidates_not_processed = 0
    for candidate in structured_json:
        candidate_name = candidate['candidate_name'] if 'candidate_name' in candidate else ''
        we_vote_id = candidate['we_vote_id'] if 'we_vote_id' in candidate else ''
        google_civic_election_id = \
            candidate['google_civic_election_id'] if 'google_civic_election_id' in candidate else ''
        ocd_division_id = candidate['ocd_division_id'] if 'ocd_division_id' in candidate else ''
        contest_office_we_vote_id = \
            candidate['contest_office_we_vote_id'] if 'contest_office_we_vote_id' in candidate else ''

        # This routine imports from another We Vote server, so a contest_office_id doesn't come from import
        # Look it up for this local database. If we don't find it, then we know the contest_office hasn't been imported
        # from another server yet, so we fail out.
        contest_office_manager = ContestOfficeManager()
        contest_office_id = contest_office_manager.fetch_contest_office_id_from_we_vote_id(
            contest_office_we_vote_id)
        if positive_value_exists(candidate_name) and positive_value_exists(google_civic_election_id) \
                and positive_value_exists(we_vote_id) and positive_value_exists(contest_office_id):
            proceed_to_update_or_create = True
        # elif positive_value_exists(candidate_name) and positive_value_exists(google_civic_election_id) \
        #         and positive_value_exists(we_vote_id) and positive_value_exists(ocd_division_id) \
        #         and positive_value_exists(contest_office_we_vote_id):
        #     proceed_to_update_or_create = True
        else:
            proceed_to_update_or_create = False
        if proceed_to_update_or_create:
            updated_candidate_campaign_values = {
                # Values we search against
                'google_civic_election_id': google_civic_election_id,
                'ocd_division_id': ocd_division_id,
                'contest_office_we_vote_id': contest_office_we_vote_id,
                'candidate_name': candidate_name,
                # The rest of the values
                'we_vote_id': we_vote_id,
                'maplight_id': candidate['maplight_id'] if 'maplight_id' in candidate else None,
                'contest_office_id': contest_office_id,
                'politician_we_vote_id':
                    candidate['politician_we_vote_id'] if 'politician_we_vote_id' in candidate else '',
                'state_code': candidate['state_code'] if 'state_code' in candidate else '',
                'party': candidate['party'] if 'party' in candidate else '',
                'order_on_ballot': candidate['order_on_ballot'] if 'order_on_ballot' in candidate else 0,
                'candidate_url': candidate['candidate_url'] if 'candidate_url' in candidate else '',
                'photo_url': candidate['photo_url'] if 'photo_url' in candidate else '',
                'photo_url_from_maplight':
                    candidate['photo_url_from_maplight'] if 'photo_url_from_maplight' in candidate else '',
                'facebook_url': candidate['facebook_url'] if 'facebook_url' in candidate else '',
                'twitter_url': candidate['twitter_url'] if 'twitter_url' in candidate else '',
                'google_plus_url': candidate['google_plus_url'] if 'google_plus_url' in candidate else '',
                'youtube_url': candidate['youtube_url'] if 'youtube_url' in candidate else '',
                'google_civic_candidate_name':
                    candidate['google_civic_candidate_name'] if 'google_civic_candidate_name' in candidate else '',
                'candidate_email': candidate['candidate_email'] if 'candidate_email' in candidate else '',
                'candidate_phone': candidate['candidate_phone'] if 'candidate_phone' in candidate else '',
            }
            results = candidate_campaign_manager.update_or_create_candidate_campaign(
                we_vote_id, google_civic_election_id, ocd_division_id,
                contest_office_id, contest_office_we_vote_id,
                candidate_name, updated_candidate_campaign_values)
        else:
            candidates_not_processed += 1
            results = {
                'success': False,
                'status': 'Required value missing, cannot update or create'
            }

        if results['success']:
            if results['new_candidate_created']:
                candidates_saved += 1
            else:
                candidates_updated += 1
        else:
            candidates_not_processed += 1
            if candidates_not_processed < 5 and request is not None:
                messages.add_message(request, messages.ERROR,
                                     results['status'] + "candidate_name: {candidate_name}"
                                                         ", google_civic_election_id: {google_civic_election_id}"
                                                         ", we_vote_id: {we_vote_id}"
                                                         ", contest_office_id: {contest_office_id}"
                                                         ", contest_office_we_vote_id: {contest_office_we_vote_id}"
                                                         "".format(
                                         candidate_name=candidate_name,
                                         google_civic_election_id=google_civic_election_id,
                                         we_vote_id=we_vote_id,
                                         contest_office_id=contest_office_id,
                                         contest_office_we_vote_id=contest_office_we_vote_id,
                                     ))
    candidates_results = {
        'saved': candidates_saved,
        'updated': candidates_updated,
        'not_processed': candidates_not_processed,
    }
    return candidates_results
Beispiel #6
0
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)
    # else:
    # Load saved json from local file

    # messages.add_message(request, messages.INFO, "Loading CandidateCampaigns from local file")

    with open("candidate/import_data/candidate_campaigns_sample.json"
              ) as json_data:
        structured_json = json.load(json_data)

    candidate_campaign_manager = CandidateCampaignManager()
    candidates_saved = 0
    candidates_updated = 0
    candidates_not_processed = 0
    for candidate in structured_json:
        candidate_name = candidate[
            'candidate_name'] if 'candidate_name' in candidate else ''
        we_vote_id = candidate[
            'we_vote_id'] if 'we_vote_id' in candidate else ''
        google_civic_election_id = \
            candidate['google_civic_election_id'] if 'google_civic_election_id' in candidate else ''
        ocd_division_id = candidate[
            'ocd_division_id'] if 'ocd_division_id' in candidate else ''
        contest_office_we_vote_id = \
            candidate['contest_office_we_vote_id'] if 'contest_office_we_vote_id' in candidate else ''

        # This routine imports from another We Vote server, so a contest_office_id doesn't come from import
        # Look it up for this local database. If we don't find it, then we know the contest_office hasn't been imported
        # from another server yet, so we fail out.
        contest_office_manager = ContestOfficeManager()
        contest_office_id = contest_office_manager.fetch_contest_office_id_from_we_vote_id(
            contest_office_we_vote_id)
        if positive_value_exists(candidate_name) and positive_value_exists(google_civic_election_id) \
                and positive_value_exists(we_vote_id) and positive_value_exists(contest_office_id):
            proceed_to_update_or_create = True
        # elif positive_value_exists(candidate_name) and positive_value_exists(google_civic_election_id) \
        #         and positive_value_exists(we_vote_id) and positive_value_exists(ocd_division_id) \
        #         and positive_value_exists(contest_office_we_vote_id):
        #     proceed_to_update_or_create = True
        else:
            proceed_to_update_or_create = False
        if proceed_to_update_or_create:
            updated_candidate_campaign_values = {
                # Values we search against
                'google_civic_election_id':
                google_civic_election_id,
                'ocd_division_id':
                ocd_division_id,
                'contest_office_we_vote_id':
                contest_office_we_vote_id,
                'candidate_name':
                candidate_name,
                # The rest of the values
                'we_vote_id':
                we_vote_id,
                'maplight_id':
                candidate['maplight_id']
                if 'maplight_id' in candidate else None,
                'contest_office_id':
                contest_office_id,
                'politician_we_vote_id':
                candidate['politician_we_vote_id']
                if 'politician_we_vote_id' in candidate else '',
                'state_code':
                candidate['state_code'] if 'state_code' in candidate else '',
                'party':
                candidate['party'] if 'party' in candidate else '',
                'order_on_ballot':
                candidate['order_on_ballot']
                if 'order_on_ballot' in candidate else 0,
                'candidate_url':
                candidate['candidate_url']
                if 'candidate_url' in candidate else '',
                'photo_url':
                candidate['photo_url'] if 'photo_url' in candidate else '',
                'photo_url_from_maplight':
                candidate['photo_url_from_maplight']
                if 'photo_url_from_maplight' in candidate else '',
                'facebook_url':
                candidate['facebook_url']
                if 'facebook_url' in candidate else '',
                'twitter_url':
                candidate['twitter_url'] if 'twitter_url' in candidate else '',
                'google_plus_url':
                candidate['google_plus_url']
                if 'google_plus_url' in candidate else '',
                'youtube_url':
                candidate['youtube_url'] if 'youtube_url' in candidate else '',
                'google_civic_candidate_name':
                candidate['google_civic_candidate_name']
                if 'google_civic_candidate_name' in candidate else '',
                'candidate_email':
                candidate['candidate_email']
                if 'candidate_email' in candidate else '',
                'candidate_phone':
                candidate['candidate_phone']
                if 'candidate_phone' in candidate else '',
            }
            results = candidate_campaign_manager.update_or_create_candidate_campaign(
                we_vote_id, google_civic_election_id, ocd_division_id,
                contest_office_id, contest_office_we_vote_id, candidate_name,
                updated_candidate_campaign_values)
        else:
            candidates_not_processed += 1
            results = {
                'success': False,
                'status': 'Required value missing, cannot update or create'
            }

        if results['success']:
            if results['new_candidate_created']:
                candidates_saved += 1
            else:
                candidates_updated += 1
        else:
            candidates_not_processed += 1
            if candidates_not_processed < 5 and request is not None:
                messages.add_message(
                    request, messages.ERROR,
                    results['status'] + "candidate_name: {candidate_name}"
                    ", google_civic_election_id: {google_civic_election_id}"
                    ", we_vote_id: {we_vote_id}"
                    ", contest_office_id: {contest_office_id}"
                    ", contest_office_we_vote_id: {contest_office_we_vote_id}"
                    "".format(
                        candidate_name=candidate_name,
                        google_civic_election_id=google_civic_election_id,
                        we_vote_id=we_vote_id,
                        contest_office_id=contest_office_id,
                        contest_office_we_vote_id=contest_office_we_vote_id,
                    ))
    candidates_results = {
        'saved': candidates_saved,
        'updated': candidates_updated,
        'not_processed': candidates_not_processed,
    }
    return candidates_results