def update_social_media_statistics_in_other_tables(organization): """ Update other tables that use any of these social media statistics :param organization: :return: """ voter_guide_manager = VoterGuideManager() voter_guide_results = voter_guide_manager.update_voter_guide_social_media_statistics( organization) if voter_guide_results['success'] and voter_guide_results['voter_guide']: voter_guide = voter_guide_results['voter_guide'] else: voter_guide = VoterGuide() status = "FINISHED_UPDATE_SOCIAL_MEDIA_STATISTICS_IN_OTHER_TABLES" results = { 'success': True, 'status': status, 'organization': organization, 'voter_guide': voter_guide, } return results
def update_social_media_statistics_in_other_tables(organization): """ Update other tables that use any of these social media statistics :param organization: :return: """ voter_guide_manager = VoterGuideManager() voter_guide_results = voter_guide_manager.update_voter_guide_social_media_statistics(organization) if voter_guide_results['success'] and voter_guide_results['voter_guide']: voter_guide = voter_guide_results['voter_guide'] else: voter_guide = VoterGuide() status = "FINISHED_UPDATE_SOCIAL_MEDIA_STATISTICS_IN_OTHER_TABLES" results = { 'success': True, 'status': status, 'organization': organization, 'voter_guide': voter_guide, } 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_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 organization_edit_process_view(request): """ Process the new or edit organization forms :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) organization_id = convert_to_int(request.POST.get('organization_id', 0)) organization_name = request.POST.get('organization_name', '') organization_twitter_handle = request.POST.get('organization_twitter_handle', False) organization_facebook = request.POST.get('organization_facebook', False) organization_website = request.POST.get('organization_website', False) wikipedia_page_title = request.POST.get('wikipedia_page_title', False) wikipedia_photo_url = request.POST.get('wikipedia_photo_url', False) state_served_code = request.POST.get('state_served_code', False) # A positive value in google_civic_election_id or add_organization_button means we want to create a voter guide # for this org for this election google_civic_election_id = request.POST.get('google_civic_election_id', 0) # add_organization_button = request.POST.get('add_organization_button', False) # Filter incoming data organization_twitter_handle = extract_twitter_handle_from_text_string(organization_twitter_handle) # Check to see if this organization is already being used anywhere 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) try: if organization_on_stage_found: # Update if organization_name is not False: organization_on_stage.organization_name = organization_name if organization_twitter_handle is not False: organization_on_stage.organization_twitter_handle = organization_twitter_handle if organization_facebook is not False: organization_on_stage.organization_facebook = organization_facebook if organization_website is not False: organization_on_stage.organization_website = organization_website if wikipedia_page_title is not False: organization_on_stage.wikipedia_page_title = wikipedia_page_title if wikipedia_photo_url is not False: organization_on_stage.wikipedia_photo_url = wikipedia_photo_url if state_served_code is not False: organization_on_stage.state_served_code = state_served_code organization_on_stage.save() organization_id = organization_on_stage.id organization_we_vote_id = organization_on_stage.we_vote_id messages.add_message(request, messages.INFO, 'Organization updated.') else: # Create new # But first double-check that we don't have an org entry already organization_email = '' organization_list_manager = OrganizationListManager() results = organization_list_manager.organization_search_find_any_possibilities( organization_name, organization_twitter_handle, organization_website, organization_email) if results['organizations_found']: organizations_list = results['organizations_list'] organizations_count = len(organizations_list) messages.add_message(request, messages.INFO, 'We found {count} existing organizations ' 'that might match.'.format(count=organizations_count)) messages_on_stage = get_messages(request) template_values = { 'messages_on_stage': messages_on_stage, 'organizations_list': organizations_list, 'organization_name': organization_name, 'organization_twitter_handle': organization_twitter_handle, 'organization_facebook': organization_facebook, 'organization_website': organization_website, 'wikipedia_page_title': wikipedia_page_title, 'wikipedia_photo_url': wikipedia_photo_url, } return render(request, 'organization/organization_edit.html', template_values) minimum_required_variables_exist = positive_value_exists(organization_name) if not minimum_required_variables_exist: messages.add_message(request, messages.INFO, 'Missing name, which is required.') messages_on_stage = get_messages(request) template_values = { 'messages_on_stage': messages_on_stage, 'organization_name': organization_name, 'organization_twitter_handle': organization_twitter_handle, 'organization_facebook': organization_facebook, 'organization_website': organization_website, 'wikipedia_page_title': wikipedia_page_title, 'wikipedia_photo_url': wikipedia_photo_url, } return render(request, 'voter_guide/voter_guide_search.html', template_values) organization_on_stage = Organization( organization_name=organization_name, ) if organization_twitter_handle is not False: organization_on_stage.organization_twitter_handle = organization_twitter_handle if organization_facebook is not False: organization_on_stage.organization_facebook = organization_facebook if organization_website is not False: organization_on_stage.organization_website = organization_website if wikipedia_page_title is not False: organization_on_stage.wikipedia_page_title = wikipedia_page_title if wikipedia_photo_url is not False: organization_on_stage.wikipedia_photo_url = wikipedia_photo_url if state_served_code is not False: organization_on_stage.state_served_code = state_served_code organization_on_stage.save() organization_id = organization_on_stage.id organization_we_vote_id = organization_on_stage.we_vote_id messages.add_message(request, messages.INFO, 'New organization saved.') except Exception as e: messages.add_message(request, messages.ERROR, 'Could not save organization.' ' {error} [type: {error_type}]'.format(error=e, error_type=type(e))) return HttpResponseRedirect(reverse('organization:organization_list', args=())) # Create voter_guide for this election? if positive_value_exists(google_civic_election_id) and positive_value_exists(organization_we_vote_id): election_manager = ElectionManager() results = election_manager.retrieve_election(google_civic_election_id) if results['election_found']: election = results['election'] voter_guide_manager = VoterGuideManager() results = voter_guide_manager.update_or_create_organization_voter_guide_by_election_id( organization_we_vote_id, google_civic_election_id) if results['voter_guide_saved']: messages.add_message(request, messages.INFO, 'Voter guide for {election_name} election saved.' ''.format(election_name=election.election_name)) return HttpResponseRedirect(reverse('organization:organization_position_list', args=(organization_id,)) + "?google_civic_election_id=" + str(google_civic_election_id))
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 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 retrieve_voter_guides_to_follow_by_election(voter_id, google_civic_election_id): voter_guide_list_found = False position_list_manager = PositionListManager() if positive_value_exists(google_civic_election_id): all_positions_list = position_list_manager.retrieve_all_positions_for_election( google_civic_election_id, ANY_STANCE) else: voter_guide_list = [] results = { 'success': False, 'status': "VOTER_GUIDES_BALLOT_RELATED_VARIABLES_MISSING", 'voter_guide_list_found': False, 'voter_guide_list': voter_guide_list, } return results follow_organization_list_manager = FollowOrganizationList() organizations_followed_by_voter = \ follow_organization_list_manager.retrieve_follow_organization_by_voter_id_simple_id_array(voter_id) positions_list = position_list_manager.calculate_positions_not_followed_by_voter( all_positions_list, organizations_followed_by_voter) voter_guide_list = [] # Cycle through the positions held by groups that you don't currently follow voter_guide_manager = VoterGuideManager() for one_position in positions_list: if positive_value_exists(one_position.organization_we_vote_id): if one_position.google_civic_election_id: results = voter_guide_manager.retrieve_voter_guide( voter_guide_id=0, google_civic_election_id=one_position.google_civic_election_id, vote_smart_time_span=None, organization_we_vote_id=one_position.organization_we_vote_id) else: # vote_smart_time_span results = voter_guide_manager.retrieve_voter_guide( voter_guide_id=0, google_civic_election_id=0, vote_smart_time_span=one_position.vote_smart_time_span, organization_we_vote_id=one_position.organization_we_vote_id) elif positive_value_exists(one_position.public_figure_we_vote_id): results['voter_guide_found'] = False elif positive_value_exists(one_position.voter_we_vote_id): results['voter_guide_found'] = False else: results['voter_guide_found'] = False if results['voter_guide_found']: voter_guide_list.append(results['voter_guide']) status = 'SUCCESSFUL_RETRIEVE_OF_POSITIONS_NOT_FOLLOWED' success = True if len(voter_guide_list): voter_guide_list_found = True results = { 'success': success, 'status': status, 'voter_guide_list_found': voter_guide_list_found, 'voter_guide_list': voter_guide_list, } return results
def voter_guides_import_from_structured_json(structured_json): """ This pathway in requires a we_vote_id, and is not used when we import from Google Civic :param structured_json: :return: """ voter_guide_manager = VoterGuideManager() organization_manager = OrganizationManager() organization_id = 0 voter_guides_saved = 0 voter_guides_updated = 0 voter_guides_not_processed = 0 for one_voter_guide in structured_json: we_vote_id = one_voter_guide['we_vote_id'] if 'we_vote_id' in one_voter_guide else '' google_civic_election_id = one_voter_guide['google_civic_election_id'] \ if 'google_civic_election_id' in one_voter_guide else '' vote_smart_time_span = one_voter_guide['vote_smart_time_span'] \ if 'vote_smart_time_span' in one_voter_guide else '' organization_we_vote_id = one_voter_guide['organization_we_vote_id'] \ if 'organization_we_vote_id' in one_voter_guide else '' public_figure_we_vote_id = one_voter_guide['public_figure_we_vote_id'] \ if 'public_figure_we_vote_id' in one_voter_guide else '' if positive_value_exists(we_vote_id) and \ (positive_value_exists(organization_we_vote_id) or positive_value_exists(public_figure_we_vote_id)) and \ (positive_value_exists(google_civic_election_id) or positive_value_exists(vote_smart_time_span)): # Make sure we have the organization (or public figure) in this database before we import the voter guide if positive_value_exists(organization_we_vote_id): results = organization_manager.retrieve_organization_from_we_vote_id(organization_we_vote_id) if results['organization_found']: organization_id = results['organization_id'] if positive_value_exists(organization_id): proceed_to_update_or_create = True else: proceed_to_update_or_create = False elif positive_value_exists(public_figure_we_vote_id): # TODO DALE Update this to work with public_figure public_figure_id = organization_manager.retrieve_organization_from_we_vote_id(public_figure_we_vote_id) if positive_value_exists(public_figure_id): proceed_to_update_or_create = True else: proceed_to_update_or_create = False else: proceed_to_update_or_create = False else: proceed_to_update_or_create = False if proceed_to_update_or_create: if positive_value_exists(organization_we_vote_id) and positive_value_exists(google_civic_election_id): results = voter_guide_manager.update_or_create_organization_voter_guide_by_election_id( organization_we_vote_id, google_civic_election_id) elif positive_value_exists(organization_we_vote_id) and positive_value_exists(vote_smart_time_span): results = voter_guide_manager.update_or_create_organization_voter_guide_by_time_span( organization_we_vote_id, vote_smart_time_span) elif positive_value_exists(public_figure_we_vote_id) and positive_value_exists(google_civic_election_id): results = voter_guide_manager.update_or_create_public_figure_voter_guide( google_civic_election_id, public_figure_we_vote_id) else: results = { 'success': False, 'status': 'Required value missing, cannot update or create (1)' } else: voter_guides_not_processed += 1 results = { 'success': False, 'status': 'Required value missing, cannot update or create (2)' } if results['success']: if results['new_voter_guide_created']: voter_guides_saved += 1 else: voter_guides_updated += 1 else: voter_guides_not_processed += 1 voter_guides_results = { 'success': True, 'status': "VOTER_GUIDES_IMPORT_PROCESS_COMPLETE", 'saved': voter_guides_saved, 'updated': voter_guides_updated, 'not_processed': voter_guides_not_processed, } return voter_guides_results
def retrieve_voter_guides_to_follow_by_ballot_item(voter_id, kind_of_ballot_item, ballot_item_we_vote_id, search_string): voter_guide_list_found = False retrieve_public_positions = True # The alternate is positions for friends-only. Since this method returns positions # to follow, we never need to return friend's positions here position_list_manager = PositionListManager() if (kind_of_ballot_item == CANDIDATE) and positive_value_exists(ballot_item_we_vote_id): candidate_id = 0 all_positions_list = position_list_manager.retrieve_all_positions_for_candidate_campaign( retrieve_public_positions, candidate_id, ballot_item_we_vote_id, ANY_STANCE) elif (kind_of_ballot_item == MEASURE) and positive_value_exists(ballot_item_we_vote_id): measure_id = 0 all_positions_list = position_list_manager.retrieve_all_positions_for_contest_measure( retrieve_public_positions, measure_id, ballot_item_we_vote_id, ANY_STANCE) elif (kind_of_ballot_item == OFFICE) and positive_value_exists(ballot_item_we_vote_id): office_id = 0 all_positions_list = position_list_manager.retrieve_all_positions_for_contest_office( office_id, ballot_item_we_vote_id, ANY_STANCE) else: voter_guide_list = [] results = { 'success': False, 'status': "VOTER_GUIDES_BALLOT_RELATED_VARIABLES_MISSING", 'search_string': search_string, 'voter_guide_list_found': False, 'voter_guide_list': voter_guide_list, } return results follow_organization_list_manager = FollowOrganizationList() organizations_followed_by_voter = \ follow_organization_list_manager.retrieve_follow_organization_by_voter_id_simple_id_array(voter_id) positions_list = position_list_manager.calculate_positions_not_followed_by_voter( all_positions_list, organizations_followed_by_voter) voter_guide_list = [] # Cycle through the positions held by groups that you don't currently follow voter_guide_manager = VoterGuideManager() for one_position in positions_list: if positive_value_exists(one_position.organization_we_vote_id): if one_position.google_civic_election_id: results = voter_guide_manager.retrieve_voter_guide( voter_guide_id=0, google_civic_election_id=one_position.google_civic_election_id, vote_smart_time_span=None, organization_we_vote_id=one_position.organization_we_vote_id) else: # vote_smart_time_span results = voter_guide_manager.retrieve_voter_guide( voter_guide_id=0, google_civic_election_id=0, vote_smart_time_span=one_position.vote_smart_time_span, organization_we_vote_id=one_position.organization_we_vote_id) elif positive_value_exists(one_position.public_figure_we_vote_id): results['voter_guide_found'] = False elif positive_value_exists(one_position.voter_we_vote_id): results['voter_guide_found'] = False else: results['voter_guide_found'] = False if results['voter_guide_found']: voter_guide = results['voter_guide'] # If we passed in search_string, make sure they are in this entry. # If they aren't, don't return voter guide if positive_value_exists(search_string): search_string = str(search_string) # Make sure search_string is a string twitter_handle = str(voter_guide.twitter_handle) display_name = str(voter_guide.display_name) if search_string.lower() in twitter_handle.lower() or search_string.lower() in display_name.lower(): voter_guide_list.append(voter_guide) else: voter_guide_list.append(voter_guide) status = 'SUCCESSFUL_RETRIEVE_OF_VOTER_GUIDES_BY_BALLOT_ITEM' success = True if len(voter_guide_list): voter_guide_list_found = True results = { 'success': success, 'status': status, 'search_string': search_string, 'voter_guide_list_found': voter_guide_list_found, 'voter_guide_list': voter_guide_list, } return results
def retrieve_voter_guides_to_follow_by_ballot_item(voter_id, kind_of_ballot_item, ballot_item_we_vote_id, search_string): voter_guide_list_found = False position_list_manager = PositionListManager() if (kind_of_ballot_item == CANDIDATE) and positive_value_exists(ballot_item_we_vote_id): candidate_id = 0 all_positions_list = position_list_manager.retrieve_all_positions_for_candidate_campaign( candidate_id, ballot_item_we_vote_id, ANY_STANCE) elif (kind_of_ballot_item == MEASURE) and positive_value_exists(ballot_item_we_vote_id): measure_id = 0 all_positions_list = position_list_manager.retrieve_all_positions_for_contest_measure( measure_id, ballot_item_we_vote_id, ANY_STANCE) elif (kind_of_ballot_item == OFFICE) and positive_value_exists(ballot_item_we_vote_id): office_id = 0 all_positions_list = position_list_manager.retrieve_all_positions_for_contest_office( office_id, ballot_item_we_vote_id, ANY_STANCE) else: voter_guide_list = [] results = { 'success': False, 'status': "VOTER_GUIDES_BALLOT_RELATED_VARIABLES_MISSING", 'search_string': search_string, 'voter_guide_list_found': False, 'voter_guide_list': voter_guide_list, } return results follow_organization_list_manager = FollowOrganizationList() organizations_followed_by_voter = \ follow_organization_list_manager.retrieve_follow_organization_by_voter_id_simple_id_array(voter_id) positions_list = position_list_manager.calculate_positions_not_followed_by_voter( all_positions_list, organizations_followed_by_voter) voter_guide_list = [] # Cycle through the positions held by groups that you don't currently follow voter_guide_manager = VoterGuideManager() for one_position in positions_list: if positive_value_exists(one_position.organization_we_vote_id): if one_position.google_civic_election_id: results = voter_guide_manager.retrieve_voter_guide( voter_guide_id=0, google_civic_election_id=one_position. google_civic_election_id, vote_smart_time_span=None, organization_we_vote_id=one_position. organization_we_vote_id) else: # vote_smart_time_span results = voter_guide_manager.retrieve_voter_guide( voter_guide_id=0, google_civic_election_id=0, vote_smart_time_span=one_position.vote_smart_time_span, organization_we_vote_id=one_position. organization_we_vote_id) elif positive_value_exists(one_position.public_figure_we_vote_id): results['voter_guide_found'] = False elif positive_value_exists(one_position.voter_we_vote_id): results['voter_guide_found'] = False else: results['voter_guide_found'] = False if results['voter_guide_found']: voter_guide = results['voter_guide'] # If we passed in search_string, make sure they are in this entry. # If they aren't, don't return voter guide if positive_value_exists(search_string): search_string = str( search_string) # Make sure search_string is a string twitter_handle = str(voter_guide.twitter_handle) display_name = str(voter_guide.display_name) if search_string.lower() in twitter_handle.lower( ) or search_string.lower() in display_name.lower(): voter_guide_list.append(voter_guide) else: voter_guide_list.append(voter_guide) status = 'SUCCESSFUL_RETRIEVE_OF_VOTER_GUIDES_BY_BALLOT_ITEM' success = True if len(voter_guide_list): voter_guide_list_found = True results = { 'success': success, 'status': status, 'search_string': search_string, 'voter_guide_list_found': voter_guide_list_found, 'voter_guide_list': voter_guide_list, } return results
def voter_guides_import_from_structured_json(structured_json): """ This pathway in requires a we_vote_id, and is not used when we import from Google Civic :param structured_json: :return: """ voter_guide_manager = VoterGuideManager() organization_manager = OrganizationManager() organization_id = 0 voter_guides_saved = 0 voter_guides_updated = 0 voter_guides_not_processed = 0 for one_voter_guide in structured_json: we_vote_id = one_voter_guide[ 'we_vote_id'] if 'we_vote_id' in one_voter_guide else '' google_civic_election_id = one_voter_guide['google_civic_election_id'] \ if 'google_civic_election_id' in one_voter_guide else '' vote_smart_time_span = one_voter_guide['vote_smart_time_span'] \ if 'vote_smart_time_span' in one_voter_guide else '' organization_we_vote_id = one_voter_guide['organization_we_vote_id'] \ if 'organization_we_vote_id' in one_voter_guide else '' public_figure_we_vote_id = one_voter_guide['public_figure_we_vote_id'] \ if 'public_figure_we_vote_id' in one_voter_guide else '' if positive_value_exists(we_vote_id) and \ (positive_value_exists(organization_we_vote_id) or positive_value_exists(public_figure_we_vote_id)) and \ (positive_value_exists(google_civic_election_id) or positive_value_exists(vote_smart_time_span)): # Make sure we have the organization (or public figure) in this database before we import the voter guide if positive_value_exists(organization_we_vote_id): results = organization_manager.retrieve_organization_from_we_vote_id( organization_we_vote_id) if results['organization_found']: organization_id = results['organization_id'] if positive_value_exists(organization_id): proceed_to_update_or_create = True else: proceed_to_update_or_create = False elif positive_value_exists(public_figure_we_vote_id): # TODO DALE Update this to work with public_figure public_figure_id = organization_manager.retrieve_organization_from_we_vote_id( public_figure_we_vote_id) if positive_value_exists(public_figure_id): proceed_to_update_or_create = True else: proceed_to_update_or_create = False else: proceed_to_update_or_create = False else: proceed_to_update_or_create = False if proceed_to_update_or_create: if positive_value_exists( organization_we_vote_id) and positive_value_exists( google_civic_election_id): results = voter_guide_manager.update_or_create_organization_voter_guide_by_election_id( organization_we_vote_id, google_civic_election_id) elif positive_value_exists( organization_we_vote_id) and positive_value_exists( vote_smart_time_span): results = voter_guide_manager.update_or_create_organization_voter_guide_by_time_span( organization_we_vote_id, vote_smart_time_span) elif positive_value_exists( public_figure_we_vote_id) and positive_value_exists( google_civic_election_id): results = voter_guide_manager.update_or_create_public_figure_voter_guide( google_civic_election_id, public_figure_we_vote_id) else: results = { 'success': False, 'status': 'Required value missing, cannot update or create (1)' } else: voter_guides_not_processed += 1 results = { 'success': False, 'status': 'Required value missing, cannot update or create (2)' } if results['success']: if results['new_voter_guide_created']: voter_guides_saved += 1 else: voter_guides_updated += 1 else: voter_guides_not_processed += 1 voter_guides_results = { 'success': True, 'status': "VOTER_GUIDES_IMPORT_PROCESS_COMPLETE", 'saved': voter_guides_saved, 'updated': voter_guides_updated, 'not_processed': voter_guides_not_processed, } return voter_guides_results