this is my views.py :
from django.contrib.auth import get_user_model
from dj_rest_auth.registration.views import RegisterView
from dj_rest_auth.views import UserDetailsView
from users.serializers import CustomRegisterSerializer
from rest_framework.views import APIView
from rest_framework.response import Response
from allauth.account.models import EmailConfirmation, EmailConfirmationHMAC
from .serializers import CustomUserDetailsSerializer
from rest_framework import status
from django.utils import timezone
class CustomRegisterView(RegisterView):
serializer_class = CustomRegisterSerializer
def post(self, request, *args, **kwargs):
if request.user.is_authenticated:
return Response(
{"detail": "You are already registered and logged in."},
status=status.HTTP_400_BAD_REQUEST
)
return super().post(request, *args, **kwargs)
def perform_create(self, serializer):
user = serializer.save(self.request)
email_address = user.emailaddress_set.get(email=user.email)
email_confirmation = EmailConfirmation.create(email_address)
email_confirmation.sent = timezone.now()
email_confirmation.save()
class CustomUserDetailsView(UserDetailsView):
serializer_class = CustomUserDetailsSerializer
class EmailConfirmationView(APIView):
def get(self, request, key):
try:
email_confirmation = self.get_email_confirmation(key)
except EmailConfirmation.DoesNotExist:
return Response({'error': 'Invalid key'}, status=status.HTTP_400_BAD_REQUEST)
if email_confirmation.email_address.verified:
return Response({'message': 'Email already verified'}, status=status.HTTP_200_OK)
email_confirmation.confirm()
user = self.get_user(email_confirmation)
# Perform additional actions if needed, such as updating user fields
user.is_active = True
user.save()
return Response({'message': 'Email confirmed successfully'}, status=status.HTTP_200_OK)
def get_email_confirmation(self, key):
email_confirmation = EmailConfirmationHMAC.from_key(key)
if not email_confirmation:
raise EmailConfirmation.DoesNotExist
return email_confirmation
def get_user(self, email_confirmation):
User = get_user_model()
return User.objects.get(email=email_confirmation.email_address.email)
this is my urls.py :
from users.views import CustomUserDetailsView
from .views import (
ProjectViewSets,
ThumbnailViewSets,
ProjectByCategoryAPI,
)
from dj_rest_auth.registration.views import VerifyEmailView
from rest_framework.routers import DefaultRouter
from django.urls import path, include, re_path
from dj_rest_auth.views import (
PasswordChangeView,
PasswordResetView,
PasswordResetConfirmView,
)
from users.views import (
CustomRegisterView,
CustomUserDetailsView,
EmailConfirmationView
)
router = DefaultRouter()
router.register("projects", ProjectViewSets, basename="project")
router.register("images", ThumbnailViewSets, basename="image")
urlpatterns = [
path("password/reset/", PasswordResetView.as_view(),
name="rest_password_reset"),
path(
"password/reset/confirm/<uidb64>/<token>/",
PasswordResetConfirmView.as_view(),
name="password_reset_confirm",
),
path("", include(router.urls)),
path(
"projects/<str:category>/filter",
ProjectByCategoryAPI.as_view(),
name="project_list",
),
path("dj-rest-auth/", include("dj_rest_auth.urls")),
path(
"dj-rest-auth/registration/", CustomRegisterView.as_view(), name="rest_register"
),
re_path(
"account-confirm-email/",
VerifyEmailView.as_view(),
name="account_email_verification_sent",
),
re_path(
"account-confirm-email/(?P<key>[-:\w]+)/$",
VerifyEmailView.as_view(),
name="account_confirm_email",
),
path('email-confirmation/<str:key>/', EmailConfirmationView.as_view()),
path(
"dj-rest-auth/user/", CustomUserDetailsView().as_view(), name="user-details"
),
]