Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions candidate/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from wevote_functions.functions import add_period_to_middle_name_initial, add_period_to_name_prefix_and_suffix, \
convert_to_int, convert_to_political_party_constant, \
extract_instagram_handle_from_text_string, extract_twitter_handle_from_text_string, extract_website_from_url, \
is_url_valid, MIDDLE_INITIAL_SUBSTRINGS, \
is_url_valid, MIDDLE_INITIAL_WITH_PERIOD_SUBSTRINGS, MIDDLE_INITIAL_WITHOUT_PERIOD_SUBSTRINGS, \
normalize_sms_phone_number_for_voter_update, positive_value_exists, process_request_from_master, \
remove_period_from_middle_name_initial, remove_period_from_name_prefix_and_suffix
from wevote_functions.functions_date import convert_date_to_we_vote_date_string, \
Expand Down Expand Up @@ -393,9 +393,9 @@ def choose_best_candidate_url(
best_url_string = 'CANDIDATE1'

results = {
'best_url': best_url_string,
'status': status,
'success': success,
'best_url_string': best_url_string,
'status': status,
'success': success,
}
return results

Expand Down Expand Up @@ -631,16 +631,31 @@ def preferred_name_identifier(candidate1_name, candidate2_name):
# For example, Kristi C. Morris <=> Kristi Morris: Should choose "Kristi Morris"
candidate1_has_middle_initial = False
candidate2_has_middle_initial = False

# Did we find an exact match in the name for a middle initial WITH a period at the end?
modified_candidate1_name = candidate1_name
modified_candidate2_name = candidate2_name
for middle_initial_substring in MIDDLE_INITIAL_SUBSTRINGS:
for middle_initial_substring in MIDDLE_INITIAL_WITH_PERIOD_SUBSTRINGS:
# Remove middle_initial_substring
modified_candidate1_name = modified_candidate1_name.replace(middle_initial_substring, " ")
modified_candidate2_name = modified_candidate2_name.replace(middle_initial_substring, " ")
# Check if the difference between these two lengths is 4 characters, independent of whichever one is longer
if abs(len(candidate1_name) - len(modified_candidate1_name)) == 4:
candidate1_has_middle_initial = True
if abs(len(candidate2_name) - len(modified_candidate2_name)) == 4:
candidate2_has_middle_initial = True

if len(candidate1_name) != len(modified_candidate1_name):
# Did we find an exact match in the name for a middle initial WITHOUT a period at the end?
modified_candidate1_name = candidate1_name
modified_candidate2_name = candidate2_name
for middle_initial_substring in MIDDLE_INITIAL_WITHOUT_PERIOD_SUBSTRINGS:
# Remove middle_initial_substring
modified_candidate1_name = modified_candidate1_name.replace(middle_initial_substring, " ")
modified_candidate2_name = modified_candidate2_name.replace(middle_initial_substring, " ")
# Check if the difference between these two lengths is 3 characters, independent of whichever one is longer
if abs(len(candidate1_name) - len(modified_candidate1_name)) == 3:
candidate1_has_middle_initial = True
if len(candidate2_name) != len(modified_candidate2_name):
if abs(len(candidate2_name) - len(modified_candidate2_name)) == 3:
candidate2_has_middle_initial = True

if candidate1_has_middle_initial and candidate2_has_middle_initial:
Expand All @@ -661,6 +676,10 @@ def merge_if_duplicate_candidates(candidate1_on_stage, candidate2_on_stage, conf
candidate1_we_vote_id = candidate1_on_stage.we_vote_id
candidate2_we_vote_id = candidate2_on_stage.we_vote_id

# look_at_list = ['wv2mcand9540', 'wv2mcand26630']
# if candidate1_we_vote_id in look_at_list or candidate2_we_vote_id in look_at_list:
# status += "CANDIDATE_WE_WANT_TO_LOOK_AT "

# conflict_values mostly comes from figure_out_candidate_conflict_values

# Are there any comparisons that require admin intervention?
Expand All @@ -669,6 +688,7 @@ def merge_if_duplicate_candidates(candidate1_on_stage, candidate2_on_stage, conf
# Don't let conflict stop us with any of these fields
if attribute == "ballotpedia_candidate_id" \
or attribute == "ballotpedia_office_id" \
or attribute == "ballotpedia_photo_url" \
or attribute == "ballotpedia_race_id" \
or attribute == "candidate_url" \
or attribute == "facebook_url" \
Expand Down
78 changes: 76 additions & 2 deletions politician/views_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.messages import get_messages
from django.core.cache import cache

Check failure on line 13 in politician/views_admin.py

View workflow job for this annotation

GitHub Actions / Ruff

ruff (unused-import)

politician/views_admin.py:13:31: unused-import: `django.core.cache.cache` imported but unused help: Remove unused import: `django.core.cache.cache`
from django.core.exceptions import ObjectDoesNotExist
from django.db import transaction
from django.db.models import Q
Expand Down Expand Up @@ -1447,6 +1447,7 @@
ballotpedia_politician_url = request.GET.get('ballotpedia_politician_url', False)
birth_date = request.GET.get('birth_date', False)
ballotpedia_politician_name = request.GET.get('ballotpedia_politician_name', False)
politician_search = request.GET.get('politician_search', False)
facebook_url = request.GET.get('facebook_url', False)
facebook_url2 = request.GET.get('facebook_url2', False)
facebook_url3 = request.GET.get('facebook_url3', False)
Expand Down Expand Up @@ -1728,7 +1729,79 @@
description="Retrieve Politician objects for this politician")

duplicate_politician_list = []
if positive_value_exists(politician_on_stage.politician_name) or \
if positive_value_exists(politician_search):
try:
duplicate_politician_list = Politician.objects.using('readonly').all()
duplicate_politician_list = duplicate_politician_list.exclude(
we_vote_id=politician_on_stage.we_vote_id)
duplicate_politician_list = duplicate_politician_list.filter(
state_code__iexact=politician_on_stage.state_code)

search_words = politician_search.split()
for one_word in search_words:
filters = []

new_filter = Q(first_name__iexact=one_word)
filters.append(new_filter)

new_filter = (
Q(google_civic_candidate_name__icontains=one_word) |
Q(google_civic_candidate_name2__icontains=one_word) |
Q(google_civic_candidate_name3__icontains=one_word)
)
filters.append(new_filter)

new_filter = Q(last_name__iexact=one_word)
filters.append(new_filter)

new_filter = Q(linked_campaignx_we_vote_id=one_word)
filters.append(new_filter)

new_filter = (
Q(politician_email__icontains=one_word) |
Q(politician_email2__icontains=one_word) |
Q(politician_email3__icontains=one_word)
)
filters.append(new_filter)

new_filter = Q(politician_name__icontains=one_word)
filters.append(new_filter)

new_filter = (
Q(politician_twitter_handle__icontains=one_word) |
Q(politician_twitter_handle2__icontains=one_word) |
Q(politician_twitter_handle3__icontains=one_word) |
Q(politician_twitter_handle4__icontains=one_word) |
Q(politician_twitter_handle5__icontains=one_word)
)
filters.append(new_filter)

new_filter = Q(political_party__icontains=one_word)
filters.append(new_filter)

new_filter = Q(seo_friendly_path__icontains=one_word)
filters.append(new_filter)

new_filter = Q(vote_usa_politician_id__icontains=one_word)
filters.append(new_filter)

new_filter = Q(we_vote_id=one_word)
filters.append(new_filter)

# Add the first query
if len(filters):
final_filters = filters.pop()

# ...and "OR" the remaining items in the list
for item in filters:
final_filters |= item

duplicate_politician_list = duplicate_politician_list.filter(final_filters)
duplicate_politician_list = duplicate_politician_list.order_by('politician_name')[:20]
except ObjectDoesNotExist:
# This is fine, create new
pass
elif positive_value_exists(politician_on_stage.politician_name) or \
positive_value_exists(politician_on_stage.first_name) or \
positive_value_exists(politician_on_stage.last_name) or \
positive_value_exists(politician_on_stage.politician_twitter_handle) or \
Expand Down Expand Up @@ -1826,7 +1899,8 @@
except ObjectDoesNotExist:
# This is fine, create new
pass
speed_statistics.end(context="Find possible duplicate politicians")

speed_statistics.end(context="Find possible duplicate politicians")

# ##################################
# Find Representatives Linked to this Politician
Expand Down
2 changes: 1 addition & 1 deletion templates/candidate/candidate_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
{% if candidate %}
Edit Candidate:
{% if candidate.we_vote_hosted_profile_image_url_medium %}
<div style='display: inline-block;

Check warning on line 35 in templates/candidate/candidate_edit.html

View workflow job for this annotation

GitHub Actions / djlint

H021 Inline styles should be avoided.

Check warning on line 35 in templates/candidate/candidate_edit.html

View workflow job for this annotation

GitHub Actions / djlint

H008 Attributes should be double quoted.
height: 48px;
width: 96px;
background:{{ candidate.profile_image_background_color }}'>
<img src='{{ candidate.we_vote_hosted_profile_image_url_medium }}'

Check warning on line 39 in templates/candidate/candidate_edit.html

View workflow job for this annotation

GitHub Actions / djlint

H021 Inline styles should be avoided.

Check warning on line 39 in templates/candidate/candidate_edit.html

View workflow job for this annotation

GitHub Actions / djlint

H013 Img tag should have an alt attribute.

Check warning on line 39 in templates/candidate/candidate_edit.html

View workflow job for this annotation

GitHub Actions / djlint

H008 Attributes should be double quoted.
style='margin-left: 25%' />
</div>
{% endif %}
Expand Down Expand Up @@ -101,7 +101,7 @@
{% endif %}
</div>
{% if candidate.supporters_count %}
<div class="u-no-break" style="color: darkgray; font-size: 10px;">

Check warning on line 104 in templates/candidate/candidate_edit.html

View workflow job for this annotation

GitHub Actions / djlint

H021 Inline styles should be avoided.
supporters_count: {{ candidate.supporters_count }}
</div>
{% endif %}
Expand All @@ -116,11 +116,11 @@
<div>
{% if candidate %}
{% if candidate.candidate_name_normalized|length|get_digit:"-1" > 0 %}
<span style='display: inline-block;

Check warning on line 119 in templates/candidate/candidate_edit.html

View workflow job for this annotation

GitHub Actions / djlint

H021 Inline styles should be avoided.

Check warning on line 119 in templates/candidate/candidate_edit.html

View workflow job for this annotation

GitHub Actions / djlint

H008 Attributes should be double quoted.
width: 42%;
text-align: left;
padding-left: 6px'>
<span id='hidden_name' style="display:none;">{{ candidate.candidate_name_normalized }}</span>

Check warning on line 123 in templates/candidate/candidate_edit.html

View workflow job for this annotation

GitHub Actions / djlint

H021 Inline styles should be avoided.

Check warning on line 123 in templates/candidate/candidate_edit.html

View workflow job for this annotation

GitHub Actions / djlint

H008 Attributes should be double quoted.
<button type="button"
id="update_name_id"
name="update_name"
Expand Down Expand Up @@ -1032,7 +1032,6 @@
<br />


{% if related_candidate_list %}
<h3>Possible Duplicate Candidates for {{ candidate.candidate_name }}</h3>
<div style="margin-bottom: 15px;">
<label for="use_trigram_match_id">
Expand All @@ -1048,6 +1047,7 @@
<span style="color: #6c757d; margin-left: 20px;">Currently viewing exact matching results</span>
{% endif %}
</div>
{% if related_candidate_list %}
<table class="table">
<thead>
<tr>
Expand Down
Loading
Loading