def update_or_create_politician_from_candidate(self, candidate): """ Take a We Vote candidate_campaign object, and map it to update_or_create_politician :param candidate: :return: """ first_name = extract_first_name_from_full_name(candidate.candidate_name) middle_name = extract_middle_name_from_full_name(candidate.candidate_name) last_name = extract_last_name_from_full_name(candidate.candidate_name) political_party = convert_to_political_party_constant(candidate.party) # TODO Add all other identifiers from other systems updated_politician_values = { 'vote_smart_id': candidate.vote_smart_id, 'maplight_id': candidate.maplight_id, 'politician_name': candidate.candidate_name, 'google_civic_candidate_name': candidate.google_civic_candidate_name, 'state_code': candidate.state_code, 'politician_twitter_handle': candidate.candidate_twitter_handle, 'first_name': first_name, 'middle_name': middle_name, 'last_name': last_name, 'political_party': political_party, } return self.update_or_create_politician( candidate.politician_we_vote_id, candidate.vote_smart_id, candidate.maplight_id, candidate.candidate_name, candidate.state_code, candidate.candidate_twitter_handle, first_name, middle_name, last_name, updated_politician_values)
def update_or_create_politician_from_candidate(self, candidate): """ Take a We Vote candidate_campaign object, and map it to update_or_create_politician :param candidate: :return: """ first_name = extract_first_name_from_full_name( candidate.candidate_name) middle_name = extract_middle_name_from_full_name( candidate.candidate_name) last_name = extract_last_name_from_full_name(candidate.candidate_name) political_party = convert_to_political_party_constant(candidate.party) # TODO Add all other identifiers from other systems updated_politician_values = { 'vote_smart_id': candidate.vote_smart_id, 'maplight_id': candidate.maplight_id, 'politician_name': candidate.candidate_name, 'google_civic_candidate_name': candidate.google_civic_candidate_name, 'state_code': candidate.state_code, 'politician_twitter_handle': candidate.candidate_twitter_handle, 'first_name': first_name, 'middle_name': middle_name, 'last_name': last_name, 'political_party': political_party, } return self.update_or_create_politician( candidate.politician_we_vote_id, candidate.vote_smart_id, candidate.maplight_id, candidate.candidate_name, candidate.state_code, candidate.candidate_twitter_handle, first_name, middle_name, last_name, updated_politician_values)
def extract_first_name(self): full_name = self.candidate_name return extract_first_name_from_full_name(full_name)
def extract_first_name(self): full_name = self.display_candidate_name() return extract_first_name_from_full_name(full_name)
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=()))