Repository navigation
feat: report archived course runs in the criteria-groups GET (#840) - #944
Conversation
…#840) Adds archived_course_keys to CompetencyCriteriaTree and to the criteria-groups response: the course run keys whose course-level group for the selected competency is archived, sorted, each once, following the course_keys scope. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Drop the docstring paragraph about the create path, refresh the stale summary and view docstrings, reword the walk comments, and tighten the tests: one run archived under two parents is listed once, an active Rule Group under an archived course-level group stays hidden, and the duplicate 403 view test is folded into the existing one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
) The function mixed run ranking, the group-tree walk, request scoping, and the criteria fetch in one body. Each is now a module-private helper with its own unit tests: _rank_requested_course_runs, _walk_group_tree, _scope_groups_to_requested_runs, and _fetch_criteria_in_group_order. The walk collects every archived course run and the caller limits that list to the requested runs, so behavior and query counts are unchanged. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
) get_competency_criteria_tree() resolved the requested course keys to CourseRun ids with get_course_run_ids(), only to compare those ids with each group's course_id. The groups query already loads each group's course, so the walk now records every kept group's branch by course key and the request is matched by key in Python. A scoped read drops the run-id lookup, from four queries to three. Matching by key is the same comparison the lookup made: CourseRun.course_key is case sensitive and so is CourseKey equality. The walk also collects archived course keys directly, so the caller filters that set by the requested keys instead of mapping run ids back to keys at the end. _rank_requested_course_runs and _archived_course_level_run are gone: the first is a single dict comprehension at its one call site, and the second is one condition in the walk, covered by the walk tests. The names and comments now say what a value means. course_key_of_branch maps each kept group's id to the course key of the branch it sits in, or to None when the group is in no branch. The walk's comments state what each step guarantees, and the rank comment says a rank is only the position of a key in the request, used to order the returned branches. Adds a test that a requested key matching no course run changes nothing, wherever it sits in the request. Co-Authored-By: Jesper Hodge <19345795+jesperhodge@users.noreply.github.com> Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…nedx#840) The docstring described the result in terms of "reachable" groups and the groups "above any course", which a reader had to decode from the code. It now starts with the shape of the tree, with a diagram of the root, course-level groups, Rule Groups, and criteria, and then covers what is left out, how course_keys scopes the result, the order of groups and criteria, the two counts, and when a course run is listed in archived_course_keys. Each claim was checked against the code. The group order is stated as sorted by (parent_id, ordering, id), which keeps the children of one parent together and is neither depth-first nor strictly level by level. The groups list is empty when the root is archived, so it does not always start with the root. The root is not listed as an archived course run because the API creates it with no course. Co-Authored-By: Jesper Hodge <19345795+jesperhodge@users.noreply.github.com> Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
Thanks for the pull request, @ufedaseyeuconsultant! This repository is currently maintained by Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review. 🔘 Get product approvalIf you haven't already, check this list to see if your contribution needs to go through the product review process.
🔘 Provide contextTo help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:
🔘 Get a green buildIf one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green. DetailsWhere can I find more information?If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources: When can I expect my changes to be merged?Our goal is to get community contributions seen and reviewed as efficiently as possible. However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:
💡 As a result it may take up to several weeks or months to complete a review and merge your PR. |
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
mgwozdz-unicon
left a comment
There was a problem hiding this comment.
Claude and I reviewed this at 2f71e8d, and Claude drafted this comment. Every scenario of #840 is implemented, each has a test that fails when its behavior is broken, and we found no defects. The branch merges cleanly into the current main, and comparing course keys in Python matches the same rows as the lookup it replaced. Item 1 needs to change before merge. The rest are smaller requests, mostly about names, docstrings, and tests.
-
Compute
archived_course_keysseparately from the tree walk (api.py)._walk_group_treescores 18 on cognitive complexity (uvx complexipy@8.3.0), and we aim for 12 or less. It is hard to follow because it does two jobs: it decides which groups are returned, and it collects the archived course runs. The second job is what needs theparent_was_keptflag and theelsebranch.- Please compute the list on its own, from the groups the query already loaded: every archived group that has a course and whose parent is the root. The query counts stay at 1, 2, and 3.
- The walk then only skips an archived group with everything below it, and records the course run of each remaining group. Please have it return the list of active groups as well, so that
get_competency_criteria_treeno longer testsgroup.id in course_key_of_branchto decide what is returned, and name the function after what it returns, for example_find_active_groups. - This gives the list the same definition of a course-level group that
_create_leaf_groupuses when it rejects a new criterion (#717), so "can no longer take new criteria" in the docstring holds by construction. Today the list uses a broader definition, the first group with a course on the path from the root, and the two agree only because the API never creates a group between the root and a course-level group. Please update the docstring bullet that starts "Precisely" to match, since it describes the broader rule. - In the tests,
test_archived_course_keys_reports_a_course_level_group_below_a_course_less_groupandtest_archived_course_keys_lists_a_run_once_when_archived_under_two_course_less_groupsgo away, and thetest_walk_group_tree_...tests change with the function. Please droptest_walk_group_tree_prunes_the_subtree_of_an_archived_group_above_any_coursein any case: its tree cannot come from the API, and what it pins is a course run that is neither in the tree nor on the list. The existingtest_get_competency_criteria_tree_returns_nothing_under_an_archived_rootcovers the same rule.
-
Names (
api.py, both test files).course_key_of_branchreads as one course key for one branch. It is a dictionary from a group's id to the course key of the branch that group sits in. Please rename it togroup_id_to_branch_course_key_map.parent_branch_keyandbranch_keyhold course keys:parent_branch_course_keyandbranch_course_key.- "kept" in the new helpers and "reachable" in the
CompetencyCriteriaTreedocstring, the view docstring, and the new testtest_walk_group_tree_maps_each_reachable_group_to_its_branch_coursemean the same thing. Please use one clearer wording everywhere, for example "in the active tree" (is_in_active_tree,active_groups). rank_by_course_keyis a dictionary such as{course_key: 1}, and the reader has to work out that the number is the key's position in the request. Please pass_scope_groups_to_requested_coursesthe plain list of requested keys instead, named something likecourse_keys_in_request_order, and let the helper number them. The line that limits the archived list to the requested keys can then test against the requested keys directly. The helper's own name says "courses" where everything else says "course runs".- "Rule Group" is the frontend's term, and this PR is the first to use it in the backend. Please say "leaf group", as
_create_leaf_groupand ADR 0002 do, in theapi.pydocstrings and comments and in the 25 places in the two test files (test names, docstrings,rule_group_carries_course,rule_group_b,x_rule_group).
-
The docstring of
get_competency_criteria_tree(api.py). Please correct these passages:- "Any other group outside every branch" announces a kind of group the API never creates, and the reader learns that only several lines later. Please describe the tree the API creates and say plainly that the root always comes back while it is active.
- Delete "even rows that are not archived themselves" (archiving a group archives everything below it) and "It does not select a single course run's tree" (a request with one key does return one run's branch).
- "An archived root does not hide the archived course-level groups below it" reads as an odd rule without its reason. Please add it: the root is archived automatically once its last course-level group is, so an archived root is the normal state of a competency whose course runs are all archived.
- The paragraph on archived course runs has a line that ends early after "sorted by
str," and needs rewrapping.
-
Other docstrings and comments (
api.py,views.py).- The
CompetencyCriteriaTreedocstring and the view docstring say "archived course runs". The course run is not archived, its course-level group for this competency is, and Studio already uses "archived" for runs past their end date. Please use the wording of the function's summary line. In the view docstring, "the sorted course run key strings that have an archived course-level group" says that a string has a group. _scope_groups_to_requested_courses: please rewrite the docstring to say what the function does for the trees the API creates (keep the root, keep the requested branches in the order requested, drop the others), and remove the comment block directly under it, which repeats it. The two reasons worth keeping can sit next to their lines: the root gets 0 so that it sorts first, andsorted()is stable.- The comment above the position dictionary in
get_competency_criteria_treesays it "only orders the returned branches". It also filters the archived list.
- The
-
Add a test that pins the sort of
archived_course_keys(test_api.py). The keys come out of a set. Withsorted(..., key=str)removed, the tests passed in 4 of 6 runs with different hash seeds, and a sort by run code also passed, because every test uses the run "2026". Please add one test with six or more archived course runs, with different course codes, created in an order that is not alphabetical, and assert the exact list. -
Tests that build a tree the API cannot produce (
test_api.py). Please either build a tree the API can leave behind, or say in the docstring that it cannot.test_archived_course_keys_lists_a_run_whose_rows_are_left_out_of_the_tree:x_active_rule_groupis an active group under an archived course-level group. The test does not need it, and the docstring says "a run with only archived rows". Please archive it or remove it with its assert. The last assert in this test cannot fail, because the line before it already requires the criteria to be exactlyy_criterion.test_archived_course_keys_ignores_archived_rule_groups_and_criteria_under_an_active_course_level_group: please say in the docstring what the tree is and why the test runs twice. The second run puts the course on the archived leaf group, which only a row written outside the API can have, so that the test fails if the code reports every archived group with a course. The archived leaf group has no variable name;archived_leaf_group,active_leaf_group, andleaf_group_carries_coursewould show the tree.test_scope_groups_to_requested_courses_keeps_groups_above_any_course_firstuses a group with no course between the root and the course-level group.test_walk_group_tree_places_a_group_re_parented_under_a_later_id_parent_by_its_real_parent: please describe the tree after the move in the docstring, and explain two things that were confusing. The groups have to be moved withupdate()because a child with a smaller id than its parent can exist only if it was created first and moved afterward. The assert on the query order checks the setup, that A is listed before its parent B, and the three final asserts show that every group still got the right course key.- In the rewritten walk tests, please keep an active leaf group next to an archived one under an active course-level group (the cascade archives a course-level group whose last active child is archived), name a group after where it sits (
nested_under_leaf_group, notnested), assert both returned values, and leave out asserts that an earlier assert already implies.
-
Smaller test items (
test_api.py,test_views.py)._create_course_level_groupis defined word for word in both files. Please move it totests/openedx_learning/applets/cbe/utils.py.test_archived_course_keys_follows_the_course_keys_scope: please add a comment on the lines that turn the short names in thescopeparameter into course keys.- The three
test_scope_groups_to_requested_courses_...tests then pass that list ([course_key(course_run)],[]) in place of a bare{course_key(course_run): 1}. Please say in the third test's docstring what was requested and in what order. test_tree_response_lists_an_archived_course_run_the_caller_cannot_author_in: the docstring says "Reading the tree needs no tagging permission", and the view requiresoel_tagging.view_taxonomy. What the test shows is that no authoring access to the course run is needed.
-
PR description. Three statements need correcting. ADR 0002 does not describe a shape "where every group in a course subtree has the course": Decision 2 says only the course-level group carries it, since the 2026-10-22 changelog entry.
_course_key_ofhas no unit test, so "unit tests for each helper" is not accurate. An archive reaches the root only when the root's last course-level group is archived.
openedx#840) Addresses review items 1 to 4 on the criteria tree read. archived_course_keys is now computed by _find_archived_course_keys from the groups the query already loaded: every archived group that has a course and whose parent is a root. That is the group _create_leaf_group checks when it rejects a new criterion, so the docstring's claim that such a course run can no longer take new criteria holds by construction. The query counts stay at 1, 2, and 3. The tree walk is now _find_active_groups. It skips an archived group with everything below it and returns both the active groups, in query order, and the map from each active group's id to its branch course key, so get_competency_criteria_tree no longer tests membership in that map to decide what to return. The walk scores 11 on cognitive complexity instead of 18, and get_competency_criteria_tree scores 8 instead of 12. _scope_groups_to_requested_courses is now _scope_groups_to_requested_course_runs and takes the plain list of requested keys in request order, numbering them itself, and the scope helper tests pass that list. Variables are renamed to group_id_to_branch_course_key_map, parent_branch_course_key, and branch_course_key. The docstrings and comments use "active tree" in place of "kept" and "reachable", "leaf group" in place of "Rule Group", and "course runs whose rules are archived" in place of "archived course runs". The get_competency_criteria_tree docstring describes the tree the API creates, states that the root always comes back while it is active, gives the reason an archived root is the normal state, and drops the two misleading sentences. The tests follow the code: the two archived-key tests and the walk test that relied on a course-less group above a course-level group are removed, the walk tests are reworked for the new function and assert both returned values, and "Rule Group" naming becomes "leaf group" in both test files. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…enedx#840) Addresses review items 5 to 7 on the criteria tree tests. A new test archives six course runs with different course codes, created out of alphabetical order, and asserts the exact sorted list. It fails when the sort is removed and when the list is sorted by run code, and it passes under every PYTHONHASHSEED value tried. The test for a run whose rows are left out of the tree no longer builds an active leaf group under an archived course-level group, and its final assert, which the criteria assert before it already implied, is gone. The test that ignores archived leaf groups states its tree in the docstring, explains why it runs twice, and names the leaf group variables after their role. The scope test with a course-less group above a course-level group says in its docstring that the API cannot create that tree. The re-parenting test describes the tree after the move, why the groups are moved with update(), and what the query-order assert and the final asserts show. _create_course_level_group moves to the shared cbe test utils as create_course_level_group instead of being defined in both test modules. The scope-keys test comments the lines that turn short names into course keys. The ordering test among the scope helper tests says what was requested and in what order. The view test about an archived course run the caller cannot author in says it shows that no authoring access is needed. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
mgwozdz-unicon
left a comment
There was a problem hiding this comment.
Claude and I re-reviewed this at 76790ff, and Claude drafted this comment. All 8 items from the last review are done, and every scenario of #840 is still covered. The three functions now score 11, 9, and 8 on cognitive complexity (uvx complexipy@8.3.0), and the new sort test fails under every hash seed we tried once the sort is removed. Approving. Two small docstring nits are left:
-
CompetencyCriteriaTreeViewdocstring (views.py). Thecourse_keysparagraph still says "the groups above any course" three times. Theget_competency_criteria_treedocstring now says "the root" for the same thing, so please use "the root" here as well. -
Two
_find_active_groupstests (test_api.py).test_find_active_groups_drops_everything_below_an_archived_course_level_groupandtest_find_active_groups_drops_an_archived_leaf_group_and_its_subtree_but_keeps_its_active_siblingeach build a tree the API cannot leave behind: an active group under an archived one, and a group nested under a leaf group. Please add a docstring sentence to each saying the API cannot create that tree, as the archived-root test next to them already does.
…#840) The view docstring's course_keys paragraph names the root the way get_competency_criteria_tree's docstring does, instead of "the groups above any course". The two _find_active_groups tests whose trees the API cannot create now say so in their docstrings, like the archived-root test next to them. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
@mgwozdz-unicon Done |
Description
Closes #840.
GET /api/cbe/v1/competencies/<tag_id>/criteria-groups/(#681, with #682'scourse_keysscoping) returns a competency's criteria tree. Its response now also hasarchived_course_keys: the course run keys whose course-level group for this competency is archived. Since #717, creating a criterion for such a course run is rejected with a 400, and nothing unarchives a course-level group in the MVP, so the Courses & Content panel on the Competency Management page (#841) can use this list to tell an author, when they expand such a course run, that it no longer accepts new associations, instead of letting them run into the rejection.{ "groups": [...], "criteria": [...], "criteria_count": 3, "total_criteria_count": 3, "archived_course_keys": ["course-v1:OrgX+CS101+2026"] }The key is always present. Its value is a list of course run key strings, sorted, each listed once, and
[]when nothing is archived. The other keys are unchanged.How it works
get_competency_criteria_tree(tag, course_keys)insrc/openedx_learning/applets/cbe/api.pyreads every group of the competency, archived ones included, and computes the two results separately from that one load:_find_archived_course_keysreports a course run when an archived group carries acourseand sits directly under a root. That is the same definition of a course-level group that [BE] Enforce archived in Competency Criteria creation #717's create-rejection checks, so "can no longer take new criteria" holds by construction, and the two rules cannot drift apart. An archived root does not hide the archived course-level groups under it, because the list is computed from every loaded group, not from the walk; an archived root is also the normal state of a competency whose course runs are all archived, since archiving the last course-level group archives the root automatically. An archived leaf group is never reported: the API writes leaf groups without acourse(ADR 0002 Decision 2 puts the course only on the course-level group), and a leaf group that carries one, which only a row written directly through the ORM can have, still fails the parent-is-a-root test._find_active_groupswalks the groups down from the roots, skips an archived group with everything below it, and returns the active groups with the course run branch each one sits in. The tree (groups,criteria, and both counts) is built from that, unchanged in content.course_keysscope as the tree: nocourse_keysreports every run, a list reports only the runs it names, and an empty list reports none.course.course_key, which the groups query already loads), so no lookup resolves keys to course run ids. Queries: 2 withoutcourse_keys, 3 with it (the third is the unscopedtotal_criteria_count), 1 when the competency has no groups.CompetencyCriteriaTreeinapi.pycarries the newarchived_course_keys: list[CourseKey], and the view returns it as strings.The existing
IsAuthenticatedandoel_tagging.view_taxonomychecks run before the tree is read: a caller who cannot view the competency's taxonomy gets a 403 with no list. The list does not depend on the caller's Studio access to the listed courses, because #841 shows its message for an archived course run to every author who can see that course run, including one who cannot edit it.Who is affected
Supporting information
docs/openedx_learning/decisions/0002-competency-criteria-model.rst: Decision 2 forcourse_idon the course-level group, Decision 7 for archiving instead of deleting once learner status exists.claude-opus-5-5) planned the change and ran the final review; Claude Sonnet 5.5 sub-agents wrote the code and tests under that plan and reviewed each task. I reviewed the result. The review-response commits were planned and verified by Claude Fable 5 with Claude Sonnet 5.5 implementing.Testing instructions
Automated:
pytest tests/openedx_learning/applets/cbe -o addopts=""(-o addopts=""skips the coverage flagstox.iniadds).lint-imports,pylint,pycodestyle,isort --check-only,pydocstyle srcandmypy --show-traceback(mypy also checks the tests).The new tests in
test_api.pyandtest_views.pycover every scenario in #840, plus unit tests for the two helpers the tree read is split into:str-sorted list.course_keysreports X and Y,[X, Z]reports X,[]reports nothing at the API, and over a real request[X, Z]reports X.groupsandcriteria.Each new test was checked by breaking the code it covers and confirming it fails for the stated reason.
Manual, against a devstack, with a competency tag
<tag_id>that has criteria for course runs X and Y:/api/cbe/v1/competencies/<tag_id>/criteria-groups/. Expect"archived_course_keys": [].StudentCompetencyCriterionStatusrow from a Django shell), then DELETE/api/cbe/v1/competencies/<tag_id>/criteria-groups/<leaf_group_id>/. Expect 200 with"archived": true."archived_course_keys": ["<X>"]and none of X's groups or criteria.?course_keys=<Y>. Expect"archived_course_keys": []. GET with?course_keys=<X>,<Y>. Expect["<X>"].Deadline
None.
Other information
mainnow, so this PR replaces it with the same commits rebased ontomain.🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
Bug Fixes