Repository navigation
feat: read and partially update course grading through authoring v3 #39227
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
taimoor-ahmed-1
wants to merge
2
commits into
openedx:master
Choose a base branch
from
edly-io:feat/standardize-course-grading-api
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.
+2,209
−450
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
83 changes: 83 additions & 0 deletions
83
cms/djangoapps/contentstore/rest_api/v3/grading_service.py
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 |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| """Service layer for the v3 course grading endpoint.""" | ||
|
|
||
| from django.conf import settings | ||
|
|
||
| from cms.djangoapps.contentstore.rest_api.v3.serializers.grading import GRADING_POLICY_FIELDS | ||
| from cms.djangoapps.contentstore.utils import get_course_grading | ||
| from cms.djangoapps.models.settings.course_grading import CourseGradingModel | ||
| from openedx.core.djangoapps.credit.api import is_credit_course | ||
| from openedx.core.djangoapps.credit.tasks import update_credit_course_requirements | ||
| from xmodule.modulestore.django import modulestore | ||
|
|
||
|
|
||
| def get_grading_policy(course_key): | ||
| """Return the stored grading policy of the course ``course_key``.""" | ||
| return CourseGradingModel.fetch(course_key) | ||
|
|
||
|
|
||
| def get_full_grading_settings(course_key): | ||
| """ | ||
| Return the grading policy of ``course_key`` with the context the Studio grading page shows beside it. | ||
|
|
||
| The returned mapping holds the four policy fields plus the graded subsections per | ||
| assignment type, the default letter grade names, and the credit and proctoring context. | ||
| """ | ||
| with modulestore().bulk_operations(course_key): | ||
| show_credit_eligibility = is_credit_course(course_key) and settings.ENABLE_CREDIT_ELIGIBILITY | ||
| context = get_course_grading(course_key) | ||
| policy = context["course_details"] | ||
| return { | ||
| "graders": policy.graders, | ||
| "grade_cutoffs": policy.grade_cutoffs, | ||
| "grace_period": policy.grace_period, | ||
| "minimum_grade_credit": policy.minimum_grade_credit, | ||
| "course_assignment_lists": context["course_assignment_lists"], | ||
| "default_grade_designations": context["default_grade_designations"], | ||
| "is_credit_course": context["is_credit_course"], | ||
| "show_credit_eligibility": show_credit_eligibility, | ||
| "mfe_proctored_exam_settings_url": context["mfe_proctored_exam_settings_url"], | ||
| } | ||
|
|
||
|
|
||
| def update_grading_policy(course_key, changes, user): | ||
| """ | ||
| Apply ``changes`` to the grading policy of ``course_key`` and return the stored policy. | ||
|
|
||
| Only the fields present in ``changes`` are written. A body holding all four fields is | ||
| applied exactly as a whole-policy update. When ``graders`` is absent the stored graders | ||
| are never written back: their weights are fractions that do not survive a round trip | ||
| through the percentages the API exchanges, so rewriting them would alter weights | ||
| nobody changed. Grade cutoffs are written, and a grading policy change announced, | ||
| only when they differ from the stored ones. | ||
|
|
||
| The credit requirements of the course are refreshed whenever ``minimum_grade_credit`` | ||
| is present, whether or not its value changes. | ||
| """ | ||
| if "minimum_grade_credit" in changes: | ||
| update_credit_course_requirements.delay(str(course_key)) | ||
|
|
||
| if all(field in changes for field in GRADING_POLICY_FIELDS): | ||
| return CourseGradingModel.update_from_json(course_key, changes, user) | ||
|
|
||
| if "graders" in changes: | ||
| # The whole-policy update needs every field. The stored cutoffs leave them | ||
| # unchanged, and None leaves the grace period and credit minimum unchanged. | ||
| stored_cutoffs = modulestore().get_course(course_key).grade_cutoffs | ||
| policy = { | ||
| "grade_cutoffs": stored_cutoffs, | ||
| "grace_period": None, | ||
| "minimum_grade_credit": None, | ||
| **changes, | ||
| } | ||
| return CourseGradingModel.update_from_json(course_key, policy, user) | ||
|
|
||
| if "grace_period" in changes: | ||
| CourseGradingModel.update_grace_period_from_json(course_key, changes["grace_period"], user) | ||
| if "minimum_grade_credit" in changes: | ||
| CourseGradingModel.update_minimum_grade_credit_from_json(course_key, changes["minimum_grade_credit"], user) | ||
| # Last, so the grading policy change is announced after every other field is stored, | ||
| # as the whole-policy update does. | ||
| if "grade_cutoffs" in changes: | ||
| if modulestore().get_course(course_key).grade_cutoffs != changes["grade_cutoffs"]: | ||
| CourseGradingModel.update_cutoffs_from_json(course_key, changes["grade_cutoffs"], user) | ||
| return CourseGradingModel.fetch(course_key) | ||
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 |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| """Permissions for the v3 contentstore API.""" | ||
|
|
||
| from openedx_authz.constants.permissions import COURSES_EDIT_GRADING_SETTINGS, COURSES_VIEW_GRADING_SETTINGS | ||
| from rest_framework.permissions import BasePermission | ||
|
|
||
| from openedx.core.djangoapps.authz.constants import LegacyAuthoringPermission | ||
| from openedx.core.djangoapps.authz.decorators import user_has_course_permission | ||
|
|
||
|
|
||
| class _GradingSettingsPermission(BasePermission): | ||
| """ | ||
| Allow callers holding ``authz_permission`` on the course in the path. | ||
|
|
||
| The course comes from the view's ``get_course_key()``, which refuses an | ||
| unparsable or unknown course with a 404 before permission is checked. On | ||
| courses using policy-based authoring permissions that policy decides; | ||
| elsewhere Studio read access to the course is required. | ||
| """ | ||
|
|
||
| authz_permission = None | ||
|
|
||
| def has_permission(self, request, view): | ||
| return user_has_course_permission( | ||
| request.user, | ||
| self.authz_permission, | ||
| view.get_course_key(), | ||
| LegacyAuthoringPermission.READ, | ||
| ) | ||
|
|
||
|
|
||
| class CanViewGradingSettings(_GradingSettingsPermission): | ||
| """Allow callers who may view the grading settings of the course in the path.""" | ||
|
|
||
| authz_permission = COURSES_VIEW_GRADING_SETTINGS.identifier | ||
|
|
||
|
|
||
| class CanEditGradingSettings(_GradingSettingsPermission): | ||
| """ | ||
| Allow callers who may edit the grading settings of the course in the path. | ||
|
|
||
| Where policy-based permissions are off, Studio read access is enough to | ||
| edit, as for every other grading endpoint in Studio. | ||
| """ | ||
|
|
||
| authz_permission = COURSES_EDIT_GRADING_SETTINGS.identifier |
1 change: 1 addition & 0 deletions
1
cms/djangoapps/contentstore/rest_api/v3/serializers/__init__.py
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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """Serializers for the v3 contentstore API.""" |
Oops, something went wrong.
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.
Minor: this queues the credit task even for
"minimum_grade_credit": null, which changes nothing. Master does the same, so fine to leave as is, just noting it.