|
15 | 15 | from rest_framework.views import APIView |
16 | 16 |
|
17 | 17 | from openedx_authz import api |
| 18 | +from openedx_authz.rest_api.enums import RoleOperationError, RoleOperationStatus |
18 | 19 | from openedx_authz.rest_api.utils import ( |
19 | 20 | filter_users, |
20 | 21 | get_user_by_username_or_email, |
@@ -159,8 +160,8 @@ class RoleUserAPIView(APIView): |
159 | 160 | **Response Format (PUT/DELETE)** |
160 | 161 | Returns HTTP 207 Multi-Status with: |
161 | 162 | { |
162 | | - "completed": [{"user_identifier": "...", "status": "role_added|role_removed"}], |
163 | | - "errors": [{"user_identifier": "...", "error": "error_type"}] |
| 163 | + "completed": [{"user_identifier": "john_doe", "status": "role_added|role_removed"}], |
| 164 | + "errors": [{"user_identifier": "jane_doe", "error": "error_type"}] |
164 | 165 | } |
165 | 166 |
|
166 | 167 | **Authentication and Permissions** |
@@ -230,18 +231,23 @@ def put(self, request: HttpRequest) -> Response: |
230 | 231 |
|
231 | 232 | completed, errors = [], [] |
232 | 233 | for user_identifier in serializer.validated_data["users"]: |
| 234 | + response_dict = {"user_identifier": user_identifier} |
233 | 235 | try: |
234 | 236 | user = get_user_by_username_or_email(user_identifier) |
235 | 237 | result = api.assign_role_to_user_in_scope(user.username, role_name, scope) |
236 | | - if not result: |
237 | | - errors.append({"user_identifier": user_identifier, "error": "user_already_has_role"}) |
238 | | - continue |
239 | | - completed.append({"user_identifier": user_identifier, "status": "role_added"}) |
| 238 | + if result: |
| 239 | + response_dict["status"] = RoleOperationStatus.ROLE_ADDED |
| 240 | + completed.append(response_dict) |
| 241 | + else: |
| 242 | + response_dict["error"] = RoleOperationError.USER_ALREADY_HAS_ROLE |
| 243 | + errors.append(response_dict) |
240 | 244 | except User.DoesNotExist: |
241 | | - errors.append({"user_identifier": user_identifier, "error": "user_not_found"}) |
| 245 | + response_dict["error"] = RoleOperationError.USER_NOT_FOUND |
| 246 | + errors.append(response_dict) |
242 | 247 | except Exception as e: # pylint: disable=broad-exception-caught |
243 | 248 | logger.error(f"Error assigning role to user {user_identifier}: {e}") |
244 | | - errors.append({"user_identifier": user_identifier, "error": "role_assignment_failed"}) |
| 249 | + response_dict["error"] = RoleOperationError.ROLE_ASSIGNMENT_ERROR |
| 250 | + errors.append(response_dict) |
245 | 251 |
|
246 | 252 | response_data = {"completed": completed, "errors": errors} |
247 | 253 | return Response(response_data, status=status.HTTP_207_MULTI_STATUS) |
@@ -271,18 +277,23 @@ def delete(self, request: HttpRequest) -> Response: |
271 | 277 |
|
272 | 278 | completed, errors = [], [] |
273 | 279 | for user_identifier in user_identifiers: |
| 280 | + response_dict = {"user_identifier": user_identifier} |
274 | 281 | try: |
275 | 282 | user = get_user_by_username_or_email(user_identifier) |
276 | 283 | result = api.unassign_role_from_user(user.username, role_name, scope) |
277 | | - if not result: |
278 | | - errors.append({"user_identifier": user_identifier, "error": "user_does_not_have_role"}) |
279 | | - continue |
280 | | - completed.append({"user_identifier": user_identifier, "status": "role_removed"}) |
| 284 | + if result: |
| 285 | + response_dict["status"] = RoleOperationStatus.ROLE_REMOVED |
| 286 | + completed.append(response_dict) |
| 287 | + else: |
| 288 | + response_dict["error"] = RoleOperationError.USER_DOES_NOT_HAVE_ROLE |
| 289 | + errors.append(response_dict) |
281 | 290 | except User.DoesNotExist: |
282 | | - errors.append({"user_identifier": user_identifier, "error": "user_not_found"}) |
| 291 | + response_dict["error"] = RoleOperationError.USER_NOT_FOUND |
| 292 | + errors.append(response_dict) |
283 | 293 | except Exception as e: # pylint: disable=broad-exception-caught |
284 | 294 | logger.error(f"Error removing role from user {user_identifier}: {e}") |
285 | | - errors.append({"user_identifier": user_identifier, "error": "removal_failed"}) |
| 295 | + response_dict["error"] = RoleOperationError.ROLE_REMOVAL_ERROR |
| 296 | + errors.append(response_dict) |
286 | 297 |
|
287 | 298 | response_data = {"completed": completed, "errors": errors} |
288 | 299 | return Response(response_data, status=status.HTTP_207_MULTI_STATUS) |
|
0 commit comments