Repository navigation
feat: copy content tags and competency criteria on course rerun #39372
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
Open
alezconsultant
wants to merge
1
commit into
openedx:master
Choose a base branch
from
alezconsultant:alezconsultant/694-rerun-copy-criteria
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,26 +5,36 @@ | |
|
|
||
| import csv | ||
| import io | ||
| import logging | ||
| from itertools import groupby | ||
| from typing import Iterator # noqa: UP035 | ||
|
|
||
| import openedx_tagging.api as oel_tagging | ||
| from django.conf import settings | ||
| from django.core.exceptions import ValidationError | ||
| from django.db import transaction | ||
| from django.db.models import Exists, OuterRef, Q, QuerySet | ||
| from django.utils.timezone import now | ||
| from opaque_keys.edx.keys import CollectionKey, ContainerKey, CourseKey, UsageKey | ||
| from opaque_keys.edx.locator import LibraryLocatorV2 | ||
| from openedx_catalog import api as catalog_api | ||
| from openedx_catalog.models_api import CourseRun | ||
| from openedx_events.content_authoring.data import ContentObjectChangedData, ContentObjectData | ||
| from openedx_events.content_authoring.signals import CONTENT_OBJECT_ASSOCIATIONS_CHANGED, CONTENT_OBJECT_TAGS_CHANGED | ||
| from openedx_learning.api import copy_competency_criteria_for_course_rerun | ||
| from openedx_tagging.models import ObjectTag, Taxonomy | ||
| from openedx_tagging.models.utils import TAGS_CSV_SEPARATOR | ||
| from organizations.models import Organization | ||
|
|
||
| from xmodule.modulestore.django import modulestore | ||
|
|
||
| from .helpers.objecttag_export_helpers import build_object_tree_with_objecttags, iterate_with_level | ||
| from .models import TaxonomyOrg | ||
| from .types import ContentKey, TagValuesByObjectIdDict, TagValuesByTaxonomyIdDict, TaxonomyDict | ||
| from .utils import check_taxonomy_context_key_org, get_content_key_from_string, get_context_key_from_key | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class InvalidOrgException(ValidationError): | ||
| """ | ||
|
|
@@ -394,6 +404,86 @@ def copy_object_tags( | |
| pass | ||
|
|
||
|
|
||
| def copy_course_tags_and_criteria(source_course_key: CourseKey, destination_course_key: CourseKey) -> None: | ||
| """ | ||
| Copy the source course run's tags, as editable tags, and its competency criteria onto the destination run. | ||
|
|
||
| Taxonomies not enabled for the destination org are skipped, along with their criteria. | ||
| It also creates the destination run's catalog CourseRun when the source run has one and the destination does not. | ||
| """ | ||
| with transaction.atomic(): | ||
| all_object_tags, taxonomies = get_all_object_tags(source_course_key, prefetch_orgs=True) | ||
| skipped_taxonomy_ids = _get_skipped_taxonomy_ids(taxonomies, source_course_key, destination_course_key) | ||
|
|
||
| for object_id, tags_by_taxonomy_id in all_object_tags.items(): | ||
| destination_object_id = _map_object_id_into_course(object_id, source_course_key, destination_course_key) | ||
| if destination_object_id is None: | ||
| continue | ||
| for taxonomy_id, tag_values in tags_by_taxonomy_id.items(): | ||
| if taxonomy_id not in skipped_taxonomy_ids: | ||
| tag_object(destination_object_id, taxonomies[taxonomy_id], tag_values) | ||
|
|
||
| _create_destination_course_run_if_missing(source_course_key, destination_course_key) | ||
| copy_competency_criteria_for_course_rerun( | ||
| source_course_key, destination_course_key, skipped_taxonomy_ids=skipped_taxonomy_ids, | ||
| ) | ||
|
|
||
|
|
||
| def _get_skipped_taxonomy_ids( | ||
| taxonomies: TaxonomyDict, source_course_key: CourseKey, destination_course_key: CourseKey, | ||
| ) -> set[int]: | ||
| """ | ||
| Return the ids of the taxonomies not enabled for the destination run's org, logging a warning for each. | ||
| """ | ||
| skipped_taxonomy_ids = set() | ||
| for taxonomy_id, taxonomy in taxonomies.items(): | ||
| if not check_taxonomy_context_key_org(taxonomy, destination_course_key): | ||
| skipped_taxonomy_ids.add(taxonomy_id) | ||
| log.warning( | ||
| 'Not copying tags or criteria of taxonomy "%s" from %s to %s: ' | ||
| 'it is not enabled for the destination organization.', | ||
| taxonomy.name, source_course_key, destination_course_key, | ||
| ) | ||
| return skipped_taxonomy_ids | ||
|
|
||
|
|
||
| def _map_object_id_into_course( | ||
|
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. Maybe |
||
| object_id: str, source_course_key: CourseKey, destination_course_key: CourseKey, | ||
| ) -> str | None: | ||
| """ | ||
| Return the destination run's id for the source run's course or block, or None for another course's block. | ||
| """ | ||
| if object_id == str(source_course_key): | ||
| return str(destination_course_key) | ||
| usage_key = UsageKey.from_string(object_id) | ||
| # get_all_object_tags() matches by prefix, so it can return blocks of a course whose key extends ours. | ||
| if usage_key.course_key != source_course_key: | ||
| return None | ||
| return str(usage_key.map_into_course(destination_course_key)) | ||
|
|
||
|
|
||
| def _create_destination_course_run_if_missing(source_course_key: CourseKey, destination_course_key: CourseKey) -> None: | ||
| """ | ||
| Create the destination run's CourseRun from the cloned course when the source run has one and it does not. | ||
| """ | ||
| try: | ||
| catalog_api.get_course_run(source_course_key) | ||
| except CourseRun.DoesNotExist: | ||
| return | ||
| try: | ||
| catalog_api.get_course_run(destination_course_key) | ||
| except CourseRun.DoesNotExist: | ||
| # The course publish handler creates this row when it has run. | ||
| course = modulestore().get_course(destination_course_key) | ||
| language_codes = {code for code, _name in settings.ALL_LANGUAGES} | ||
| catalog_api.create_course_run_for_modulestore_course_with( | ||
| course_key=destination_course_key, | ||
| # The catalog rejects a title longer than its column and a language outside ALL_LANGUAGES. | ||
| title=course.display_name[:CourseRun._meta.get_field("title").max_length], | ||
| language_short=course.language if course.language in language_codes else None, | ||
| ) | ||
|
|
||
|
|
||
| def tag_object( | ||
| object_id: str, | ||
| taxonomy: Taxonomy, | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why would we want to create a catalog CourseRun here?
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.
It sounds like a quite serious side effect in a function that's only there to add tags during the copy process and not responsible to create or publish courses.