Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
from django.http import HttpResponse, HttpResponseBadRequest
from django.utils.translation import gettext as _
from edx_django_utils.plugins import pluggable_override
from openedx.core.djangoapps.content_libraries.api import ContainerMetadata, ContainerType, LibraryXBlockMetadata
from openedx_content import api as content_api
from openedx.core.djangoapps.content_libraries.api import ContainerMetadata, LibraryXBlockMetadata
from openedx.core.djangoapps.content_tagging.api import get_object_tag_counts
from edx_proctoring.api import (
does_backend_support_onboarding,
Expand Down Expand Up @@ -572,18 +573,15 @@ def sync_library_content(
block_type = upstream_child.usage_key.block_type
elif isinstance(upstream_child, ContainerMetadata):
upstream_key = str(upstream_child.container_key)
match upstream_child.container_type:
case ContainerType.Unit:
block_type = "vertical"
case ContainerType.Subsection:
block_type = "sequential"
case _:
# We don't support other container types for now.
log.error(
"Unexpected upstream child container type: %s",
upstream_child.container_type,
)
continue
# convert "unit" -> "vertical", "subsection" -> "sequential"
block_type = content_api.get_container_type(upstream_child.container_type_code).olx_tag_name
if not block_type or block_type not in ("vertical", "sequential"):
# We don't support other container types for now.
log.error(
"Unexpected upstream child container type: %s",
upstream_child.container_type,
)
continue
else:
log.error(
"Unexpected type of upstream child: %s",
Expand Down
9 changes: 4 additions & 5 deletions cms/djangoapps/modulestore_migrator/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
LibraryLocatorV2,
LibraryUsageLocatorV2,
)

from openedx.core.djangoapps.content_libraries.api import ContainerType
from openedx_content.models_api import Unit, Subsection, Section


class CompositionLevel(Enum):
Expand All @@ -32,9 +31,9 @@ class CompositionLevel(Enum):
Component = 'component'

# Container types currently supported by Content Libraries
Unit = ContainerType.Unit.value
Subsection = ContainerType.Subsection.value
Section = ContainerType.Section.value
Unit = Unit.type_code # "unit"
Subsection = Subsection.type_code # "subsection"
Section = Section.type_code # "section"

@property
def is_container(self) -> bool:
Expand Down
22 changes: 9 additions & 13 deletions cms/djangoapps/modulestore_migrator/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
from common.djangoapps.split_modulestore_django.models import SplitModulestoreCourseIndex
from common.djangoapps.util.date_utils import DEFAULT_DATE_TIME_FORMAT, strftime_localized
from openedx.core.djangoapps.content_libraries import api as libraries_api
from openedx.core.djangoapps.content_libraries.api import ContainerType, get_library
from openedx.core.djangoapps.content_libraries.api import get_library
from openedx.core.djangoapps.content_staging import api as staging_api
from xmodule.modulestore import exceptions as modulestore_exceptions
from xmodule.modulestore.django import modulestore
Expand Down Expand Up @@ -766,11 +766,11 @@ def _migrate_node(
# do not support in libraries as of Ulmo.
should_migrate_node: bool
should_migrate_children: bool
container_type: ContainerType | None # if None, it's a Component
container_type: content_api.ContainerType | None # if None, it's a Component
if source_node.tag == "wiki":
return _MigratedNode(None, [])
try:
container_type = ContainerType.from_source_olx_tag(source_node.tag)
container_type = libraries_api.container_type_for_olx_tag(source_node.tag)
except ValueError:
container_type = None
if source_node.tag in {"course", "library"}:
Expand All @@ -780,7 +780,7 @@ def _migrate_node(
should_migrate_node = True
should_migrate_children = False
else:
node_level = CompositionLevel(container_type.value)
node_level = CompositionLevel(container_type.type_code)
should_migrate_node = not node_level.is_higher_than(context.composition_level)
should_migrate_children = True
migrated_children: list[_MigratedNode] = []
Expand Down Expand Up @@ -842,7 +842,7 @@ def _migrate_container(
*,
context: _MigrationContext,
source_key: UsageKey,
container_type: ContainerType,
container_type: content_api.ContainerType,
title: str,
children: list[PublishableEntityVersion],
) -> tuple[PublishableEntityVersion, str | None]:
Expand Down Expand Up @@ -889,13 +889,9 @@ def _migrate_container(
container_publishable_entity_version = content_api.create_next_container_version(
container.container_pk,
title=title,
entity_rows=[
content_api.ContainerEntityRow(entity_pk=child.entity_id, version_pk=None)
for child in children
],
entities=[child.entity for child in children],
created=context.created_at,
created_by=context.created_by,
container_version_cls=container_type.container_model_classes[1],
).publishable_entity_version

# Publish the container
Expand Down Expand Up @@ -990,7 +986,7 @@ def _migrate_component(
def _get_distinct_target_container_key(
context: _MigrationContext,
source_key: UsageKey,
container_type: ContainerType,
container_type: content_api.ContainerType,
title: str,
) -> LibraryContainerLocator:
"""
Expand All @@ -1012,14 +1008,14 @@ def _get_distinct_target_container_key(
# Use base base slug if available
if base_slug not in context.used_container_slugs:
return LibraryContainerLocator(
context.target_library_key, container_type.value, base_slug
context.target_library_key, container_type.type_code, base_slug
)
# Try numbered variations until we find one that doesn't exist
for i in range(1, _MAX_UNIQUE_SLUG_ATTEMPTS + 1):
candidate_slug = f"{base_slug}_{i}"
if candidate_slug not in context.used_container_slugs:
return LibraryContainerLocator(
context.target_library_key, container_type.value, candidate_slug
context.target_library_key, container_type.type_code, candidate_slug
)
# It would be extremely unlikely for us to run out of attempts
raise RuntimeError(
Expand Down
10 changes: 5 additions & 5 deletions openedx/core/djangoapps/content/search/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -827,19 +827,19 @@ def update_library_containers_collections(
"""
library_key = collection_key.lib_key
library = lib_api.get_library(library_key)
containers = content_api.get_collection_containers(
container_entities = content_api.get_collection_entities(
library.learning_package_id,
collection_key.collection_id,
)
).exclude(container=None).select_related("container")

paginator = Paginator(containers, batch_size)
paginator = Paginator(container_entities, batch_size)
for page in paginator.page_range:
docs = []

for container in paginator.page(page).object_list:
for container_entity in paginator.page(page).object_list:
container_key = lib_api.library_container_locator(
library_key,
container,
container_entity.container,
)
doc = searchable_doc_for_key(container_key)
doc.update(searchable_doc_collections(container_key))
Expand Down
7 changes: 4 additions & 3 deletions openedx/core/djangoapps/content/search/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from unittest.mock import MagicMock, Mock, call, patch
from opaque_keys.edx.keys import UsageKey
from opaque_keys.edx.locator import LibraryCollectionLocator, LibraryContainerLocator
from openedx_content import models_api as content_models

import ddt
import pytest
Expand Down Expand Up @@ -224,15 +225,15 @@ def setUp(self) -> None:
with freeze_time(self.created_date):
self.unit = library_api.create_container(
library_key=self.library.key,
container_type=library_api.ContainerType.Unit,
container_type=content_models.Unit,
slug="unit-1",
title="Unit 1",
user_id=None,
)
self.unit_key = "lct:org1:lib:unit:unit-1"
self.subsection = library_api.create_container(
self.library.key,
container_type=library_api.ContainerType.Subsection,
container_type=content_models.Subsection,
slug="subsection-1",
title="Subsection 1",
user_id=None,
Expand All @@ -245,7 +246,7 @@ def setUp(self) -> None:
self.subsection_key = "lct:org1:lib:subsection:subsection-1"
self.section = library_api.create_container(
self.library.key,
container_type=library_api.ContainerType.Section,
container_type=content_models.Section,
slug="section-1",
title="Section 1",
user_id=None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from freezegun import freeze_time
from opaque_keys.edx.locator import LibraryCollectionLocator, LibraryContainerLocator
from openedx_content import api as content_api
from openedx_content import models_api as content_models
from organizations.models import Organization

from openedx.core.djangoapps.content_libraries import api as library_api
Expand Down Expand Up @@ -92,7 +93,7 @@ def setUpClass(cls):
)
cls.container = library_api.create_container(
cls.library.key,
container_type=library_api.ContainerType.Unit,
container_type=content_models.Unit,
slug="unit1",
title="A Unit in the Search Index",
user_id=None,
Expand All @@ -102,7 +103,7 @@ def setUpClass(cls):
)
cls.subsection = library_api.create_container(
cls.library.key,
container_type=library_api.ContainerType.Subsection,
container_type=content_models.Subsection,
slug="subsection1",
title="A Subsection in the Search Index",
user_id=None,
Expand All @@ -112,7 +113,7 @@ def setUpClass(cls):
)
cls.section = library_api.create_container(
cls.library.key,
container_type=library_api.ContainerType.Section,
container_type=content_models.Section,
slug="section1",
title="A Section in the Search Index",
user_id=None,
Expand Down
4 changes: 2 additions & 2 deletions openedx/core/djangoapps/content_libraries/api/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@
get_containers_contains_item,
update_container_children,
ContainerMetadata,
ContainerType,
)
from .container_metadata import container_type_for_olx_tag
from .collections import library_collection_locator
from .libraries import PublishableItem
from .. import tasks
Expand Down Expand Up @@ -547,7 +547,7 @@ def _import_staged_block_as_container(

container = create_container(
library_key=library_key,
container_type=ContainerType.from_source_olx_tag(olx_node.tag),
container_type=container_type_for_olx_tag(olx_node.tag),
slug=None, # auto-generate slug from title
title=title,
user_id=user.id,
Expand Down
Loading
Loading