Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cms/djangoapps/contentstore/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
from common.djangoapps.util.monitoring import monitor_import_failure
from openedx.core.djangoapps.content.learning_sequences.api import key_supports_outlines
from openedx.core.djangoapps.content_libraries import api as v2contentlib_api
from openedx.core.djangoapps.content_tagging.api import make_copied_tags_editable
from openedx.core.djangoapps.content_tagging.api import copy_course_tags_and_criteria, make_copied_tags_editable
from openedx.core.djangoapps.course_apps.toggles import exams_ida_enabled
from openedx.core.djangoapps.discussions.config.waffle import ENABLE_NEW_STRUCTURE_DISCUSSIONS
from openedx.core.djangoapps.discussions.models import DiscussionsConfiguration, Provider
Expand Down Expand Up @@ -186,6 +186,8 @@ def rerun_course(source_course_key_string, destination_course_key_string, user_i
# set initial permissions for the user to access the course.
initialize_permissions(destination_course_key, User.objects.get(id=user_id))

copy_course_tags_and_criteria(source_course_key, destination_course_key)

# update state: Succeeded
CourseRerunState.objects.succeeded(course_key=destination_course_key)

Expand Down
146 changes: 142 additions & 4 deletions cms/djangoapps/contentstore/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,27 @@
from edx_toggles.toggles.testutils import override_waffle_flag
from opaque_keys.edx.keys import CourseKey
from opaque_keys.edx.locator import CourseLocator
from openedx_catalog.api import create_course_run_for_modulestore_course_with
from openedx_catalog.models_api import CourseRun
from openedx_learning.api import associate_competency_criterion, create_competency_taxonomy
from openedx_learning.models_api import CompetencyCriteriaGroup, CompetencyCriterion, RuleType
from openedx_tagging.rules import ObjectTagPermissionItem
from organizations.models import OrganizationCourse
from organizations.tests.factories import OrganizationFactory
from user_tasks.models import UserTaskArtifact, UserTaskStatus

from cms.djangoapps.contentstore.tests.test_libraries import LibraryTestCase
from cms.djangoapps.contentstore.tests.utils import CourseTestCase
from common.djangoapps.course_action_state.managers import CourseRerunUIStateManager
from common.djangoapps.course_action_state.models import CourseRerunState
from common.djangoapps.student.tests.factories import UserFactory
from openedx.core.djangoapps.content_tagging import api as tagging_api
from openedx.core.djangoapps.course_apps.toggles import EXAMS_IDA
from openedx.core.djangoapps.discussions.config.waffle import ENABLE_NEW_STRUCTURE_DISCUSSIONS
from openedx.core.djangoapps.discussions.models import DiscussionsConfiguration, Provider
from openedx.core.djangoapps.embargo.models import Country, CountryAccessRule, RestrictedCourse
from xmodule.modulestore import ModuleStoreEnum
from xmodule.modulestore.django import modulestore # pylint: disable=wrong-import-order
from xmodule.modulestore.django import SignalHandler, modulestore # pylint: disable=wrong-import-order
from xmodule.modulestore.tests.django_utils import TEST_DATA_SPLIT_MODULESTORE, ModuleStoreTestCase
from xmodule.modulestore.tests.factories import ( # pylint: disable=wrong-import-order
BlockFactory,
Expand Down Expand Up @@ -151,9 +158,59 @@ class RerunCourseTaskTestCase(CourseTestCase): # pylint: disable=missing-class-

MODULESTORE = TEST_DATA_SPLIT_MODULESTORE

def _rerun_course(self, old_course_key, new_course_key):
CourseRerunState.objects.initiated(old_course_key, new_course_key, self.user, 'Test Re-run')
rerun_course(str(old_course_key), str(new_course_key), self.user.id)
def _rerun_course(self, old_course_key, new_course_key, user=None):
user = user or self.user
CourseRerunState.objects.initiated(old_course_key, new_course_key, user, 'Test Re-run')
rerun_course(str(old_course_key), str(new_course_key), user.id)

def _tag_course_and_add_criterion(self, org=None):
"""
Tag the course, give a new problem a competency criterion, and return the problem's key.

With `org`, the taxonomies are enabled for that organization only, otherwise for all.
"""
problem = BlockFactory.create(
parent=BlockFactory.create(
parent=BlockFactory.create(parent=self.course, category="sequential"), category="vertical",
),
category="problem",
)
problem_key = self.course.id.make_usage_key("problem", problem.location.block_id)
plain_taxonomy = tagging_api.create_taxonomy(name="Plain")
competency_taxonomy = create_competency_taxonomy(name="Competencies")
for taxonomy in (plain_taxonomy, competency_taxonomy):
if org:
tagging_api.set_taxonomy_orgs(taxonomy, orgs=[org])
else:
tagging_api.set_taxonomy_orgs(taxonomy, all_orgs=True)
tagging_api.add_tag_to_taxonomy(taxonomy=plain_taxonomy, tag="Alpha")
competency = tagging_api.add_tag_to_taxonomy(taxonomy=competency_taxonomy, tag="Writing")
tagging_api.tag_object(object_id=str(self.course.id), taxonomy=plain_taxonomy, tags=["Alpha"])
create_course_run_for_modulestore_course_with(self.course.id, title=self.course.display_name)
associate_competency_criterion(
competency.id,
str(problem_key),
rule_type_override=RuleType.GRADE,
rule_payload_override={"op": "gte", "value": 0.8, "scale": "percent"},
)
return problem_key

def _assert_rerun_state(self, new_course_key, state):
"""Assert the rerun of `new_course_key` is in `state`."""
assert CourseRerunState.objects.find_first(course_key=new_course_key).state == state

def _assert_one_copy(self, new_course_key, problem_key):
"""Assert the new run has one copy of the course's tags and of `problem_key`'s tags and criterion."""
new_problem_key = str(problem_key.map_into_course(new_course_key))
assert [tag.value for tag in tagging_api.get_object_tags(str(new_course_key))] == ["Alpha"]
assert [tag.value for tag in tagging_api.get_object_tags(new_problem_key)] == ["Writing"]
assert CompetencyCriteriaGroup.objects.filter(course__course_key=new_course_key).count() == 1
assert CompetencyCriterion.objects.filter(object_tag__object_id=new_problem_key).count() == 1

def _assert_nothing_copied(self, new_course_key):
"""Assert the new run has no tags and no criteria."""
assert not tagging_api.get_all_object_tags(new_course_key)[0]
assert not CompetencyCriteriaGroup.objects.filter(course__course_key=new_course_key).exists()

def test_success(self):
""" The task should clone the OrganizationCourse and RestrictedCourse data. """
Expand Down Expand Up @@ -210,6 +267,7 @@ def test_success_different_org(self):

organization = OrganizationFactory(short_name=old_course_key.org)
OrganizationCourse.objects.create(course_id=old_course_id, organization=organization)
self._tag_course_and_add_criterion(org=organization)

# Run the task!
self._rerun_course(old_course_key, new_course_key)
Expand All @@ -218,6 +276,86 @@ def test_success_different_org(self):
self.assertEqual(OrganizationCourse.objects.count(), 2) # noqa: PT009
OrganizationCourse.objects.get(course_id=new_course_id, organization__short_name='neworg')

# The source taxonomies are not enabled for the new org, so nothing is copied and the rerun still succeeds.
self._assert_rerun_state(new_course_key, CourseRerunUIStateManager.State.SUCCEEDED)
self._assert_nothing_copied(new_course_key)

def test_rerun_copies_tags_and_criteria(self):
problem_key = self._tag_course_and_add_criterion()
new_course_key = CourseLocator(org=self.course.id.org, course=self.course.id.course, run='rerun')

self._rerun_course(self.course.id, new_course_key)

self._assert_rerun_state(new_course_key, CourseRerunUIStateManager.State.SUCCEEDED)
self._assert_one_copy(new_course_key, problem_key)

def test_rerun_by_user_without_tagging_permission_copies_everything(self):
problem_key = self._tag_course_and_add_criterion()
new_course_key = CourseLocator(org=self.course.id.org, course=self.course.id.course, run='rerun')
author = UserFactory()
competency_taxonomy = tagging_api.get_object_tags(str(problem_key))[0].taxonomy
permission_item = ObjectTagPermissionItem(taxonomy=competency_taxonomy, object_id=str(self.course.id))
assert not author.has_perm("oel_tagging.can_tag_object", permission_item)

self._rerun_course(self.course.id, new_course_key, user=author)

self._assert_rerun_state(new_course_key, CourseRerunUIStateManager.State.SUCCEEDED)
self._assert_one_copy(new_course_key, problem_key)

def _fail_criteria_copy(self):
"""Patch the criteria copy, which runs after the tags are written, to raise."""
return patch(
'openedx.core.djangoapps.content_tagging.api.copy_competency_criteria_for_course_rerun',
side_effect=Exception('criteria copy failed'),
)

def test_rerun_copy_failure_fails_and_deletes_course(self):
self._tag_course_and_add_criterion()
new_course_key = CourseLocator(org=self.course.id.org, course=self.course.id.course, run='rerun')

with self._fail_criteria_copy() as criteria_copy:
self._rerun_course(self.course.id, new_course_key)

criteria_copy.assert_called_once()

self._assert_rerun_state(new_course_key, CourseRerunUIStateManager.State.FAILED)
assert modulestore().get_course(new_course_key) is None
self._assert_nothing_copied(new_course_key)

def test_rerun_retry_after_failure_has_one_copy(self):
problem_key = self._tag_course_and_add_criterion()
new_course_key = CourseLocator(org=self.course.id.org, course=self.course.id.course, run='rerun')
with self._fail_criteria_copy():
self._rerun_course(self.course.id, new_course_key)
self._assert_rerun_state(new_course_key, CourseRerunUIStateManager.State.FAILED)
self._assert_nothing_copied(new_course_key)

self._rerun_course(self.course.id, new_course_key)

self._assert_rerun_state(new_course_key, CourseRerunUIStateManager.State.SUCCEEDED)
self._assert_one_copy(new_course_key, problem_key)

def test_rerun_retry_after_late_failure_has_one_copy(self):
problem_key = self._tag_course_and_add_criterion()
new_course_key = CourseLocator(org=self.course.id.org, course=self.course.id.course, run='rerun')
new_problem_key = str(problem_key.map_into_course(new_course_key))
# Deleting the failed run's course sends course_deleted, whose receiver removes the destination CourseRun.
with SignalHandler.course_deleted.for_state(is_enabled=True), \
patch('edxval.api.copy_course_videos', side_effect=Exception('video copy failed')):
self._rerun_course(self.course.id, new_course_key)
self._assert_rerun_state(new_course_key, CourseRerunUIStateManager.State.FAILED)
# The tags committed before the failure and outlive the CourseRun, so the retry meets them.
assert not CourseRun.objects.filter(course_key=new_course_key).exists()
assert not CompetencyCriteriaGroup.objects.filter(course__course_key=new_course_key).exists()
assert not CompetencyCriterion.objects.filter(object_tag__object_id=new_problem_key).exists()
assert [tag.value for tag in tagging_api.get_object_tags(str(new_course_key))] == ["Alpha"]
assert [tag.value for tag in tagging_api.get_object_tags(new_problem_key)] == ["Writing"]

self._rerun_course(self.course.id, new_course_key)

self._assert_rerun_state(new_course_key, CourseRerunUIStateManager.State.SUCCEEDED)
self._assert_one_copy(new_course_key, problem_key)


@override_settings(CONTENTSTORE=TEST_DATA_CONTENTSTORE)
class RegisterExamsTaskTestCase(CourseTestCase): # pylint: disable=missing-class-docstring
Expand Down
90 changes: 90 additions & 0 deletions openedx/core/djangoapps/content_tagging/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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.

Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
Contributor

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.

"""
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(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe _map_object_id_from_one_course_to_another?

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,
Expand Down
Loading
Loading