Skip to content

Commit 1b72c4e

Browse files
authored
Merge pull request #1237 from makeabilitylab/1206-fix-same-name-problem
1206 fix same name problem
2 parents 552ce8f + 67f460e commit 1b72c4e

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

makeabilitylab/settings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@
7272
ALLOWED_HOSTS = ['*']
7373

7474
# Makeability Lab Global Variables, including Makeability Lab version
75-
ML_WEBSITE_VERSION = "1.9.9.9" # Keep this updated with each release and also change the short description below
76-
ML_WEBSITE_VERSION_DESCRIPTION = "Fixes poster upload bugs"
75+
ML_WEBSITE_VERSION = "2.0" # Keep this updated with each release and also change the short description below
76+
ML_WEBSITE_VERSION_DESCRIPTION = "Generates unique url_names for duplicate member names to avoid conflicts"
7777
DATE_MAKEABILITYLAB_FORMED = datetime.date(2012, 1, 1) # Date Makeability Lab was formed
7878
MAX_BANNERS = 7 # Maximum number of banners on a page
7979

website/models/person.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,8 +623,20 @@ def save(self, *args, **kwargs):
623623
if bool(re.search('[^a-zA-Z]', c)) and c in special_chars:
624624
url_name_cleaned = url_name_cleaned.replace(c, special_chars.get(c))
625625

626-
# Finally, clean remaining characters (EX: dashes, periods).
626+
# Clean remaining characters (EX: dashes, periods).
627627
url_name_cleaned = re.sub('[^a-zA-Z]', '', url_name_cleaned)
628+
629+
# Check for collisions and append numeric suffix if needed
630+
# We exclude the current person (by pk) when checking for duplicates to allow updates
631+
base_url_name = url_name_cleaned
632+
counter = 2
633+
634+
# Keep incrementing counter until we find a unique url_name
635+
while Person.objects.filter(url_name=url_name_cleaned).exclude(pk=self.pk).exists():
636+
url_name_cleaned = f"{base_url_name}{counter}"
637+
counter += 1
638+
_logger.debug(f"URL name collision detected for {self.get_full_name()}. Trying {url_name_cleaned}")
639+
628640
self.url_name = url_name_cleaned
629641

630642
# Next, automatically set the bio_date_modified field

website/views/member.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import website.utils.ml_utils as ml_utils
44
from django.shortcuts import render, get_object_or_404, redirect
55
from django.db.models import Q
6+
from django.core.exceptions import MultipleObjectsReturned
67

78
# For logging
89
import time
@@ -36,6 +37,13 @@ def member(request, member_name=None, member_id=None):
3637
try:
3738
# Try a case-insensitive exact match
3839
person = get_object_or_404(Person, url_name__iexact=member_name)
40+
except MultipleObjectsReturned:
41+
# This should not happen if url_name uniqueness is working correctly
42+
# Log error and return the most recently modified person as fallback
43+
_logger.error(f"Multiple people found with url_name={member_name}! This indicates url_name uniqueness is broken. Returning most recent.")
44+
person = Person.objects.filter(url_name__iexact=member_name).order_by('-modified_date').first()
45+
if person is None:
46+
raise Http404("No person matches the given query.")
3947
except Http404:
4048
_logger.debug(f"{member_name} not found for url_name, looking for closest match in database")
4149
closest_urlname = get_closest_urlname_in_database(member_name)

0 commit comments

Comments
 (0)