-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Notify new user if their email domain matches existing Perma partner(…
…s) (#3596) * Initial pass at user email/registrar domain match function * Implement basic suggested registrars test and fix existing tests * Use SQL regex pattern to check hostname match * Support multiple suggested registrars; fix mutable default arg bug * Fix suggest_registrars so it ignores path after base domain
- Loading branch information
Showing
3 changed files
with
68 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
In order to complete your account activation, please confirm your email address and set a password. It’s as simple as clicking the link below. The link will be valid for the next {% widthratio activation_expires 3600 1 %} hours. | ||
|
||
{{ activation_route }} | ||
{{ activation_route }} | ||
|
||
{% if suggested_registrars %}Note: We’ve also found {% if suggested_registrars|length == 1 %}a Perma.cc partner institution{% else %}some Perma.cc partner institutions{% endif %} with whom you might be affiliated. You may be able to request a sponsored account by contacting them: | ||
{% for suggested_registrar in suggested_registrars %} | ||
- {{ suggested_registrar.name }}: {{ suggested_registrar.email }}{% endfor %}{% endif %} | ||
|
||
Thanks, | ||
The Perma.cc Team |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
from datetime import timedelta | ||
import itertools | ||
import logging | ||
import re | ||
from typing import Literal | ||
|
||
import celery | ||
|
@@ -1988,7 +1989,26 @@ def firm_request_response(request): | |
return render(request, 'registration/firm_request.html') | ||
|
||
|
||
def email_new_user(request, user, template="email/new_user.txt", context={}): | ||
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 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 domains end with `harvard.edu`. | ||
""" | ||
_, email_domain = user.email.split('@') | ||
base_domain = '.'.join(email_domain.rsplit('.', 2)[-2:]) | ||
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', 'name')[:limit] | ||
) | ||
return registrars | ||
|
||
|
||
def email_new_user(request, user, template='email/new_user.txt', context=None): | ||
""" | ||
Send email to newly created accounts | ||
""" | ||
|
@@ -1997,11 +2017,20 @@ def email_new_user(request, user, template="email/new_user.txt", context={}): | |
urlsafe_base64_encode(force_bytes(user.pk)), | ||
default_token_generator.make_token(user), | ||
])) | ||
context.update({ | ||
'activation_route': activation_route, | ||
'activation_expires': settings.PASSWORD_RESET_TIMEOUT, | ||
'request': request | ||
}) | ||
|
||
# Include context variables | ||
template_is_default = template == 'email/new_user.txt' | ||
context = context if context is not None else {} | ||
context.update( | ||
{ | ||
'activation_expires': settings.PASSWORD_RESET_TIMEOUT, | ||
'activation_route': activation_route, | ||
'request': request, | ||
# Only query DB if we're using the default template; otherwise there's no need | ||
'suggested_registrars': suggest_registrars(user) if template_is_default else [], | ||
} | ||
) | ||
|
||
send_user_email( | ||
user.raw_email, | ||
template, | ||
|