1
|
|
|
from django.contrib.auth import get_user_model |
|
|
|
|
2
|
|
|
from django.contrib.auth.tokens import default_token_generator |
|
|
|
|
3
|
|
|
|
4
|
|
|
from rest_framework import generics, permissions, status, views |
|
|
|
|
5
|
|
|
from rest_framework.response import Response |
|
|
|
|
6
|
|
|
from rest_framework.reverse import reverse |
|
|
|
|
7
|
|
|
|
8
|
|
|
from djoser.conf import settings |
9
|
|
|
from djoser.compat import get_user_email_field_name |
10
|
|
|
|
11
|
|
|
from . import utils, signals |
12
|
|
|
|
13
|
|
|
User = get_user_model() |
|
|
|
|
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class RootView(views.APIView): |
17
|
|
|
""" |
18
|
|
|
Root endpoint - use one of sub endpoints. |
19
|
|
|
""" |
20
|
|
|
permission_classes = ( |
21
|
|
|
permissions.AllowAny, |
22
|
|
|
) |
23
|
|
|
urls_mapping = { |
24
|
|
|
'me': 'user', |
25
|
|
|
'register': 'register', |
26
|
|
|
'activate': 'activate', |
27
|
|
|
'change-' + User.USERNAME_FIELD: 'set_username', |
28
|
|
|
'change-password': 'set_password', |
29
|
|
|
'password-reset': 'password_reset', |
30
|
|
|
'password-reset-confirm': 'password_reset_confirm', |
31
|
|
|
} |
32
|
|
|
urls_extra_mapping = None |
33
|
|
|
|
34
|
|
|
def get_urls_mapping(self, **kwargs): |
|
|
|
|
35
|
|
|
mapping = self.urls_mapping.copy() |
36
|
|
|
mapping.update(kwargs) |
37
|
|
|
if self.urls_extra_mapping: |
38
|
|
|
mapping.update(self.urls_extra_mapping) |
39
|
|
|
mapping.update(settings.ROOT_VIEW_URLS_MAPPING) |
40
|
|
|
return mapping |
41
|
|
|
|
42
|
|
|
def get(self, request, format=None): |
|
|
|
|
43
|
|
|
return Response( |
44
|
|
|
dict([(key, reverse(url_name, request=request, format=format)) |
45
|
|
|
for key, url_name in self.get_urls_mapping().items()]) |
46
|
|
|
) |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
class RegistrationView(generics.CreateAPIView): |
50
|
|
|
""" |
51
|
|
|
Use this endpoint to register new user. |
52
|
|
|
""" |
53
|
|
|
serializer_class = settings.SERIALIZERS.user_registration |
54
|
|
|
permission_classes = ( |
55
|
|
|
permissions.AllowAny, |
56
|
|
|
) |
57
|
|
|
|
58
|
|
|
def perform_create(self, serializer): |
|
|
|
|
59
|
|
|
user = serializer.save() |
60
|
|
|
signals.user_registered.send( |
61
|
|
|
sender=self.__class__, user=user, request=self.request |
62
|
|
|
) |
63
|
|
|
email_factory_cls = None |
64
|
|
|
if settings.SEND_ACTIVATION_EMAIL: |
65
|
|
|
email_factory_cls = utils.UserActivationEmailFactory |
66
|
|
|
elif settings.SEND_CONFIRMATION_EMAIL: |
67
|
|
|
email_factory_cls = utils.UserConfirmationEmailFactory |
68
|
|
|
|
69
|
|
|
if email_factory_cls is not None: |
70
|
|
|
utils.send_email(self.request, email_factory_cls, user) |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
class LoginView(utils.ActionViewMixin, generics.GenericAPIView): |
74
|
|
|
""" |
75
|
|
|
Use this endpoint to obtain user authentication token. |
76
|
|
|
""" |
77
|
|
|
serializer_class = settings.SERIALIZERS.login |
78
|
|
|
permission_classes = ( |
79
|
|
|
permissions.AllowAny, |
80
|
|
|
) |
81
|
|
|
|
82
|
|
|
def _action(self, serializer): |
83
|
|
|
token = utils.login_user(self.request, serializer.user) |
84
|
|
|
token_serializer_class = settings.SERIALIZERS.token |
85
|
|
|
return Response( |
86
|
|
|
data=token_serializer_class(token).data, |
87
|
|
|
status=status.HTTP_200_OK, |
88
|
|
|
) |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
class LogoutView(views.APIView): |
92
|
|
|
""" |
93
|
|
|
Use this endpoint to logout user (remove user authentication token). |
94
|
|
|
""" |
95
|
|
|
permission_classes = ( |
96
|
|
|
permissions.IsAuthenticated, |
97
|
|
|
) |
98
|
|
|
|
99
|
|
|
def post(self, request): |
|
|
|
|
100
|
|
|
utils.logout_user(request) |
101
|
|
|
return Response(status=status.HTTP_204_NO_CONTENT) |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
class PasswordResetView(utils.ActionViewMixin, generics.GenericAPIView): |
105
|
|
|
""" |
106
|
|
|
Use this endpoint to send email to user with password reset link. |
107
|
|
|
""" |
108
|
|
|
serializer_class = settings.SERIALIZERS.password_reset |
109
|
|
|
permission_classes = ( |
110
|
|
|
permissions.AllowAny, |
111
|
|
|
) |
112
|
|
|
|
113
|
|
|
_users = None |
114
|
|
|
|
115
|
|
|
def _action(self, serializer): |
116
|
|
|
for user in self.get_users(serializer.data['email']): |
117
|
|
|
self.send_password_reset_email(user) |
118
|
|
|
return Response(status=status.HTTP_204_NO_CONTENT) |
119
|
|
|
|
120
|
|
|
def get_users(self, email): |
|
|
|
|
121
|
|
|
if self._users is None: |
122
|
|
|
email_field_name = get_user_email_field_name(User) |
123
|
|
|
users = User._default_manager.filter(**{ |
|
|
|
|
124
|
|
|
email_field_name + '__iexact': email |
125
|
|
|
}) |
126
|
|
|
self._users = [ |
127
|
|
|
u for u in users if u.is_active and u.has_usable_password() |
128
|
|
|
] |
129
|
|
|
return self._users |
130
|
|
|
|
131
|
|
|
def send_password_reset_email(self, user): |
|
|
|
|
132
|
|
|
email_factory = utils.UserPasswordResetEmailFactory.from_request( |
133
|
|
|
self.request, user=user |
134
|
|
|
) |
135
|
|
|
email = email_factory.create() |
136
|
|
|
email.send() |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
class SetPasswordView(utils.ActionViewMixin, generics.GenericAPIView): |
140
|
|
|
""" |
141
|
|
|
Use this endpoint to change user password. |
142
|
|
|
""" |
143
|
|
|
permission_classes = ( |
144
|
|
|
permissions.IsAuthenticated, |
145
|
|
|
) |
146
|
|
|
|
147
|
|
|
def get_serializer_class(self): |
|
|
|
|
148
|
|
|
if settings.SET_PASSWORD_RETYPE: |
149
|
|
|
return settings.SERIALIZERS.set_password_retype |
150
|
|
|
return settings.SERIALIZERS.set_password |
151
|
|
|
|
152
|
|
|
def _action(self, serializer): |
153
|
|
|
self.request.user.set_password(serializer.data['new_password']) |
154
|
|
|
self.request.user.save() |
155
|
|
|
|
156
|
|
|
if settings.LOGOUT_ON_PASSWORD_CHANGE: |
157
|
|
|
utils.logout_user(self.request) |
158
|
|
|
|
159
|
|
|
return Response(status=status.HTTP_204_NO_CONTENT) |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
class PasswordResetConfirmView(utils.ActionViewMixin, generics.GenericAPIView): |
163
|
|
|
""" |
164
|
|
|
Use this endpoint to finish reset password process. |
165
|
|
|
""" |
166
|
|
|
permission_classes = ( |
167
|
|
|
permissions.AllowAny, |
168
|
|
|
) |
169
|
|
|
token_generator = default_token_generator |
170
|
|
|
|
171
|
|
|
def get_serializer_class(self): |
|
|
|
|
172
|
|
|
if settings.PASSWORD_RESET_CONFIRM_RETYPE: |
173
|
|
|
return settings.SERIALIZERS.password_reset_confirm_retype |
174
|
|
|
return settings.SERIALIZERS.password_reset_confirm |
175
|
|
|
|
176
|
|
|
def _action(self, serializer): |
|
|
|
|
177
|
|
|
serializer.user.set_password(serializer.data['new_password']) |
178
|
|
|
serializer.user.save() |
179
|
|
|
return Response(status=status.HTTP_204_NO_CONTENT) |
180
|
|
|
|
181
|
|
|
|
182
|
|
|
class ActivationView(utils.ActionViewMixin, generics.GenericAPIView): |
183
|
|
|
""" |
184
|
|
|
Use this endpoint to activate user account. |
185
|
|
|
""" |
186
|
|
|
serializer_class = settings.SERIALIZERS.activation |
187
|
|
|
permission_classes = ( |
188
|
|
|
permissions.AllowAny, |
189
|
|
|
) |
190
|
|
|
token_generator = default_token_generator |
191
|
|
|
|
192
|
|
|
def _action(self, serializer): |
193
|
|
|
serializer.user.is_active = True |
194
|
|
|
serializer.user.save() |
195
|
|
|
signals.user_activated.send( |
196
|
|
|
sender=self.__class__, user=serializer.user, request=self.request) |
197
|
|
|
|
198
|
|
|
if settings.SEND_CONFIRMATION_EMAIL: |
199
|
|
|
email_factory = utils.UserConfirmationEmailFactory.from_request( |
200
|
|
|
self.request, user=serializer.user) |
201
|
|
|
email = email_factory.create() |
202
|
|
|
email.send() |
203
|
|
|
return Response(status=status.HTTP_204_NO_CONTENT) |
204
|
|
|
|
205
|
|
|
|
206
|
|
|
class SetUsernameView(utils.ActionViewMixin, generics.GenericAPIView): |
207
|
|
|
""" |
208
|
|
|
Use this endpoint to change user username. |
209
|
|
|
""" |
210
|
|
|
permission_classes = (permissions.IsAuthenticated,) |
211
|
|
|
|
212
|
|
|
def get_serializer_class(self): |
|
|
|
|
213
|
|
|
if settings.SET_USERNAME_RETYPE: |
214
|
|
|
return settings.SERIALIZERS.set_username_retype |
215
|
|
|
return settings.SERIALIZERS.set_username |
216
|
|
|
|
217
|
|
|
def _action(self, serializer): |
218
|
|
|
user = self.request.user |
219
|
|
|
new_username = serializer.data['new_' + User.USERNAME_FIELD] |
220
|
|
|
|
221
|
|
|
setattr(user, User.USERNAME_FIELD, new_username) |
222
|
|
|
if settings.SEND_ACTIVATION_EMAIL: |
223
|
|
|
user.is_active = False |
224
|
|
|
email_factory_cls = utils.UserActivationEmailFactory |
225
|
|
|
utils.send_email(self.request, email_factory_cls, user) |
226
|
|
|
user.save() |
227
|
|
|
|
228
|
|
|
return Response(status=status.HTTP_204_NO_CONTENT) |
229
|
|
|
|
230
|
|
|
|
231
|
|
|
class UserView(generics.RetrieveUpdateAPIView): |
232
|
|
|
""" |
233
|
|
|
Use this endpoint to retrieve/update user. |
234
|
|
|
""" |
235
|
|
|
model = User |
236
|
|
|
serializer_class = settings.SERIALIZERS.user |
237
|
|
|
permission_classes = (permissions.IsAuthenticated,) |
238
|
|
|
|
239
|
|
|
def get_object(self, *args, **kwargs): |
|
|
|
|
240
|
|
|
return self.request.user |
241
|
|
|
|
242
|
|
|
def perform_update(self, serializer): |
|
|
|
|
243
|
|
|
super(UserView, self).perform_update(serializer) |
244
|
|
|
user = serializer.instance |
245
|
|
|
if settings.SEND_ACTIVATION_EMAIL and not user.is_active: |
246
|
|
|
email_factory_cls = utils.UserActivationEmailFactory |
247
|
|
|
utils.send_email(self.request, email_factory_cls, user) |
248
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.