Repository navigation
feat: clean up content tags on deletion and diff course tag imports - #39417
ufedaseyeuconsultant wants to merge 3 commits into
Conversation
Deleting a section or subsection in Studio removed only the deleted block's own ObjectTags. The XBLOCK_DELETED event that triggers tag cleanup fires after the subtree has been removed, so its descendants can no longer be read and their tags were left behind. A new receiver on the modulestore pre_item_delete signal walks the subtree before the delete and removes each descendant's tags through the existing _delete_tags helper. The deleted block's own tags are still removed by delete_tag_xblock. A failure during the walk is logged as a warning and never blocks the deletion. Part of openedx/openedx-core#895. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The delete_course management command path sends only the legacy SignalHandler.course_deleted signal and never sends XBLOCK_DELETED, so the ObjectTags of a deleted course and of its blocks were orphaned. Add a course_deleted receiver that finds the course's tagged object ids in the ObjectTag table, since the course structure is already gone when the signal fires. It matches the course key itself and every block key under the course's "block-v1:" prefix, including the "+type@" delimiter so a sibling run such as "run2" is untouched, and deletes each through the tagging API. A cleanup failure is logged and never blocks the deletion. Refs openedx/openedx-core#895 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Importing a course's tags.csv deleted every ObjectTag of each listed block and then re-created them. A re-import therefore gave every tagged block new ObjectTag rows, which breaks anything keyed to those rows. Once openedx/openedx-core#779 lands, it would also permanently archive every rule with learner progress on every re-import. The import now applies only the differences. Before the listed tags are applied, the rows of every taxonomy the file no longer lists for a block are cleared, including disabled taxonomies and rows whose taxonomy was deleted. Free-text taxonomies are left alone, because the export omits their values and their absence from the file says nothing. Each listed taxonomy's tag_object call then adds new values and removes omitted ones. After the rows are applied, any tagged object of the course that is not in the file loses its tags. An object whose tags other data protects is logged and skipped instead of failing the import. A tags.csv with no data rows, like a missing file, changes nothing. Refs openedx/openedx-core#895 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. |
Description
The openedx-platform slice of openedx/openedx-core#895 (items 5 to 7 of its list): deleting content now also cleans up the content's tag associations (
ObjectTagrows), and a course tag re-import applies only the differences. The openedx-core slices (the criteria-group cleanup listeners that react to these deletions) come in separate PRs on that repo.Three changes, one commit each, all in
openedx/core/djangoapps/content_tagging/:handlers.py). The existingXBLOCK_DELETEDhandler removes only the deleted block's own tags, because that event fires after the subtree is unreadable, so deleting a section or subsection in Studio stranded the tags of everything inside it. A new receiver on the modulestore'spre_item_deletesignal walks the block's children while they are still readable and deletes each descendant's tags. The signal fires once per branch; the second firing re-walks whatever the other branch still holds, which is harmless because deleting tags is idempotent.handlers.py).delete_course(the management command andcms.djangoapps.contentstore.utils.delete_course) never sendsXBLOCK_DELETED; it sends only the modulestore'scourse_deletedsignal, after the course structure is gone. Nothing deleted the course'sObjectTagrows, so they were orphaned forever. A new receiver oncourse_deletedfinds the course's tagged object ids in theObjectTagtable (the course key itself, plus ids starting with the course's block prefix, where the+type@delimiter keepscourse-v1:A+B+Cfrom matching the blocks ofcourse-v1:A+B+C2) and deletes them throughdelete_object_tags(). For a key format other thancourse-v1:only the exact course-key tags are deleted.api.py).set_exported_object_tags()clears only the taxonomies a block's tags.csv row no longer lists, then letstag_object()apply each listed taxonomy's differences, so an unchanged re-import keeps everyObjectTagrow and its id, and anything keyed to those rows stays valid. The existing rows are read from theObjectTagmodel directly, becauseget_object_tags()hides disabled taxonomies, and the clearing runs before the apply loop so stale rows do not count toward the 100-tags-per-object limit. Free-text taxonomies are never cleared, because the export cannot represent their values, so their absence from the file says nothing. Rows whose taxonomy was deleted are removed keyed on the missing taxonomy, because their cached export id can collide with a live taxonomy's. After the rows,import_course_tags_from_csv()sweeps the tags of course blocks absent from the file: the import has already replaced the course tree, so a block the file does not list is no longer part of the course (the block itself becomes orphaned modulestore data rather than being deleted). An export with no tags.csv, or one whose tags.csv has no data rows, changes nothing: a real export always writes at least the course row, so a data-less file is treated as corrupt input rather than as "the course has no tags".Both receivers log a warning (with the traceback) when cleanup fails and never block the deletion that triggered them; the two
# pylint: disable=broad-exceptlines carry that intent and follow the pattern ofcourse_date_signals/handlers.pyandnotifications/tasks.py.Deleting an
ObjectTagcan raiseProtectedErrorwhen another app protects the row (in openedx-core 1.26.0, a CBE learner-status row does exactly that, and it is a normal outcome until openedx/openedx-core#779 turns such deletes into archiving). Each object's cleanup therefore runs in its own savepoint and aProtectedErroron one object is logged and skipped, so one protected row neither strands the cleanup of the remaining objects nor fails a course import. Every deletion goes throughdelete_object_tags()/tag_object()/ theObjectTagmanager, so when #779 lands these paths inherit the archive behavior without change.Who is affected
delete_coursenow removes the course's tags; the log carries one info line per cleaned object plus a per-course summary line, and one warning per failed cleanup.Supporting information
claude-fable-5) planned the change and reviewed it; Claude Sonnet 5.5 sub-agents wrote the code and tests under that plan. I reviewed the result.Testing instructions
Automated:
pytest openedx/core/djangoapps/content_tagging/tests/test_handlers.py openedx/core/djangoapps/content_tagging/tests/test_api.py --ds=cms.envs.test(58 pass at this branch's head; the--dsflag matters, under LMS settings the handler tests skip as CMS-only).New tests: in
test_handlers.py, deleting a section removes the tags of its subsections, units, and components, including children ofsplit_testandlibrary_contentblocks, while tags outside the section survive; deleting a unit removes its components' tags; a firing for an already deleted block logs a debug line and returns; deleting a course removes its course-key and block tags while a sibling course (...+C2) keeps everything; a non-course-v1:course key removes only the exact-key tags; a cleanup failure is logged as a warning; aProtectedErroron one object is skipped and the remaining objects are still cleaned. Intest_api.py, re-importing a course's own export keeps everyObjectTagrow with the same primary keys, including free-text rows; dropping a closed value keeps the free-text rows; a dropped tag value, a dropped taxonomy (including a disabled one), and a taxonomy-less row are removed from a listed block; a block absent from the file loses its tags, and so does a tagged course key the file does not list; a missing tags.csv and a tags.csv without data rows change no rows; aProtectedErrorwhile clearing or sweeping is skipped and the import completes; moving a large tag set between taxonomies stays within the per-object tag limit; the non-course-v1:sweep covers only the exact key.Manual, against a devstack with a tagged course:
./manage.py cms delete_course <course-id>: the course's tags are gone; the log names the count.Deadline
None.
Other information
SignalHandler.pre_item_deleteandSignalHandler.course_deletedsignals because no openedx-events equivalents exist:XBLOCK_DELETEDfires after the subtree is unreadable, and there is noCOURSE_DELETEDevent.XBLOCK_DELETEDalready does for the deleted block itself; the alternative, keeping tags until the published copy also goes, would leave the tags of permanently deleted drafts behind.get_all_object_tags()builds the same block-id prefix without the+type@delimiter, so it can match a sibling run (...+C2); that is untouched here and is a candidate follow-up fix.tag_objectraises,run_importslogs it and reports the import as successful anyway, which is pre-existingrun_importsbehavior). Both cases need more than 100 tags on one block.🤖 Generated with Claude Code