-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Wgu jesse stewart/instructor dashboard certificates bulk #38464
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wgu-jesse-stewart
wants to merge
27
commits into
openedx:master
Choose a base branch
from
WGU-Open-edX:wgu-jesse-stewart/instructor_dashboard_certificates_bulk
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+155
−16
Open
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
1f6fb47
feat: add certificate management v2 API endpoints
wgu-jesse-stewart d2de81c
fix: linting
wgu-jesse-stewart 4a355f5
fix: linting
wgu-jesse-stewart ea687fe
fix: linting
wgu-jesse-stewart 5f7d3df
fix: linting
wgu-jesse-stewart 5d08560
feat: PR feedback
wgu-jesse-stewart a394008
fix: Removed the unused invalidated_user_ids
wgu-jesse-stewart 0d8809e
feat: update tests
wgu-jesse-stewart 33364c0
feat: update tests
wgu-jesse-stewart 2d2c62a
feat: update tests
wgu-jesse-stewart 6b9a2e9
feat: update tests
wgu-jesse-stewart 66e1243
feat: PR feedback
wgu-jesse-stewart 1b43fc5
fix: tests
wgu-jesse-stewart d8ff883
feat: PR feedback
wgu-jesse-stewart 684a245
fix: build
wgu-jesse-stewart 2902be9
fix: tests
wgu-jesse-stewart daf8ce0
feat: wrap create_certificate_invalidation_entry in atomic
wgu-jesse-stewart 397b8f8
Merge branch 'master' into wgu-jesse-stewart/instructor_dashboard_cer…
wgu-jesse-stewart 9e74963
feat: add logging and max_length
wgu-jesse-stewart 0592918
Merge branch 'wgu-jesse-stewart/instructor_dashboard_certificates_v2'…
wgu-jesse-stewart a317b19
feat: show all exceptions granted records
wgu-jesse-stewart 5736077
fix: tests
wgu-jesse-stewart a56b2c3
feat: PR feedback
wgu-jesse-stewart 7acb2a4
feat: adds bulk grant exception
wgu-jesse-stewart df144dd
Merge branch 'master' into wgu-jesse-stewart/instructor_dashboard_cer…
wgu-jesse-stewart 3634c66
fix: linting
wgu-jesse-stewart 472329e
fix: tests
wgu-jesse-stewart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -2117,6 +2117,122 @@ def _validate_certificates_for_invalidation(learner_to_user, course_key): | |||||||
| return certificates_to_invalidate, errors | ||||||||
|
|
||||||||
|
|
||||||||
| class BulkCertificateExceptionsView(DeveloperErrorViewMixin, APIView): | ||||||||
| """ | ||||||||
| View to grant certificate exceptions via CSV upload. | ||||||||
|
|
||||||||
| **Example Requests** | ||||||||
|
|
||||||||
| POST /api/instructor/v2/courses/{course_id}/certificates/exceptions/bulk | ||||||||
|
|
||||||||
| **POST Request Body** | ||||||||
|
|
||||||||
| Form data with CSV file uploaded as 'file' field. | ||||||||
| CSV format: username_or_email,notes (optional second column) | ||||||||
|
|
||||||||
| **Returns** | ||||||||
|
|
||||||||
| * 200: OK - Bulk exceptions processed with success/error details | ||||||||
| * 400: Bad Request - Invalid CSV file or format | ||||||||
| * 401: Unauthorized - User is not authenticated | ||||||||
| * 403: Forbidden - User lacks instructor permissions | ||||||||
| """ | ||||||||
| permission_classes = (IsAuthenticated, permissions.InstructorPermission) | ||||||||
| permission_name = permissions.CERTIFICATE_EXCEPTION_VIEW | ||||||||
|
|
||||||||
| def post(self, request, course_id): | ||||||||
| """Grant certificate exceptions via CSV upload.""" | ||||||||
| course_key = CourseKey.from_string(course_id) | ||||||||
| # Validate that the course exists | ||||||||
| get_course_by_id(course_key) | ||||||||
|
|
||||||||
| # Check if file was uploaded | ||||||||
| if 'file' not in request.FILES: | ||||||||
| return Response( | ||||||||
| {'message': _('No file uploaded')}, | ||||||||
| status=status.HTTP_400_BAD_REQUEST | ||||||||
| ) | ||||||||
|
|
||||||||
| uploaded_file = request.FILES['file'] | ||||||||
|
|
||||||||
| # Validate file type | ||||||||
| if not uploaded_file.name.endswith('.csv'): | ||||||||
| return Response( | ||||||||
| {'message': _('File must be in CSV format')}, | ||||||||
| status=status.HTTP_400_BAD_REQUEST | ||||||||
| ) | ||||||||
|
|
||||||||
| results = { | ||||||||
| 'success': [], | ||||||||
| 'errors': [] | ||||||||
| } | ||||||||
|
|
||||||||
| try: | ||||||||
| # Read and parse CSV file | ||||||||
| file_content = uploaded_file.read().decode('utf-8-sig') | ||||||||
| csv_reader = csv.reader(file_content.splitlines()) | ||||||||
|
|
||||||||
| learners_with_notes = [] | ||||||||
| for _row_num, row in enumerate(csv_reader, start=1): | ||||||||
| if not row or not row[0].strip(): | ||||||||
| continue # Skip empty rows | ||||||||
|
|
||||||||
| learner = row[0].strip() | ||||||||
| notes = row[1].strip() if len(row) > 1 and row[1].strip() else '' | ||||||||
|
|
||||||||
| learners_with_notes.append((learner, notes)) | ||||||||
|
|
||||||||
| if not learners_with_notes: | ||||||||
| return Response( | ||||||||
| {'message': _('CSV file is empty or contains no valid entries')}, | ||||||||
| status=status.HTTP_400_BAD_REQUEST | ||||||||
| ) | ||||||||
|
|
||||||||
| # Extract just the learners for resolution | ||||||||
| learners = [learner for learner, _ in learners_with_notes] | ||||||||
|
|
||||||||
| # Resolve all usernames/emails to users upfront | ||||||||
| learner_to_user, user_errors = _resolve_learners_to_users(learners) | ||||||||
| results['errors'].extend(user_errors) | ||||||||
|
|
||||||||
| # Validate learners for certificate exceptions | ||||||||
| exceptions_to_create, validation_errors = _validate_learners_for_certificate_exceptions( | ||||||||
| learner_to_user, course_key | ||||||||
| ) | ||||||||
| results['errors'].extend(validation_errors) | ||||||||
|
|
||||||||
| # Create all exceptions using the certificates API | ||||||||
| for learner, user in exceptions_to_create: | ||||||||
| # Find the notes for this learner | ||||||||
| notes = next((n for l, n in learners_with_notes if l == learner), '') | ||||||||
|
Comment on lines
+2206
to
+2207
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the dict built above:
Suggested change
|
||||||||
|
|
||||||||
| try: | ||||||||
| certs_api.create_or_update_certificate_allowlist_entry(user, course_key, notes) | ||||||||
| log.info( | ||||||||
| "Certificate exception granted for user %s (%s) in course %s by %s via CSV upload", | ||||||||
| user.id, learner, course_key, request.user.username | ||||||||
| ) | ||||||||
| results['success'].append(learner) | ||||||||
| except Exception as exc: # pylint: disable=broad-except | ||||||||
| log.exception( | ||||||||
| "Error creating certificate exception for user %s in course %s", | ||||||||
| user.id, course_key | ||||||||
| ) | ||||||||
| results['errors'].append({ | ||||||||
| 'learner': learner, | ||||||||
| 'message': str(exc) | ||||||||
| }) | ||||||||
|
|
||||||||
| return Response(results, status=status.HTTP_200_OK) | ||||||||
|
|
||||||||
| except Exception as exc: # pylint: disable=broad-except | ||||||||
| log.exception("Error processing CSV file for certificate exceptions") | ||||||||
| return Response( | ||||||||
| {'message': _('Error processing CSV file: {error}').format(error=str(exc))}, | ||||||||
| status=status.HTTP_400_BAD_REQUEST | ||||||||
| ) | ||||||||
|
|
||||||||
|
|
||||||||
| class CertificateInvalidationsView(DeveloperErrorViewMixin, APIView): | ||||||||
| """ | ||||||||
| View to invalidate or re-validate certificates. | ||||||||
|
|
||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: the
next()scan on line 2207 does a linear search throughlearners_with_notesfor every learner, making the creation loop O(n²). Build a dict here and index into it below.