|
1 | 1 | from django.contrib.auth import get_user_model
|
2 | 2 | from django.core.mail import send_mail
|
3 | 3 | from django.template.loader import render_to_string
|
4 |
| -from django.urls import reverse |
5 | 4 | from django.utils.translation import gettext as _
|
6 | 5 |
|
7 | 6 | from rest_framework import generics, status, views
|
@@ -38,27 +37,59 @@ def post(self, request):
|
38 | 37 |
|
39 | 38 | If the setting email_confirmation_send_email is true (default),
|
40 | 39 | the function send_email will be called. That function requires
|
41 |
| - that your app define a route named app-auth:email-confirmation |
42 |
| - with an id parameter; the view for this route should get an |
43 |
| - email confirmation instance using the ID and call the confirm |
44 |
| - method. To use a field that's not named 'id', define the setting |
45 |
| - email_confirmation_lookup_param (this will change the URL pattern). |
| 40 | + that your project defines defines two email templates: |
| 41 | + - rest_auth_toolkit/email_confirmation.txt |
| 42 | + - rest_auth_toolkit/email_confirmation.html |
| 43 | +
|
| 44 | + The templates will be passed the User and EmailConfirmation instances |
| 45 | + (as variables *user* and *confirmation*). To help generating links, |
| 46 | + a variable *base_url* with a value like "https://domain" (scheme, |
| 47 | + domain and optional port depending on the request, but no path), which |
| 48 | + lets you write code like `{{ base_url }}{% url "my-route" %}`. |
| 49 | +
|
| 50 | + It is up to your project to define what the link is. The demo app |
| 51 | + demonstrates a simple Django view that validates the email validation |
| 52 | + token in the URL; for a project with a front-end site (e.g. a JavaScript |
| 53 | + app) on a different domain than the Django API, a custom template tag |
| 54 | + could be used to generate the right URL for the front-end site. |
| 55 | +
|
| 56 | + If the setting is false, the user will be active immediately. |
46 | 57 | """
|
47 | 58 | deserializer = self.get_serializer(data=request.data)
|
48 | 59 | deserializer.is_valid(raise_exception=True)
|
49 |
| - user = deserializer.save() |
50 | 60 |
|
51 |
| - if self.email_confirmation_class is None: |
52 |
| - raise MissingSetting('email_confirmation_string') |
| 61 | + confirm_email = get_setting('email_confirmation_send_email', True) |
53 | 62 |
|
54 |
| - confirmation = self.email_confirmation_class.objects.create(user=user) |
55 |
| - if get_setting('email_confirmation_send_email', True): |
| 63 | + if not confirm_email: |
| 64 | + deserializer.save(is_active=True) |
| 65 | + else: |
| 66 | + user = deserializer.save() |
| 67 | + |
| 68 | + if self.email_confirmation_class is None: |
| 69 | + raise MissingSetting('email_confirmation_class') |
| 70 | + |
| 71 | + confirmation = self.email_confirmation_class.objects.create(user=user) |
56 | 72 | email_field = user.get_email_field_name()
|
57 | 73 | send_email(request, user, getattr(user, email_field), confirmation)
|
58 | 74 |
|
59 | 75 | return Response(status=status.HTTP_201_CREATED)
|
60 | 76 |
|
61 | 77 |
|
| 78 | +class EmailConfirmationView(generics.GenericAPIView): |
| 79 | + """Validate an email address after sign-up. |
| 80 | +
|
| 81 | + Response: 200 OK (no content) |
| 82 | +
|
| 83 | + Error response (code 400): |
| 84 | +
|
| 85 | + ```json |
| 86 | + {"errors": {"token": "Error message"}} |
| 87 | + ``` |
| 88 | + """ |
| 89 | + def post(self, request): |
| 90 | + pass |
| 91 | + |
| 92 | + |
62 | 93 | class LoginView(generics.GenericAPIView):
|
63 | 94 | """Authenticate a user, return an API auth token if valid.
|
64 | 95 |
|
@@ -146,14 +177,14 @@ def send_email(request, user, address, confirmation):
|
146 | 177 | subject = _('Confirm your email address')
|
147 | 178 | from_address = get_setting('email_confirmation_from')
|
148 | 179 |
|
149 |
| - lookup_field = get_setting('email_confirmation_lookup_field', 'id') |
150 |
| - confirmation_url = request.build_absolute_uri( |
151 |
| - reverse('app-auth:email-confirmation', |
152 |
| - kwargs={lookup_field: getattr(confirmation, lookup_field)})) |
153 | 180 | # The url template tag doesn't include scheme/domain/port, pass a helper
|
154 | 181 | base_url = request.build_absolute_uri('/')[:-1]
|
155 | 182 |
|
156 |
| - context = {'base_url': base_url, 'confirmation_url': confirmation_url} |
| 183 | + context = { |
| 184 | + 'user': user, |
| 185 | + 'confirmation': confirmation, |
| 186 | + 'base_url': base_url, |
| 187 | + } |
157 | 188 | txt_content = render_to_string('rest_auth_toolkit/email_confirmation.txt', context)
|
158 | 189 | html_content = render_to_string('rest_auth_toolkit/email_confirmation.html', context)
|
159 | 190 |
|
|
0 commit comments