forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverification_email.py
More file actions
74 lines (62 loc) · 2.46 KB
/
verification_email.py
File metadata and controls
74 lines (62 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from flask import request
from graphql import GraphQLError
from notifications_python_client.notifications import NotificationsAPIClient
from app import logger
from json_web_token import tokenize
from models import Users
def get_verification_email_status(notify_client, response):
try:
email_status = notify_client.get_notification_by_id(response.get("id")).get(
"status"
)
return email_status
except Exception as e:
logger.error(f"Error when retrieving email status: {str(e)}")
return "Error, when sending verification email, please try again."
def send_verification_email(user: Users, client: NotificationsAPIClient):
"""
This function allows a user object to be passed in during account creation
and send an email to be used for verifying accounts
:param user: A instance of /models/User.py
:param client: An instance of NotificationsAPIClient
:return: None
"""
# Create Notify Client
notify_client = client
# Check to see if users preferred lang is English or French
if user.preferred_lang == "french":
email_template_id = "f2c9b64a-c754-4ffd-93e9-33fdb0b5ae0b"
else:
email_template_id = "6e3368a7-0d75-47b1-b4b2-878234e554c9"
# URL Generation
parameters = {"user_id": user.id}
token = tokenize(parameters=parameters, exp_period=24)
url = str(request.url_root) + "validate/" + str(token)
# Send Email
try:
response = notify_client.send_email_notification(
email_address=user.user_name,
template_id=email_template_id,
personalisation={"user": user.display_name, "verify_email_url": url},
)
except Exception as e:
logger.error(
f"Error when sending user: {user.id}'s verification email: {str(e)}"
)
raise GraphQLError("Error, when sending verification email, please try again.")
# Check Email status
email_status = get_verification_email_status(
notify_client=notify_client, response=response,
)
if (
email_status == "permanent-failure"
or email_status == "temporary-failure"
or email_status == "technical-failure"
):
logger.warning(
f"{email_status} occurred when attempting to send {user.id}'s verification email."
)
return f"Email Send Error: {email_status}"
else:
logger.info(f"User: {user.id} successfully sent verification email.")
return email_status