-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignals.py
46 lines (40 loc) · 1.59 KB
/
signals.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
from django.core.mail import EmailMultiAlternatives
from django.dispatch import receiver
from django.template.loader import render_to_string
from django.urls import reverse
from django.conf import settings
from django_rest_passwordreset.signals import reset_password_token_created
@receiver(reset_password_token_created)
def password_reset_token_created(sender, instance, reset_password_token, *args, **kwargs):
"""
Handles password reset tokens
When a token is created, an e-mail needs to be sent to the user
:param sender: View Class that sent the signal
:param instance: View Instance that sent the signal
:param reset_password_token: Token Model Object
:param args:
:param kwargs:
:return:
"""
# send an e-mail to the user
context = {
'current_user': reset_password_token.user,
'username': reset_password_token.user.username,
'email': reset_password_token.user.email,
'reset_password_url': f"{settings.FRONTEND_URL}/reset_password?token={reset_password_token.key}"
}
# render email text
email_html_message = render_to_string('email/password_reset_email.html', context)
email_plaintext_message = render_to_string('email/password_reset_email.txt', context)
msg = EmailMultiAlternatives(
# title:
"Password Reset for Attendunce",
# message:
email_plaintext_message,
# from:
"[email protected]", # Does not matter, it will be overridden
# to:
[reset_password_token.user.email]
)
msg.attach_alternative(email_html_message, "text/html")
msg.send()