Repository navigation
feat: add bulk update competency criteria endpoint - #4
javoconsultant wants to merge 3 commits into
Conversation
tbain
left a comment
There was a problem hiding this comment.
This PR looks good to me! Codex had one concern however;
- Bug: Codex states: Retired criteria are not rejected. The implementation leaves this as a TODO and updates every selected criterion, but issue openedx#759 requires rejecting the entire batch with 409 when any criterion is retired. The corresponding test is also absent.
api.py:405-407
However I feel like the TODO is valid here since we're waiting for that logic to be introduced with openedx#716, as the comment states. So from my understanding this is a non-issue? Can you comment as to if we can implement this or if we truly need to wait for openedx#716?
Raw Codex Review:
Details
Verdict: Request changes.- Bug. Retired criteria are not rejected. The implementation leaves this as a TODO and updates every selected criterion, but issue openedx#759 requires rejecting the entire batch with
409when any criterion is retired. The corresponding test is also absent.
api.py:405-407
I would remove the previous N+1 query suggestion. It is a potential future optimization, but there is insufficient evidence that it creates a concrete problem for this ticket.
NOTE: This above line is regarding a suggestion in an original code review pass where it was worried that there was an N+1 query complexity issue but after exploring it more it seemed like kind of a non-issue given the use case of this query and further Codex refinement (it got a little overzealous, it seems), see the following details tag if interested:
The PR does include AI attribution and manual testing steps, and introduces no migrations.
…terion (openedx#716) Adds archived = models.BooleanField(default=False) to both models, in one migration, per ADR 0002 Decision 7's archive-only retirement rule. No archived_at companion field: django-simple-history (ADR 0003) already tracks both models, covering the "when" without a separate timestamp. Both models' existing `.. no_pii:` class-level docstring annotation already covers the new field, matching CompetencyRuleProfile.archived's existing non-PII precedent; no separate registry change needed. This ticket makes no behavior changes: nothing here sets, reads, or filters on the field. That is downstream tickets' scope (openedx#674, openedx#675, openedx#681). Co-Authored-By: Claude Sonnet 5 <[email protected]>
Pins the same contract CompetencyRuleProfile.archived already has a test for (test_rule_profile.py's test_creating_a_rule_profile_persists_its_columns): a freshly created row is not archived unless told otherwise. Co-Authored-By: Claude Sonnet 5 <[email protected]>
@tbain I just checked, and openedx#716 is now ready for review. I’ll close this PR and create a new one targeting openedx#716, where I’ll apply the necessary fixes. NEW PR: ufedaseyeuconsultant#2 |
31341be to
3f44e66
Compare
3f44e66 to
db1b8f7
Compare
Description
Implements openedx#759: one request that changes the rule for a batch of existing
CompetencyCriterionrows in a leaf group. An author who wants to correct a shared threshold(say, several assignments all evaluated at "75% or higher") no longer has to edit each criterion
one at a time.
The body names the criteria, plus exactly one rule source: a rule profile to follow, or a
complete pair of rule values.
{"criterion_ids": [12, 13], "rule_type_override": "Grade", "rule_payload_override": {"op": "gte", "value": 0.75, "scale": "percent"}}{"criterion_ids": [12, 13], "rule_profile_id": 1}The response is
200with oneCompetencyCriterionSerializerrow per named criterion, inrequest order, including rows the update didn't change. All three rule columns are returned as
stored, so the response alone shows whether each criterion now follows a profile or has its own
values. Either the whole batch is applied or none of it is.
This branch is based on
alezconsultant/665-criterion-endpoint(jesperhodge#14).That branch has the create endpoint,
CompetencyCriterionSerializer, and the CBE routes this PRextends. This PR adds one commit,
31341be.Which roles this affects: Platform Administrator and Course Author, through Studio once the
frontend work (openedx#709, openedx#710) calls it. Developer: a new public API function and endpoint.
Changes
src/openedx_learning/applets/cbe/api.py: addsbulk_update_competency_criteria()(public,added to
__all__) and three private helpers:_validate_rule_source()validates the batch's rule source once, not once per criterion._apply_rule_source()writes one criterion's three rule columns in a single save, andskips the save when nothing would change.
_resolve_applicable_rule_profile()is a stand-in for [OOS - BE] Reassign existing Competency Criteria when a taxonomy-level rule profile is created openedx/openedx-core#679's shared resolution helper(see "Deviations from [BE] Update/Edit Competency Criteria openedx/openedx-core#759").
src/openedx_learning/applets/cbe/rest_api/v1/serializers.py: addsCompetencyCriterionBulkUpdateSerializer. It checks field types only and rejects keys itdoesn't recognize.
src/openedx_learning/applets/cbe/rest_api/v1/views.py: addsCompetencyCriterionBulkUpdateView, which accepts only PATCH. It parses the body, calls theAPI function, and converts Django's
ValidationErrorto DRF's.src/openedx_learning/applets/cbe/rest_api/v1/urls.py: registers the route ascriterion-bulk-update.tests/openedx_learning/applets/cbe/test_api.pyandtest_views.py: tests covering thescenarios below.
This PR adds no migrations, models, or fields.
How it behaves
A criterion follows a profile or has its own values, never both. Naming a profile clears
a criterion's override values, and supplying values clears its profile link (ADR-0002
Decision 4). All three columns are written in one
save(update_fields=[...]), so no historyrow records both, or neither.
Values that match the applicable profile return the criterion to that profile. If the
submitted values exactly match the profile that currently applies to a criterion, the
criterion is stored as following that profile, not as a redundant override. The comparison
uses parsed dicts, so key order doesn't matter. It is decided per criterion: once scoped
profiles exist, two criteria in the same group can resolve to different profiles.
The payload rules aren't duplicated here. The submitted rule is validated once for the
batch by calling
full_clean()on an unsavedCompetencyCriterion. The payload rules(allowed
opvalues, a threshold in[0.0, 1.0], onlyGradeimplemented) stay in the modellayer.
History records only real changes. A criterion that already has the requested rule is
skipped and gets no new history row. A changed criterion gets exactly one row, with:
_history_userset from the caller, so attribution doesn't depend onHistoryRequestMiddleware._change_reasonof"Rule values set"or"Reassigned to rule profile".QuerySet.update()is never used, because it skipspost_saveand would write no history.The permission check lives in the API function, not on the view. It checks
oel_tagging.can_tag_objectagainst the group's taxonomy and the leaf's course. Leaf groupshave no
course_id, so the course comes from the leaf's course-level parent. An in-processStudio caller is refused in exactly the same cases as an HTTP caller.
Learner status is never read or written. Warning an author when learners already have a
recorded status belongs to [EPIC-CC] Competency Guardrails openedx/openedx-core#723, which will sit in front of this endpoint.
criterion_ids; both, neither, or only half of the rule forms; an unknown or archived profile; a payload the model rejects; an unrecognized body key; a group that isn't a leafoel_tagging.can_tag_objectfor the group's taxonomy and courseDeviations from openedx#759
named criterion is retired.
CompetencyCriterionhas noarchivedfield on this base yet;[BE] Add archived field to Competency Criteria & Group models openedx/openedx-core#716 adds it. A
TODO(#716)marks where that check goes in the check order. The ticket's 409scenario and its tests aren't in this PR.
_resolve_applicable_rule_profile()returns the systemdefault, or
Noneonce the default is archived. In this phase the system default is the onlyprofile that exists. It is already called once per criterion, so replacing it with [OOS - BE] Reassign existing Competency Criteria when a taxonomy-level rule profile is created openedx/openedx-core#679's
helper won't change the code that calls it.
rule_profile_id, not the ticket'scompetency_rule_profile_id. Onthis branch,
CompetencyCriterionSerializerusesrule_profile_idin both requests andresponses, and this endpoint matches it. The Python API keeps the ticket's
competency_rule_profile_idkeyword.in the view, before the API function checks permission. So a caller without permission who
sends a malformed body gets 400, not 403. Inside the API function, only the group lookup and
the leaf check run before the permission check, because the check needs the leaf's course.
Supporting information
archived, then the 409 check), [OOS - BE] Reassign existing Competency Criteria when a taxonomy-level rule profile is created openedx/openedx-core#679 (the sharedprofile-resolution helper), and [EPIC-CC] Competency Guardrails openedx/openedx-core#723 (Studio's learner-status warning and confirmation).
associations, and the rule that a criterion has either a profile or its own values.
appended to.
Testing instructions
Automated:
Manual steps. Use a Studio instance with
api/cbe/mounted (the openedx-platform companionchange from openedx#14). Log in as a user with Studio write access to the course and view access to
the competency taxonomy.
POST /api/cbe/v1/competencies/<tag_id>/criteria/. Pass the first response'sgroup_idinthe second request so both criteria land in the same leaf. Note both criterion ids and the
group_id.PATCH /api/cbe/v1/criteria-groups/<group_id>/criteria/bulk-update/with{"criterion_ids": [<a>, <b>], "rule_type_override": "Grade", "rule_payload_override": {"op": "gte", "value": 0.75, "scale": "percent"}}.Expect
200. Both rows should haverule_profile_id: nulland the submitted values.{"scale": "percent", "value": 0.8, "op": "gte"}(the key order is deliberately different).Expect both rows to follow the default again:
rule_profile_idset and both overridefields
null.200, and no new history rows. To check the history, runthis in a Django shell:
Reassigned to rule profile(step 3) andRule values set(step 2), both attributed to your user.
rule_profile_idtogether with the override pair:400onrule_profile_id."value": 75instead of a fraction:400.999999, added tocriterion_ids:404.400ongroup_id.403.Verification
Run locally on
31341be:pytest tests --no-cov: 1014 passed, 1 skipped. The CBE suite alone: 216 passed, 57 of themnew bulk-update cases (each parametrized case counted separately).
pylint,mypy,pycodestyle,isort --check-only: clean on every changed file.pydocstyle src(run the same way tox runs it) is also clean.lint-imports: 2 contracts kept, 0 broken.makemigrations --check --dry-run openedx_learning: no changes detected.🤖 Generated with Claude Code