def organization_edit_existing_position_form_view(request, organization_id, position_id):
"""
In edit, you can only change your stance and comments, not who or what the position is about
:param request:
:param organization_id:
:param position_id:
:return:
"""
authority_required = {'verified_volunteer'} # admin, verified_volunteer
if not voter_has_authority(request, authority_required):
return redirect_to_sign_in_page(request, authority_required)
messages_on_stage = get_messages(request)
organization_id = convert_to_int(organization_id)
position_id = convert_to_int(position_id)
organization_on_stage_found = False
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 edit a position.')
return HttpResponseRedirect(reverse('organization:organization_position_list', args=([organization_id])))
# Get the existing position
organization_position_on_stage = PositionEntered()
organization_position_on_stage_found = False
position_entered_manager = PositionEnteredManager()
results = position_entered_manager.retrieve_position_from_id(position_id)
if results['position_found']:
organization_position_on_stage_found = True
organization_position_on_stage = results['position']
if not organization_position_on_stage_found:
messages.add_message(request, messages.INFO,
'Could not find organization position when trying to edit.')
return HttpResponseRedirect(reverse('organization:organization_position_list', args=([organization_id])))
# Note: We have access to the candidate campaign through organization_position_on_stage.candidate_campaign
election_list = Election.objects.all()
if organization_position_on_stage_found:
template_values = {
'is_in_edit_mode': True,
'messages_on_stage': messages_on_stage,
'organization': organization_on_stage,
'organization_position': organization_position_on_stage,
'possible_stances_list': ORGANIZATION_STANCE_CHOICES,
'stance_selected': organization_position_on_stage.stance,
'election_list': election_list,
}
return render(request, 'organization/organization_position_edit.html', template_values)
def candidate_politician_match_view(request):
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_id = request.GET.get('candidate_id', 0)
candidate_id = convert_to_int(candidate_id)
# google_civic_election_id is included for interface usability reasons and isn't used in the processing
google_civic_election_id = request.GET.get('google_civic_election_id', 0)
google_civic_election_id = convert_to_int(google_civic_election_id)
if not positive_value_exists(candidate_id):
messages.add_message(request, messages.ERROR, "The candidate_id variable was not passed in.")
return HttpResponseRedirect(reverse('candidate:candidate_edit', args=(candidate_id,)))
candidate_campaign_manager = CandidateCampaignManager()
results = candidate_campaign_manager.retrieve_candidate_campaign_from_id(candidate_id)
if not positive_value_exists(results['candidate_campaign_found']):
messages.add_message(request, messages.ERROR,
"Candidate '{candidate_id}' not found.".format(candidate_id=candidate_id))
return HttpResponseRedirect(reverse('candidate:candidate_edit', args=(candidate_id,)))
we_vote_candidate = results['candidate_campaign']
# Make sure we have a politician for this candidate. If we don't, create a politician entry, and save the
# politician_we_vote_id in the candidate
results = candidate_politician_match(we_vote_candidate)
display_messages = True
if results['status'] and display_messages:
messages.add_message(request, messages.INFO, results['status'])
return HttpResponseRedirect(reverse('candidate:candidate_edit', args=(candidate_id,)) +
"?google_civic_election_id=" + str(google_civic_election_id))
def positions_public_count_for_contest_measure(measure_id, measure_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 measure
"""
# 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_contest_measure = \
position_list_manager.retrieve_public_positions_count_for_contest_measure(
measure_id, measure_we_vote_id, stance_we_are_looking_for)
if 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)
json_data = {
'status': 'SUCCESSFUL_RETRIEVE_OF_PUBLIC_POSITION_COUNT_FOR_CONTEST_MEASURE',
'success': True,
'count': all_positions_count_for_contest_measure,
'ballot_item_id': convert_to_int(measure_id),
'ballot_item_we_vote_id': measure_we_vote_id,
'kind_of_ballot_item': MEASURE,
}
results = {
'json_data': json_data,
}
return results
def voter_authenticate_manually_process_view(request):
voter_api_device_id = get_voter_api_device_id(request) # We look in the cookies for voter_api_device_id
voter_id = fetch_voter_id_from_voter_device_link(voter_api_device_id)
voter_id = convert_to_int(voter_id)
voter_signed_in = False
try:
voter_on_stage = Voter.objects.get(id=voter_id)
# If the account associated with this voter_api_device_id is an admin, complete Django authentication
if voter_on_stage.is_admin:
voter_on_stage.backend = 'django.contrib.auth.backends.ModelBackend'
login(request, voter_on_stage)
messages.add_message(request, messages.INFO, 'Voter logged in.')
voter_signed_in = True
else:
messages.add_message(request, messages.INFO, 'This account does not have Admin access.')
except Voter.MultipleObjectsReturned as e:
handle_record_found_more_than_one_exception(e, logger=logger)
messages.add_message(request, messages.ERROR, 'More than one voter found. Voter not logged in.')
except Voter.DoesNotExist:
# This is fine, we will display an error
messages.add_message(request, messages.ERROR, 'Voter not found. Voter not logged in.')
if voter_signed_in:
return HttpResponseRedirect(reverse('admin_tools:admin_home', args=()))
else:
return HttpResponseRedirect(reverse('voter:authenticate_manually', args=()))
def retrieve_candidate_photos_for_election_view(request, election_id):
authority_required = {'admin'} # admin, verified_volunteer
if not voter_has_authority(request, authority_required):
return redirect_to_sign_in_page(request, authority_required)
google_civic_election_id = convert_to_int(election_id)
# We only want to process if a google_civic_election_id comes in
if not positive_value_exists(google_civic_election_id):
messages.add_message(request, messages.ERROR, "Google Civic Election ID required.")
return HttpResponseRedirect(reverse('candidate:candidate_list', args=()))
try:
candidate_list = CandidateCampaign.objects.order_by('candidate_name')
if positive_value_exists(google_civic_election_id):
candidate_list = candidate_list.filter(google_civic_election_id=google_civic_election_id)
except CandidateCampaign.DoesNotExist:
pass
display_messages = False
force_retrieve = False
# Loop through all of the candidates in this election
for we_vote_candidate in candidate_list:
retrieve_candidate_results = retrieve_candidate_photos(we_vote_candidate, force_retrieve)
if retrieve_candidate_results['status'] and display_messages:
messages.add_message(request, messages.INFO, retrieve_candidate_results['status'])
return HttpResponseRedirect(reverse('candidate:candidate_list', args=()) + "?google_civic_election_id={var}".format(
var=google_civic_election_id))
def refresh_twitter_candidate_details_for_election(google_civic_election_id):
twitter_handles_added = 0
profiles_refreshed_with_twitter_data = 0
google_civic_election_id = convert_to_int(google_civic_election_id)
candidate_list_manager = CandidateCampaignListManager()
return_list_of_objects = True
candidates_results = candidate_list_manager.retrieve_all_candidates_for_upcoming_election(
google_civic_election_id, return_list_of_objects)
if candidates_results['candidate_list_found']:
candidate_list = candidates_results['candidate_list_objects']
for candidate in candidate_list:
# Extract twitter_handle from google_civic_election information
if positive_value_exists(candidate.twitter_url) \
and not positive_value_exists(candidate.candidate_twitter_handle):
# If we got a twitter_url from Google Civic, and we haven't already stored a twitter handle, move it
candidate.candidate_twitter_handle = extract_twitter_handle_from_text_string(candidate.twitter_url)
candidate.save()
twitter_handles_added += 1
if positive_value_exists(candidate.candidate_twitter_handle):
refresh_twitter_candidate_details(candidate)
profiles_refreshed_with_twitter_data += 1
status = "CANDIDATE_SOCIAL_MEDIA_RETRIEVED"
results = {
'success': True,
'status': status,
'twitter_handles_added': twitter_handles_added,
'profiles_refreshed_with_twitter_data': profiles_refreshed_with_twitter_data,
}
return results
请发表评论