Skip to content

Commit

Permalink
Fix suggest_registrars so it ignores path after base domain
Browse files Browse the repository at this point in the history
  • Loading branch information
cmsetzer committed Aug 27, 2024
1 parent bd127dc commit af10f59
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
10 changes: 2 additions & 8 deletions perma_web/perma/tests/test_views_user_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -2656,15 +2656,9 @@ def test_suggested_registrars(self):
message = mail.outbox[0]
lines = message.body.splitlines()
captures = []
capture_line = False
for line in lines:
if capture_line is True:
captures.append(line.lstrip('- '))
continue
if line.startswith('Note: We’ve also found'):
capture_line = True
elif line == '':
capture_line = False
if line.lstrip().startswith('- '):
captures.append(line.strip('- '))

# Validate suggested registrar(s)
self.assertEqual(len(captures), 1)
Expand Down
11 changes: 5 additions & 6 deletions perma_web/perma/views/user_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -1992,19 +1992,18 @@ def firm_request_response(request):
def suggest_registrars(user: LinkUser, limit: int = 5) -> BaseManager[Registrar]:
"""Suggest potential registrars for a user based on email domain.
This queries the database for registrars whose website URL matches
the base domain from the user's email address. For example, if the
This queries the database for registrars whose website matches the
base domain from the user's email address. For example, if the
user's email is `[email protected]`, this will suggest
registrars whose URLs end with `harvard.edu`.
registrars whose domains end with `harvard.edu`.
"""
_, email_domain = user.email.split('@')
base_domain = '.'.join(email_domain.rsplit('.', 2)[-2:])
pattern = f'{re.escape(base_domain)}/?$'
pattern = f'^https?://([a-zA-Z0-9\\-\\.]+\\.)?{re.escape(base_domain)}(/.*)?$'
registrars = (
Registrar.objects.exclude(status='pending')
.filter(website__iregex=pattern)
.order_by('-link_count')
.all()[:limit]
.order_by('-link_count', 'name')[:limit]
)
return registrars

Expand Down

0 comments on commit af10f59

Please sign in to comment.