11import uuid
22
3+ from allauth .account .views import ConfirmEmailView
4+ from allauth .account .utils import complete_signup
5+ from allauth .account .internal import flows
6+ from allauth .account .models import (
7+ get_emailconfirmation_model ,
8+ )
9+
10+ from django .http import Http404
11+
312from django .utils .decorators import method_decorator
413from django .views .decorators .cache import cache_page
514from django .views .decorators .vary import vary_on_cookie
1524
1625from users .models import User
1726from users .api .serializers import UserSerializer , RegisterSerializer
18-
27+ from config . settings . base import CONFIRM_EMAIL_ON_GET
1928
2029class UserViewSet (RetrieveModelMixin , ListModelMixin , UpdateModelMixin , GenericViewSet ):
2130 serializer_class = UserSerializer
@@ -44,6 +53,7 @@ def post(self, request):
4453 serializer = RegisterSerializer (data = request .data )
4554 if serializer .is_valid ():
4655 user = serializer .save ()
56+ complete_signup (request , user , 'mandatory' , None ) # Trigger email verification
4757 refresh = RefreshToken .for_user (user )
4858 return Response ({
4959 'user' : RegisterSerializer (user ).data ,
@@ -53,6 +63,38 @@ def post(self, request):
5363 return Response (serializer .errors , status = status .HTTP_400_BAD_REQUEST )
5464
5565
66+ class CustomConfirmEmailView (APIView ):
67+ permission_classes = [permissions .AllowAny ]
68+
69+ def __init__ (self , ** kwargs ):
70+ super ().__init__ ()
71+ self .object = None
72+
73+ def get_object (self , queryset = None ):
74+ key = self .kwargs ["key" ]
75+ model = get_emailconfirmation_model ()
76+ email_confirmation = model .from_key (key )
77+ if not email_confirmation :
78+ raise Http404 ()
79+ return email_confirmation
80+
81+ def get (self , * args , ** kwargs ):
82+ if CONFIRM_EMAIL_ON_GET :
83+ self .object = verification = self .get_object ()
84+ response = flows .email_verification .verify_email_indirectly (
85+ self .request ,
86+ verification .email_address .user ,
87+ verification .email_address .user .email
88+ )
89+ if response :
90+ return Response ({
91+ "detail" : "Email verified successfully." ,
92+ "data" : verification .email_address .user .email
93+ },
94+ status = status .HTTP_200_OK )
95+ return Response ({"detail" : "Not accepting GET request on the endpoint" }, status = status .HTTP_400_BAD_REQUEST )
96+
97+
5698class UserProfileView (APIView ):
5799 permission_classes = [permissions .IsAuthenticated ]
58100
0 commit comments