Skip to content

Commit

Permalink
backup of code
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-gray-tangent committed Jul 18, 2024
1 parent 630f2b5 commit 1087ae6
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 56 deletions.
33 changes: 22 additions & 11 deletions app/accounts/service/active_email.py
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()
45 changes: 14 additions & 31 deletions app/accounts/views.py
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
Expand All @@ -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:
Expand Down Expand Up @@ -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(
Expand Down
63 changes: 52 additions & 11 deletions app/templates/accounts/activation_email.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,58 @@
<head>
<meta charset="UTF-8">
<title>Activate Your Account</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
}
.container {
width: 100%;
max-width: 600px;
margin: 0 auto;
background-color: #ffffff;
padding: 20px;
border: 1px solid #dddddd;
}
h1 {
color: #333333;
}
p {
color: #666666;
}
a {
color: #1a73e8;
text-decoration: none;
}
.button {
display: inline-block;
padding: 10px 20px;
background-color: #1a73e8;
color: #ffffff;
text-align: center;
border-radius: 5px;
text-decoration: none;
}
.button:hover {
background-color: #1558b0;
}
</style>
</head>
<body>
<p>Hi {{ user.username }},</p>
<p>Thank you for signing up for our service. Please click the link below to activate your account:</p>
<p>
<a href="http://{{ domain }}{% url 'accounts:activate' uidb64=uid token=token %}">
Activate your account
</a>
</p>
<p>If you did not sign up for this account, please ignore this email.</p>
<p>Thank you,</p>
<p>The Team</p>
<div class="container">
<h1>Activate Your Account</h1>
<p>Hi {{ user.username }},</p>
<p>Thank you for signing up for our service. Please click the link below to activate your account:</p>
<p>
<a href="http://{{ domain }}{% url 'accounts:activate' uidb64=uid token=token %}" class="button">
Activate your account
</a>
</p>
<p>If you did not sign up for this account, please ignore this email.</p>
<p>Thank you,</p>
<p>The Team</p>
</div>
</body>
</html>
</html>
5 changes: 2 additions & 3 deletions app/templates/accounts/activation_sent.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
<div class="card body-card">
<div class="user-account-body">
<h2>Activation Email Sent</h2>
<p>Thank you for registering. An activation email has been sent to your email address. Please check
your
email and click on the activation link to activate your account.</p>
<p>Thank you for registering. An activation email has been sent to your email address.</p>
<p>Please check your email and click on the activation link to activate your account.</p>
<p>If you did not receive the email, please check your spam folder or
<a href="{% url 'accounts:resend_activation' %}">resend the activation email</a>.</p>
<a href="{% url 'accounts:login' %}">Login</a>
Expand Down

0 comments on commit 1087ae6

Please sign in to comment.