Skip to content

Commit

Permalink
Merge branch 'master' into county_field
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Nov 13, 2023
2 parents 19eccfd + f992daf commit 81432c9
Show file tree
Hide file tree
Showing 6 changed files with 282 additions and 269 deletions.
440 changes: 220 additions & 220 deletions Pipfile.lock

Large diffs are not rendered by default.

10 changes: 0 additions & 10 deletions cfl_common/common/email_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,6 @@ def emailChangeNotificationEmail(request, new_email_address):
}


def emailChangeDuplicateNotificationEmail(request, email):
return {
"subject": f"Duplicate account",
"message": (
f"A user is already registered with this email address: {email}.\n"
f"Please change your email address to something else."
),
}


def userAlreadyRegisteredEmail(request, email, is_independent_student=False):
if is_independent_student:
login_url = reverse("independent_student_login")
Expand Down
81 changes: 49 additions & 32 deletions cfl_common/common/helpers/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from common.email_messages import (
emailChangeNotificationEmail,
emailChangeVerificationEmail,
emailChangeDuplicateNotificationEmail,
emailVerificationNeededEmail,
parentsEmailVerificationNeededEmail,
)
Expand Down Expand Up @@ -61,8 +60,8 @@ def send_email(

if replace_url:
verify_url = replace_url["verify_url"]
verify_replace_url = re.sub(f'(.*/verify_email/)(.*)', f'\\1', verify_url)
html_body = re.sub(f'({verify_url})(.*){verify_url}', f'\\1\\2{verify_replace_url}', original_html_body)
verify_replace_url = re.sub(f"(.*/verify_email/)(.*)", f"\\1", verify_url)
html_body = re.sub(f"({verify_url})(.*){verify_url}", f"\\1\\2{verify_replace_url}", original_html_body)

# make message using templates
message = EmailMultiAlternatives(subject, plaintext_body, sender, recipients)
Expand All @@ -78,6 +77,7 @@ def generate_token(user, new_email="", preverified=False):

return generate_token_for_email(user.email, new_email)


def generate_token_for_email(email: str, new_email: str = ""):
return jwt.encode(
{
Expand Down Expand Up @@ -124,18 +124,39 @@ def send_verification_email(request, user, data, new_email=None, age=None):
# if the user is a teacher
if age is None:
message = emailVerificationNeededEmail(request, verification)
send_email(VERIFICATION_EMAIL, [user.email], message["subject"], message["message"], message["subject"], replace_url=message["url"])
send_email(
VERIFICATION_EMAIL,
[user.email],
message["subject"],
message["message"],
message["subject"],
replace_url=message["url"],
)

if _newsletter_ticked(data):
add_to_dotmailer(user.first_name, user.last_name, user.email, DotmailerUserType.TEACHER)
# if the user is an independent student
else:
if age < 13:
message = parentsEmailVerificationNeededEmail(request, user, verification)
send_email(VERIFICATION_EMAIL, [user.email], message["subject"], message["message"], message["subject"], replace_url=message["url"])
send_email(
VERIFICATION_EMAIL,
[user.email],
message["subject"],
message["message"],
message["subject"],
replace_url=message["url"],
)
else:
message = emailVerificationNeededEmail(request, verification)
send_email(VERIFICATION_EMAIL, [user.email], message["subject"], message["message"], message["subject"], replace_url=message["url"])
send_email(
VERIFICATION_EMAIL,
[user.email],
message["subject"],
message["message"],
message["subject"],
replace_url=message["url"],
)

if _newsletter_ticked(data):
add_to_dotmailer(user.first_name, user.last_name, user.email, DotmailerUserType.STUDENT)
Expand All @@ -144,10 +165,14 @@ def send_verification_email(request, user, data, new_email=None, age=None):
verification = generate_token(user, new_email)

message = emailChangeVerificationEmail(request, verification)
send_email(VERIFICATION_EMAIL, [user.email], message["subject"], message["message"], message["subject"], replace_url=message["url"])

message = emailChangeNotificationEmail(request, new_email)
send_email(VERIFICATION_EMAIL, [user.email], message["subject"], message["message"], message["subject"])
send_email(
VERIFICATION_EMAIL,
[new_email],
message["subject"],
message["message"],
message["subject"],
replace_url=message["url"],
)


def add_to_dotmailer(first_name: str, last_name: str, email: str, user_type: DotmailerUserType):
Expand Down Expand Up @@ -255,17 +280,12 @@ def update_indy_email(user, request, data):
if new_email != "" and new_email != user.email:
changing_email = True
users_with_email = User.objects.filter(email=new_email)
# email is already taken
if users_with_email.exists():
email_message = emailChangeDuplicateNotificationEmail(request, new_email)
send_email(
NOTIFICATION_EMAIL,
[user.email],
email_message["subject"],
email_message["message"],
email_message["subject"],
)
else:

message = emailChangeNotificationEmail(request, new_email)
send_email(VERIFICATION_EMAIL, [user.email], message["subject"], message["message"], message["subject"])

# email is available
if not users_with_email.exists():
# new email to set and verify
send_verification_email(request, user, data, new_email)
return changing_email, new_email
Expand All @@ -278,17 +298,14 @@ def update_email(user: Teacher or Student, request, data):
if new_email != "" and new_email != user.new_user.email:
changing_email = True
users_with_email = User.objects.filter(email=new_email)
# email is already taken
if users_with_email.exists():
email_message = emailChangeDuplicateNotificationEmail(request, new_email)
send_email(
NOTIFICATION_EMAIL,
[user.new_user.email],
email_message["subject"],
email_message["message"],
email_message["subject"],
)
else:

message = emailChangeNotificationEmail(request, new_email)
send_email(
VERIFICATION_EMAIL, [user.new_user.email], message["subject"], message["message"], message["subject"]
)

# email is available
if not users_with_email.exists():
# new email to set and verify
send_verification_email(request, user.new_user, data, new_email)
return changing_email, new_email
2 changes: 1 addition & 1 deletion portal/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "6.38.2"
__version__ = "6.38.3"
9 changes: 6 additions & 3 deletions portal/tests/test_independent_student.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ def test_change_email(self):
assert is_email_updated_message_showing(self.selenium)

subject = str(mail.outbox[0].subject)
assert subject == "Duplicate account"
assert subject == "Email address update"
mail.outbox = []

# Try changing email to an existing teacher's email
Expand All @@ -373,7 +373,7 @@ def test_change_email(self):
assert is_email_updated_message_showing(self.selenium)

subject = str(mail.outbox[0].subject)
assert subject == "Duplicate account"
assert subject == "Email address update"
mail.outbox = []

page = (
Expand Down Expand Up @@ -402,7 +402,10 @@ def test_change_email(self):

page = page.logout()

page = email_utils.follow_change_email_link_to_independent_dashboard(page, mail.outbox[0])
subject = str(mail.outbox[0].subject)
assert subject == "Email address update"

page = email_utils.follow_change_email_link_to_independent_dashboard(page, mail.outbox[1])
mail.outbox = []

page = page.independent_student_login(new_email, password)
Expand Down
9 changes: 6 additions & 3 deletions portal/tests/test_teacher.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ def test_change_email(self):
assert is_email_updated_message_showing(self.selenium)

subject = str(mail.outbox[0].subject)
assert subject == "Duplicate account"
assert subject == "Email address update"
mail.outbox = []

# Try changing email to an existing indy student's email, should fail
Expand All @@ -567,7 +567,7 @@ def test_change_email(self):
assert is_email_updated_message_showing(self.selenium)

subject = str(mail.outbox[0].subject)
assert subject == "Duplicate account"
assert subject == "Email address update"
mail.outbox = []

page = self.go_to_homepage()
Expand All @@ -586,7 +586,10 @@ def test_change_email(self):

page = page.logout()

page = email_utils.follow_change_email_link_to_dashboard(page, mail.outbox[0])
subject = str(mail.outbox[0].subject)
assert subject == "Email address update"

page = email_utils.follow_change_email_link_to_dashboard(page, mail.outbox[1])
mail.outbox = []

page = page.login(new_email, password).open_account_tab()
Expand Down

0 comments on commit 81432c9

Please sign in to comment.