本文整理汇总了Python中candidate.models.CandidateCampaignManager类的典型用法代码示例。如果您正苦于以下问题:Python CandidateCampaignManager类的具体用法?Python CandidateCampaignManager怎么用?Python CandidateCampaignManager使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CandidateCampaignManager类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: refresh_twitter_candidate_details
def refresh_twitter_candidate_details(candidate_campaign):
candidate_campaign_manager = CandidateCampaignManager()
if not candidate_campaign:
status = "TWITTER_CANDIDATE_DETAILS_NOT_RETRIEVED-CANDIDATE_MISSING"
results = {
'success': False,
'status': status,
}
return results
if candidate_campaign.candidate_twitter_handle:
status = "TWITTER_CANDIDATE_DETAILS-REACHING_OUT_TO_TWITTER"
results = retrieve_twitter_user_info(candidate_campaign.candidate_twitter_handle)
if results['success']:
status = "TWITTER_CANDIDATE_DETAILS_RETRIEVED_FROM_TWITTER"
save_results = candidate_campaign_manager.update_candidate_twitter_details(
candidate_campaign, results['twitter_json'])
else:
status = "TWITTER_CANDIDATE_DETAILS-CLEARING_DETAILS"
save_results = candidate_campaign_manager.clear_candidate_twitter_details(candidate_campaign)
results = {
'success': True,
'status': status,
}
return results
示例2: scrape_and_save_social_media_for_candidates_in_one_election
def scrape_and_save_social_media_for_candidates_in_one_election(google_civic_election_id=0):
facebook_pages_found = 0
twitter_handles_found = 0
force_retrieve = False
status = ""
google_civic_election_id = convert_to_int(google_civic_election_id)
candidate_manager = CandidateCampaignManager()
candidate_list_manager = CandidateCampaignListManager()
return_list_of_objects = True
results = candidate_list_manager.retrieve_all_candidates_for_upcoming_election(google_civic_election_id,
return_list_of_objects)
status += results['status']
if results['success']:
candidate_list = results['candidate_list_objects']
else:
candidate_list = []
for candidate in candidate_list:
twitter_handle = False
facebook_page = False
if not candidate.candidate_url:
continue
if (not positive_value_exists(candidate.candidate_twitter_handle)) or force_retrieve:
scrape_results = scrape_social_media_from_one_site(candidate.candidate_url)
# Only include a change if we have a new value (do not try to save blank value)
if scrape_results['twitter_handle_found'] and positive_value_exists(scrape_results['twitter_handle']):
twitter_handle = scrape_results['twitter_handle']
twitter_handles_found += 1
if scrape_results['facebook_page_found'] and positive_value_exists(scrape_results['facebook_page']):
facebook_page = scrape_results['facebook_page']
facebook_pages_found += 1
save_results = candidate_manager.update_candidate_social_media(candidate, twitter_handle, facebook_page)
# ######################################
# We refresh the Twitter information in another function
status = "ORGANIZATION_SOCIAL_MEDIA_RETRIEVED"
results = {
'success': True,
'status': status,
'twitter_handles_found': twitter_handles_found,
'facebook_pages_found': facebook_pages_found,
}
return results
示例3: refresh_twitter_candidate_details_view
def refresh_twitter_candidate_details_view(request, candidate_id):
authority_required = {'verified_volunteer'} # admin, verified_volunteer
if not voter_has_authority(request, authority_required):
return redirect_to_sign_in_page(request, authority_required)
candidate_manager = CandidateCampaignManager()
results = candidate_manager.retrieve_candidate_campaign(candidate_id)
if not results['candidate_campaign_found']:
messages.add_message(request, messages.INFO, results['status'])
return HttpResponseRedirect(reverse('candidate:candidate_edit', args=(candidate_id,)))
candidate_campaign = results['candidate_campaign']
results = refresh_twitter_candidate_details(candidate_campaign)
return HttpResponseRedirect(reverse('candidate:candidate_edit', args=(candidate_id,)))
示例4: import_one_candidate_ratings_view
def import_one_candidate_ratings_view(request, vote_smart_candidate_id):
one_group_results = retrieve_vote_smart_ratings_by_candidate_into_local_db(vote_smart_candidate_id)
if one_group_results['success']:
messages.add_message(request, messages.INFO, "Ratings for one candidate retrieved. ")
else:
messages.add_message(request, messages.ERROR, "Ratings for one candidate NOT retrieved. "
"(error: {error_message})"
"".format(error_message=one_group_results['status']))
candidate_manager = CandidateCampaignManager()
results = candidate_manager.retrieve_candidate_campaign_from_vote_smart_id(vote_smart_candidate_id)
if results['candidate_campaign_found']:
candidate = results['candidate_campaign']
candidate_campaign_id = candidate.id
return HttpResponseRedirect(reverse('candidate:candidate_edit', args=(candidate_campaign_id,)))
else:
return HttpResponseRedirect(reverse('candidate:candidate_list', args=()))
示例5: ballot_item_we_vote_id
def ballot_item_we_vote_id(self):
if self.candidate_campaign_we_vote_id:
return self.candidate_campaign_we_vote_id
elif self.contest_office_we_vote_id:
return self.contest_office_we_vote_id
elif self.contest_measure_we_vote_id:
return self.contest_measure_we_vote_id
elif self.candidate_campaign_id:
candidate_campaign_manager = CandidateCampaignManager()
return candidate_campaign_manager.fetch_candidate_campaign_we_vote_id_from_id(self.candidate_campaign_id)
elif self.contest_measure_id:
contest_measure_manager = ContestMeasureManager()
return contest_measure_manager.fetch_contest_measure_we_vote_id_from_id(self.contest_measure_id)
elif self.contest_office_id:
contest_office_manager = ContestOfficeManager()
return contest_office_manager.fetch_contest_office_we_vote_id_from_id(self.contest_office_id)
else:
return 'not_found'
示例6: import_one_politician_ratings_view
def import_one_politician_ratings_view(request, vote_smart_candidate_id): # TODO DALE update to politician
authority_required = {'admin'} # admin, verified_volunteer
if not voter_has_authority(request, authority_required):
return redirect_to_sign_in_page(request, authority_required)
# retrieve_vote_smart_ratings_for_candidate_into_local_db can be used for both We Vote candidate or politician
one_group_results = retrieve_vote_smart_ratings_for_candidate_into_local_db(vote_smart_candidate_id)
if one_group_results['success']:
messages.add_message(request, messages.INFO, "Ratings for one candidate retrieved. ")
else:
messages.add_message(request, messages.ERROR, "Ratings for one candidate NOT retrieved. "
"(error: {error_message})"
"".format(error_message=one_group_results['status']))
candidate_manager = CandidateCampaignManager()
results = candidate_manager.retrieve_candidate_campaign_from_vote_smart_id(vote_smart_candidate_id)
if results['candidate_campaign_found']:
candidate = results['candidate_campaign']
candidate_campaign_id = candidate.id
return HttpResponseRedirect(reverse('candidate:candidate_edit', args=(candidate_campaign_id,)))
else:
return HttpResponseRedirect(reverse('candidate:candidate_list', args=()))
示例7: positions_public_count_for_candidate_campaign
def positions_public_count_for_candidate_campaign(candidate_id, candidate_we_vote_id, stance_we_are_looking_for):
"""
We want to return a JSON file with the number of orgs and public figures who support
this particular candidate's campaign
"""
# This implementation is built to make only two database calls. All other calculations are done here in the
# application layer
position_list_manager = PositionListManager()
all_positions_count_for_candidate_campaign = \
position_list_manager.retrieve_public_positions_count_for_candidate_campaign(
candidate_id,
candidate_we_vote_id,
stance_we_are_looking_for)
if 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)
json_data = {
'status': 'SUCCESSFUL_RETRIEVE_OF_PUBLIC_POSITION_COUNT_RE_CANDIDATE',
'success': True,
'count': all_positions_count_for_candidate_campaign,
'ballot_item_id': convert_to_int(candidate_id),
'ballot_item_we_vote_id': candidate_we_vote_id,
'kind_of_ballot_item': CANDIDATE,
}
results = {
'json_data': json_data,
}
return results
示例8: transfer_maplight_data_to_we_vote_tables
def transfer_maplight_data_to_we_vote_tables(request):
# TODO We need to perhaps set up a table for these mappings that volunteers can add to?
# We need a plan for how volunteers can help us add to these mappings
# One possibility -- ask volunteers to update this Google Sheet, then write a csv importer:
# https://docs.google.com/spreadsheets/d/1havD7GCxmBhi-zLLMdOpSJlU_DtBjvb5IJNiXgno9Bk/edit#gid=0
politician_name_mapping_list = []
one_mapping = {
"google_civic_name": "Betty T. Yee",
"maplight_display_name": "Betty Yee",
"maplight_original_name": "Betty T Yee",
}
politician_name_mapping_list.append(one_mapping)
one_mapping = {
"google_civic_name": "Edmund G. \"Jerry\" Brown",
"maplight_display_name": "Jerry Brown",
"maplight_original_name": "",
}
politician_name_mapping_list.append(one_mapping)
candidate_campaign_manager = CandidateCampaignManager()
maplight_candidates_current_query = MapLightCandidate.objects.all()
for one_candidate_from_maplight_table in maplight_candidates_current_query:
found_by_id = False
# Try to find a matching candidate
results = candidate_campaign_manager.retrieve_candidate_campaign_from_id_maplight(
one_candidate_from_maplight_table.candidate_id)
if not results['success']:
logger.warn(u"Candidate NOT found by MapLight id: {name}".format(
name=one_candidate_from_maplight_table.candidate_id
))
results = candidate_campaign_manager.retrieve_candidate_campaign_from_candidate_name(
one_candidate_from_maplight_table.display_name)
if not results['success']:
logger.warn(u"Candidate NOT found by display_name: {name}".format(
name=one_candidate_from_maplight_table.display_name
))
results = candidate_campaign_manager.retrieve_candidate_campaign_from_candidate_name(
one_candidate_from_maplight_table.original_name)
if not results['success']:
logger.warn(u"Candidate NOT found by original_name: {name}".format(
name=one_candidate_from_maplight_table.original_name
))
one_mapping_google_civic_name = ''
for one_mapping_found in politician_name_mapping_list:
if positive_value_exists(one_mapping_found['maplight_display_name']) \
and one_mapping_found['maplight_display_name'] == \
one_candidate_from_maplight_table.display_name:
one_mapping_google_civic_name = one_mapping_found['google_civic_name']
break
if positive_value_exists(one_mapping_google_civic_name):
results = candidate_campaign_manager.retrieve_candidate_campaign_from_candidate_name(
one_mapping_google_civic_name)
if not results['success'] or not positive_value_exists(one_mapping_google_civic_name):
logger.warn(u"Candidate NOT found by mapping to google_civic name: {name}".format(
name=one_mapping_google_civic_name
))
continue # Go to the next candidate
candidate_campaign_on_stage = results['candidate_campaign']
# Just in case the logic above let us through to here accidentally without a candidate_name value, don't proceed
if not positive_value_exists(candidate_campaign_on_stage.candidate_name):
continue
logger.debug(u"Candidate {name} found".format(
name=candidate_campaign_on_stage.candidate_name
))
try:
# Tie the maplight id to our record
if not found_by_id:
candidate_campaign_on_stage.id_maplight = one_candidate_from_maplight_table.candidate_id
# Bring over the photo
candidate_campaign_on_stage.photo_url_from_maplight = one_candidate_from_maplight_table.photo
# We can bring over other data as needed, like gender for example
candidate_campaign_on_stage.save()
except Exception as e:
handle_record_not_saved_exception(e, logger=logger)
messages.add_message(request, messages.INFO, 'MapLight data woven into We Vote tables.')
return HttpResponseRedirect(reverse('import_export:import_export_index', args=()))
示例9: toggle_voter_starred_item
def toggle_voter_starred_item(
self, voter_id, star_status, candidate_campaign_id=0, contest_office_id=0, contest_measure_id=0,
contest_office_we_vote_id='', candidate_campaign_we_vote_id='', contest_measure_we_vote_id=''):
# Does a star_item entry exist from this voter already exist?
star_item_manager = StarItemManager()
star_item_id = 0
results = star_item_manager.retrieve_star_item(
star_item_id, voter_id,
contest_office_id, candidate_campaign_id, contest_measure_id)
star_item_on_stage_found = False
star_item_on_stage_id = 0
star_item_on_stage = StarItem()
if results['star_item_found']:
star_item_on_stage = results['star_item']
# Update this star_item entry with new values - we do not delete because we might be able to use
try:
star_item_on_stage.star_status = star_status
# We don't need to update date_last_changed here because set set auto_now=True in the field
star_item_on_stage.save()
star_item_on_stage_id = star_item_on_stage.id
star_item_on_stage_found = True
status = 'UPDATE ' + star_status
except Exception as e:
status = 'FAILED_TO_UPDATE ' + star_status
handle_record_not_saved_exception(e, logger=logger, exception_message_optional=status)
elif results['MultipleObjectsReturned']:
logger.warn("star_item: delete all but one and take it over?")
status = 'TOGGLE_ITEM_STARRED MultipleObjectsReturned ' + star_status
elif results['DoesNotExist']:
try:
# Create new star_item entry
if candidate_campaign_id and not candidate_campaign_we_vote_id:
candidate_campaign_manager = CandidateCampaignManager()
candidate_campaign_we_vote_id = \
candidate_campaign_manager.fetch_candidate_campaign_we_vote_id_from_id(candidate_campaign_id)
if contest_measure_id and not contest_measure_we_vote_id:
contest_measure_manager = ContestMeasureManager()
contest_measure_we_vote_id = contest_measure_manager.fetch_contest_measure_we_vote_id_from_id(
contest_measure_id)
if contest_office_id and not contest_office_we_vote_id:
contest_office_manager = ContestOfficeManager()
contest_office_we_vote_id = contest_office_manager.fetch_contest_office_we_vote_id_from_id(
contest_office_id)
# NOTE: For speed purposes, we are not validating the existence of the items being starred
# although we could if the we_vote_id is not returned.
star_item_on_stage = StarItem(
voter_id=voter_id,
candidate_campaign_id=candidate_campaign_id,
candidate_campaign_we_vote_id=candidate_campaign_we_vote_id,
contest_office_id=contest_office_id,
contest_office_we_vote_id=contest_office_we_vote_id,
contest_measure_id=contest_measure_id,
contest_measure_we_vote_id=contest_measure_we_vote_id,
star_status=star_status,
# We don't need to update date_last_changed here because set set auto_now=True in the field
)
star_item_on_stage.save()
star_item_on_stage_id = star_item_on_stage.id
star_item_on_stage_found = True
status = 'CREATE ' + star_status
except Exception as e:
status = 'FAILED_TO_UPDATE ' + star_status
handle_record_not_saved_exception(e, logger=logger, exception_message_optional=status)
else:
status = results['status']
results = {
'success': True if star_item_on_stage_found else False,
'status': status,
'star_item_found': star_item_on_stage_found,
'star_item_id': star_item_on_stage_id,
'star_item': star_item_on_stage,
}
return results
示例10: transfer_vote_smart_ratings_to_positions_for_candidate
def transfer_vote_smart_ratings_to_positions_for_candidate(candidate_campaign_id):
candidate_manager = CandidateCampaignManager()
candidate_results = candidate_manager.retrieve_candidate_campaign_from_id(candidate_campaign_id)
if candidate_results['candidate_campaign_found']:
# Working with Vote Smart data
candidate_campaign = candidate_results['candidate_campaign']
if not positive_value_exists(candidate_campaign.vote_smart_id):
status = "VOTE_SMART_ID_HAS_NOT_BEEN_RETRIEVED_YET_FOR_THIS_CANDIDATE: " \
"{candidate_campaign_id}".format(candidate_campaign_id=candidate_campaign_id)
success = False
results = {
'status': status,
'success': success,
}
return results
else:
try:
rating_list_query = VoteSmartRatingOneCandidate.objects.order_by('-timeSpan') # Desc order
rating_list = rating_list_query.filter(candidateId=candidate_campaign.vote_smart_id)
except Exception as error_instance:
# Catch the error message coming back from Vote Smart and pass it in the status
error_message = error_instance.args
status = "EXCEPTION_RAISED: {error_message}".format(error_message=error_message)
success = False
results = {
'status': status,
'success': success,
}
return results
ratings_status = ""
position_manager = PositionEnteredManager()
special_interest_group_manager = VoteSmartSpecialInterestGroupManager()
for one_candidate_rating in rating_list:
# Make sure we have all of the required variables
if not one_candidate_rating.sigId:
ratings_status += "MISSING_SPECIAL_INTEREST_GROUP_ID-{ratingId} * " \
"".format(ratingId=one_candidate_rating.ratingId)
continue
# Make sure an organization exists and is updated with Vote Smart info
update_results = special_interest_group_manager.update_or_create_we_vote_organization(
one_candidate_rating.sigId)
if not update_results['organization_found']:
# TRY AGAIN: Reach out to Vote Smart and try to retrieve this special interest group by sigId
one_group_results = retrieve_vote_smart_special_interest_group_into_local_db(one_candidate_rating.sigId)
if one_group_results['success']:
update_results = special_interest_group_manager.update_or_create_we_vote_organization(
one_candidate_rating.sigId)
if not update_results['organization_found']:
ratings_status += "COULD_NOT_FIND_OR_SAVE_NEW_SIG-{sigId}-{status} * " \
"".format(sigId=one_candidate_rating.sigId,
status=update_results['status'])
continue
else:
we_vote_organization = update_results['organization']
# Check to see if a position already exists
# TODO DALE Note: we need to consider searching with a time span variable
# (in addition to just org and candidate identifiers) since I believe
# Google Civic gives a person a new candidate campaign ID each election,
# while Vote Smart uses the same candidateId from year to year
organization_position_results = position_manager.retrieve_organization_candidate_campaign_position(
we_vote_organization.id, candidate_campaign_id)
if positive_value_exists(organization_position_results['position_found']):
# For now, we only want to create positions that don't exist
continue
else:
position_results = position_manager.update_or_create_position(
position_id=0,
position_we_vote_id=False,
organization_we_vote_id=we_vote_organization.we_vote_id,
public_figure_we_vote_id=False,
voter_we_vote_id=False,
google_civic_election_id=False,
ballot_item_display_name=candidate_campaign.candidate_name,
office_we_vote_id=False,
candidate_we_vote_id=candidate_campaign.we_vote_id,
measure_we_vote_id=False,
stance=PERCENT_RATING,
statement_text=one_candidate_rating.ratingText,
statement_html=False,
more_info_url=False,
vote_smart_time_span=one_candidate_rating.timeSpan,
vote_smart_rating_id=one_candidate_rating.ratingId,
vote_smart_rating=one_candidate_rating.rating,
vote_smart_rating_name=one_candidate_rating.ratingName,
)
if not positive_value_exists(position_results['success']):
ratings_status += "COULD_NOT_CREATE_POSITION-{sigId}-{status} * " \
"".format(sigId=one_candidate_rating.sigId,
status=position_results['status'])
success = True
status = "TRANSFER_PROCESS_COMPLETED: " + ratings_status
#.........这里部分代码省略.........
示例11: organization_position_new_view
def organization_position_new_view(request, organization_id):
authority_required = {'verified_volunteer'} # admin, verified_volunteer
authority_results = retrieve_voter_authority(request)
if not voter_has_authority(request, authority_required, authority_results):
return redirect_to_sign_in_page(request, authority_required)
google_civic_election_id = request.GET.get('google_civic_election_id', 0)
candidate_we_vote_id = request.GET.get('candidate_we_vote_id', False)
# Take in some incoming values
candidate_not_found = request.GET.get('candidate_not_found', False)
stance = request.GET.get('stance', SUPPORT) # Set a default if stance comes in empty
statement_text = request.GET.get('statement_text', '') # Set a default if stance comes in empty
more_info_url = request.GET.get('more_info_url', '')
# We pass candidate_we_vote_id to this page to pre-populate the form
candidate_campaign_id = 0
if positive_value_exists(candidate_we_vote_id):
candidate_campaign_manager = CandidateCampaignManager()
results = candidate_campaign_manager.retrieve_candidate_campaign_from_we_vote_id(candidate_we_vote_id)
if results['candidate_campaign_found']:
candidate_campaign = results['candidate_campaign']
candidate_campaign_id = candidate_campaign.id
messages_on_stage = get_messages(request)
organization_id = convert_to_int(organization_id)
all_is_well = True
organization_on_stage_found = False
organization_on_stage = Organization()
try:
organization_on_stage = Organization.objects.get(id=organization_id)
organization_on_stage_found = True
except Organization.MultipleObjectsReturned as e:
handle_record_found_more_than_one_exception(e, logger=logger)
except Organization.DoesNotExist:
# This is fine, create new
pass
if not organization_on_stage_found:
messages.add_message(request, messages.INFO,
'Could not find organization when trying to create a new position.')
return HttpResponseRedirect(reverse('organization:organization_position_list', args=([organization_id])))
# Prepare a drop down of candidates competing in this election
candidate_campaign_list = CandidateCampaignListManager()
candidate_campaigns_for_this_election_list = []
if positive_value_exists(google_civic_election_id):
results = candidate_campaign_list.retrieve_all_candidates_for_upcoming_election(google_civic_election_id, True)
if results['candidate_list_found']:
candidate_campaigns_for_this_election_list = results['candidate_list_objects']
else:
candidate_campaigns_for_this_election_list \
= candidate_campaign_list.retrieve_candidate_campaigns_from_all_elections_list()
try:
organization_position_list = PositionEntered.objects.order_by('stance')
organization_position_list = organization_position_list.filter(organization_id=organization_id)
if positive_value_exists(google_civic_election_id):
organization_position_list = organization_position_list.filter(
google_civic_election_id=google_civic_election_id)
organization_position_list = organization_position_list.order_by(
'google_civic_election_id', '-vote_smart_time_span')
if len(organization_position_list):
organization_position_list_found = True
except Exception as e:
organization_position_list = []
if all_is_well:
election_list = Election.objects.order_by('-election_day_text')
template_values = {
'candidate_campaigns_for_this_election_list': candidate_campaigns_for_this_election_list,
'candidate_campaign_id': candidate_campaign_id,
'messages_on_stage': messages_on_stage,
'organization': organization_on_stage,
'organization_position_candidate_campaign_id': 0,
'possible_stances_list': ORGANIZATION_STANCE_CHOICES,
'stance_selected': stance,
'election_list': election_list,
'google_civic_election_id': google_civic_election_id,
'organization_position_list': organization_position_list,
'voter_authority': authority_results,
# Incoming values from error state
'candidate_not_found': candidate_not_found,
'stance': stance,
'statement_text': statement_text,
'more_info_url': more_info_url,
}
return render(request, 'organization/organization_position_edit.html', template_values)
示例12: position_list_for_ballot_item_for_api
def position_list_for_ballot_item_for_api(voter_device_id, # positionListForBallotItem
office_id, office_we_vote_id,
candidate_id, candidate_we_vote_id,
measure_id, measure_we_vote_id,
stance_we_are_looking_for=ANY_STANCE,
show_positions_this_voter_follows=True):
"""
We want to return a JSON file with the position identifiers from orgs, friends and public figures the voter follows
This list of information is used to retrieve the detailed information
"""
position_manager = PositionEnteredManager()
# Get voter_id from the voter_device_id so we can know who is supporting/opposing
results = is_voter_device_id_valid(voter_device_id)
if not results['success']:
position_list = []
json_data = {
'status': 'VALID_VOTER_DEVICE_ID_MISSING',
'success': False,
'count': 0,
'kind_of_ballot_item': "UNKNOWN",
'ballot_item_id': 0,
'position_list': position_list,
}
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):
position_list = []
json_data = {
'status': "VALID_VOTER_ID_MISSING ",
'success': False,
'count': 0,
'kind_of_ballot_item': "UNKNOWN",
'ballot_item_id': 0,
'position_list': position_list,
}
return HttpResponse(json.dumps(json_data), content_type='application/json')
position_list_manager = PositionListManager()
ballot_item_found = False
if positive_value_exists(candidate_id) or positive_value_exists(candidate_we_vote_id):
all_positions_list = position_list_manager.retrieve_all_positions_for_candidate_campaign(
candidate_id, candidate_we_vote_id, stance_we_are_looking_for)
kind_of_ballot_item = CANDIDATE
# Since we want to return the id and we_vote_id, and we don't know for sure that there are any positions
# for this ballot_item, we retrieve the following so we can get the id and we_vote_id (per the request of
# the WebApp team)
candidate_campaign_manager = CandidateCampaignManager()
if positive_value_exists(candidate_id):
results = candidate_campaign_manager.retrieve_candidate_campaign_from_id(candidate_id)
else:
results = candidate_campaign_manager.retrieve_candidate_campaign_from_we_vote_id(candidate_we_vote_id)
if results['candidate_campaign_found']:
candidate_campaign = results['candidate_campaign']
ballot_item_id = candidate_campaign.id
ballot_item_we_vote_id = candidate_campaign.we_vote_id
ballot_item_found = True
else:
ballot_item_id = candidate_id
ballot_item_we_vote_id = candidate_we_vote_id
elif positive_value_exists(measure_id) or positive_value_exists(measure_we_vote_id):
all_positions_list = position_list_manager.retrieve_all_positions_for_contest_measure(
measure_id, measure_we_vote_id, stance_we_are_looking_for)
kind_of_ballot_item = MEASURE
# Since we want to return the id and we_vote_id, and we don't know for sure that there are any positions
# for this ballot_item, we retrieve the following so we can get the id and we_vote_id (per the request of
# the WebApp team)
contest_measure_manager = ContestMeasureManager()
if positive_value_exists(measure_id):
results = contest_measure_manager.retrieve_contest_measure_from_id(measure_id)
else:
results = contest_measure_manager.retrieve_contest_measure_from_we_vote_id(measure_we_vote_id)
if results['contest_measure_found']:
contest_measure = results['contest_measure']
ballot_item_id = contest_measure.id
ballot_item_we_vote_id = contest_measure.we_vote_id
ballot_item_found = True
else:
ballot_item_id = measure_id
ballot_item_we_vote_id = measure_we_vote_id
elif positive_value_exists(office_id) or positive_value_exists(office_we_vote_id):
all_positions_list = position_list_manager.retrieve_all_positions_for_contest_office(
office_id, office_we_vote_id, stance_we_are_looking_for)
kind_of_ballot_item = OFFICE
# Since we want to return the id and we_vote_id, and we don't know for sure that there are any positions
# for this ballot_item, we retrieve the following so we can get the id and we_vote_id (per the request of
# the WebApp team)
contest_office_manager = ContestOfficeManager()
if positive_value_exists(office_id):
results = contest_office_manager.retrieve_contest_office_from_id(office_id)
else:
results = contest_office_manager.retrieve_contest_office_from_we_vote_id(office_we_vote_id)
if results['contest_office_found']:
contest_office = results['contest_office']
#.........这里部分代码省略.........
示例13: quick_info_edit_process_view
#.........这里部分代码省略.........
ready_to_save = True
# If entering text specific to this ballot item
elif not positive_value_exists(use_master_entry) or positive_value_exists(use_unique_text):
if not positive_value_exists(language):
messages.add_message(request, messages.ERROR, "Please choose a language for your new entry.")
ready_to_save = False
elif not (positive_value_exists(info_text) or positive_value_exists(info_html)):
messages.add_message(request, messages.ERROR,
"Please enter the text/html information about this ballot item.")
ready_to_save = False
elif not positive_value_exists(more_info_url):
messages.add_message(request, messages.ERROR, "Please enter the source URL for this description.")
ready_to_save = False
else:
ready_to_save = True
else:
messages.add_message(request, messages.ERROR, "More information needed to save this entry.")
ready_to_save = False
if not ready_to_save:
# Could we also just call the view directly with the request, instead of redirecting the browser?
if positive_value_exists(quick_info_id):
return quick_info_edit_view(request, quick_info_id)
# return HttpResponseRedirect(reverse('quick_info:quick_info_edit', args=()) + url_variables)
else:
return quick_info_new_view(request)
# return HttpResponseRedirect(reverse('quick_info:quick_info_new', args=()) + url_variables)
# Now that we know we are ready to save, we want to wipe out the values we don't want to save
if positive_value_exists(use_master_entry):
info_html = ""
info_text = ""
more_info_url = ""
more_info_credit = NOT_SPECIFIED
else:
quick_info_master_we_vote_id = ""
# Figure out what text to use for the Ballot Item Label
if not positive_value_exists(ballot_item_display_name):
if positive_value_exists(contest_office_we_vote_id):
contest_office_manager = ContestOfficeManager()
results = contest_office_manager.retrieve_contest_office_from_we_vote_id(contest_office_we_vote_id)
if results['success']:
contest_office = results['contest_office']
ballot_item_display_name = contest_office.office_name
else:
ballot_item_display_name = ''
elif positive_value_exists(candidate_campaign_we_vote_id):
candidate_campaign_manager = CandidateCampaignManager()
results = candidate_campaign_manager.retrieve_candidate_campaign_from_we_vote_id(
candidate_campaign_we_vote_id)
if results['success']:
candidate_campaign = results['candidate_campaign']
ballot_item_display_name = candidate_campaign.candidate_name
else:
ballot_item_display_name = ''
# if positive_value_exists(politician_we_vote_id):
# ballot_item_display_name = ''
elif positive_value_exists(contest_measure_we_vote_id):
contest_measure_manager = ContestMeasureManager()
results = contest_measure_manager.retrieve_contest_measure_from_we_vote_id(contest_measure_we_vote_id)
if results['success']:
contest_measure = results['contest_measure']
ballot_item_display_name = contest_measure.measure_title
else:
ballot_item_display_name = ""
last_editor_we_vote_id = "" # TODO We need to calculate this
quick_info_manager = QuickInfoManager()
results = quick_info_manager.update_or_create_quick_info(
quick_info_id=quick_info_id,
quick_info_we_vote_id=quick_info_we_vote_id,
ballot_item_display_name=ballot_item_display_name,
contest_office_we_vote_id=contest_office_we_vote_id,
candidate_campaign_we_vote_id=candidate_campaign_we_vote_id,
politician_we_vote_id=politician_we_vote_id,
contest_measure_we_vote_id=contest_measure_we_vote_id,
info_html=info_html,
info_text=info_text,
language=language,
last_editor_we_vote_id=last_editor_we_vote_id,
quick_info_master_we_vote_id=quick_info_master_we_vote_id,
more_info_url=more_info_url,
more_info_credit=more_info_credit,
google_civic_election_id=google_civic_election_id
)
if results['success']:
messages.add_message(request, messages.INFO, results['status'])
else:
messages.add_message(request, messages.ERROR,
results['status'] + "language: {language}".format(
language=language,
))
if positive_value_exists(quick_info_id):
return quick_info_edit_view(request, quick_info_id)
else:
return quick_info_new_view(request)
return HttpResponseRedirect(reverse('quick_info:quick_info_list', args=()))
示例14: voter_star_status_retrieve_for_api
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')
#.........这里部分代码省略.........
示例15: process_candidates_from_structured_json
def process_candidates_from_structured_json(
candidates_structured_json, google_civic_election_id, ocd_division_id, state_code, contest_office_id,
contest_office_we_vote_id):
"""
"candidates": [
{
"name": "Nancy Pelosi",
"party": "Democratic"
},
{
"name": "John Dennis",
"party": "Republican",
"candidateUrl": "http://www.johndennisforcongress.com/",
"channels": [
{
"type": "Facebook",
"id": "https://www.facebook.com/johndennis2010"
},
{
"type": "Twitter",
"id": "https://twitter.com/johndennis2012"
"""
results = {}
for one_candidate in candidates_structured_json:
candidate_name = one_candidate['name'] if 'name' in one_candidate else ''
# For some reason Google Civic API violates the JSON standard and uses a / in front of '
candidate_name = candidate_name.replace('/', "'")
# We want to save the name exactly as it comes from the Google Civic API
google_civic_candidate_name = one_candidate['name'] if 'name' in one_candidate else ''
party = one_candidate['party'] if 'party' in one_candidate else ''
order_on_ballot = one_candidate['orderOnBallot'] if 'orderOnBallot' in one_candidate else 0
candidate_url = one_candidate['candidateUrl'] if 'candidateUrl' in one_candidate else ''
photo_url = one_candidate['photoUrl'] if 'photoUrl' in one_candidate else ''
email = one_candidate['email'] if 'email' in one_candidate else ''
phone = one_candidate['phone'] if 'phone' in one_candidate else ''
# Make sure we start with empty channel values
facebook_url = ''
twitter_url = ''
google_plus_url = ''
youtube_url = ''
if 'channels' in one_candidate:
channels = one_candidate['channels']
for one_channel in channels:
if 'type' in one_channel:
if one_channel['type'] == 'Facebook':
facebook_url = one_channel['id'] if 'id' in one_channel else ''
if one_channel['type'] == 'Twitter':
twitter_url = one_channel['id'] if 'id' in one_channel else ''
if one_channel['type'] == 'GooglePlus':
google_plus_url = one_channel['id'] if 'id' in one_channel else ''
if one_channel['type'] == 'YouTube':
youtube_url = one_channel['id'] if 'id' in one_channel else ''
# DALE 2016-02-20 It would be helpful to call a service here that disambiguated the candidate
# ...and linked to a politician
# ...and looked to see if there were any other candidate_campaign entries for this election (in case the
# Google Civic contest_office name changed so we generated another contest)
we_vote_id = ''
# Make sure we have the minimum variables required to uniquely identify a candidate
if google_civic_election_id and contest_office_id and candidate_name:
# NOT using " and ocd_division_id"
# Make sure there isn't an alternate entry for this election and contest_office (under a similar but
# slightly different name TODO
# Note: This doesn't deal with duplicate Presidential candidates. These duplicates are caused because
# candidates are tied to a particular google_civic_election_id, so there is a different candidate entry
# for each Presidential candidate for each state.
updated_candidate_campaign_values = {
# Values we search against
'google_civic_election_id': google_civic_election_id,
'ocd_division_id': ocd_division_id,
# Note: When we decide to start updating candidate_name elsewhere within We Vote, we should stop
# updating candidate_name via subsequent Google Civic imports
'candidate_name': candidate_name,
# The rest of the values
'state_code': state_code, # Not required due to federal candidates
'party': party,
'candidate_email': email,
'candidate_phone': phone,
'order_on_ballot': order_on_ballot,
'candidate_url': candidate_url,
'photo_url': photo_url,
'facebook_url': facebook_url,
'twitter_url': twitter_url,
'google_plus_url': google_plus_url,
'youtube_url': youtube_url,
'google_civic_candidate_name': google_civic_candidate_name,
# 2016-02-20 Google Civic sometimes changes the name of contests, which can create a new contest
# so we may need to update the candidate to a new contest_office_id
'contest_office_id': contest_office_id,
'contest_office_we_vote_id': contest_office_we_vote_id,
}
candidate_campaign_manager = CandidateCampaignManager()
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,
google_civic_candidate_name, updated_candidate_campaign_values)
#.........这里部分代码省略.........