Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix verify email link #2165

Merged
merged 3 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion portal/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class TeacherAdmin(admin.ModelAdmin, ExportActionMixin):
class UserProfileAdmin(admin.ModelAdmin, ExportActionMixin):
search_fields = ["user__first_name", "user__last_name", "user__username", "user__date_joined"]
list_filter = ["user__date_joined"]
list_display = ["user", "__str__"]
list_display = ["user", "__str__", "is_verified"]
readonly_fields = ["user"]


Expand Down
35 changes: 25 additions & 10 deletions portal/views/cron/user.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
from datetime import timedelta
from itertools import chain
from typing import List

from common.models import Teacher, Student
from django.contrib.auth.models import User
Expand Down Expand Up @@ -30,15 +31,15 @@
USER_2ND_VERIFY_EMAIL_REMINDER_TEXT = (
"Please go to the link below to verify your email address:"
"\n{email_verification_url}."
"You will not be able to use your account until it is verified."
"\nYou will not be able to use your account until it is verified."
"\n\nBy activating the account you confirm that you have read and agreed to"
" our terms ({terms_url}) and our privacy notice ({privacy_notice_url}). If"
" your account is not verified within 5 days we will delete it."
)
USER_DELETE_UNVERIFIED_ACCOUNT_DAYS = 19


def get_unverified_emails(days: int):
def get_unverified_emails(days: int) -> List[str]:
now = timezone.now()

teacher_emails = Teacher.objects.filter(
Expand All @@ -57,15 +58,27 @@ def get_unverified_emails(days: int):
return list(chain(teacher_emails, student_emails))


def build_absolute_google_uri(request, location: str) -> str:
"""
This is needed specifically for emails sent by cron jobs as the protocol for cron jobs is HTTP
and the service name is wrongly parsed.
"""
url = request.build_absolute_uri(location)
url = url.replace("http", "https")
url = url.replace(".decent", "-dot-decent")

return url


class FirstVerifyEmailReminderView(CronMixin, APIView):
def get(self, request):
emails = get_unverified_emails(USER_1ST_VERIFY_EMAIL_REMINDER_DAYS)

logging.info(f"{len(emails)} emails unverified.")

if emails:
terms_url = request.build_absolute_uri(reverse("terms"))
privacy_notice_url = request.build_absolute_uri(reverse("privacy_notice"))
terms_url = build_absolute_google_uri(request, reverse("terms"))
privacy_notice_url = build_absolute_google_uri(request, reverse("privacy_notice"))

sent_email_count = 0
for email in emails:
Expand All @@ -76,11 +89,12 @@ def get(self, request):
subject="Awaiting verification",
title="Awaiting verification",
text_content=USER_1ST_VERIFY_EMAIL_REMINDER_TEXT.format(
email_verification_url=request.build_absolute_uri(
email_verification_url=build_absolute_google_uri(
request,
reverse(
"verify_email",
kwargs={"token": generate_token_for_email(email)},
)
),
),
terms_url=terms_url,
privacy_notice_url=privacy_notice_url,
Expand All @@ -103,8 +117,8 @@ def get(self, request):
logging.info(f"{len(emails)} emails unverified.")

if emails:
terms_url = request.build_absolute_uri(reverse("terms"))
privacy_notice_url = request.build_absolute_uri(reverse("privacy_notice"))
terms_url = build_absolute_google_uri(request, reverse("terms"))
privacy_notice_url = build_absolute_google_uri(request, reverse("privacy_notice"))

sent_email_count = 0
for email in emails:
Expand All @@ -115,11 +129,12 @@ def get(self, request):
subject="Your account needs verification",
title="Your account needs verification",
text_content=USER_2ND_VERIFY_EMAIL_REMINDER_TEXT.format(
email_verification_url=request.build_absolute_uri(
email_verification_url=build_absolute_google_uri(
request,
reverse(
"verify_email",
kwargs={"token": generate_token_for_email(email)},
)
),
),
terms_url=terms_url,
privacy_notice_url=privacy_notice_url,
Expand Down