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 voter_supporting_candidate_campaign_view(request, candidate_campaign_id): print "voter_supporting_candidate_campaign_view {candidate_campaign_id}".format( candidate_campaign_id=candidate_campaign_id) voter_id = 1 position_entered_manager = PositionEnteredManager() results = position_entered_manager.toggle_on_voter_support_for_candidate_campaign( voter_id, candidate_campaign_id) if results['success']: return JsonResponse({0: "success"}) else: return JsonResponse({0: "failure"})
def voter_supporting_candidate_campaign_view(request, candidate_campaign_id): # print "voter_supporting_candidate_campaign_view {candidate_campaign_id}".format( # candidate_campaign_id=candidate_campaign_id) voter_device_id = get_voter_device_id(request) voter_id = fetch_voter_id_from_voter_device_link(voter_device_id) position_entered_manager = PositionEnteredManager() results = position_entered_manager.toggle_on_voter_support_for_candidate_campaign(voter_id, candidate_campaign_id) if results['success']: return JsonResponse({0: "success"}) else: return JsonResponse({0: "failure"})
def voter_opposing_candidate_campaign_view(request, candidate_campaign_id): # print "voter_opposing_candidate_campaign_view {candidate_campaign_id}".format( # candidate_campaign_id=candidate_campaign_id) voter_device_id = get_voter_device_id(request) voter_id = fetch_voter_id_from_voter_device_link(voter_device_id) position_entered_manager = PositionEnteredManager() results = position_entered_manager.toggle_on_voter_oppose_for_candidate_campaign( voter_id, candidate_campaign_id) if results['success']: return JsonResponse({0: "success"}) else: return JsonResponse({0: "failure"})
def voter_stop_opposing_candidate_campaign_view(request, candidate_campaign_id): logger.debug("voter_stop_opposing_candidate_campaign_view {candidate_campaign_id}".format( candidate_campaign_id=candidate_campaign_id )) voter_api_device_id = get_voter_api_device_id(request) voter_id = fetch_voter_id_from_voter_device_link(voter_api_device_id) position_entered_manager = PositionEnteredManager() results = position_entered_manager.toggle_off_voter_oppose_for_candidate_campaign(voter_id, candidate_campaign_id) if results['success']: return JsonResponse({0: "success"}) else: return JsonResponse({0: "failure"})
def organization_delete_existing_position_process_form_view( request, organization_id, position_id): """ :param request: :param organization_id: :param position_id: :return: """ # If person isn't signed in, we don't want to let them visit this page yet if not request.user.is_authenticated(): return redirect('/admin') organization_id = convert_to_int(organization_id) position_id = convert_to_int(position_id) # Get the existing position organization_position_on_stage_found = False if position_id > 0: 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 this organization's position when trying to delete." ) return HttpResponseRedirect( reverse('organization:organization_position_list', args=([organization_id]))) try: organization_position_on_stage.delete() except Exception as e: handle_record_not_deleted_exception(e) messages.add_message(request, messages.ERROR, 'Could not delete position.') return HttpResponseRedirect( reverse('organization:organization_position_list', args=([organization_id]))) messages.add_message(request, messages.INFO, 'Position deleted.') return HttpResponseRedirect( reverse('organization:organization_position_list', args=([organization_id])))
def organization_delete_existing_position_process_form_view( request, organization_id, position_we_vote_id): """ :param request: :param organization_id: :param position_we_vote_id: :return: """ authority_required = {'admin'} # admin, verified_volunteer if not voter_has_authority(request, authority_required): return redirect_to_sign_in_page(request, authority_required) organization_id = convert_to_int(organization_id) # Get the existing position organization_position_on_stage_found = False if positive_value_exists(position_we_vote_id): organization_position_on_stage = PositionEntered() organization_position_on_stage_found = False position_entered_manager = PositionEnteredManager() results = position_entered_manager.retrieve_position_from_we_vote_id( position_we_vote_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 this organization's position when trying to delete." ) return HttpResponseRedirect( reverse('organization:organization_position_list', args=([organization_id]))) try: organization_position_on_stage.delete() except Exception as e: handle_record_not_deleted_exception(e, logger=logger) messages.add_message(request, messages.ERROR, 'Could not delete position.') return HttpResponseRedirect( reverse('organization:organization_position_list', args=([organization_id]))) messages.add_message(request, messages.INFO, 'Position deleted.') return HttpResponseRedirect( reverse('organization:organization_position_edit', args=([organization_id])))
def voter_stop_supporting_candidate_campaign_view(request, candidate_campaign_id): logger.debug( "voter_stop_supporting_candidate_campaign_view {candidate_campaign_id}" .format(candidate_campaign_id=candidate_campaign_id)) voter_api_device_id = get_voter_api_device_id(request) voter_id = fetch_voter_id_from_voter_device_link(voter_api_device_id) position_entered_manager = PositionEnteredManager() results = position_entered_manager.toggle_off_voter_support_for_candidate_campaign( voter_id, candidate_campaign_id) if results['success']: return JsonResponse({0: "success"}) else: return JsonResponse({0: "failure"})
def voter_stance_for_candidate_campaign_view(request, candidate_campaign_id): print "voter_stance_for_candidate_campaign_view {candidate_campaign_id}".format( candidate_campaign_id=candidate_campaign_id) voter_id = 1 position_entered_manager = PositionEnteredManager() results = position_entered_manager.retrieve_voter_candidate_campaign_position( voter_id, candidate_campaign_id) if results['position_found']: if results['is_support']: return JsonResponse({0: "support"}) elif results['is_oppose']: return JsonResponse({0: "oppose"}) elif results['is_no_stance']: return JsonResponse({0: "no_stance"}) elif results['is_information_only']: return JsonResponse({0: "information_only"}) elif results['is_still_deciding']: return JsonResponse({0: "still_deciding"}) return JsonResponse({0: "failure"})
def organization_delete_existing_position_process_form_view(request, organization_id, position_id): """ :param request: :param organization_id: :param position_id: :return: """ # If person isn't signed in, we don't want to let them visit this page yet if not request.user.is_authenticated(): return redirect('/admin') organization_id = convert_to_int(organization_id) position_id = convert_to_int(position_id) # Get the existing position organization_position_on_stage_found = False if position_id > 0: 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 this organization's position when trying to delete.") return HttpResponseRedirect(reverse('organization:organization_position_list', args=([organization_id]))) try: organization_position_on_stage.delete() except Exception as e: handle_record_not_deleted_exception(e) messages.add_message(request, messages.ERROR, 'Could not delete position.') return HttpResponseRedirect(reverse('organization:organization_position_list', args=([organization_id]))) messages.add_message(request, messages.INFO, 'Position deleted.') return HttpResponseRedirect(reverse('organization:organization_position_list', args=([organization_id])))
def organization_delete_existing_position_process_form_view(request, organization_id, position_id): """ :param request: :param organization_id: :param position_id: :return: """ authority_required = {'admin'} # admin, verified_volunteer if not voter_has_authority(request, authority_required): return redirect_to_sign_in_page(request, authority_required) organization_id = convert_to_int(organization_id) position_id = convert_to_int(position_id) # Get the existing position organization_position_on_stage_found = False if position_id > 0: 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 this organization's position when trying to delete.") return HttpResponseRedirect(reverse('organization:organization_position_list', args=([organization_id]))) try: organization_position_on_stage.delete() except Exception as e: handle_record_not_deleted_exception(e, logger=logger) messages.add_message(request, messages.ERROR, 'Could not delete position.') return HttpResponseRedirect(reverse('organization:organization_position_list', args=([organization_id]))) messages.add_message(request, messages.INFO, 'Position deleted.') return HttpResponseRedirect(reverse('organization:organization_position_edit', args=([organization_id])))
def voter_stance_for_candidate_campaign_view(request, candidate_campaign_id): # print "voter_stance_for_candidate_campaign_view {candidate_campaign_id}".format( # candidate_campaign_id=candidate_campaign_id) voter_device_id = get_voter_device_id(request) voter_id = fetch_voter_id_from_voter_device_link(voter_device_id) position_entered_manager = PositionEnteredManager() results = position_entered_manager.retrieve_voter_candidate_campaign_position(voter_id, candidate_campaign_id) if results['position_found']: if results['is_support']: return JsonResponse({0: "support"}) elif results['is_oppose']: return JsonResponse({0: "oppose"}) elif results['is_no_stance']: return JsonResponse({0: "no_stance"}) elif results['is_information_only']: return JsonResponse({0: "information_only"}) elif results['is_still_deciding']: return JsonResponse({0: "still_deciding"}) return JsonResponse({0: "failure"})
def voter_stance_for_candidate_campaign_view(request, candidate_campaign_id): logger.debug( "voter_stance_for_candidate_campaign_view {candidate_campaign_id}". format(candidate_campaign_id=candidate_campaign_id)) voter_api_device_id = get_voter_api_device_id(request) voter_id = fetch_voter_id_from_voter_device_link(voter_api_device_id) position_entered_manager = PositionEnteredManager() results = position_entered_manager.retrieve_voter_candidate_campaign_position( voter_id, candidate_campaign_id) if results['position_found']: if results['is_support']: return JsonResponse({0: "support"}) elif results['is_oppose']: return JsonResponse({0: "oppose"}) elif results['is_no_stance']: return JsonResponse({0: "no_stance"}) elif results['is_information_only']: return JsonResponse({0: "information_only"}) elif results['is_still_deciding']: return JsonResponse({0: "still_deciding"}) return JsonResponse({0: "failure"})
def voter_opposing_save(voter_device_id, candidate_id, measure_id): # 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']: json_data = { 'status': 'VALID_VOTER_DEVICE_ID_MISSING', 'success': False, } 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, } return HttpResponse(json.dumps(json_data), content_type='application/json') position_entered_manager = PositionEnteredManager() if positive_value_exists(candidate_id): results = position_entered_manager.toggle_on_voter_oppose_for_candidate_campaign(voter_id, candidate_id) # toggle_off_voter_support_for_candidate_campaign status = "OPPOSING_CANDIDATE " + results['status'] success = results['success'] elif positive_value_exists(measure_id): results = position_entered_manager.toggle_on_voter_oppose_for_contest_measure(voter_id, measure_id) status = "OPPOSING_MEASURE " + results['status'] success = results['success'] else: status = 'UNABLE_TO_SAVE-CANDIDATE_ID_AND_MEASURE_ID_MISSING' success = False json_data = { 'status': status, 'success': success, } return HttpResponse(json.dumps(json_data), content_type='application/json')
def organization_position_list_view(request, organization_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) messages_on_stage = get_messages(request) organization_id = convert_to_int(organization_id) google_civic_election_id = convert_to_int(request.GET.get('google_civic_election_id', 0)) candidate_we_vote_id = request.GET.get('candidate_we_vote_id', '') organization_on_stage = Organization() organization_on_stage_found = False try: organization_query = Organization.objects.filter(id=organization_id) if organization_query.count(): organization_on_stage = organization_query[0] organization_on_stage_found = True except Exception as e: handle_record_not_found_exception(e, logger=logger) organization_on_stage_found = False if not organization_on_stage_found: messages.add_message(request, messages.ERROR, 'Could not find organization when trying to retrieve positions.') return HttpResponseRedirect(reverse('organization:organization_list', args=())) else: organization_position_list_found = False 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 = [] for one_position in organization_position_list: position_manager = PositionEnteredManager() one_position = position_manager.refresh_cached_position_info(one_position) election_list = Election.objects.order_by('-election_day_text') if organization_position_list_found: template_values = { 'messages_on_stage': messages_on_stage, 'organization': organization_on_stage, 'organization_position_list': organization_position_list, 'election_list': election_list, 'google_civic_election_id': google_civic_election_id, 'candidate_we_vote_id': candidate_we_vote_id, } else: template_values = { 'messages_on_stage': messages_on_stage, 'organization': organization_on_stage, 'election_list': election_list, 'google_civic_election_id': google_civic_election_id, 'candidate_we_vote_id': candidate_we_vote_id, } return render(request, 'organization/organization_position_list.html', template_values)
def organization_save_new_or_edit_existing_position_process_form_view(request): """ :param request: :return: """ organization_id = convert_to_int(request.POST['organization_id']) position_id = convert_to_int(request.POST['position_id']) candidate_campaign_id = convert_to_int( request.POST['candidate_campaign_id']) measure_campaign_id = convert_to_int(request.POST['measure_campaign_id']) stance = request.POST.get( 'stance', SUPPORT) # Set a default if stance comes in empty statement_text = request.POST.get( 'statement_text', '') # Set a default if stance comes in empty more_info_url = request.POST.get('more_info_url', '') # Make sure this is a valid organization before we try to save a position organization_on_stage_found = False try: organization_query = Organization.objects.filter(id=organization_id) if len(organization_query): # organization_on_stage = organization_query[0] organization_on_stage_found = True except Exception as e: # If we can't retrieve the organization, we cannot proceed handle_record_not_found_exception(e, logger=logger) if not organization_on_stage_found: messages.add_message( request, messages.ERROR, "Could not find the organization when trying to create or edit a new position." ) return HttpResponseRedirect( reverse('organization:organization_list', args=())) # Now retrieve the CandidateCampaign or the MeasureCampaign so we can save it with the Position # We need either candidate_campaign_id or measure_campaign_id if candidate_campaign_id: try: candidate_campaign_on_stage = CandidateCampaign.objects.get( id=candidate_campaign_id) candidate_campaign_on_stage_found = True except CandidateCampaign.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) except CandidateCampaign.DoesNotExist as e: handle_record_not_found_exception(e, logger=logger) if not candidate_campaign_on_stage_found: messages.add_message( request, messages.ERROR, "Could not find Candidate's campaign when trying to create or edit a new position." ) if position_id: return HttpResponseRedirect( reverse('organization:organization_position_edit', args=([organization_id], [position_id]))) else: return HttpResponseRedirect( reverse('organization:organization_position_new', args=([organization_id]))) elif measure_campaign_id: logger.warn( "measure_campaign_id FOUND. Look for MeasureCampaign here.") else: logger.warn( "Neither candidate_campaign_id nor measure_campaign_id found") messages.add_message(request, messages.ERROR, "Unable to find either Candidate or Measure.") return HttpResponseRedirect( reverse('organization:organization_position_list', args=([organization_id]))) organization_position_on_stage_found = False logger.info("position_id: {position_id}".format(position_id=position_id)) # Retrieve position from position_id if it exists already if position_id > 0: 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: # If a position_id hasn't been passed in, then we are trying to create a new position. # Check to make sure a position for this org and candidate doesn't already exist position_entered_manager = PositionEnteredManager() results = position_entered_manager.retrieve_organization_candidate_campaign_position( organization_id, candidate_campaign_id) if results['MultipleObjectsReturned']: messages.add_message( request, messages.ERROR, "We found more than one existing positions for this candidate. Please delete all but one position." ) return HttpResponseRedirect( reverse('organization:organization_position_list', args=([organization_id]))) elif results['position_found']: organization_position_on_stage_found = True organization_position_on_stage = results['position'] # Now save existing, or create new try: if organization_position_on_stage_found: # Update the position organization_position_on_stage.stance = stance organization_position_on_stage.statement_text = statement_text organization_position_on_stage.more_info_url = more_info_url organization_position_on_stage.save() messages.add_message( request, messages.INFO, "Position on {candidate_name} updated.".format( candidate_name=candidate_campaign_on_stage.candidate_name)) else: # Create new organization_position_on_stage = PositionEntered( organization_id=organization_id, candidate_campaign_id=candidate_campaign_on_stage.id, stance=stance, statement_text=statement_text, more_info_url=more_info_url, ) organization_position_on_stage.save() messages.add_message( request, messages.INFO, "New position on {candidate_name} saved.".format( candidate_name=candidate_campaign_on_stage.candidate_name)) except Exception as e: handle_record_not_saved_exception(e, logger=logger) logger.error("Problem saving PositionEntered for CandidateCampaign") return HttpResponseRedirect( reverse('organization:organization_position_list', args=([organization_id])))
def organization_save_new_or_edit_existing_position_process_form_view(request): """ :param request: :return: """ # If person isn't signed in, we don't want to let them visit this page yet if not request.user.is_authenticated(): return redirect('/admin') organization_id = convert_to_int(request.POST['organization_id']) position_id = convert_to_int(request.POST['position_id']) candidate_campaign_id = convert_to_int(request.POST['candidate_campaign_id']) measure_campaign_id = convert_to_int(request.POST['measure_campaign_id']) stance = request.POST.get('stance', SUPPORT) # Set a default if stance comes in empty statement_text = request.POST.get('statement_text', '') # Set a default if stance comes in empty more_info_url = request.POST.get('more_info_url', '') # Make sure this is a valid organization before we try to save a position organization_on_stage_found = False try: organization_query = Organization.objects.filter(id=organization_id) if len(organization_query): organization_on_stage = organization_query[0] organization_on_stage_found = True except Exception as e: # If we can't retrieve the organization, we cannot proceed handle_record_not_found_exception(e) if not organization_on_stage_found: messages.add_message( request, messages.ERROR, "Could not find the organization when trying to create or edit a new position.") return HttpResponseRedirect(reverse('organization:organization_list', args=())) # Now retrieve the CandidateCampaign or the MeasureCampaign so we can save it with the Position # We need either candidate_campaign_id or measure_campaign_id if candidate_campaign_id: try: candidate_campaign_on_stage = CandidateCampaign.objects.get(id=candidate_campaign_id) candidate_campaign_on_stage_found = True except CandidateCampaign.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e) except CandidateCampaign.DoesNotExist as e: handle_record_not_found_exception(e) if not candidate_campaign_on_stage_found: messages.add_message( request, messages.ERROR, "Could not find Candidate's campaign when trying to create or edit a new position.") if position_id: return HttpResponseRedirect( reverse('organization:organization_position_edit', args=([organization_id], [position_id])) ) else: return HttpResponseRedirect( reverse('organization:organization_position_new', args=([organization_id])) ) elif measure_campaign_id: print "measure_campaign_id FOUND. Look for MeasureCampaign here." else: print "Neither candidate_campaign_id nor measure_campaign_id found" messages.add_message( request, messages.ERROR, "Unable to find either Candidate or Measure.") return HttpResponseRedirect( reverse('organization:organization_position_list', args=([organization_id])) ) organization_position_on_stage_found = False print "position_id: {position_id}".format(position_id=position_id) # Retrieve position from position_id if it exists already if position_id > 0: 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: # If a position_id hasn't been passed in, then we are trying to create a new position. # Check to make sure a position for this org and candidate doesn't already exist position_entered_manager = PositionEnteredManager() results = position_entered_manager.retrieve_organization_candidate_campaign_position(organization_id, candidate_campaign_id) if results['MultipleObjectsReturned']: messages.add_message( request, messages.ERROR, "We found more than one existing positions for this candidate. Please delete all but one position.") return HttpResponseRedirect( reverse('organization:organization_position_list', args=([organization_id])) ) elif results['position_found']: organization_position_on_stage_found = True organization_position_on_stage = results['position'] # Now save existing, or create new try: if organization_position_on_stage_found: # Update the position organization_position_on_stage.stance = stance organization_position_on_stage.statement_text = statement_text organization_position_on_stage.more_info_url = more_info_url organization_position_on_stage.save() messages.add_message( request, messages.INFO, "Position on {candidate_name} updated.".format(candidate_name=candidate_campaign_on_stage.candidate_name)) else: # Create new organization_position_on_stage = PositionEntered( organization_id=organization_id, candidate_campaign_id=candidate_campaign_on_stage.id, stance=stance, statement_text=statement_text, more_info_url=more_info_url, ) organization_position_on_stage.save() messages.add_message( request, messages.INFO, "New position on {candidate_name} saved.".format(candidate_name=candidate_campaign_on_stage.candidate_name)) except Exception as e: handle_record_not_saved_exception(e) print "Problem saving PositionEntered for CandidateCampaign" return HttpResponseRedirect(reverse('organization:organization_position_list', args=([organization_id])))
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 results = { 'status': status, 'success': success, } return results
def position_we_vote_id(self): position_manager = PositionEnteredManager() return position_manager.fetch_we_vote_id_from_local_id(self.voter_id)
def voter_supporting_save_for_api(voter_device_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 supporting/opposing, voterSupportingSave results = is_voter_device_id_valid(voter_device_id) if not results['success']: json_data = { 'status': 'VALID_VOTER_DEVICE_ID_MISSING', 'success': False, '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, 'ballot_item_id': 0, 'ballot_item_we_vote_id': '', 'kind_of_ballot_item': '', } return HttpResponse(json.dumps(json_data), content_type='application/json') position_entered_manager = PositionEnteredManager() 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) results = position_entered_manager.toggle_on_voter_support_for_candidate_campaign(voter_id, candidate_id) status = "SUPPORTING_CANDIDATE " + results['status'] success = results['success'] json_data = { 'status': status, 'success': success, 'ballot_item_id': convert_to_int(candidate_id), 'ballot_item_we_vote_id': candidate_we_vote_id, 'kind_of_ballot_item': CANDIDATE, } 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) results = position_entered_manager.toggle_on_voter_support_for_contest_measure(voter_id, measure_id) status = "SUPPORTING_MEASURE " + results['status'] success = results['success'] json_data = { 'status': status, 'success': success, 'ballot_item_id': convert_to_int(measure_id), 'ballot_item_we_vote_id': measure_we_vote_id, 'kind_of_ballot_item': MEASURE, } return HttpResponse(json.dumps(json_data), content_type='application/json') else: status = 'UNABLE_TO_SAVE-CANDIDATE_ID_AND_MEASURE_ID_MISSING' success = False json_data = { 'status': status, 'success': success, 'ballot_item_id': 0, 'ballot_item_we_vote_id': '', 'kind_of_ballot_item': '', } return HttpResponse(json.dumps(json_data), content_type='application/json')
def voter_guides_to_follow_retrieve_for_api( voter_device_id, # voterGuidesToFollow kind_of_ballot_item='', ballot_item_we_vote_id='', google_civic_election_id=0, search_string='', maximum_number_to_retrieve=0): # Get voter_id from the voter_device_id so we can figure out which voter_guides to offer results = is_voter_device_id_valid(voter_device_id) if not results['success']: json_data = { 'status': 'ERROR_GUIDES_TO_FOLLOW_NO_VOTER_DEVICE_ID', 'success': False, 'voter_device_id': voter_device_id, 'voter_guides': [], 'google_civic_election_id': google_civic_election_id, 'search_string': search_string, 'ballot_item_we_vote_id': ballot_item_we_vote_id, } results = { 'success': False, 'google_civic_election_id': 0, # Force the reset of google_civic_election_id cookie 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'json_data': json_data, } return results voter_id = fetch_voter_id_from_voter_device_link(voter_device_id) if not positive_value_exists(voter_id): json_data = { 'status': "ERROR_GUIDES_TO_FOLLOW_VOTER_NOT_FOUND_FROM_VOTER_DEVICE_ID", 'success': False, 'voter_device_id': voter_device_id, 'voter_guides': [], 'google_civic_election_id': google_civic_election_id, 'search_string': search_string, 'ballot_item_we_vote_id': ballot_item_we_vote_id, } results = { 'success': False, 'google_civic_election_id': 0, # Force the reset of google_civic_election_id cookie 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'json_data': json_data, } return results voter_guide_list = [] voter_guides = [] try: if positive_value_exists( kind_of_ballot_item) and positive_value_exists( ballot_item_we_vote_id): results = retrieve_voter_guides_to_follow_by_ballot_item( voter_id, kind_of_ballot_item, ballot_item_we_vote_id, search_string) success = results['success'] status = results['status'] voter_guide_list = results['voter_guide_list'] elif positive_value_exists(google_civic_election_id): # This retrieve also does the reordering results = retrieve_voter_guides_to_follow_by_election_for_api( voter_id, google_civic_election_id, search_string, maximum_number_to_retrieve, 'twitter_followers_count', 'desc') success = results['success'] status = results['status'] voter_guide_list = results['voter_guide_list'] else: results = retrieve_voter_guides_to_follow_generic_for_api( voter_id, search_string, maximum_number_to_retrieve, 'twitter_followers_count', 'desc') success = results['success'] status = results['status'] voter_guide_list = results['voter_guide_list'] except Exception as e: status = 'FAILED voter_guides_to_follow_retrieve_for_api, retrieve_voter_guides_for_election ' \ '{error} [type: {error_type}]'.format(error=e, error_type=type(e)) success = False if success: voter_manager = VoterManager() results = voter_manager.retrieve_voter_by_id(voter_id) linked_organization_we_vote_id = "" if results['voter_found']: voter = results['voter'] linked_organization_we_vote_id = voter.linked_organization_we_vote_id number_added_to_list = 0 position_manager = PositionEnteredManager() position = PositionEntered() for voter_guide in voter_guide_list: if positive_value_exists(voter_guide.organization_we_vote_id) \ and linked_organization_we_vote_id == voter_guide.organization_we_vote_id: # Do not return your own voter guide to follow continue position_found = False one_voter_guide = { 'we_vote_id': voter_guide.we_vote_id, 'google_civic_election_id': voter_guide.google_civic_election_id, 'time_span': voter_guide.vote_smart_time_span, 'voter_guide_display_name': voter_guide.voter_guide_display_name(), 'voter_guide_image_url': voter_guide.voter_guide_image_url(), 'voter_guide_owner_type': voter_guide.voter_guide_owner_type, 'organization_we_vote_id': voter_guide.organization_we_vote_id, 'public_figure_we_vote_id': voter_guide.public_figure_we_vote_id, 'twitter_description': voter_guide.twitter_description, 'twitter_followers_count': voter_guide.twitter_followers_count, 'twitter_handle': voter_guide.twitter_handle, 'owner_voter_id': voter_guide.owner_voter_id, 'last_updated': voter_guide.last_updated.strftime('%Y-%m-%d %H:%M'), } if positive_value_exists(ballot_item_we_vote_id): if kind_of_ballot_item == CANDIDATE: organization_manager = OrganizationManager() organization_id = organization_manager.fetch_organization_id( voter_guide.organization_we_vote_id) results = position_manager.retrieve_organization_candidate_campaign_position_with_we_vote_id( organization_id, ballot_item_we_vote_id) if results['position_found']: position = results['position'] position_found = True elif kind_of_ballot_item == MEASURE: organization_manager = OrganizationManager() organization_id = organization_manager.fetch_organization_id( voter_guide.organization_we_vote_id) results = position_manager.retrieve_organization_contest_measure_position_with_we_vote_id( organization_id, ballot_item_we_vote_id) if results['position_found']: position = results['position'] position_found = True if position_found: one_voter_guide['is_support'] = position.is_support() one_voter_guide[ 'is_positive_rating'] = position.is_positive_rating() one_voter_guide[ 'is_support_or_positive_rating'] = position.is_support_or_positive_rating( ) one_voter_guide['is_oppose'] = position.is_oppose() one_voter_guide[ 'is_negative_rating'] = position.is_negative_rating() one_voter_guide[ 'is_oppose_or_negative_rating'] = position.is_oppose_or_negative_rating( ) one_voter_guide[ 'is_information_only'] = position.is_information_only( ) one_voter_guide[ 'ballot_item_display_name'] = position.ballot_item_display_name one_voter_guide[ 'speaker_display_name'] = position.speaker_display_name one_voter_guide['statement_text'] = position.statement_text one_voter_guide['more_info_url'] = position.more_info_url one_voter_guide[ 'vote_smart_rating'] = position.vote_smart_rating one_voter_guide[ 'vote_smart_time_span'] = position.vote_smart_time_span voter_guides.append(one_voter_guide.copy()) if positive_value_exists(maximum_number_to_retrieve): number_added_to_list += 1 if number_added_to_list >= maximum_number_to_retrieve: break if len(voter_guides): json_data = { 'status': status + ' VOTER_GUIDES_TO_FOLLOW_RETRIEVED', 'success': True, 'voter_device_id': voter_device_id, 'voter_guides': voter_guides, 'google_civic_election_id': google_civic_election_id, 'search_string': search_string, 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'maximum_number_to_retrieve': maximum_number_to_retrieve, } else: json_data = { 'status': status + ' NO_VOTER_GUIDES_FOUND', 'success': True, 'voter_device_id': voter_device_id, 'voter_guides': voter_guides, 'google_civic_election_id': google_civic_election_id, 'search_string': search_string, 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'maximum_number_to_retrieve': maximum_number_to_retrieve, } results = { 'success': success, 'google_civic_election_id': google_civic_election_id, 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'json_data': json_data, } return results else: json_data = { 'status': status, 'success': False, 'voter_device_id': voter_device_id, 'voter_guides': [], 'google_civic_election_id': google_civic_election_id, 'search_string': search_string, 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'maximum_number_to_retrieve': maximum_number_to_retrieve, } results = { 'success': False, 'google_civic_election_id': 0, # Force the reset of google_civic_election_id cookie 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'json_data': json_data, } return results
def organization_position_edit_process_view(request): """ :param request: :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) google_civic_election_id = convert_to_int(request.POST.get('google_civic_election_id', 0)) organization_id = convert_to_int(request.POST.get('organization_id', 0)) position_we_vote_id = request.POST.get('position_we_vote_id', '') candidate_campaign_id = convert_to_int(request.POST.get('candidate_campaign_id', 0)) contest_measure_id = convert_to_int(request.POST.get('contest_measure_id', 0)) stance = request.POST.get('stance', SUPPORT) # Set a default if stance comes in empty statement_text = request.POST.get('statement_text', '') # Set a default if stance comes in empty more_info_url = request.POST.get('more_info_url', '') go_back_to_add_new = False candidate_campaign_we_vote_id = "" google_civic_candidate_name = "" contest_measure_we_vote_id = "" google_civic_measure_title = "" candidate_campaign_on_stage_found = False contest_measure_on_stage_found = False organization_position_on_stage = PositionEntered() organization_on_stage = Organization() candidate_campaign_on_stage = CandidateCampaign() contest_measure_on_stage = ContestMeasure() state_code = "" position_entered_manager = PositionEnteredManager() # Make sure this is a valid organization before we try to save a position organization_on_stage_found = False organization_we_vote_id = "" try: organization_query = Organization.objects.filter(id=organization_id) if organization_query.count(): organization_on_stage = organization_query[0] organization_we_vote_id = organization_on_stage.we_vote_id organization_on_stage_found = True except Exception as e: # If we can't retrieve the organization, we cannot proceed handle_record_not_found_exception(e, logger=logger) if not organization_on_stage_found: messages.add_message( request, messages.ERROR, "Could not find the organization when trying to create or edit a new position.") return HttpResponseRedirect(reverse('organization:organization_list', args=())) # Now retrieve the CandidateCampaign or the ContestMeasure so we can save it with the Position # We need either candidate_campaign_id or contest_measure_id if candidate_campaign_id: try: candidate_campaign_on_stage = CandidateCampaign.objects.get(id=candidate_campaign_id) candidate_campaign_on_stage_found = True candidate_campaign_we_vote_id = candidate_campaign_on_stage.we_vote_id google_civic_candidate_name = candidate_campaign_on_stage.google_civic_candidate_name state_code = candidate_campaign_on_stage.state_code except CandidateCampaign.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) except CandidateCampaign.DoesNotExist as e: handle_record_not_found_exception(e, logger=logger) if not candidate_campaign_on_stage_found: messages.add_message( request, messages.ERROR, "Could not find Candidate's campaign when trying to create or edit a new position.") if positive_value_exists(position_we_vote_id): return HttpResponseRedirect( reverse('organization:organization_position_edit', args=([organization_id], [position_we_vote_id])) + "?google_civic_election_id=" + str(google_civic_election_id) + "&stance=" + stance + "&statement_text=" + statement_text + "&more_info_url=" + more_info_url + "&candidate_and_measure_not_found=1" ) else: return HttpResponseRedirect( reverse('organization:organization_position_new', args=([organization_id])) + "?google_civic_election_id=" + str(google_civic_election_id) + "&stance=" + stance + "&statement_text=" + statement_text + "&more_info_url=" + more_info_url + "&candidate_and_measure_not_found=1" ) contest_measure_id = 0 elif contest_measure_id: try: contest_measure_on_stage = ContestMeasure.objects.get(id=contest_measure_id) contest_measure_on_stage_found = True contest_measure_we_vote_id = contest_measure_on_stage.we_vote_id google_civic_measure_title = contest_measure_on_stage.google_civic_measure_title state_code = contest_measure_on_stage.state_code except CandidateCampaign.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) except CandidateCampaign.DoesNotExist as e: handle_record_not_found_exception(e, logger=logger) if not contest_measure_on_stage_found: messages.add_message( request, messages.ERROR, "Could not find measure when trying to create or edit a new position.") if positive_value_exists(position_we_vote_id): return HttpResponseRedirect( reverse('organization:organization_position_edit', args=([organization_id], [position_we_vote_id])) + "?google_civic_election_id=" + str(google_civic_election_id) + "&stance=" + stance + "&statement_text=" + statement_text + "&more_info_url=" + more_info_url + "&candidate_and_measure_not_found=1" ) else: return HttpResponseRedirect( reverse('organization:organization_position_new', args=([organization_id])) + "?google_civic_election_id=" + str(google_civic_election_id) + "&stance=" + stance + "&statement_text=" + statement_text + "&more_info_url=" + more_info_url + "&candidate_and_measure_not_found=1" ) candidate_campaign_id = 0 else: messages.add_message( request, messages.ERROR, "Unable to find either Candidate or Measure.") return HttpResponseRedirect( reverse('organization:organization_position_new', args=([organization_id])) + "?google_civic_election_id=" + str(google_civic_election_id) + "&stance=" + stance + "&statement_text=" + statement_text + "&more_info_url=" + more_info_url + "&candidate_and_measure_not_found=1" ) organization_position_on_stage_found = False # Retrieve position from position_we_vote_id if it exists already if positive_value_exists(position_we_vote_id): results = position_entered_manager.retrieve_position_from_we_vote_id(position_we_vote_id) if results['position_found']: organization_position_on_stage_found = True organization_position_on_stage = results['position'] organization_position_found_from_new_form = False if not organization_position_on_stage_found: # Position not found from position_we_vote_id # If a position_we_vote_id hasn't been passed in, then we are trying to create a new position. # Check to make sure a position for this org, candidate and election doesn't already exist if candidate_campaign_id: results = position_entered_manager.retrieve_organization_candidate_campaign_position( organization_id, candidate_campaign_id, google_civic_election_id) elif contest_measure_id: results = position_entered_manager.retrieve_organization_contest_measure_position( organization_id, contest_measure_id, google_civic_election_id) else: messages.add_message( request, messages.ERROR, "Missing both candidate_campaign_id and contest_measure_id.") return HttpResponseRedirect( reverse('organization:organization_position_list', args=([organization_id])) ) if results['MultipleObjectsReturned']: messages.add_message( request, messages.ERROR, "We found more than one existing positions for this candidate. Please delete all but one position.") return HttpResponseRedirect( reverse('organization:organization_position_list', args=([organization_id])) ) elif results['position_found']: organization_position_on_stage_found = True organization_position_on_stage = results['position'] organization_position_found_from_new_form = True # Now save existing, or create new success = False try: if organization_position_on_stage_found: # Update the position organization_position_on_stage.stance = stance organization_position_on_stage.google_civic_election_id = google_civic_election_id if not organization_position_found_from_new_form or positive_value_exists(more_info_url): # Only update this if we came from update form, or there is a value in the incoming variable organization_position_on_stage.more_info_url = more_info_url if not organization_position_found_from_new_form or positive_value_exists(statement_text): # Only update this if we came from update form, or there is a value in the incoming variable organization_position_on_stage.statement_text = statement_text if not positive_value_exists(organization_position_on_stage.organization_we_vote_id): organization_position_on_stage.organization_we_vote_id = organization_on_stage.we_vote_id organization_position_on_stage.candidate_campaign_id = candidate_campaign_id organization_position_on_stage.candidate_campaign_we_vote_id = candidate_campaign_we_vote_id organization_position_on_stage.google_civic_candidate_name = google_civic_candidate_name organization_position_on_stage.contest_measure_id = contest_measure_id organization_position_on_stage.contest_measure_we_vote_id = contest_measure_we_vote_id organization_position_on_stage.google_civic_measure_title = google_civic_measure_title organization_position_on_stage.state_code = state_code organization_position_on_stage.save() organization_position_on_stage = position_entered_manager.refresh_cached_position_info( organization_position_on_stage) success = True if positive_value_exists(candidate_campaign_we_vote_id): messages.add_message( request, messages.INFO, "Position on {candidate_name} updated.".format( candidate_name=candidate_campaign_on_stage.display_candidate_name())) elif positive_value_exists(contest_measure_we_vote_id): messages.add_message( request, messages.INFO, "Position on {measure_title} updated.".format( measure_title=contest_measure_on_stage.measure_title)) else: # Create new # Note that since we are processing a volunteer/admin entry tool, we can always save to the PositionEntered # table, and don't need to worry about PositionForFriends organization_position_on_stage = PositionEntered( organization_id=organization_id, organization_we_vote_id=organization_we_vote_id, candidate_campaign_id=candidate_campaign_id, candidate_campaign_we_vote_id=candidate_campaign_we_vote_id, google_civic_candidate_name=google_civic_candidate_name, contest_measure_id=contest_measure_id, contest_measure_we_vote_id=contest_measure_we_vote_id, google_civic_measure_title=google_civic_measure_title, google_civic_election_id=google_civic_election_id, stance=stance, statement_text=statement_text, more_info_url=more_info_url, state_code=state_code, ) organization_position_on_stage.save() organization_position_on_stage = position_entered_manager.refresh_cached_position_info( organization_position_on_stage) success = True if positive_value_exists(candidate_campaign_we_vote_id): messages.add_message( request, messages.INFO, "New position on {candidate_name} saved.".format( candidate_name=candidate_campaign_on_stage.display_candidate_name())) elif positive_value_exists(contest_measure_we_vote_id): messages.add_message( request, messages.INFO, "New position on {measure_title} saved.".format( measure_title=contest_measure_on_stage.measure_title)) go_back_to_add_new = True except Exception as e: pass # If the position was saved, then update the voter_guide entry if success: voter_guide_manager = VoterGuideManager() results = voter_guide_manager.update_or_create_organization_voter_guide_by_election_id( organization_on_stage.we_vote_id, google_civic_election_id) # if results['success']: if go_back_to_add_new: return HttpResponseRedirect( reverse('organization:organization_position_new', args=(organization_on_stage.id,)) + "?google_civic_election_id=" + str(google_civic_election_id)) else: return HttpResponseRedirect( reverse('organization:organization_position_list', args=(organization_on_stage.id,)))
def organization_position_edit_process_view(request): """ :param request: :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) google_civic_election_id = convert_to_int(request.POST.get('google_civic_election_id', 0)) organization_id = convert_to_int(request.POST.get('organization_id', 0)) position_id = convert_to_int(request.POST.get('position_id', 0)) candidate_campaign_id = convert_to_int(request.POST.get('candidate_campaign_id', 0)) contest_measure_id = convert_to_int(request.POST.get('contest_measure_id', 0)) stance = request.POST.get('stance', SUPPORT) # Set a default if stance comes in empty statement_text = request.POST.get('statement_text', '') # Set a default if stance comes in empty more_info_url = request.POST.get('more_info_url', '') go_back_to_add_new = False # Make sure this is a valid organization before we try to save a position organization_on_stage_found = False try: organization_query = Organization.objects.filter(id=organization_id) if organization_query.count(): organization_on_stage = organization_query[0] organization_on_stage_found = True except Exception as e: # If we can't retrieve the organization, we cannot proceed handle_record_not_found_exception(e, logger=logger) if not organization_on_stage_found: messages.add_message( request, messages.ERROR, "Could not find the organization when trying to create or edit a new position.") return HttpResponseRedirect(reverse('organization:organization_list', args=())) # Now retrieve the CandidateCampaign or the ContestMeasure so we can save it with the Position # We need either candidate_campaign_id or contest_measure_id if candidate_campaign_id: try: candidate_campaign_on_stage = CandidateCampaign.objects.get(id=candidate_campaign_id) candidate_campaign_on_stage_found = True except CandidateCampaign.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) except CandidateCampaign.DoesNotExist as e: handle_record_not_found_exception(e, logger=logger) if not candidate_campaign_on_stage_found: messages.add_message( request, messages.ERROR, "Could not find Candidate's campaign when trying to create or edit a new position.") if position_id: return HttpResponseRedirect( reverse('organization:organization_position_edit', args=([organization_id], [position_id])) ) else: return HttpResponseRedirect( reverse('organization:organization_position_new', args=([organization_id])) ) elif contest_measure_id: logger.warn("contest_measure_id FOUND. Look for ContestMeasure here.") else: logger.warn("Neither candidate_campaign_id nor contest_measure_id found") messages.add_message( request, messages.ERROR, "Unable to find either Candidate or Measure.") return HttpResponseRedirect( reverse('organization:organization_position_new', args=([organization_id])) + "?google_civic_election_id=" + str(google_civic_election_id) + "&stance=" + stance + "&statement_text=" + statement_text + "&more_info_url=" + more_info_url + "&candidate_not_found=1" ) organization_position_on_stage_found = False logger.info("position_id: {position_id}".format(position_id=position_id)) # Retrieve position from position_id if it exists already if position_id > 0: 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'] organization_position_found_from_new_form = False if not organization_position_on_stage_found: # If not found from position_id # If a position_id hasn't been passed in, then we are trying to create a new position. # Check to make sure a position for this org, candidate and election doesn't already exist position_entered_manager = PositionEnteredManager() results = position_entered_manager.retrieve_organization_candidate_campaign_position( organization_id, candidate_campaign_id, google_civic_election_id) if results['MultipleObjectsReturned']: messages.add_message( request, messages.ERROR, "We found more than one existing positions for this candidate. Please delete all but one position.") return HttpResponseRedirect( reverse('organization:organization_position_list', args=([organization_id])) ) elif results['position_found']: organization_position_on_stage_found = True organization_position_on_stage = results['position'] organization_position_found_from_new_form = True # Now save existing, or create new success = False try: if organization_position_on_stage_found: # Update the position organization_position_on_stage.stance = stance organization_position_on_stage.google_civic_election_id = google_civic_election_id if not organization_position_found_from_new_form or positive_value_exists(more_info_url): # Only update this if we came from update form, or there is a value in the incoming variable organization_position_on_stage.more_info_url = more_info_url if not organization_position_found_from_new_form or positive_value_exists(statement_text): # Only update this if we came from update form, or there is a value in the incoming variable organization_position_on_stage.statement_text = statement_text if not positive_value_exists(organization_position_on_stage.organization_we_vote_id): organization_position_on_stage.organization_we_vote_id = organization_on_stage.we_vote_id if not positive_value_exists(organization_position_on_stage.candidate_campaign_we_vote_id): organization_position_on_stage.candidate_campaign_we_vote_id = candidate_campaign_on_stage.we_vote_id if not positive_value_exists(organization_position_on_stage.google_civic_candidate_name): organization_position_on_stage.google_civic_candidate_name = \ candidate_campaign_on_stage.google_civic_candidate_name organization_position_on_stage.save() success = True messages.add_message( request, messages.INFO, "Position on {candidate_name} updated.".format( candidate_name=candidate_campaign_on_stage.display_candidate_name())) else: # Create new organization_position_on_stage = PositionEntered( organization_id=organization_id, organization_we_vote_id=organization_on_stage.we_vote_id, candidate_campaign_id=candidate_campaign_on_stage.id, candidate_campaign_we_vote_id=candidate_campaign_on_stage.we_vote_id, # Save candidate_campaign_on_stage so we can re-link candidates to positions if we_vote_id is lost google_civic_candidate_name=candidate_campaign_on_stage.google_civic_candidate_name, google_civic_election_id=google_civic_election_id, stance=stance, statement_text=statement_text, more_info_url=more_info_url, ) organization_position_on_stage.save() success = True messages.add_message( request, messages.INFO, "New position on {candidate_name} saved.".format( candidate_name=candidate_campaign_on_stage.display_candidate_name())) go_back_to_add_new = True except Exception as e: handle_record_not_saved_exception(e, logger=logger) logger.error("Problem saving PositionEntered for CandidateCampaign") # If the position was saved, then update the voter_guide entry if success: voter_guide_manager = VoterGuideManager() results = voter_guide_manager.update_or_create_organization_voter_guide_by_election_id( organization_on_stage.we_vote_id, google_civic_election_id) # if results['success']: if go_back_to_add_new: return HttpResponseRedirect( reverse('organization:organization_position_new', args=(organization_on_stage.id,)) + "?google_civic_election_id=" + str(google_civic_election_id)) else: return HttpResponseRedirect( reverse('organization:organization_position_list', args=(organization_on_stage.id,)))
def voter_supporting_save_for_api(voter_device_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 supporting/opposing, voterSupportingSave results = is_voter_device_id_valid(voter_device_id) if not results['success']: json_data = { 'status': 'VALID_VOTER_DEVICE_ID_MISSING', 'success': False, '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, 'ballot_item_id': 0, 'ballot_item_we_vote_id': '', 'kind_of_ballot_item': '', } return HttpResponse(json.dumps(json_data), content_type='application/json') position_entered_manager = PositionEnteredManager() 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) results = position_entered_manager.toggle_on_voter_support_for_candidate_campaign( voter_id, candidate_id) status = "SUPPORTING_CANDIDATE " + results['status'] success = results['success'] json_data = { 'status': status, 'success': success, 'ballot_item_id': convert_to_int(candidate_id), 'ballot_item_we_vote_id': candidate_we_vote_id, 'kind_of_ballot_item': CANDIDATE, } 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) results = position_entered_manager.toggle_on_voter_support_for_contest_measure( voter_id, measure_id) status = "SUPPORTING_MEASURE " + results['status'] success = results['success'] json_data = { 'status': status, 'success': success, 'ballot_item_id': convert_to_int(measure_id), 'ballot_item_we_vote_id': measure_we_vote_id, 'kind_of_ballot_item': MEASURE, } return HttpResponse(json.dumps(json_data), content_type='application/json') else: status = 'UNABLE_TO_SAVE-CANDIDATE_ID_AND_MEASURE_ID_MISSING' success = False json_data = { 'status': status, 'success': success, 'ballot_item_id': 0, 'ballot_item_we_vote_id': '', 'kind_of_ballot_item': '', } return HttpResponse(json.dumps(json_data), content_type='application/json')
def organization_save_new_or_edit_existing_position_process_form_view(request): """ :param request: :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) google_civic_election_id = convert_to_int(request.POST['google_civic_election_id']) organization_id = convert_to_int(request.POST['organization_id']) position_id = convert_to_int(request.POST['position_id']) candidate_campaign_id = convert_to_int(request.POST['candidate_campaign_id']) contest_measure_id = convert_to_int(request.POST['contest_measure_id']) stance = request.POST.get('stance', SUPPORT) # Set a default if stance comes in empty statement_text = request.POST.get('statement_text', '') # Set a default if stance comes in empty more_info_url = request.POST.get('more_info_url', '') # Make sure this is a valid organization before we try to save a position organization_on_stage_found = False try: organization_query = Organization.objects.filter(id=organization_id) if organization_query.count(): organization_on_stage = organization_query[0] organization_on_stage_found = True except Exception as e: # If we can't retrieve the organization, we cannot proceed handle_record_not_found_exception(e, logger=logger) if not organization_on_stage_found: messages.add_message( request, messages.ERROR, "Could not find the organization when trying to create or edit a new position.") return HttpResponseRedirect(reverse('organization:organization_list', args=())) # Now retrieve the CandidateCampaign or the ContestMeasure so we can save it with the Position # We need either candidate_campaign_id or contest_measure_id if candidate_campaign_id: try: candidate_campaign_on_stage = CandidateCampaign.objects.get(id=candidate_campaign_id) candidate_campaign_on_stage_found = True except CandidateCampaign.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) except CandidateCampaign.DoesNotExist as e: handle_record_not_found_exception(e, logger=logger) if not candidate_campaign_on_stage_found: messages.add_message( request, messages.ERROR, "Could not find Candidate's campaign when trying to create or edit a new position.") if position_id: return HttpResponseRedirect( reverse('organization:organization_position_edit', args=([organization_id], [position_id])) ) else: return HttpResponseRedirect( reverse('organization:organization_position_new', args=([organization_id])) ) elif contest_measure_id: logger.warn("contest_measure_id FOUND. Look for ContestMeasure here.") else: logger.warn("Neither candidate_campaign_id nor contest_measure_id found") messages.add_message( request, messages.ERROR, "Unable to find either Candidate or Measure.") return HttpResponseRedirect( reverse('organization:organization_position_list', args=([organization_id])) ) organization_position_on_stage_found = False logger.info("position_id: {position_id}".format(position_id=position_id)) # Retrieve position from position_id if it exists already if position_id > 0: 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: # If a position_id hasn't been passed in, then we are trying to create a new position. # Check to make sure a position for this org and candidate doesn't already exist position_entered_manager = PositionEnteredManager() results = position_entered_manager.retrieve_organization_candidate_campaign_position( organization_id, candidate_campaign_id) if results['MultipleObjectsReturned']: messages.add_message( request, messages.ERROR, "We found more than one existing positions for this candidate. Please delete all but one position.") return HttpResponseRedirect( reverse('organization:organization_position_list', args=([organization_id])) ) elif results['position_found']: organization_position_on_stage_found = True organization_position_on_stage = results['position'] # Now save existing, or create new success = False try: if organization_position_on_stage_found: # Update the position organization_position_on_stage.stance = stance organization_position_on_stage.google_civic_election_id = google_civic_election_id organization_position_on_stage.more_info_url = more_info_url organization_position_on_stage.statement_text = statement_text if not positive_value_exists(organization_position_on_stage.organization_we_vote_id): organization_position_on_stage.organization_we_vote_id = organization_on_stage.we_vote_id if not positive_value_exists(organization_position_on_stage.candidate_campaign_we_vote_id): organization_position_on_stage.candidate_campaign_we_vote_id = candidate_campaign_on_stage.we_vote_id if not positive_value_exists(organization_position_on_stage.google_civic_candidate_name): organization_position_on_stage.google_civic_candidate_name = \ candidate_campaign_on_stage.google_civic_candidate_name organization_position_on_stage.save() success = True messages.add_message( request, messages.INFO, "Position on {candidate_name} updated.".format( candidate_name=candidate_campaign_on_stage.candidate_name)) else: # Create new organization_position_on_stage = PositionEntered( organization_id=organization_id, organization_we_vote_id=organization_on_stage.we_vote_id, candidate_campaign_id=candidate_campaign_on_stage.id, candidate_campaign_we_vote_id=candidate_campaign_on_stage.we_vote_id, # Save candidate_campaign_on_stage so we can re-link candidates to positions if we_vote_id is lost google_civic_candidate_name=candidate_campaign_on_stage.google_civic_candidate_name, google_civic_election_id=google_civic_election_id, stance=stance, statement_text=statement_text, more_info_url=more_info_url, ) organization_position_on_stage.save() success = True messages.add_message( request, messages.INFO, "New position on {candidate_name} saved.".format( candidate_name=candidate_campaign_on_stage.candidate_name)) except Exception as e: handle_record_not_saved_exception(e, logger=logger) logger.error("Problem saving PositionEntered for CandidateCampaign") # If the position was saved, then update the voter_guide entry if success: voter_guide_manager = VoterGuideManager() results = voter_guide_manager.update_or_create_organization_voter_guide_by_election_id( organization_on_stage.we_vote_id, google_civic_election_id) # if results['success']: return HttpResponseRedirect(reverse('organization:organization_position_list', args=(organization_on_stage.id,)))
def position_we_vote_id(self): position_manager = PositionEnteredManager() retrieve_position_for_friends = False # Revisit this return position_manager.fetch_we_vote_id_from_local_id(self.voter_id, retrieve_position_for_friends)
def transfer_vote_smart_ratings_to_positions( candidate_campaign_id, politician_id): # TODO DALE Update for politician we_vote_organizations_created = 0 organization_positions_that_exist = 0 organization_positions_created = 0 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, 'we_vote_organizations_created': we_vote_organizations_created, 'organization_positions_that_exist': organization_positions_that_exist, 'organization_positions_created': organization_positions_created, } 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, 'we_vote_organizations_created': we_vote_organizations_created, 'organization_positions_that_exist': organization_positions_that_exist, 'organization_positions_created': organization_positions_created, } 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'] if update_results['organization_created']: we_vote_organizations_created += 1 # 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 organization_positions_that_exist += 1 continue else: position_results = position_manager.update_or_create_position( 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. display_candidate_name(), office_we_vote_id=False, candidate_we_vote_id=candidate_campaign.we_vote_id, measure_we_vote_id=False, stance=PERCENT_RATING, set_as_public_position=True, 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 positive_value_exists(position_results['success']): organization_positions_created += 1 else: 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 results = { 'status': status, 'success': success, 'we_vote_organizations_created': we_vote_organizations_created, 'organization_positions_that_exist': organization_positions_that_exist, 'organization_positions_created': organization_positions_created, } return results
def voter_guides_to_follow_retrieve_for_api(voter_device_id, # voterGuidesToFollow kind_of_ballot_item='', ballot_item_we_vote_id='', google_civic_election_id=0, search_string='', maximum_number_to_retrieve=0): # Get voter_id from the voter_device_id so we can figure out which voter_guides to offer results = is_voter_device_id_valid(voter_device_id) if not results['success']: json_data = { 'status': 'ERROR_GUIDES_TO_FOLLOW_NO_VOTER_DEVICE_ID', 'success': False, 'voter_device_id': voter_device_id, 'voter_guides': [], 'google_civic_election_id': google_civic_election_id, 'search_string': search_string, 'ballot_item_we_vote_id': ballot_item_we_vote_id, } results = { 'success': False, 'google_civic_election_id': 0, # Force the reset of google_civic_election_id cookie 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'json_data': json_data, } return results voter_id = fetch_voter_id_from_voter_device_link(voter_device_id) if not positive_value_exists(voter_id): json_data = { 'status': "ERROR_GUIDES_TO_FOLLOW_VOTER_NOT_FOUND_FROM_VOTER_DEVICE_ID", 'success': False, 'voter_device_id': voter_device_id, 'voter_guides': [], 'google_civic_election_id': google_civic_election_id, 'search_string': search_string, 'ballot_item_we_vote_id': ballot_item_we_vote_id, } results = { 'success': False, 'google_civic_election_id': 0, # Force the reset of google_civic_election_id cookie 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'json_data': json_data, } return results voter_guide_list = [] voter_guides = [] try: if positive_value_exists(kind_of_ballot_item) and positive_value_exists(ballot_item_we_vote_id): results = retrieve_voter_guides_to_follow_by_ballot_item(voter_id, kind_of_ballot_item, ballot_item_we_vote_id, search_string) success = results['success'] status = results['status'] voter_guide_list = results['voter_guide_list'] elif positive_value_exists(google_civic_election_id): # This retrieve also does the reordering results = retrieve_voter_guides_to_follow_by_election_for_api(voter_id, google_civic_election_id, search_string, maximum_number_to_retrieve, 'twitter_followers_count', 'desc') success = results['success'] status = results['status'] voter_guide_list = results['voter_guide_list'] else: results = retrieve_voter_guides_to_follow_generic_for_api(voter_id, search_string, maximum_number_to_retrieve, 'twitter_followers_count', 'desc') success = results['success'] status = results['status'] voter_guide_list = results['voter_guide_list'] except Exception as e: status = 'FAILED voter_guides_to_follow_retrieve_for_api, retrieve_voter_guides_for_election ' \ '{error} [type: {error_type}]'.format(error=e, error_type=type(e)) success = False if success: voter_manager = VoterManager() results = voter_manager.retrieve_voter_by_id(voter_id) linked_organization_we_vote_id = "" if results['voter_found']: voter = results['voter'] linked_organization_we_vote_id = voter.linked_organization_we_vote_id number_added_to_list = 0 position_manager = PositionEnteredManager() position = PositionEntered() for voter_guide in voter_guide_list: if positive_value_exists(voter_guide.organization_we_vote_id) \ and linked_organization_we_vote_id == voter_guide.organization_we_vote_id: # Do not return your own voter guide to follow continue position_found = False one_voter_guide = { 'we_vote_id': voter_guide.we_vote_id, 'google_civic_election_id': voter_guide.google_civic_election_id, 'time_span': voter_guide.vote_smart_time_span, 'voter_guide_display_name': voter_guide.voter_guide_display_name(), 'voter_guide_image_url': voter_guide.voter_guide_image_url(), 'voter_guide_owner_type': voter_guide.voter_guide_owner_type, 'organization_we_vote_id': voter_guide.organization_we_vote_id, 'public_figure_we_vote_id': voter_guide.public_figure_we_vote_id, 'twitter_description': voter_guide.twitter_description, 'twitter_followers_count': voter_guide.twitter_followers_count, 'twitter_handle': voter_guide.twitter_handle, 'owner_voter_id': voter_guide.owner_voter_id, 'last_updated': voter_guide.last_updated.strftime('%Y-%m-%d %H:%M'), } if positive_value_exists(ballot_item_we_vote_id): if kind_of_ballot_item == CANDIDATE: organization_manager = OrganizationManager() organization_id = organization_manager.fetch_organization_id( voter_guide.organization_we_vote_id) results = position_manager.retrieve_organization_candidate_campaign_position_with_we_vote_id( organization_id, ballot_item_we_vote_id) if results['position_found']: position = results['position'] position_found = True elif kind_of_ballot_item == MEASURE: organization_manager = OrganizationManager() organization_id = organization_manager.fetch_organization_id( voter_guide.organization_we_vote_id) results = position_manager.retrieve_organization_contest_measure_position_with_we_vote_id( organization_id, ballot_item_we_vote_id) if results['position_found']: position = results['position'] position_found = True if position_found: one_voter_guide['is_support'] = position.is_support() one_voter_guide['is_positive_rating'] = position.is_positive_rating() one_voter_guide['is_support_or_positive_rating'] = position.is_support_or_positive_rating() one_voter_guide['is_oppose'] = position.is_oppose() one_voter_guide['is_negative_rating'] = position.is_negative_rating() one_voter_guide['is_oppose_or_negative_rating'] = position.is_oppose_or_negative_rating() one_voter_guide['is_information_only'] = position.is_information_only() one_voter_guide['ballot_item_display_name'] = position.ballot_item_display_name one_voter_guide['speaker_display_name'] = position.speaker_display_name one_voter_guide['statement_text'] = position.statement_text one_voter_guide['more_info_url'] = position.more_info_url one_voter_guide['vote_smart_rating'] = position.vote_smart_rating one_voter_guide['vote_smart_time_span'] = position.vote_smart_time_span voter_guides.append(one_voter_guide.copy()) if positive_value_exists(maximum_number_to_retrieve): number_added_to_list += 1 if number_added_to_list >= maximum_number_to_retrieve: break if len(voter_guides): json_data = { 'status': status + ' VOTER_GUIDES_TO_FOLLOW_RETRIEVED', 'success': True, 'voter_device_id': voter_device_id, 'voter_guides': voter_guides, 'google_civic_election_id': google_civic_election_id, 'search_string': search_string, 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'maximum_number_to_retrieve': maximum_number_to_retrieve, } else: json_data = { 'status': status + ' NO_VOTER_GUIDES_FOUND', 'success': True, 'voter_device_id': voter_device_id, 'voter_guides': voter_guides, 'google_civic_election_id': google_civic_election_id, 'search_string': search_string, 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'maximum_number_to_retrieve': maximum_number_to_retrieve, } results = { 'success': success, 'google_civic_election_id': google_civic_election_id, 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'json_data': json_data, } return results else: json_data = { 'status': status, 'success': False, 'voter_device_id': voter_device_id, 'voter_guides': [], 'google_civic_election_id': google_civic_election_id, 'search_string': search_string, 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'maximum_number_to_retrieve': maximum_number_to_retrieve, } results = { 'success': False, 'google_civic_election_id': 0, # Force the reset of google_civic_election_id cookie 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'json_data': json_data, } return results