Skip to content

Commit 867e246

Browse files
chore: bump opaque-keys to v3, update content libraries key usages (#36588)
See openedx/opaque-keys#379
1 parent 77fda46 commit 867e246

14 files changed

Lines changed: 64 additions & 81 deletions

File tree

openedx/core/djangoapps/content/search/api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ def update_library_components_collections(
760760
761761
Because there may be a lot of components, we send these updates to Meilisearch in batches.
762762
"""
763-
library_key = collection_key.library_key
763+
library_key = collection_key.lib_key
764764
library = lib_api.get_library(library_key)
765765
components = authoring_api.get_collection_components(
766766
library.learning_package_id,
@@ -795,7 +795,7 @@ def update_library_containers_collections(
795795
796796
Because there may be a lot of containers, we send these updates to Meilisearch in batches.
797797
"""
798-
library_key = collection_key.library_key
798+
library_key = collection_key.lib_key
799799
library = lib_api.get_library(library_key)
800800
containers = authoring_api.get_collection_containers(
801801
library.learning_package_id,

openedx/core/djangoapps/content/search/documents.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from django.core.exceptions import ObjectDoesNotExist
1010
from django.utils.text import slugify
11-
from opaque_keys.edx.keys import LearningContextKey, UsageKey, OpaqueKey
11+
from opaque_keys.edx.keys import ContainerKey, LearningContextKey, UsageKey, OpaqueKey
1212
from opaque_keys.edx.locator import LibraryCollectionLocator, LibraryContainerLocator
1313
from openedx_learning.api import authoring as authoring_api
1414
from openedx_learning.api.authoring_models import Collection
@@ -523,7 +523,7 @@ def searchable_doc_for_collection(
523523
).count()
524524

525525
doc.update({
526-
Fields.context_key: str(collection_key.library_key),
526+
Fields.context_key: str(collection_key.context_key),
527527
Fields.org: str(collection_key.org),
528528
Fields.usage_key: str(collection_key),
529529
Fields.block_id: collection.key,
@@ -536,7 +536,7 @@ def searchable_doc_for_collection(
536536
Fields.published: {
537537
Fields.published_num_children: published_num_children,
538538
},
539-
Fields.access_id: _meili_access_id_from_context_key(collection_key.library_key),
539+
Fields.access_id: _meili_access_id_from_context_key(collection_key.context_key),
540540
Fields.breadcrumbs: [{"display_name": collection.learning_package.title}],
541541
})
542542

@@ -549,7 +549,7 @@ def searchable_doc_for_collection(
549549

550550

551551
def searchable_doc_for_container(
552-
container_key: LibraryContainerLocator,
552+
container_key: ContainerKey,
553553
) -> dict:
554554
"""
555555
Generate a dictionary document suitable for ingestion into a search engine
@@ -562,15 +562,15 @@ def searchable_doc_for_container(
562562
"""
563563
doc = {
564564
Fields.id: meili_id_from_opaque_key(container_key),
565-
Fields.context_key: str(container_key.library_key),
565+
Fields.context_key: str(container_key.context_key),
566566
Fields.org: str(container_key.org),
567567
# In the future, this may be either course_container or library_container
568568
Fields.type: DocType.library_container,
569569
# To check if it is "unit", "section", "subsection", etc..
570570
Fields.block_type: container_key.container_type,
571571
Fields.usage_key: str(container_key), # Field name isn't exact but this is the closest match
572572
Fields.block_id: container_key.container_id, # Field name isn't exact but this is the closest match
573-
Fields.access_id: _meili_access_id_from_context_key(container_key.library_key),
573+
Fields.access_id: _meili_access_id_from_context_key(container_key.context_key),
574574
Fields.publish_status: PublishStatus.never,
575575
Fields.last_published: None,
576576
}
@@ -596,7 +596,7 @@ def searchable_doc_for_container(
596596
Fields.publish_status: publish_status,
597597
Fields.last_published: container.last_published.timestamp() if container.last_published else None,
598598
})
599-
library = lib_api.get_library(container_key.library_key)
599+
library = lib_api.get_library(container_key.context_key)
600600
if library:
601601
doc[Fields.breadcrumbs] = [{"display_name": library.title}]
602602

openedx/core/djangoapps/content/search/tasks.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def update_library_collection_index_doc(collection_key_str: str) -> None:
9898
Celery task to update the content index document for a library collection
9999
"""
100100
collection_key = LibraryCollectionLocator.from_string(collection_key_str)
101-
library_key = collection_key.library_key
101+
library_key = collection_key.lib_key
102102

103103
log.info("Updating content index documents for collection %s in library%s", collection_key, library_key)
104104

@@ -112,7 +112,7 @@ def update_library_components_collections(collection_key_str: str) -> None:
112112
Celery task to update the "collections" field for components in the given content library collection.
113113
"""
114114
collection_key = LibraryCollectionLocator.from_string(collection_key_str)
115-
library_key = collection_key.library_key
115+
library_key = collection_key.lib_key
116116

117117
log.info("Updating document.collections for library %s collection %s components", library_key, collection_key)
118118

@@ -126,7 +126,7 @@ def update_library_containers_collections(collection_key_str: str) -> None:
126126
Celery task to update the "collections" field for containers in the given content library collection.
127127
"""
128128
collection_key = LibraryCollectionLocator.from_string(collection_key_str)
129-
library_key = collection_key.library_key
129+
library_key = collection_key.lib_key
130130

131131
log.info("Updating document.collections for library %s collection %s containers", library_key, collection_key)
132132

@@ -140,7 +140,7 @@ def update_library_container_index_doc(container_key_str: str) -> None:
140140
Celery task to update the content index document for a library container
141141
"""
142142
container_key = LibraryContainerLocator.from_string(container_key_str)
143-
library_key = container_key.library_key
143+
library_key = container_key.lib_key
144144

145145
log.info("Updating content index documents for container %s in library%s", container_key, library_key)
146146

openedx/core/djangoapps/content_libraries/api/collections.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ def get_library_collection_from_locator(
251251
"""
252252
Return a Collection using the LibraryCollectionLocator
253253
"""
254-
library_key = collection_locator.library_key
254+
library_key = collection_locator.lib_key
255255
collection_key = collection_locator.collection_id
256256
content_library = ContentLibrary.objects.get_by_key(library_key) # type: ignore[attr-defined]
257257
assert content_library.learning_package_id is not None # shouldn't happen but it's technically possible.

openedx/core/djangoapps/content_libraries/api/containers.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def get_container_from_key(container_key: LibraryContainerLocator, isDeleted=Fal
179179
Raises ContentLibraryContainerNotFound if no container found, or if the container has been soft deleted.
180180
"""
181181
assert isinstance(container_key, LibraryContainerLocator)
182-
content_library = ContentLibrary.objects.get_by_key(container_key.library_key)
182+
content_library = ContentLibrary.objects.get_by_key(container_key.lib_key)
183183
learning_package = content_library.learning_package
184184
assert learning_package is not None
185185
container = authoring_api.get_container_by_key(
@@ -204,7 +204,7 @@ def get_container(container_key: LibraryContainerLocator, include_collections=Fa
204204
else:
205205
associated_collections = None
206206
container_meta = ContainerMetadata.from_container(
207-
container_key.library_key,
207+
container_key.lib_key,
208208
container,
209209
associated_collections=associated_collections,
210210
)
@@ -268,7 +268,7 @@ def update_container(
268268
Update a container (e.g. a Unit) title.
269269
"""
270270
container = get_container_from_key(container_key)
271-
library_key = container_key.library_key
271+
library_key = container_key.lib_key
272272

273273
assert container.unit
274274
unit_version = authoring_api.create_next_unit_version(
@@ -295,7 +295,7 @@ def delete_container(
295295
296296
No-op if container doesn't exist or has already been soft-deleted.
297297
"""
298-
library_key = container_key.library_key
298+
library_key = container_key.lib_key
299299
container = get_container_from_key(container_key)
300300

301301
affected_collections = authoring_api.get_entity_collections(
@@ -330,7 +330,7 @@ def restore_container(container_key: LibraryContainerLocator) -> None:
330330
"""
331331
Restore the specified library container.
332332
"""
333-
library_key = container_key.library_key
333+
library_key = container_key.lib_key
334334
container = get_container_from_key(container_key, isDeleted=True)
335335

336336
affected_collections = authoring_api.get_entity_collections(
@@ -380,13 +380,13 @@ def get_container_children(
380380
if container_key.container_type == ContainerType.Unit.value:
381381
child_components = authoring_api.get_components_in_unit(container.unit, published=published)
382382
return [LibraryXBlockMetadata.from_component(
383-
container_key.library_key,
383+
container_key.lib_key,
384384
entry.component
385385
) for entry in child_components]
386386
else:
387387
child_entities = authoring_api.get_entities_in_container(container, published=published)
388388
return [ContainerMetadata.from_container(
389-
container_key.library_key,
389+
container_key.lib_key,
390390
entry.entity
391391
) for entry in child_entities]
392392

@@ -411,7 +411,7 @@ def update_container_children(
411411
"""
412412
Adds children components or containers to given container.
413413
"""
414-
library_key = container_key.library_key
414+
library_key = container_key.lib_key
415415
container_type = container_key.container_type
416416
container = get_container_from_key(container_key)
417417
match container_type:
@@ -459,7 +459,7 @@ def publish_container_changes(container_key: LibraryContainerLocator, user_id: i
459459
containers/blocks.
460460
"""
461461
container = get_container_from_key(container_key)
462-
library_key = container_key.library_key
462+
library_key = container_key.lib_key
463463
content_library = ContentLibrary.objects.get_by_key(library_key) # type: ignore[attr-defined]
464464
learning_package = content_library.learning_package
465465
assert learning_package

openedx/core/djangoapps/content_libraries/rest_api/containers.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def get(self, request, container_key: LibraryContainerLocator):
7777
Get information about a container
7878
"""
7979
api.require_permission_for_library_key(
80-
container_key.library_key,
80+
container_key.lib_key,
8181
request.user,
8282
permissions.CAN_VIEW_THIS_CONTENT_LIBRARY,
8383
)
@@ -94,7 +94,7 @@ def patch(self, request, container_key: LibraryContainerLocator):
9494
Update a Container.
9595
"""
9696
api.require_permission_for_library_key(
97-
container_key.library_key,
97+
container_key.lib_key,
9898
request.user,
9999
permissions.CAN_EDIT_THIS_CONTENT_LIBRARY,
100100
)
@@ -115,7 +115,7 @@ def delete(self, request, container_key: LibraryContainerLocator):
115115
Delete a Container (soft delete).
116116
"""
117117
api.require_permission_for_library_key(
118-
container_key.library_key,
118+
container_key.lib_key,
119119
request.user,
120120
permissions.CAN_EDIT_THIS_CONTENT_LIBRARY,
121121
)
@@ -180,7 +180,7 @@ def get(self, request, container_key: LibraryContainerLocator):
180180
"""
181181
published = request.GET.get('published', False)
182182
api.require_permission_for_library_key(
183-
container_key.library_key,
183+
container_key.lib_key,
184184
request.user,
185185
permissions.CAN_VIEW_THIS_CONTENT_LIBRARY,
186186
)
@@ -201,7 +201,7 @@ def _update_component_children(
201201
Helper function to update children in container.
202202
"""
203203
api.require_permission_for_library_key(
204-
container_key.library_key,
204+
container_key.lib_key,
205205
request.user,
206206
permissions.CAN_EDIT_THIS_CONTENT_LIBRARY,
207207
)
@@ -288,7 +288,7 @@ def post(self, request, container_key: LibraryContainerLocator) -> Response:
288288
Restores a soft-deleted library container
289289
"""
290290
api.require_permission_for_library_key(
291-
container_key.library_key,
291+
container_key.lib_key,
292292
request.user,
293293
permissions.CAN_EDIT_THIS_CONTENT_LIBRARY,
294294
)
@@ -310,7 +310,7 @@ def patch(self, request: RestRequest, container_key: LibraryContainerLocator) ->
310310
Collection and Components must all be part of the given library/learning package.
311311
"""
312312
content_library = api.require_permission_for_library_key(
313-
container_key.library_key,
313+
container_key.lib_key,
314314
request.user,
315315
permissions.CAN_EDIT_THIS_CONTENT_LIBRARY
316316
)
@@ -320,7 +320,7 @@ def patch(self, request: RestRequest, container_key: LibraryContainerLocator) ->
320320

321321
collection_keys = serializer.validated_data['collection_keys']
322322
api.set_library_item_collections(
323-
library_key=container_key.library_key,
323+
library_key=container_key.lib_key,
324324
publishable_entity=container.publishable_entity,
325325
collection_keys=collection_keys,
326326
created_by=request.user.id,
@@ -342,7 +342,7 @@ def post(self, request: RestRequest, container_key: LibraryContainerLocator) ->
342342
Publish the container and its children
343343
"""
344344
api.require_permission_for_library_key(
345-
container_key.library_key,
345+
container_key.lib_key,
346346
request.user,
347347
permissions.CAN_EDIT_THIS_CONTENT_LIBRARY,
348348
)

openedx/core/djangoapps/content_libraries/rest_api/serializers.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
from rest_framework.exceptions import ValidationError
88

99
from opaque_keys import OpaqueKey
10-
from opaque_keys.edx.keys import UsageKeyV2
11-
from opaque_keys.edx.locator import LibraryContainerLocator
10+
from opaque_keys.edx.locator import LibraryContainerLocator, LibraryUsageLocatorV2
1211
from opaque_keys import InvalidKeyError
1312

1413
from openedx_learning.api.authoring_models import Collection
@@ -319,22 +318,22 @@ class ContentLibraryCollectionUpdateSerializer(serializers.Serializer):
319318

320319
class UsageKeyV2Serializer(serializers.BaseSerializer):
321320
"""
322-
Serializes a UsageKeyV2.
321+
Serializes a library Component (XBlock) key.
323322
"""
324-
def to_representation(self, value: UsageKeyV2) -> str:
323+
def to_representation(self, value: LibraryUsageLocatorV2) -> str:
325324
"""
326-
Returns the UsageKeyV2 value as a string.
325+
Returns the LibraryUsageLocatorV2 value as a string.
327326
"""
328327
return str(value)
329328

330-
def to_internal_value(self, value: str) -> UsageKeyV2:
329+
def to_internal_value(self, value: str) -> LibraryUsageLocatorV2:
331330
"""
332-
Returns a UsageKeyV2 from the string value.
331+
Returns a LibraryUsageLocatorV2 from the string value.
333332
334-
Raises ValidationError if invalid UsageKeyV2.
333+
Raises ValidationError if invalid LibraryUsageLocatorV2.
335334
"""
336335
try:
337-
return UsageKeyV2.from_string(value)
336+
return LibraryUsageLocatorV2.from_string(value)
338337
except InvalidKeyError as err:
339338
raise ValidationError from err
340339

@@ -359,12 +358,12 @@ def to_representation(self, value: OpaqueKey) -> str:
359358

360359
def to_internal_value(self, value: str) -> OpaqueKey:
361360
"""
362-
Returns a UsageKeyV2 or a LibraryContainerLocator from the string value.
361+
Returns a LibraryUsageLocatorV2 or a LibraryContainerLocator from the string value.
363362
364363
Raises ValidationError if invalid UsageKeyV2 or LibraryContainerLocator.
365364
"""
366365
try:
367-
return UsageKeyV2.from_string(value)
366+
return LibraryUsageLocatorV2.from_string(value)
368367
except InvalidKeyError:
369368
try:
370369
return LibraryContainerLocator.from_string(value)

openedx/core/djangoapps/content_tagging/api.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
from itertools import groupby
88
import csv
99
from typing import Iterator
10-
from opaque_keys.edx.keys import UsageKey
10+
from opaque_keys.edx.keys import CourseKey, CollectionKey, ContainerKey, UsageKey
1111

1212
import openedx_tagging.core.tagging.api as oel_tagging
1313
from django.db.models import Exists, OuterRef, Q, QuerySet
1414
from django.utils.timezone import now
15-
from opaque_keys.edx.keys import CourseKey, LibraryItemKey
1615
from opaque_keys.edx.locator import LibraryLocatorV2
1716
from openedx_tagging.core.tagging.models import ObjectTag, Taxonomy
1817
from openedx_tagging.core.tagging.models.utils import TAGS_CSV_SEPARATOR
@@ -230,8 +229,8 @@ def generate_csv_rows(object_id, buffer) -> Iterator[str]:
230229
"""
231230
content_key = get_content_key_from_string(object_id)
232231

233-
if isinstance(content_key, (UsageKey, LibraryItemKey)):
234-
raise ValueError("The object_id must be a CourseKey or a LibraryLocatorV2.")
232+
if isinstance(content_key, (UsageKey, CollectionKey, ContainerKey)):
233+
raise ValueError("The object_id must be a component, collection, or container.")
235234

236235
all_object_tags, taxonomies = get_all_object_tags(content_key)
237236
tagged_content = build_object_tree_with_objecttags(content_key, all_object_tags)

openedx/core/djangoapps/content_tagging/types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
from typing import Dict, List, Union
77

8-
from opaque_keys.edx.keys import CourseKey, UsageKey, LibraryItemKey
8+
from opaque_keys.edx.keys import CourseKey, UsageKey, CollectionKey, ContainerKey
99
from opaque_keys.edx.locator import LibraryLocatorV2
1010
from openedx_tagging.core.tagging.models import Taxonomy
1111

12-
ContentKey = Union[LibraryLocatorV2, CourseKey, UsageKey, LibraryItemKey]
12+
ContentKey = Union[LibraryLocatorV2, CourseKey, UsageKey, CollectionKey, ContainerKey]
1313
ContextKey = Union[LibraryLocatorV2, CourseKey]
1414

1515
TagValuesByTaxonomyIdDict = Dict[int, List[str]]

0 commit comments

Comments
 (0)