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
base: master
Are you sure you want to change the base?
Changes from all commits
af83431
4eaab2e
07c78fe
8ef03ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| """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 the caller needs ``legacy_permission`` on the course in Studio. | ||
| """ | ||
|
|
||
| authz_permission = None | ||
| legacy_permission = None | ||
|
|
||
| def has_permission(self, request, view): | ||
| return user_has_course_permission( | ||
| request.user, | ||
| self.authz_permission, | ||
| view.get_course_key(), | ||
| self.legacy_permission, | ||
| ) | ||
|
|
||
|
|
||
| class CanViewGradingSettings(_GradingSettingsPermission): | ||
| """Allow callers who may view the grading settings of the course in the path.""" | ||
|
|
||
| authz_permission = COURSES_VIEW_GRADING_SETTINGS.identifier | ||
| legacy_permission = LegacyAuthoringPermission.READ | ||
|
|
||
|
|
||
| class CanEditGradingSettings(_GradingSettingsPermission): | ||
|
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. nit: Use
Contributor
Author
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. Done in 8ef03ef: each permission class names its fallback, |
||
| """Allow callers who may edit the grading settings of the course in the path.""" | ||
|
|
||
| authz_permission = COURSES_EDIT_GRADING_SETTINGS.identifier | ||
| legacy_permission = LegacyAuthoringPermission.WRITE | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """Serializers for the v3 contentstore API.""" |
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.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.
Left as on master, noted in the description's follow-ups. The round-trip test asserts the task is queued, since a read always carries the key.