def refresh_twitter_candidate_details_for_election(google_civic_election_id): twitter_handles_added = 0 profiles_refreshed_with_twitter_data = 0 google_civic_election_id = convert_to_int(google_civic_election_id) candidate_list_manager = CandidateCampaignListManager() return_list_of_objects = True candidates_results = candidate_list_manager.retrieve_all_candidates_for_upcoming_election( google_civic_election_id, return_list_of_objects) if candidates_results['candidate_list_found']: candidate_list = candidates_results['candidate_list_objects'] for candidate in candidate_list: # Extract twitter_handle from google_civic_election information if positive_value_exists(candidate.twitter_url) \ and not positive_value_exists(candidate.candidate_twitter_handle): # If we got a twitter_url from Google Civic, and we haven't already stored a twitter handle, move it candidate.candidate_twitter_handle = extract_twitter_handle_from_text_string(candidate.twitter_url) candidate.save() twitter_handles_added += 1 if positive_value_exists(candidate.candidate_twitter_handle): refresh_twitter_candidate_details(candidate) profiles_refreshed_with_twitter_data += 1 status = "CANDIDATE_SOCIAL_MEDIA_RETRIEVED" results = { 'success': True, 'status': status, 'twitter_handles_added': twitter_handles_added, 'profiles_refreshed_with_twitter_data': profiles_refreshed_with_twitter_data, } return results
def fetch_twitter_handle(self): if positive_value_exists(self.candidate_twitter_handle): return self.candidate_twitter_handle elif self.twitter_url: # Extract the twitter handle from twitter_url if we don't have it stored as a handle yet return extract_twitter_handle_from_text_string(self.twitter_url) return self.twitter_url
def retrieve_candidates_from_non_unique_identifiers(self, twitter_handle, google_civic_election_id=0): candidate_list_objects = [] candidate_list_found = False twitter_handle_filtered = extract_twitter_handle_from_text_string(twitter_handle) try: candidate_queryset = CandidateCampaign.objects.all() candidate_queryset = candidate_queryset.filter(candidate_twitter_handle__iexact=twitter_handle_filtered) if positive_value_exists(google_civic_election_id): candidate_queryset = candidate_queryset.filter(google_civic_election_id=google_civic_election_id) candidate_queryset = candidate_queryset.order_by('-id') candidate_list_objects = candidate_queryset if len(candidate_list_objects): candidate_list_found = True status = 'CANDIDATES_RETRIEVED_FROM_TWITTER_HANDLE' success = True else: status = 'NO_CANDIDATES_RETRIEVED_FROM_TWITTER_HANDLE' success = True except CandidateCampaign.DoesNotExist: # No candidates found. Not a problem. status = 'NO_CANDIDATES_FOUND_FROM_TWITTER_HANDLE_DoesNotExist' candidate_list_objects = [] success = True except Exception as e: handle_exception(e, logger=logger) status = 'FAILED retrieve_candidates_from_non_unique_identifiers ' \ '{error} [type: {error_type}]'.format(error=e, error_type=type(e)) success = False results = { 'success': success, 'status': status, 'google_civic_election_id': google_civic_election_id, 'candidate_list_found': candidate_list_found, 'candidate_list': candidate_list_objects, } return results
def refresh_twitter_candidate_details_for_election(google_civic_election_id): twitter_handles_added = 0 profiles_refreshed_with_twitter_data = 0 google_civic_election_id = convert_to_int(google_civic_election_id) candidate_list_manager = CandidateCampaignListManager() return_list_of_objects = True candidates_results = candidate_list_manager.retrieve_all_candidates_for_upcoming_election( google_civic_election_id, return_list_of_objects) if candidates_results['candidate_list_found']: candidate_list = candidates_results['candidate_list_objects'] for candidate in candidate_list: # Extract twitter_handle from google_civic_election information if positive_value_exists(candidate.twitter_url) \ and not positive_value_exists(candidate.candidate_twitter_handle): # If we got a twitter_url from Google Civic, and we haven't already stored a twitter handle, move it candidate.candidate_twitter_handle = extract_twitter_handle_from_text_string( candidate.twitter_url) candidate.save() twitter_handles_added += 1 if positive_value_exists(candidate.candidate_twitter_handle): refresh_twitter_candidate_details(candidate) profiles_refreshed_with_twitter_data += 1 status = "CANDIDATE_SOCIAL_MEDIA_RETRIEVED" results = { 'success': True, 'status': status, 'twitter_handles_added': twitter_handles_added, 'profiles_refreshed_with_twitter_data': profiles_refreshed_with_twitter_data, } return results
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 voter_guide_search_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) add_organization_button = request.POST.get('add_organization_button', False) if add_organization_button: return organization_edit_process_view(request) organization_name = request.POST.get('organization_name', '') organization_twitter_handle = request.POST.get('organization_twitter_handle', '') organization_facebook = request.POST.get('organization_facebook', '') organization_website = request.POST.get('organization_website', '') # state_served_code = request.POST.get('state_served_code', False) # Save this variable so we have it on the "Add New Position" page google_civic_election_id = request.POST.get('google_civic_election_id', 0) # Filter incoming data organization_twitter_handle = extract_twitter_handle_from_text_string(organization_twitter_handle) # Search for organizations that match organization_email = '' organization_list_manager = OrganizationListManager() results = organization_list_manager.organization_search_find_any_possibilities( organization_name, organization_twitter_handle, organization_website, organization_email, organization_facebook) if results['organizations_found']: organizations_list = results['organizations_list'] organizations_count = len(organizations_list) messages.add_message(request, messages.INFO, 'We found {count} existing organization(s) ' 'that might match.'.format(count=organizations_count)) else: organizations_list = [] messages.add_message(request, messages.INFO, 'No voter guides found with those search terms. ' 'Please try again. ') election_manager = ElectionManager() upcoming_election_list = [] results = election_manager.retrieve_upcoming_elections() if results['success']: upcoming_election_list = results['election_list'] 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, 'upcoming_election_list': upcoming_election_list, 'google_civic_election_id': google_civic_election_id, } return render(request, 'voter_guide/voter_guide_search.html', template_values)
def voter_guide_search_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) add_organization_button = request.POST.get('add_organization_button', False) if add_organization_button: return organization_edit_process_view(request) organization_name = request.POST.get('organization_name', '') organization_twitter_handle = request.POST.get( 'organization_twitter_handle', '') organization_facebook = request.POST.get('organization_facebook', '') organization_website = request.POST.get('organization_website', '') # state_served_code = request.POST.get('state_served_code', False) # Save this variable so we have it on the "Add New Position" page google_civic_election_id = request.POST.get('google_civic_election_id', 0) # Filter incoming data organization_twitter_handle = extract_twitter_handle_from_text_string( organization_twitter_handle) # Search for organizations that match organization_email = '' organization_list_manager = OrganizationListManager() results = organization_list_manager.organization_search_find_any_possibilities( organization_name, organization_twitter_handle, organization_website, organization_email, organization_facebook) if results['organizations_found']: organizations_list = results['organizations_list'] organizations_count = len(organizations_list) messages.add_message( request, messages.INFO, 'We found {count} existing organization(s) ' 'that might match.'.format(count=organizations_count)) else: organizations_list = [] messages.add_message( request, messages.INFO, 'No voter guides found with those search terms. ' 'Please try again. ') election_manager = ElectionManager() upcoming_election_list = [] results = election_manager.retrieve_upcoming_elections() if results['success']: upcoming_election_list = results['election_list'] 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, 'upcoming_election_list': upcoming_election_list, 'google_civic_election_id': google_civic_election_id, } return render(request, 'voter_guide/voter_guide_search.html', template_values)
def organization_save_for_api(voter_device_id, organization_id, organization_we_vote_id, # organizationSave organization_name, organization_email, organization_website, organization_twitter_handle, organization_facebook, organization_image, refresh_from_twitter): organization_id = convert_to_int(organization_id) organization_we_vote_id = organization_we_vote_id.strip().lower() # Make sure we are only working with the twitter handle, and not the "https" or "@" organization_twitter_handle = extract_twitter_handle_from_text_string(organization_twitter_handle) existing_unique_identifier_found = positive_value_exists(organization_id) \ or positive_value_exists(organization_we_vote_id) new_unique_identifier_found = positive_value_exists(organization_twitter_handle) \ or positive_value_exists(organization_website) unique_identifier_found = existing_unique_identifier_found or new_unique_identifier_found # We must have one of these: twitter_handle or website, AND organization_name required_variables_for_new_entry = positive_value_exists(organization_twitter_handle) \ or positive_value_exists(organization_website) and positive_value_exists(organization_name) if not unique_identifier_found: results = { 'status': "ORGANIZATION_REQUIRED_UNIQUE_IDENTIFIER_VARIABLES_MISSING", 'success': False, 'organization_id': organization_id, 'organization_we_vote_id': organization_we_vote_id, 'new_organization_created': False, 'organization_name': organization_name, 'organization_email': organization_email, 'organization_website': organization_website, 'organization_facebook': organization_facebook, 'organization_photo_url': organization_image, 'organization_twitter_handle': organization_twitter_handle, 'twitter_followers_count': 0, 'twitter_description': "", 'refresh_from_twitter': refresh_from_twitter, } return results elif not existing_unique_identifier_found and not required_variables_for_new_entry: results = { 'status': "NEW_ORGANIZATION_REQUIRED_VARIABLES_MISSING", 'success': False, 'organization_id': organization_id, 'organization_we_vote_id': organization_we_vote_id, 'new_organization_created': False, 'organization_name': organization_name, 'organization_email': organization_email, 'organization_website': organization_website, 'organization_facebook': organization_facebook, 'organization_photo_url': organization_image, 'organization_twitter_handle': organization_twitter_handle, 'twitter_followers_count': 0, 'twitter_description': "", 'refresh_from_twitter': refresh_from_twitter, } return results organization_manager = OrganizationManager() save_results = organization_manager.update_or_create_organization( organization_id=organization_id, we_vote_id=organization_we_vote_id, organization_website_search=organization_website, organization_twitter_search=organization_twitter_handle, organization_name=organization_name, organization_website=organization_website, organization_twitter_handle=organization_twitter_handle, organization_email=organization_email, organization_facebook=organization_facebook, organization_image=organization_image, refresh_from_twitter=refresh_from_twitter) if save_results['success']: organization = save_results['organization'] status = save_results['status'] # Now update the voter record with the organization_we_vote_id voter_manager = VoterManager() voter_results = voter_manager.retrieve_voter_from_voter_device_id(voter_device_id) if voter_results['voter_found']: voter = voter_results['voter'] # Does this voter have the same Twitter handle as this organization? If so, link this organization to # this particular voter if voter.twitter_screen_name.lower() == organization.organization_twitter_handle.lower(): try: voter.linked_organization_we_vote_id = organization.we_vote_id voter.save() except Exception as e: status += " UNABLE_TO_UPDATE_VOTER_WITH_ORGANIZATION_WE_VOTE_ID" # If not, then this is a volunteer or admin setting up an organization else: status += " DID_NOT_UPDATE_VOTER_WITH_ORGANIZATION_WE_VOTE_ID-VOTER_DOES_NOT_MATCH_TWITTER_HANDLE" results = { 'success': save_results['success'], 'status': status, 'voter_device_id': voter_device_id, 'organization_id': organization.id, 'organization_we_vote_id': organization.we_vote_id, 'new_organization_created': save_results['new_organization_created'], 'organization_name': organization.organization_name if positive_value_exists(organization.organization_name) else '', 'organization_email': organization.organization_email if positive_value_exists(organization.organization_email) else '', 'organization_website': organization.organization_website if positive_value_exists(organization.organization_website) else '', 'organization_facebook': organization.organization_facebook if positive_value_exists(organization.organization_facebook) else '', 'organization_photo_url': organization.organization_photo_url() if positive_value_exists(organization.organization_photo_url()) else '', 'organization_twitter_handle': organization.organization_twitter_handle if positive_value_exists( organization.organization_twitter_handle) else '', 'twitter_followers_count': organization.twitter_followers_count if positive_value_exists( organization.twitter_followers_count) else 0, 'twitter_description': organization.twitter_description if positive_value_exists( organization.twitter_description) else '', 'refresh_from_twitter': refresh_from_twitter, } return results else: results = { 'success': False, 'status': save_results['status'], 'voter_device_id': voter_device_id, 'organization_id': organization_id, 'organization_we_vote_id': organization_we_vote_id, 'new_organization_created': save_results['new_organization_created'], 'organization_name': organization_name, 'organization_email': organization_email, 'organization_website': organization_website, 'organization_facebook': organization_facebook, 'organization_photo_url': organization_image, 'organization_twitter_handle': organization_twitter_handle, 'twitter_followers_count': 0, 'twitter_description': "", 'refresh_from_twitter': refresh_from_twitter, } return results
def politician_edit_process_view( request): # TODO DALE Transition fully to politician """ Process the new or edit politician 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) politician_id = convert_to_int(request.POST['politician_id']) politician_name = request.POST.get('politician_name', False) google_civic_candidate_name = request.POST.get( 'google_civic_candidate_name', False) politician_twitter_handle = request.POST.get('politician_twitter_handle', False) if positive_value_exists(politician_twitter_handle): politician_twitter_handle = extract_twitter_handle_from_text_string( politician_twitter_handle) politician_url = request.POST.get('politician_url', False) political_party = request.POST.get('political_party', False) vote_smart_id = request.POST.get('vote_smart_id', False) maplight_id = request.POST.get('maplight_id', False) state_code = request.POST.get('state_code', False) politician_we_vote_id = request.POST.get('politician_we_vote_id', False) # Check to see if this politician is already being used anywhere politician_on_stage_found = False politician_on_stage = Politician() if positive_value_exists(politician_id): try: politician_query = Politician.objects.filter(id=politician_id) if len(politician_query): politician_on_stage = politician_query[0] politician_on_stage_found = True except Exception as e: pass # Check to see if there is a duplicate politician already saved for this election existing_politician_found = False if not positive_value_exists(politician_id): try: filter_list = Q() at_least_one_filter = False if positive_value_exists(vote_smart_id): at_least_one_filter = True filter_list |= Q(vote_smart_id=vote_smart_id) if positive_value_exists(maplight_id): at_least_one_filter = True filter_list |= Q(maplight_id=maplight_id) if positive_value_exists(politician_twitter_handle): at_least_one_filter = True filter_list |= Q( politician_twitter_handle=politician_twitter_handle) if at_least_one_filter: politician_duplicates_query = Politician.objects.filter( filter_list) if len(politician_duplicates_query): existing_politician_found = True except Exception as e: pass try: if existing_politician_found: messages.add_message( request, messages.ERROR, 'This politician is already saved for this election.') url_variables = "?politician_name=" + str(politician_name) + \ "&state_code=" + str(state_code) + \ "&google_civic_candidate_name=" + str(google_civic_candidate_name) + \ "&politician_twitter_handle=" + str(politician_twitter_handle) + \ "&politician_url=" + str(politician_url) + \ "&political_party=" + str(political_party) + \ "&vote_smart_id=" + str(vote_smart_id) + \ "&politician_we_vote_id=" + str(politician_we_vote_id) + \ "&maplight_id=" + str(maplight_id) return HttpResponseRedirect( reverse('politician:politician_new', args=()) + url_variables) elif politician_on_stage_found: # Update if politician_name is not False: politician_on_stage.politician_name = politician_name # Re-save first_name, middle name, and last name politician_on_stage.first_name = extract_first_name_from_full_name( politician_name) politician_on_stage.middle_name = extract_middle_name_from_full_name( politician_name) politician_on_stage.last_name = extract_last_name_from_full_name( politician_name) if state_code is not False: politician_on_stage.state_code = state_code if google_civic_candidate_name is not False: politician_on_stage.google_civic_candidate_name = google_civic_candidate_name if politician_twitter_handle is not False: politician_on_stage.politician_twitter_handle = politician_twitter_handle if politician_url is not False: politician_on_stage.politician_url = politician_url if political_party is not False: political_party = convert_to_political_party_constant( political_party) politician_on_stage.political_party = political_party if vote_smart_id is not False: politician_on_stage.vote_smart_id = vote_smart_id if maplight_id is not False: politician_on_stage.maplight_id = maplight_id politician_on_stage.save() messages.add_message(request, messages.INFO, 'Politician updated.') else: # Create new required_politician_variables = True \ if positive_value_exists(politician_name) \ else False if required_politician_variables: politician_on_stage = Politician( politician_name=politician_name, state_code=state_code, ) politician_on_stage.first_name = extract_first_name_from_full_name( politician_name) politician_on_stage.middle_name = extract_middle_name_from_full_name( politician_name) politician_on_stage.last_name = extract_last_name_from_full_name( politician_name) if google_civic_candidate_name is not False: politician_on_stage.google_civic_candidate_name = google_civic_candidate_name if politician_twitter_handle is not False: politician_on_stage.politician_twitter_handle = politician_twitter_handle if politician_url is not False: politician_on_stage.politician_url = politician_url if political_party is not False: political_party = convert_to_political_party_constant( political_party) politician_on_stage.political_party = political_party if vote_smart_id is not False: politician_on_stage.vote_smart_id = vote_smart_id if maplight_id is not False: politician_on_stage.maplight_id = maplight_id if politician_we_vote_id is not False: politician_on_stage.politician_we_vote_id = politician_we_vote_id politician_on_stage.save() politician_id = politician_on_stage.id messages.add_message(request, messages.INFO, 'New politician saved.') else: # messages.add_message(request, messages.INFO, 'Could not save -- missing required variables.') url_variables = "?politician_name=" + str(politician_name) + \ "&state_code=" + str(state_code) + \ "&google_civic_candidate_name=" + str(google_civic_candidate_name) + \ "&politician_twitter_handle=" + str(politician_twitter_handle) + \ "&politician_url=" + str(politician_url) + \ "&political_party=" + str(political_party) + \ "&vote_smart_id=" + str(vote_smart_id) + \ "&politician_we_vote_id=" + str(politician_we_vote_id) + \ "&maplight_id=" + str(maplight_id) if positive_value_exists(politician_id): return HttpResponseRedirect( reverse('politician:politician_edit', args=(politician_id, )) + url_variables) else: return HttpResponseRedirect( reverse('politician:politician_new', args=()) + url_variables) except Exception as e: handle_record_not_saved_exception(e, logger=logger) messages.add_message(request, messages.ERROR, 'Could not save politician.') return HttpResponseRedirect( reverse('politician:politician_edit', args=(politician_id, ))) if politician_id: return HttpResponseRedirect( reverse('politician:politician_edit', args=(politician_id, ))) else: return HttpResponseRedirect( reverse('politician:politician_new', args=()))
def politician_edit_process_view(request): """ Process the new or edit politician 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) politician_id = convert_to_int(request.POST['politician_id']) politician_name = request.POST.get('politician_name', False) google_civic_candidate_name = request.POST.get('google_civic_candidate_name', False) politician_twitter_handle = request.POST.get('politician_twitter_handle', False) if positive_value_exists(politician_twitter_handle): politician_twitter_handle = extract_twitter_handle_from_text_string(politician_twitter_handle) politician_url = request.POST.get('politician_url', False) political_party = request.POST.get('political_party', False) vote_smart_id = request.POST.get('vote_smart_id', False) maplight_id = request.POST.get('maplight_id', False) state_code = request.POST.get('state_code', False) politician_we_vote_id = request.POST.get('politician_we_vote_id', False) # Check to see if this politician is already being used anywhere politician_on_stage_found = False politician_on_stage = Politician() if positive_value_exists(politician_id): try: politician_query = Politician.objects.filter(id=politician_id) if len(politician_query): politician_on_stage = politician_query[0] politician_on_stage_found = True except Exception as e: pass # Check to see if there is a duplicate politician already saved for this election existing_politician_found = False if not positive_value_exists(politician_id): try: filter_list = Q() at_least_one_filter = False if positive_value_exists(vote_smart_id): at_least_one_filter = True filter_list |= Q(vote_smart_id=vote_smart_id) if positive_value_exists(maplight_id): at_least_one_filter = True filter_list |= Q(maplight_id=maplight_id) if positive_value_exists(politician_twitter_handle): at_least_one_filter = True filter_list |= Q(politician_twitter_handle=politician_twitter_handle) if at_least_one_filter: politician_duplicates_query = Politician.objects.filter(filter_list) if len(politician_duplicates_query): existing_politician_found = True except Exception as e: pass try: if existing_politician_found: messages.add_message(request, messages.ERROR, 'This politician is already saved for this election.') url_variables = "?politician_name=" + str(politician_name) + \ "&state_code=" + str(state_code) + \ "&google_civic_candidate_name=" + str(google_civic_candidate_name) + \ "&politician_twitter_handle=" + str(politician_twitter_handle) + \ "&politician_url=" + str(politician_url) + \ "&political_party=" + str(political_party) + \ "&vote_smart_id=" + str(vote_smart_id) + \ "&politician_we_vote_id=" + str(politician_we_vote_id) + \ "&maplight_id=" + str(maplight_id) return HttpResponseRedirect(reverse('politician:politician_new', args=()) + url_variables) elif politician_on_stage_found: # Update if politician_name is not False: politician_on_stage.politician_name = politician_name # Re-save first_name, middle name, and last name politician_on_stage.first_name = extract_first_name_from_full_name(politician_name) politician_on_stage.middle_name = extract_middle_name_from_full_name(politician_name) politician_on_stage.last_name = extract_last_name_from_full_name(politician_name) if state_code is not False: politician_on_stage.state_code = state_code if google_civic_candidate_name is not False: politician_on_stage.google_civic_candidate_name = google_civic_candidate_name if politician_twitter_handle is not False: politician_on_stage.politician_twitter_handle = politician_twitter_handle if politician_url is not False: politician_on_stage.politician_url = politician_url if political_party is not False: political_party = convert_to_political_party_constant(political_party) politician_on_stage.political_party = political_party if vote_smart_id is not False: politician_on_stage.vote_smart_id = vote_smart_id if maplight_id is not False: politician_on_stage.maplight_id = maplight_id politician_on_stage.save() messages.add_message(request, messages.INFO, 'Politician updated.') else: # Create new required_politician_variables = True \ if positive_value_exists(politician_name) \ else False if required_politician_variables: politician_on_stage = Politician( politician_name=politician_name, state_code=state_code, ) politician_on_stage.first_name = extract_first_name_from_full_name(politician_name) politician_on_stage.middle_name = extract_middle_name_from_full_name(politician_name) politician_on_stage.last_name = extract_last_name_from_full_name(politician_name) if google_civic_candidate_name is not False: politician_on_stage.google_civic_candidate_name = google_civic_candidate_name if politician_twitter_handle is not False: politician_on_stage.politician_twitter_handle = politician_twitter_handle if politician_url is not False: politician_on_stage.politician_url = politician_url if political_party is not False: political_party = convert_to_political_party_constant(political_party) politician_on_stage.political_party = political_party if vote_smart_id is not False: politician_on_stage.vote_smart_id = vote_smart_id if maplight_id is not False: politician_on_stage.maplight_id = maplight_id if politician_we_vote_id is not False: politician_on_stage.politician_we_vote_id = politician_we_vote_id politician_on_stage.save() politician_id = politician_on_stage.id messages.add_message(request, messages.INFO, 'New politician saved.') else: # messages.add_message(request, messages.INFO, 'Could not save -- missing required variables.') url_variables = "?politician_name=" + str(politician_name) + \ "&state_code=" + str(state_code) + \ "&google_civic_candidate_name=" + str(google_civic_candidate_name) + \ "&politician_twitter_handle=" + str(politician_twitter_handle) + \ "&politician_url=" + str(politician_url) + \ "&political_party=" + str(political_party) + \ "&vote_smart_id=" + str(vote_smart_id) + \ "&politician_we_vote_id=" + str(politician_we_vote_id) + \ "&maplight_id=" + str(maplight_id) if positive_value_exists(politician_id): return HttpResponseRedirect(reverse('politician:politician_edit', args=(politician_id,)) + url_variables) else: return HttpResponseRedirect(reverse('politician:politician_new', args=()) + url_variables) except Exception as e: handle_record_not_saved_exception(e, logger=logger) messages.add_message(request, messages.ERROR, 'Could not save politician.') return HttpResponseRedirect(reverse('politician:politician_edit', args=(politician_id,))) if politician_id: return HttpResponseRedirect(reverse('politician:politician_edit', args=(politician_id,))) else: return HttpResponseRedirect(reverse('politician:politician_new', args=()))
def candidate_edit_process_view(request): """ Process the new or edit candidate 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) candidate_id = convert_to_int(request.POST['candidate_id']) candidate_name = request.POST.get('candidate_name', False) candidate_twitter_handle = request.POST.get('candidate_twitter_handle', False) if positive_value_exists(candidate_twitter_handle): candidate_twitter_handle = extract_twitter_handle_from_text_string(candidate_twitter_handle) candidate_url = request.POST.get('candidate_url', False) contest_office_id = request.POST.get('contest_office_id', False) ballot_guide_official_statement = request.POST.get('ballot_guide_official_statement', False) party = request.POST.get('party', False) remove_duplicate_process = request.POST.get('remove_duplicate_process', False) google_civic_election_id = request.POST.get('google_civic_election_id', 0) # Check to see if this candidate is already being used anywhere candidate_on_stage_found = False candidate_on_stage = CandidateCampaign() if positive_value_exists(candidate_id): try: candidate_query = CandidateCampaign.objects.filter(id=candidate_id) if len(candidate_query): candidate_on_stage = candidate_query[0] candidate_on_stage_found = True except Exception as e: handle_record_not_found_exception(e, logger=logger) contest_office_we_vote_id = '' if positive_value_exists(contest_office_id): contest_office_manager = ContestOfficeManager() results = contest_office_manager.retrieve_contest_office_from_id(contest_office_id) if results['contest_office_found']: contest_office = results['contest_office'] contest_office_we_vote_id = contest_office.we_vote_id try: if candidate_on_stage_found: # Update if candidate_twitter_handle is not False: candidate_on_stage.candidate_twitter_handle = candidate_twitter_handle if candidate_url is not False: candidate_on_stage.candidate_url = candidate_url if ballot_guide_official_statement is not False: candidate_on_stage.ballot_guide_official_statement = ballot_guide_official_statement if party is not False: candidate_on_stage.party = party # Check to see if this is a We Vote-created election # is_we_vote_google_civic_election_id = True \ # if convert_to_int(candidate_on_stage.google_civic_election_id) >= 1000000 \ # else False if contest_office_id is not False: # We only allow updating of candidates within the We Vote Admin in candidate_on_stage.contest_office_id = contest_office_id candidate_on_stage.contest_office_we_vote_id = contest_office_we_vote_id candidate_on_stage.save() messages.add_message(request, messages.INFO, 'Candidate Campaign updated.') else: # Create new # election must be found election_manager = ElectionManager() election_results = election_manager.retrieve_election(google_civic_election_id) if election_results['election_found']: election = election_results['election'] state_code = election.get_election_state() else: messages.add_message(request, messages.ERROR, 'Could not find election -- required to save candidate.') return HttpResponseRedirect(reverse('candidate:candidate_edit', args=(candidate_id,))) required_candidate_variables = True \ if positive_value_exists(candidate_name) and positive_value_exists(contest_office_id) \ else False if required_candidate_variables: candidate_on_stage = CandidateCampaign( candidate_name=candidate_name, google_civic_election_id=google_civic_election_id, contest_office_id=contest_office_id, contest_office_we_vote_id=contest_office_we_vote_id, state_code=state_code, ) if candidate_twitter_handle is not False: candidate_on_stage.candidate_twitter_handle = candidate_twitter_handle if candidate_url is not False: candidate_on_stage.candidate_url = candidate_url if ballot_guide_official_statement is not False: candidate_on_stage.ballot_guide_official_statement = ballot_guide_official_statement if party is not False: candidate_on_stage.party = party candidate_on_stage.save() candidate_id = candidate_on_stage.id messages.add_message(request, messages.INFO, 'New candidate saved.') else: messages.add_message(request, messages.INFO, 'Could not save -- missing required variables.') if positive_value_exists(candidate_id): return HttpResponseRedirect(reverse('candidate:candidate_edit', args=(candidate_id,)) + "?google_civic_election_id=" + str(google_civic_election_id) + "&contest_office_id=" + str(contest_office_id) ) else: return HttpResponseRedirect(reverse('candidate:candidate_new', args=()) + "?google_civic_election_id=" + str(google_civic_election_id) + "&contest_office_id=" + str(contest_office_id) ) except Exception as e: handle_record_not_saved_exception(e, logger=logger) messages.add_message(request, messages.ERROR, 'Could not save candidate.') return HttpResponseRedirect(reverse('candidate:candidate_edit', args=(candidate_id,))) if remove_duplicate_process: return HttpResponseRedirect(reverse('candidate:find_and_remove_duplicate_candidates', args=()) + "?google_civic_election_id=" + str(google_civic_election_id)) else: return HttpResponseRedirect(reverse('candidate:candidate_edit', args=(candidate_id,)))