Skip to content

feat: add bulk update competency criteria endpoint - #4

Closed
javoconsultant wants to merge 3 commits into
alezconsultant:alezconsultant/665-criterion-endpointfrom
javoconsultant:javoconsultant/759-bulk-update-criteria
Closed

javoconsultant wants to merge 3 commits into
alezconsultant:alezconsultant/665-criterion-endpointfrom
javoconsultant:javoconsultant/759-bulk-update-criteria

Conversation

@javoconsultant

@javoconsultant javoconsultant commented Sep 28, 2026 •

Copy link
Copy Markdown

Description

Implements openedx#759: one request that changes the rule for a batch of existing
CompetencyCriterion rows 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.

PATCH /api/cbe/v1/criteria-groups/<int:group_id>/criteria/bulk-update/

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 200 with one CompetencyCriterionSerializer row per named criterion, in
request 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 PR
extends. 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: adds bulk_update_competency_criteria() (public,
    added to __all__) and three private helpers:
  • src/openedx_learning/applets/cbe/rest_api/v1/serializers.py: adds
    CompetencyCriterionBulkUpdateSerializer. It checks field types only and rejects keys it
    doesn't recognize.
  • src/openedx_learning/applets/cbe/rest_api/v1/views.py: adds
    CompetencyCriterionBulkUpdateView, which accepts only PATCH. It parses the body, calls the
    API function, and converts Django's ValidationError to DRF's.
  • src/openedx_learning/applets/cbe/rest_api/v1/urls.py: registers the route as
    criterion-bulk-update.
  • tests/openedx_learning/applets/cbe/test_api.py and test_views.py: tests covering the
    scenarios 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 history
    row 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 unsaved CompetencyCriterion. The payload rules
    (allowed op values, a threshold in [0.0, 1.0], only Grade implemented) stay in the model
    layer.

  • 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_user set from the caller, so attribution doesn't depend on
      HistoryRequestMiddleware.
    • a _change_reason of "Rule values set" or "Reassigned to rule profile".

    QuerySet.update() is never used, because it skips post_save and would write no history.

  • The permission check lives in the API function, not on the view. It checks
    oel_tagging.can_tag_object against the group's taxonomy and the leaf's course. Leaf groups
    have no course_id, so the course comes from the leaf's course-level parent. An in-process
    Studio 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.

Status When
200 Success, including when some or all criteria were already up to date
400 Empty or duplicate 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 leaf
401 Unauthenticated caller
403 Caller fails oel_tagging.can_tag_object for the group's taxonomy and course
404 The group doesn't exist, or any named id isn't a criterion in that group
405 Any method other than PATCH

Deviations from openedx#759

  • Retired criteria don't get a 409 yet. The ticket says to refuse the whole batch when any
    named criterion is retired. CompetencyCriterion has no archived field 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 409
    scenario and its tests aren't in this PR.
  • Profile resolution is a stand-in. _resolve_applicable_rule_profile() returns the system
    default, or None once the default is archived. In this phase the system default is the only
    profile 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.
  • The body field is rule_profile_id, not the ticket's competency_rule_profile_id. On
    this branch, CompetencyCriterionSerializer uses rule_profile_id in both requests and
    responses, and this endpoint matches it. The Python API keeps the ticket's
    competency_rule_profile_id keyword.
  • Shape errors come before permission errors. The serializer checks types and unknown keys
    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

Testing instructions

Automated:

pytest tests/openedx_learning/applets/cbe/ --no-cov

Manual steps. Use a Studio instance with api/cbe/ mounted (the openedx-platform companion
change from openedx#14). Log in as a user with Studio write access to the course and view access to
the competency taxonomy.

  1. Create two criteria for the same competency in the same course with
    POST /api/cbe/v1/competencies/<tag_id>/criteria/. Pass the first response's group_id in
    the second request so both criteria land in the same leaf. Note both criterion ids and the
    group_id.
  2. 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 have rule_profile_id: null and the submitted values.
  3. Send the same request with the seeded default's values,
    {"scale": "percent", "value": 0.8, "op": "gte"} (the key order is deliberately different).
    Expect both rows to follow the default again: rule_profile_id set and both override
    fields null.
  4. Send step 3's request again. Expect 200, and no new history rows. To check the history, run
    this in a Django shell:
    from openedx_learning.models import CompetencyCriterion
    for h in CompetencyCriterion.history.filter(id=<a>).order_by("-history_date")[:3]:
        print(h.history_change_reason, h.history_user, h.rule_profile_id, h.rule_payload_override)
    The newest two rows should be Reassigned to rule profile (step 3) and Rule values set
    (step 2), both attributed to your user.
  5. Check that each of these requests is refused and leaves both criteria unchanged:
    • rule_profile_id together with the override pair: 400 on rule_profile_id.
    • "value": 75 instead of a fraction: 400.
    • An id from another group, or 999999, added to criterion_ids: 404.
    • The course-level group's id in the URL: 400 on group_id.
    • A user without write access to the course: 403.

Verification

Run locally on 31341be:

  • pytest tests --no-cov: 1014 passed, 1 skipped. The CBE suite alone: 216 passed, 57 of them
    new 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

@tbain tbain left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR looks good to me! Codex had one concern however;

  1. 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 409 when 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:

Suggestion. Override-based updates perform the same system-default profile query once per criterion, creating an N+1 query pattern for the bulk endpoint. Consider caching/resolving profiles more efficiently while preserving future per-criterion scope semantics. [api.py:267-272](https://github.com/javoconsultant/openedx-core/blob/31341bedbb82288f51f8a0ecfc3b518277282c12/src/openedx_learning/applets/cbe/api.py#L267-L272), [api.py:335](https://github.com/javoconsultant/openedx-core/blob/31341bedbb82288f51f8a0ecfc3b518277282c12/src/openedx_learning/applets/cbe/api.py#L335)

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]>
@javoconsultant

javoconsultant commented Sep 30, 2026 •

Copy link
Copy Markdown
Author

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?

@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

@javoconsultant
javoconsultant force-pushed the javoconsultant/759-bulk-update-criteria branch from 31341be to 3f44e66 Compare September 30, 2026 13:51
@javoconsultant
javoconsultant force-pushed the javoconsultant/759-bulk-update-criteria branch from 3f44e66 to db1b8f7 Compare September 30, 2026 14:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants