|
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, get_user_email_field_name |
|
10
|
|
|
|
|
11
|
|
|
from djoser import email, utils, signals |
|
12
|
|
|
from djoser.compat import NoReverseMatch |
|
13
|
|
|
|
|
14
|
|
|
User = get_user_model() |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
class RootView(views.APIView): |
|
18
|
|
|
""" |
|
19
|
|
|
Root endpoint - use one of sub endpoints. |
|
20
|
|
|
""" |
|
21
|
|
|
permission_classes = [permissions.AllowAny] |
|
22
|
|
|
|
|
23
|
|
|
def aggregate_djoser_urlpattern_names(self): |
|
|
|
|
|
|
24
|
|
|
from djoser.urls import base, authtoken |
|
25
|
|
|
urlpattern_names = [pattern.name for pattern in base.urlpatterns] |
|
26
|
|
|
urlpattern_names += [pattern.name for pattern in authtoken.urlpatterns] |
|
27
|
|
|
urlpattern_names += self._get_jwt_urlpatterns() |
|
28
|
|
|
|
|
29
|
|
|
return urlpattern_names |
|
30
|
|
|
|
|
31
|
|
|
def get_urls_map(self, request, urlpattern_names, fmt): |
|
|
|
|
|
|
32
|
|
|
urls_map = {} |
|
33
|
|
|
for urlpattern_name in urlpattern_names: |
|
34
|
|
|
try: |
|
35
|
|
|
url = reverse(urlpattern_name, request=request, format=fmt) |
|
36
|
|
|
except NoReverseMatch: |
|
37
|
|
|
url = '' |
|
38
|
|
|
urls_map[urlpattern_name] = url |
|
39
|
|
|
return urls_map |
|
40
|
|
|
|
|
41
|
|
|
def get(self, request, fmt=None): |
|
|
|
|
|
|
42
|
|
|
urlpattern_names = self.aggregate_djoser_urlpattern_names() |
|
43
|
|
|
urls_map = self.get_urls_map(request, urlpattern_names, fmt) |
|
44
|
|
|
return Response(urls_map) |
|
45
|
|
|
|
|
46
|
|
|
def _get_jwt_urlpatterns(self): |
|
|
|
|
|
|
47
|
|
|
try: |
|
48
|
|
|
from djoser.urls import jwt |
|
49
|
|
|
return [pattern.name for pattern in jwt.urlpatterns] |
|
50
|
|
|
except ImportError: |
|
51
|
|
|
return [] |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
class UserCreateView(generics.CreateAPIView): |
|
55
|
|
View Code Duplication |
""" |
|
|
|
|
|
|
56
|
|
|
Use this endpoint to register new user. |
|
57
|
|
|
""" |
|
58
|
|
|
serializer_class = settings.SERIALIZERS.user_registration |
|
59
|
|
|
permission_classes = (permissions.AllowAny,) |
|
60
|
|
|
|
|
61
|
|
|
def perform_create(self, serializer): |
|
|
|
|
|
|
62
|
|
|
user = serializer.save() |
|
63
|
|
|
signals.user_registered.send( |
|
64
|
|
|
sender=self.__class__, user=user, request=self.request |
|
65
|
|
|
) |
|
66
|
|
|
|
|
67
|
|
|
context = {'user': user} |
|
68
|
|
|
to = [get_user_email(user)] |
|
|
|
|
|
|
69
|
|
|
if settings.SEND_ACTIVATION_EMAIL: |
|
70
|
|
|
email.ActivationEmail(self.request, context).send(to) |
|
71
|
|
|
elif settings.SEND_CONFIRMATION_EMAIL: |
|
72
|
|
|
email.ConfirmationEmail(self.request, context).send(to) |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
class UserDeleteView(generics.CreateAPIView): |
|
76
|
|
|
""" |
|
77
|
|
|
Use this endpoint to remove actually authenticated user |
|
78
|
|
|
""" |
|
79
|
|
|
serializer_class = settings.SERIALIZERS.user_delete |
|
80
|
|
|
permission_classes = (permissions.IsAuthenticated,) |
|
81
|
|
|
|
|
82
|
|
|
def get_object(self): |
|
|
|
|
|
|
83
|
|
|
return self.request.user |
|
84
|
|
|
|
|
85
|
|
|
def post(self, request, *args, **kwargs): |
|
|
|
|
|
|
86
|
|
|
instance = self.get_object() |
|
87
|
|
|
serializer = self.get_serializer(instance, data=request.data) |
|
88
|
|
|
serializer.is_valid(raise_exception=True) |
|
89
|
|
|
|
|
90
|
|
|
utils.logout_user(self.request) |
|
91
|
|
|
instance.delete() |
|
92
|
|
|
|
|
93
|
|
|
return Response(status=status.HTTP_204_NO_CONTENT) |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
class TokenCreateView(utils.ActionViewMixin, generics.GenericAPIView): |
|
97
|
|
|
""" |
|
98
|
|
|
Use this endpoint to obtain user authentication token. |
|
99
|
|
|
""" |
|
100
|
|
|
serializer_class = settings.SERIALIZERS.login |
|
101
|
|
|
permission_classes = ( |
|
102
|
|
|
permissions.AllowAny, |
|
103
|
|
|
) |
|
104
|
|
|
|
|
105
|
|
|
def _action(self, serializer): |
|
106
|
|
|
token = utils.login_user(self.request, serializer.user) |
|
107
|
|
|
token_serializer_class = settings.SERIALIZERS.token |
|
108
|
|
|
return Response( |
|
109
|
|
|
data=token_serializer_class(token).data, |
|
110
|
|
|
status=status.HTTP_200_OK, |
|
111
|
|
|
) |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
class TokenDestroyView(views.APIView): |
|
115
|
|
|
""" |
|
116
|
|
|
Use this endpoint to logout user (remove user authentication token). |
|
117
|
|
|
""" |
|
118
|
|
|
permission_classes = ( |
|
119
|
|
|
permissions.IsAuthenticated, |
|
120
|
|
|
) |
|
121
|
|
|
|
|
122
|
|
|
def post(self, request): |
|
|
|
|
|
|
123
|
|
|
utils.logout_user(request) |
|
124
|
|
|
return Response(status=status.HTTP_204_NO_CONTENT) |
|
125
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
class PasswordResetView(utils.ActionViewMixin, generics.GenericAPIView): |
|
128
|
|
|
""" |
|
129
|
|
|
Use this endpoint to send email to user with password reset link. |
|
130
|
|
|
""" |
|
131
|
|
|
serializer_class = settings.SERIALIZERS.password_reset |
|
132
|
|
|
permission_classes = ( |
|
133
|
|
|
permissions.AllowAny, |
|
134
|
|
|
) |
|
135
|
|
|
|
|
136
|
|
|
_users = None |
|
137
|
|
|
|
|
138
|
|
|
def _action(self, serializer): |
|
139
|
|
|
for user in self.get_users(serializer.data['email']): |
|
140
|
|
|
self.send_password_reset_email(user) |
|
141
|
|
|
return Response(status=status.HTTP_204_NO_CONTENT) |
|
142
|
|
|
|
|
143
|
|
|
def get_users(self, email): |
|
|
|
|
|
|
144
|
|
|
if self._users is None: |
|
145
|
|
|
email_field_name = get_user_email_field_name(User) |
|
146
|
|
|
users = User._default_manager.filter(**{ |
|
|
|
|
|
|
147
|
|
|
email_field_name + '__iexact': email |
|
148
|
|
|
}) |
|
149
|
|
|
self._users = [ |
|
150
|
|
|
u for u in users if u.is_active and u.has_usable_password() |
|
151
|
|
|
] |
|
152
|
|
|
return self._users |
|
153
|
|
|
|
|
154
|
|
|
def send_password_reset_email(self, user): |
|
|
|
|
|
|
155
|
|
|
context = {'user': user} |
|
156
|
|
|
to = [get_user_email(user)] |
|
|
|
|
|
|
157
|
|
|
email.PasswordResetEmail(self.request, context).send(to) |
|
158
|
|
|
|
|
159
|
|
|
|
|
160
|
|
|
class SetPasswordView(utils.ActionViewMixin, generics.GenericAPIView): |
|
161
|
|
|
""" |
|
162
|
|
|
Use this endpoint to change user password. |
|
163
|
|
|
""" |
|
164
|
|
|
permission_classes = ( |
|
165
|
|
|
permissions.IsAuthenticated, |
|
166
|
|
|
) |
|
167
|
|
|
|
|
168
|
|
|
def get_serializer_class(self): |
|
|
|
|
|
|
169
|
|
|
if settings.SET_PASSWORD_RETYPE: |
|
170
|
|
|
return settings.SERIALIZERS.set_password_retype |
|
171
|
|
|
return settings.SERIALIZERS.set_password |
|
172
|
|
|
|
|
173
|
|
|
def _action(self, serializer): |
|
174
|
|
|
self.request.user.set_password(serializer.data['new_password']) |
|
175
|
|
|
self.request.user.save() |
|
176
|
|
|
|
|
177
|
|
|
if settings.LOGOUT_ON_PASSWORD_CHANGE: |
|
178
|
|
|
utils.logout_user(self.request) |
|
179
|
|
|
|
|
180
|
|
|
return Response(status=status.HTTP_204_NO_CONTENT) |
|
181
|
|
|
|
|
182
|
|
|
|
|
183
|
|
|
class PasswordResetConfirmView(utils.ActionViewMixin, generics.GenericAPIView): |
|
184
|
|
|
""" |
|
185
|
|
|
Use this endpoint to finish reset password process. |
|
186
|
|
View Code Duplication |
""" |
|
|
|
|
|
|
187
|
|
|
permission_classes = ( |
|
188
|
|
|
permissions.AllowAny, |
|
189
|
|
|
) |
|
190
|
|
|
token_generator = default_token_generator |
|
191
|
|
|
|
|
192
|
|
|
def get_serializer_class(self): |
|
|
|
|
|
|
193
|
|
|
if settings.PASSWORD_RESET_CONFIRM_RETYPE: |
|
194
|
|
|
return settings.SERIALIZERS.password_reset_confirm_retype |
|
195
|
|
|
return settings.SERIALIZERS.password_reset_confirm |
|
196
|
|
|
|
|
197
|
|
|
def _action(self, serializer): |
|
|
|
|
|
|
198
|
|
|
serializer.user.set_password(serializer.data['new_password']) |
|
199
|
|
|
serializer.user.save() |
|
200
|
|
|
return Response(status=status.HTTP_204_NO_CONTENT) |
|
201
|
|
|
|
|
202
|
|
|
|
|
203
|
|
|
class ActivationView(utils.ActionViewMixin, generics.GenericAPIView): |
|
204
|
|
|
""" |
|
205
|
|
|
Use this endpoint to activate user account. |
|
206
|
|
|
""" |
|
207
|
|
|
serializer_class = settings.SERIALIZERS.activation |
|
208
|
|
|
permission_classes = ( |
|
209
|
|
|
permissions.AllowAny, |
|
210
|
|
|
) |
|
211
|
|
|
token_generator = default_token_generator |
|
212
|
|
|
|
|
213
|
|
|
def _action(self, serializer): |
|
214
|
|
|
user = serializer.user |
|
215
|
|
|
user.is_active = True |
|
216
|
|
|
user.save() |
|
217
|
|
|
|
|
218
|
|
|
signals.user_activated.send( |
|
219
|
|
|
sender=self.__class__, user=user, request=self.request |
|
220
|
|
|
) |
|
221
|
|
|
|
|
222
|
|
|
if settings.SEND_CONFIRMATION_EMAIL: |
|
223
|
|
|
context = {'user': user} |
|
224
|
|
|
to = [get_user_email(user)] |
|
|
|
|
|
|
225
|
|
|
email.ConfirmationEmail(self.request, context).send(to) |
|
226
|
|
|
|
|
227
|
|
|
return Response(status=status.HTTP_204_NO_CONTENT) |
|
228
|
|
|
|
|
229
|
|
|
|
|
230
|
|
|
class SetUsernameView(utils.ActionViewMixin, generics.GenericAPIView): |
|
231
|
|
|
""" |
|
232
|
|
|
Use this endpoint to change user username. |
|
233
|
|
|
""" |
|
234
|
|
|
permission_classes = (permissions.IsAuthenticated,) |
|
235
|
|
|
|
|
236
|
|
|
def get_serializer_class(self): |
|
|
|
|
|
|
237
|
|
|
if settings.SET_USERNAME_RETYPE: |
|
238
|
|
|
return settings.SERIALIZERS.set_username_retype |
|
239
|
|
|
return settings.SERIALIZERS.set_username |
|
240
|
|
|
|
|
241
|
|
|
def _action(self, serializer): |
|
242
|
|
|
user = self.request.user |
|
243
|
|
|
new_username = serializer.data['new_' + User.USERNAME_FIELD] |
|
244
|
|
|
|
|
245
|
|
|
setattr(user, User.USERNAME_FIELD, new_username) |
|
246
|
|
|
if settings.SEND_ACTIVATION_EMAIL: |
|
247
|
|
|
user.is_active = False |
|
248
|
|
|
context = {'user': user} |
|
249
|
|
|
to = [get_user_email(user)] |
|
|
|
|
|
|
250
|
|
|
email.ActivationEmail(self.request, context).send(to) |
|
251
|
|
|
user.save() |
|
252
|
|
|
|
|
253
|
|
|
return Response(status=status.HTTP_204_NO_CONTENT) |
|
254
|
|
|
|
|
255
|
|
|
|
|
256
|
|
|
class UserView(generics.RetrieveUpdateAPIView): |
|
257
|
|
|
""" |
|
258
|
|
|
Use this endpoint to retrieve/update user. |
|
259
|
|
|
""" |
|
260
|
|
|
model = User |
|
261
|
|
|
serializer_class = settings.SERIALIZERS.user |
|
262
|
|
|
permission_classes = (permissions.IsAuthenticated,) |
|
263
|
|
|
|
|
264
|
|
|
def get_object(self, *args, **kwargs): |
|
|
|
|
|
|
265
|
|
|
return self.request.user |
|
266
|
|
|
|
|
267
|
|
|
def perform_update(self, serializer): |
|
|
|
|
|
|
268
|
|
|
super(UserView, self).perform_update(serializer) |
|
269
|
|
|
user = serializer.instance |
|
270
|
|
|
if settings.SEND_ACTIVATION_EMAIL and not user.is_active: |
|
271
|
|
|
context = {'user': user} |
|
272
|
|
|
to = [get_user_email(user)] |
|
|
|
|
|
|
273
|
|
|
email.ActivationEmail(self.request, context).send(to) |
|
274
|
|
|
|
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.