Skip to content

Commit

Permalink
feat!: upgrading add_users_to_cohorts api to DRF ( 33 ) (#35613)
Browse files Browse the repository at this point in the history
* feat!: upgrading api to DRF.
  • Loading branch information
awais786 authored Feb 26, 2025
1 parent 0e0d1da commit 3ef635b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
48 changes: 28 additions & 20 deletions lms/djangoapps/instructor/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1609,33 +1609,41 @@ def _cohorts_csv_validator(file_storage, file_to_validate):
raise FileValidationException(msg)


@transaction.non_atomic_requests
@ensure_csrf_cookie
@cache_control(no_cache=True, no_store=True, must_revalidate=True)
@require_POST
@require_course_permission(permissions.ASSIGN_TO_COHORTS)
@common_exceptions_400
def add_users_to_cohorts(request, course_id):
@method_decorator(cache_control(no_cache=True, no_store=True, must_revalidate=True), name='dispatch')
@method_decorator(transaction.non_atomic_requests, name='dispatch')
class AddUsersToCohorts(DeveloperErrorViewMixin, APIView):
"""
View method that accepts an uploaded file (using key "uploaded-file")
containing cohort assignments for users. This method spawns a celery task
to do the assignments, and a CSV file with results is provided via data downloads.
"""
course_key = CourseKey.from_string(course_id)

try:
__, filename = store_uploaded_file(
request, 'uploaded-file', ['.csv'],
course_and_time_based_filename_generator(course_key, "cohorts"),
max_file_size=2000000, # limit to 2 MB
validator=_cohorts_csv_validator
)
# The task will assume the default file storage.
task_api.submit_cohort_students(request, course_key, filename)
except (FileValidationException, PermissionDenied) as err:
return JsonResponse({"error": str(err)}, status=400)
permission_classes = (IsAuthenticated, permissions.InstructorPermission)
permission_name = permissions.ASSIGN_TO_COHORTS

@method_decorator(ensure_csrf_cookie)
@method_decorator(transaction.non_atomic_requests)
def post(self, request, course_id):
"""
This method spawns a celery task to do the assignments, and a CSV file with results
is provided via data downloads.
"""

course_key = CourseKey.from_string(course_id)

try:
__, filename = store_uploaded_file(
request, 'uploaded-file', ['.csv'],
course_and_time_based_filename_generator(course_key, "cohorts"),
max_file_size=2000000, # limit to 2 MB
validator=_cohorts_csv_validator
)
# The task will assume the default file storage.
task_api.submit_cohort_students(request, course_key, filename)
except (FileValidationException, PermissionDenied, ValueError) as err:
return JsonResponse({"error": str(err)}, status=400)

return JsonResponse()
return JsonResponse()


# The non-atomic decorator is required because this view calls a celery
Expand Down
2 changes: 1 addition & 1 deletion lms/djangoapps/instructor/views/api_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
path('gradebook/<int:offset>', gradebook_api.spoc_gradebook, name='spoc_gradebook'),

# Cohort management
path('add_users_to_cohorts', api.add_users_to_cohorts, name='add_users_to_cohorts'),
path('add_users_to_cohorts', api.AddUsersToCohorts.as_view(), name='add_users_to_cohorts'),

# Certificates
path('enable_certificate_generation', api.enable_certificate_generation, name='enable_certificate_generation'),
Expand Down

0 comments on commit 3ef635b

Please sign in to comment.