7
7
from rest_framework .permissions import IsAuthenticated
8
8
from rest_framework .response import Response
9
9
10
- from .serializers import FacebookLoginDeserializer , LoginDeserializer , SignupDeserializer
10
+ from .models import email_confirmed
11
+ from .serializers import (FacebookLoginDeserializer , LoginDeserializer ,
12
+ SignupDeserializer , EmailConfirmationDeserializer )
11
13
from .utils import get_object_from_setting , get_setting , MissingSetting
12
14
13
15
try :
@@ -30,16 +32,20 @@ class SignupView(generics.GenericAPIView):
30
32
- rest_auth_toolkit/email_confirmation.html
31
33
32
34
The templates will be passed the User and EmailConfirmation instances
33
- (as variables *user* and *confirmation*). To help generating links,
34
- a variable *base_url* with a value like "https://domain" (scheme,
35
- domain and optional port depending on the request, but no path), which
36
- lets you write code like `{{ base_url }}{% url "my-route" %}`.
35
+ (as variables *user* and *confirmation*) as well as the request;
36
+ note that template context processors are not available in email
37
+ teamplates.
37
38
38
- It is up to your project to define what the link is. The demo app
39
- demonstrates a simple Django view that validates the email validation
40
- token in the URL; for a project with a front-end site (e.g. a JavaScript
41
- app) on a different domain than the Django API, a custom template tag
42
- could be used to generate the right URL for the front-end site.
39
+ It is up to your project to generate a link that will work, using
40
+ your own template code or custom tags.
41
+
42
+ The demo app shows one way to handle this: a Django view validates
43
+ the email confirmation token in its URL; the link is generated with
44
+ a custom template tag because Django doesn't offer a tag to create
45
+ full URLs. For a project with a front-end site (e.g. a JavaScript app)
46
+ on a different domain than the API powered by Django, the template tag
47
+ could for example use a setting to know the front-end domain name + a
48
+ mapping of front-end routes to generate the path portion of the links.
43
49
44
50
If the setting is false, the user will be active immediately.
45
51
"""
@@ -77,19 +83,51 @@ def post(self, request):
77
83
return Response (status = status .HTTP_201_CREATED )
78
84
79
85
86
+ def send_email (request , user , address , confirmation ):
87
+ """Send the confirmation email for a new user."""
88
+ subject = _ ('Confirm your email address' )
89
+ from_address = get_setting ('email_confirmation_from' )
90
+
91
+ context = {
92
+ 'user' : user ,
93
+ 'confirmation' : confirmation ,
94
+ }
95
+ txt_content = render_to_string ('rest_auth_toolkit/email_confirmation.txt' ,
96
+ context , request = request )
97
+ html_content = render_to_string ('rest_auth_toolkit/email_confirmation.html' ,
98
+ context , request = request )
99
+
100
+ send_mail (subject = subject ,
101
+ from_email = from_address , recipient_list = [address ],
102
+ message = txt_content , html_message = html_content ,
103
+ fail_silently = False )
104
+
105
+
80
106
class EmailConfirmationView (generics .GenericAPIView ):
81
107
"""Validate an email address after sign-up.
82
108
83
- Response: 200 OK (no content)
109
+ Response
110
+
111
+ `200 OK`
84
112
85
113
Error response (code 400):
86
114
87
115
```json
88
116
{"errors": {"token": "Error message"}}
89
117
```
90
118
"""
119
+ authentication_classes = ()
120
+ permission_classes = ()
121
+ serializer_class = get_object_from_setting ('email_confirmation_serializer_class' ,
122
+ EmailConfirmationDeserializer )
123
+
91
124
def post (self , request ):
92
- pass
125
+ deserializer = self .get_serializer (data = request .data )
126
+ deserializer .is_valid (raise_exception = True )
127
+
128
+ email_confirmed .send (sender = self .__class__ ,
129
+ user = deserializer .validated_data ['user' ])
130
+ return Response ()
93
131
94
132
95
133
class LoginView (generics .GenericAPIView ):
@@ -176,28 +214,6 @@ def post(self, request):
176
214
return Response (status = status .HTTP_200_OK )
177
215
178
216
179
- def send_email (request , user , address , confirmation ):
180
- """Send the confirmation email for a new user."""
181
- subject = _ ('Confirm your email address' )
182
- from_address = get_setting ('email_confirmation_from' )
183
-
184
- # The url template tag doesn't include scheme/domain/port, pass a helper
185
- base_url = request .build_absolute_uri ('/' )[:- 1 ]
186
-
187
- context = {
188
- 'user' : user ,
189
- 'confirmation' : confirmation ,
190
- 'base_url' : base_url ,
191
- }
192
- txt_content = render_to_string ('rest_auth_toolkit/email_confirmation.txt' , context )
193
- html_content = render_to_string ('rest_auth_toolkit/email_confirmation.html' , context )
194
-
195
- send_mail (subject = subject ,
196
- from_email = from_address , recipient_list = [address ],
197
- message = txt_content , html_message = html_content ,
198
- fail_silently = False )
199
-
200
-
201
217
def activate_user (sender , ** kwargs ):
202
218
"""Mark user as active when a confirmation link is visited.
203
219
0 commit comments