diff --git a/perma_web/perma/tests/test_views_user_management.py b/perma_web/perma/tests/test_views_user_management.py index 69a9e0f6f..c19cccde4 100644 --- a/perma_web/perma/tests/test_views_user_management.py +++ b/perma_web/perma/tests/test_views_user_management.py @@ -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) diff --git a/perma_web/perma/views/user_management.py b/perma_web/perma/views/user_management.py index d2872e986..078b8bc1a 100755 --- a/perma_web/perma/views/user_management.py +++ b/perma_web/perma/views/user_management.py @@ -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 `username@law.harvard.edu`, 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