-
Notifications
You must be signed in to change notification settings - Fork 2
/
views.py
323 lines (284 loc) · 12.1 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
"""
Name Affirmation HTTP-based API endpoints
"""
from edx_api_doc_tools import path_parameter, query_parameter, schema
from edx_rest_framework_extensions.auth.jwt.authentication import JwtAuthentication
from rest_framework import status as http_status
from rest_framework.authentication import SessionAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from django.contrib.auth import get_user_model
from edx_name_affirmation.api import (
create_verified_name,
create_verified_name_config,
delete_verified_name,
get_verified_name,
get_verified_name_history,
should_use_verified_name_for_certs,
update_verified_name_status
)
from edx_name_affirmation.exceptions import (
VerifiedNameAttemptIdNotGiven,
VerifiedNameDoesNotExist,
VerifiedNameMultipleAttemptIds
)
from edx_name_affirmation.serializers import (
UpdateVerifiedNameSerializer,
VerifiedNameConfigSerializer,
VerifiedNameSerializer
)
from edx_name_affirmation.statuses import VerifiedNameStatus
class AuthenticatedAPIView(APIView):
"""
Authenticate API View.
"""
authentication_classes = (SessionAuthentication, JwtAuthentication)
permission_classes = (IsAuthenticated,)
class VerifiedNameView(AuthenticatedAPIView):
"""
Endpoint for a VerifiedName.
Supports:
HTTP POST: Creates a new VerifiedName.
HTTP GET: Returns an existing VerifiedName (by username or requesting user)
HTTP PATCH: Update the status of a VerifiedName
HTTP DELETE: Delete a VerifiedName
"""
@schema(
parameters=[
query_parameter('username', str, 'The username of which verified name records might be associated'),
],
responses={
200: 'The verified_name record associated with the username provided',
403: 'User lacks required permission. Only an edX staff user can invoke this API',
404: 'The verified_name record associated with the username provided does not exist.'
},
)
def get(self, request):
"""
Get most recent verified name for the request user or for the specified username
For example: /edx_name_affirmation/v1/verified_name?username=jdoe
Example response: {
"username": "jdoe",
"verified_name": "Jonathan Doe",
"profile_name": "Jon Doe",
"verification_attempt_id": 123,
"proctored_exam_attempt_id": None,
"status": "approved",
"use_verified_name_for_certs": False,
}
"""
username = request.GET.get('username')
if username and not request.user.is_staff:
return Response(
status=http_status.HTTP_403_FORBIDDEN,
data={"detail": "Must be a Staff User to Perform this request."}
)
user = get_user_model().objects.get(username=username) if username else request.user
verified_name = get_verified_name(user, is_verified=True)
if verified_name is None:
return Response(
status=404,
data={'detail': 'There is no verified name related to this user.'}
)
serialized_data = VerifiedNameSerializer(verified_name).data
serialized_data['use_verified_name_for_certs'] = should_use_verified_name_for_certs(user)
return Response(serialized_data)
@schema(
body=VerifiedNameSerializer(),
responses={
200: 'The verified_name record associated with the username provided is successfully created',
403: 'User lacks required permission. Only an edX staff user can invoke this API',
400: 'The posted data have conflicts with already stored verified name'
},
)
def post(self, request):
"""
Creates a new VerifiedName.
Expected POST data: {
"username": "jdoe",
"verified_name": "Jonathan Doe"
"profile_name": "Jon Doe"
"verification_attempt_id": (Optional)
"proctored_exam_attempt_id": (Optional)
"platform_verification_attempt_id": (Optional)
"status": (Optional)
}
"""
username = request.data.get('username')
if username != request.user.username and not request.user.is_staff:
return Response(
status=http_status.HTTP_403_FORBIDDEN,
data={"detail": "Must be a Staff User to Perform this request."}
)
serializer = VerifiedNameSerializer(data=request.data)
if serializer.is_valid():
user = get_user_model().objects.get(username=username) if username else request.user
try:
create_verified_name(
user,
request.data.get('verified_name'),
request.data.get('profile_name'),
verification_attempt_id=request.data.get('verification_attempt_id', None),
proctored_exam_attempt_id=request.data.get('proctored_exam_attempt_id', None),
platform_verification_attempt_id=request.data.get('platform_verification_attempt_id', None),
status=request.data.get('status', VerifiedNameStatus.PENDING)
)
response_status = http_status.HTTP_200_OK
data = {}
except VerifiedNameMultipleAttemptIds as exc:
response_status = http_status.HTTP_400_BAD_REQUEST
data = {"detail": str(exc)}
else:
response_status = http_status.HTTP_400_BAD_REQUEST
data = serializer.errors
return Response(status=response_status, data=data)
@schema(
body=UpdateVerifiedNameSerializer(),
responses={
200: 'The verified_name record associated with the username provided is successfully edited',
403: 'User lacks required permission. Only an edX staff user can invoke this API',
400: 'The edit action failed validation rules'
},
)
def patch(self, request):
"""
Update verified name status
Example PATCH data: {
"username": "jdoe",
"verification_attempt_id" OR "proctored_exam_attempt_id": 123,
"status": "approved",
}
"""
if not request.user.is_staff:
return Response(
status=http_status.HTTP_403_FORBIDDEN,
data={'detail': 'Must be a staff user to update verified name status.'}
)
serializer = UpdateVerifiedNameSerializer(data=request.data)
if serializer.is_valid():
username = request.data.get('username')
user = get_user_model().objects.get(username=username)
try:
verified_name = update_verified_name_status(
user,
request.data.get('status'),
request.data.get('verification_attempt_id', None),
request.data.get('proctored_exam_attempt_id', None)
)
response_status = http_status.HTTP_200_OK
data = VerifiedNameSerializer(verified_name).data
except (VerifiedNameAttemptIdNotGiven, VerifiedNameMultipleAttemptIds) as exc:
response_status = http_status.HTTP_400_BAD_REQUEST
data = {'detail': str(exc)}
except VerifiedNameDoesNotExist as exc:
response_status = http_status.HTTP_404_NOT_FOUND
data = {'detail': str(exc)}
else:
response_status = http_status.HTTP_400_BAD_REQUEST
data = serializer.errors
return Response(status=response_status, data=data)
@schema(
parameters=[
path_parameter('verified_name_id', str, 'The database id of the verified_name to be deleted'),
],
responses={
204: 'The verified_name record associated with the id is successfully deleted from data store',
403: 'User lacks required permission. Only an edX staff user can invoke this API',
404: 'The verified_name record associated with the id provided does not exist.'
},
)
def delete(self, request, verified_name_id):
"""
Delete verified name
/edx_name_affirmation/v1/verified_name/{verified_name_id}
"""
if not request.user.is_staff:
return Response(
status=http_status.HTTP_403_FORBIDDEN,
data={'detail': 'Must be a staff user to delete a verified name.'}
)
try:
delete_verified_name(verified_name_id)
response_status = http_status.HTTP_204_NO_CONTENT
data = {}
except VerifiedNameDoesNotExist as exc:
response_status = http_status.HTTP_404_NOT_FOUND
data = {'detail': str(exc)}
return Response(status=response_status, data=data)
class VerifiedNameHistoryView(AuthenticatedAPIView):
"""
Endpoint for VerifiedName history.
/edx_name_affirmation/v1/verified_name/history?username=xxx
Supports:
HTTP GET: Return a list of VerifiedNames for the given user.
"""
@schema(
parameters=[
query_parameter('username', str, 'The username of which verified name records might be associated'),
],
responses={
200: 'The verified_name record associated with the username provided is successfully edited',
403: 'User lacks required permission. Only an edX staff user can invoke this API',
},
)
def get(self, request):
"""
Get a list of verified name objects for the given user, ordered by most recently created.
"""
username = request.GET.get('username')
if username and not request.user.is_staff:
return Response(
status=http_status.HTTP_403_FORBIDDEN,
data={"detail": "Must be a Staff User to Perform this request."}
)
user = get_user_model().objects.get(username=username) if username else request.user
verified_name_qs = get_verified_name_history(user)
serializer = VerifiedNameSerializer(verified_name_qs, many=True)
serialized_data = {
'use_verified_name_for_certs': should_use_verified_name_for_certs(user),
'results': serializer.data,
}
return Response(serialized_data)
class VerifiedNameConfigView(AuthenticatedAPIView):
"""
Endpoint for VerifiedNameConfig.
/edx_name_affirmation/v1/verified_name/config
Supports:
HTTP POST: Creates a new VerifiedNameConfig.
HTTP POST
Creates a new VerifiedName.
Example POST data: {
"username": "jdoe",
"use_verified_name_for_certs": True
}
"""
@schema(
body=VerifiedNameConfigSerializer(),
responses={
201: 'The verified_name configuration record is successfully created',
403: 'User lacks required permission. Only an edX staff user can invoke this API',
400: 'The POSTed data failed validation rules',
},
)
def post(self, request):
"""
Create VerifiedNameConfig
"""
username = request.data.get('username')
if username != request.user.username and not request.user.is_staff:
msg = 'Must be a staff user to override the requested user’s VerifiedNameConfig value'
return Response(status=http_status.HTTP_403_FORBIDDEN, data={'detail': msg})
serializer = VerifiedNameConfigSerializer(data=request.data)
if serializer.is_valid():
user = get_user_model().objects.get(username=username) if username else request.user
create_verified_name_config(
user,
use_verified_name_for_certs=request.data.get('use_verified_name_for_certs'),
)
response_status = http_status.HTTP_201_CREATED
data = {}
else:
response_status = http_status.HTTP_400_BAD_REQUEST
data = serializer.errors
return Response(status=response_status, data=data)