-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathutility.py
123 lines (100 loc) · 3.57 KB
/
utility.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from django.http import JsonResponse
from typing import Any
from .models import User
from requests import post
import jwt
import datetime
from anwesha.settings import COOKIE_ENCRYPTION_SECRET, EMAIL_MICROSERVICE_ENDPOINT
class Autherize:
"""
Authorization decorator class.
Attributes:
mode (int): Mode of the decorator.
Methods:
__init__(self, mode): Initializes the Autherize instance.
__call__(self, func): Decorator method to authorize the function.
"""
def __init__(self, mode=1) -> None:
"""
Initializes the Autherize instance.
Args:
mode (int): Mode of the decorator.
"""
self.mode = mode
def __call__(self, func) -> Any:
"""
Decorator method to authorize the function.
Args:
func: Function to be authorized.
Returns:
Any: Result of the function call or error response.
Raises:
jwt.ExpiredSignatureError: If the token is expired.
"""
def wrapper(*args, **kwargs):
request = args[1]
token = request.COOKIES.get('jwt')
if not token:
return JsonResponse({"message": "You are unauthenticated. Please log in first."}, status=401)
try:
payload = jwt.decode(token, COOKIE_ENCRYPTION_SECRET, algorithms='HS256')
except jwt.ExpiredSignatureError:
return JsonResponse({"message": "Your token is expired. Please login again."}, status=409)
user = User.objects.get(anwesha_id=payload["id"])
if not user:
return JsonResponse({"message": "User not found."}, status=404)
kwargs["user"] = user
return func(*args, **kwargs)
return wrapper
"""
This is a utility function to generate content for the email.
Parameters:
- type (int): Type of email content to generate.
- kwargs (dict): Additional keyword arguments based on the type of email.
Returns:
- str: Generated email content.
Precautions:
- Ensure that the appropriate kwargs are provided for the specified email type.
- Make sure to handle the return value of None when the email type is not supported.
"""
def mail_content(type, *args, **kwargs):
if type == 1:
email = kwargs['email_id']
user = kwargs['full_name']
anwesha_id = kwargs['anwesha_id']
payload = {
'email': email,
'id': anwesha_id,
"exp": datetime.datetime.utcnow() + datetime.timedelta(minutes=60),
"iat": datetime.datetime.utcnow()
}
token = jwt.encode(payload, COOKIE_ENCRYPTION_SECRET, algorithm='HS256')
link = "https://backend.anwesha.live/user/verifyemail/" + token
body = f'''Hello {user},
Thank you for being part of Anwesha 2023.
Your Anwesha ID is: {anwesha_id}.
Please click on the link below to verify your email address for Anwesha login:
{link}
Thanks,
Team Anwesha
'''
else:
return None
return body
def send_email_using_microservice(email_id, subject, text):
"""
Sends an email using an external mail API.
Parameters:
- email_id (str): Email ID of the recipient.
- subject (str): Subject of the email.
- text (str): Content of the email.
Precautions:
- Ensure that the EMAIL_MICROSERVICE_ENDPOINT is properly configured.
- Handle any exceptions or errors that may occur during the email sending process.
"""
PARAM = {
"to": email_id,
"subject": subject,
"text": text
}
r = post(url=EMAIL_MICROSERVICE_ENDPOINT, data=PARAM)