-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
630f2b5
commit 1087ae6
Showing
4 changed files
with
90 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,34 @@ | ||
from django.contrib.sites.shortcuts import get_current_site | ||
from django.core.mail import EmailMultiAlternatives | ||
from django.template.loader import render_to_string | ||
from django.utils.encoding import force_bytes | ||
from django.utils.http import urlsafe_base64_encode | ||
|
||
from accounts.tokens import account_activation_token | ||
|
||
|
||
class SendActiveEmailService: | ||
def __init__(self, user): | ||
self.user = user | ||
|
||
def send(self): | ||
if not change: # Only send the email when a new user is created | ||
obj.is_active = False # Deactivate account until it is confirmed | ||
obj.save() | ||
|
||
def send_activation_email(request, user): | ||
if user and request: | ||
current_site = get_current_site(request) | ||
mail_subject = "Activate your account." | ||
message = render_to_string( | ||
"accounts/activation_email.html", | ||
{ | ||
"user": obj, | ||
"user": user, | ||
"domain": current_site.domain, | ||
"uid": urlsafe_base64_encode(force_bytes(obj.pk)), | ||
"token": account_activation_token.make_token(obj), | ||
"uid": urlsafe_base64_encode(force_bytes(user.pk)), | ||
"token": account_activation_token.make_token(user), | ||
}, | ||
) | ||
send_mail(mail_subject, message, "[email protected]", [obj.email]) | ||
else: | ||
obj.save() | ||
text_content = ( | ||
"Please activate your account by clicking the link provided in the email." | ||
) | ||
email = EmailMultiAlternatives( | ||
mail_subject, text_content, "[email protected]", [user.email] | ||
) | ||
email.attach_alternative(message, "text/html") | ||
email.send() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,10 @@ | ||
import email | ||
|
||
from django.contrib.auth import get_user_model | ||
from django.contrib.auth import login as auth_login | ||
from django.contrib.sites.shortcuts import get_current_site | ||
from django.core.mail import send_mail | ||
from django.shortcuts import redirect, render | ||
from django.template.loader import render_to_string | ||
from django.utils.encoding import force_bytes, force_str | ||
from django.utils.http import urlsafe_base64_decode, urlsafe_base64_encode | ||
from django.utils.encoding import force_str | ||
from django.utils.http import urlsafe_base64_decode | ||
|
||
from accounts.service.active_email import SendActiveEmailService | ||
|
||
from .forms import CustomAuthenticationForm, CustomUserCreationForm | ||
from .tokens import account_activation_token | ||
|
@@ -22,18 +19,8 @@ def register(request): | |
user.is_active = False | ||
user.save() | ||
|
||
current_site = get_current_site(request) | ||
mail_subject = "Activate your account." | ||
message = render_to_string( | ||
"accounts/activation_email.html", | ||
{ | ||
"user": user, | ||
"domain": current_site.domain, | ||
"uid": urlsafe_base64_encode(force_bytes(user.pk)), | ||
"token": account_activation_token.make_token(user), | ||
}, | ||
) | ||
send_mail(mail_subject, message, "[email protected]", [email]) | ||
SendActiveEmailService.send_activation_email(request, user) | ||
|
||
return redirect("accounts:activation_sent") | ||
|
||
else: | ||
|
@@ -82,19 +69,15 @@ def resend_activation(request): | |
try: | ||
user = User.objects.get(email=user_email) | ||
if not user.is_active: | ||
current_site = get_current_site(request) | ||
mail_subject = "Activate your account." | ||
message = render_to_string( | ||
"accounts/activation_email.html", | ||
{ | ||
"user": user, | ||
"domain": current_site.domain, | ||
"uid": urlsafe_base64_encode(force_bytes(user.pk)), | ||
"token": account_activation_token.make_token(user), | ||
}, | ||
) | ||
send_mail(mail_subject, message, "[email protected]", [email]) | ||
SendActiveEmailService.send_activation_email(request, user) | ||
|
||
return redirect("accounts:activation_sent") | ||
|
||
else: | ||
return render( | ||
request, "accounts/resend_activation.html", {"error": "Email address active."} | ||
) | ||
|
||
except User.DoesNotExist: | ||
# Handle the case where the email does not exist | ||
return render( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters