Skip to content

Commit bc671e4

Browse files
kdmccormickclaude
andcommitted
refactor: Rename PublishableEntity.key to entity_ref for openedx-core 0.43.0
Switches callers of get_publishable_entity_by_key (now _by_ref) to the new name, and renames .key attribute reads on PublishableEntity, Component, and Container (via PublishableEntityMixin) to .entity_ref. Query filters using key__in/entity__key become entity_ref__in/ entity__entity_ref. The set_library_item_collections param entity_key is renamed to entity_ref for consistency. Collection.key reads are intentionally left for the collection_code rename in a later commit. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 2b781cb commit bc671e4

11 files changed

Lines changed: 26 additions & 25 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ def index_container_batch(batch, num_done, library_key) -> int:
535535
doc.update(searchable_doc_containers(container_key, "sections"))
536536
docs.append(doc)
537537
except Exception as err: # pylint: disable=broad-except
538-
status_cb(f"Error indexing container {container.key}: {err}")
538+
status_cb(f"Error indexing container {container.entity_ref}: {err}")
539539
num_done += 1
540540

541541
if docs:

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ def searchable_doc_collections(object_id: OpaqueKey) -> dict:
450450
component = lib_api.get_component_from_usage_key(object_id)
451451
collections = content_api.get_entity_collections(
452452
component.learning_package_id,
453-
component.key,
453+
component.entity_ref,
454454
).values('key', 'title')
455455
elif isinstance(object_id, LibraryContainerLocator):
456456
container = lib_api.get_container(object_id, include_collections=True)

openedx/core/djangoapps/content/search/tests/test_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ def test_reindex_meilisearch_library_block_error(self, mock_meilisearch) -> None
523523

524524
def mocked_from_component(lib_key, component):
525525
# Simulate an error when processing problem 1
526-
if component.key == 'xblock.v1:problem:p1':
526+
if component.entity_ref == 'xblock.v1:problem:p1':
527527
raise Exception('Error')
528528

529529
return orig_from_component(lib_key, component)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def get_library_block(usage_key: LibraryUsageLocatorV2, include_collections=Fals
178178
if include_collections:
179179
associated_collections = content_api.get_entity_collections(
180180
component.learning_package_id,
181-
component.key,
181+
component.entity_ref,
182182
).values('key', 'title')
183183
else:
184184
associated_collections = None
@@ -725,7 +725,7 @@ def send_block_deleted_signal():
725725
send_block_deleted_signal()
726726
raise
727727

728-
affected_collections = content_api.get_entity_collections(component.learning_package_id, component.key)
728+
affected_collections = content_api.get_entity_collections(component.learning_package_id, component.entity_ref)
729729
affected_containers = get_containers_contains_item(usage_key)
730730

731731
content_api.soft_delete_draft(component.id, deleted_by=user_id)
@@ -770,7 +770,7 @@ def restore_library_block(usage_key: LibraryUsageLocatorV2, user_id: int | None
770770
"""
771771
component = get_component_from_usage_key(usage_key)
772772
library_key = usage_key.context_key
773-
affected_collections = content_api.get_entity_collections(component.learning_package_id, component.key)
773+
affected_collections = content_api.get_entity_collections(component.learning_package_id, component.entity_ref)
774774

775775
# Set draft version back to the latest available component version id.
776776
content_api.set_draft_version(
@@ -985,7 +985,7 @@ def publish_component_changes(usage_key: LibraryUsageLocatorV2, user_id: int):
985985
learning_package = content_library.learning_package
986986
assert learning_package
987987
# The core publishing API is based on draft objects, so find the draft that corresponds to this component:
988-
drafts_to_publish = content_api.get_all_drafts(learning_package.id).filter(entity__key=component.key)
988+
drafts_to_publish = content_api.get_all_drafts(learning_package.id).filter(entity__entity_ref=component.entity_ref)
989989
# Publish the component and update anything that needs to be updated (e.g. search index):
990990
publish_log = content_api.publish_from_drafts(
991991
learning_package.id, draft_qset=drafts_to_publish, published_by=user_id,

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ def update_library_collection_items(
127127
assert content_library.learning_package_id
128128
assert content_library.library_key == library_key
129129

130-
# Fetch the Component.key values for the provided UsageKeys.
131-
item_keys = []
130+
# Fetch the Component.entity_ref values for the provided UsageKeys.
131+
item_refs = []
132132
for opaque_key in opaque_keys:
133133
if isinstance(opaque_key, LibraryContainerLocator):
134134
try:
@@ -139,7 +139,7 @@ def update_library_collection_items(
139139
except Collection.DoesNotExist as exc:
140140
raise ContentLibraryContainerNotFound(opaque_key) from exc
141141

142-
item_keys.append(container.key)
142+
item_refs.append(container.entity_ref)
143143
elif isinstance(opaque_key, UsageKeyV2):
144144
# Parse the block_family from the key to use as namespace.
145145
block_type = BlockTypeKey.from_string(str(opaque_key))
@@ -153,13 +153,13 @@ def update_library_collection_items(
153153
except Component.DoesNotExist as exc:
154154
raise ContentLibraryBlockNotFound(opaque_key) from exc
155155

156-
item_keys.append(component.key)
156+
item_refs.append(component.entity_ref)
157157
else:
158158
# This should never happen, but just in case.
159159
raise ValueError(f"Invalid opaque_key: {opaque_key}")
160160

161161
entities_qset = PublishableEntity.objects.filter(
162-
key__in=item_keys,
162+
entity_ref__in=item_refs,
163163
)
164164

165165
if remove:
@@ -181,7 +181,7 @@ def update_library_collection_items(
181181

182182
def set_library_item_collections(
183183
library_key: LibraryLocatorV2,
184-
entity_key: str,
184+
entity_ref: str,
185185
*,
186186
collection_keys: list[str],
187187
created_by: int | None = None,
@@ -207,12 +207,12 @@ def set_library_item_collections(
207207
assert content_library.learning_package_id
208208
assert content_library.library_key == library_key
209209

210-
publishable_entity = content_api.get_publishable_entity_by_key(
210+
publishable_entity = content_api.get_publishable_entity_by_ref(
211211
content_library.learning_package_id,
212-
key=entity_key,
212+
entity_ref=entity_ref,
213213
)
214214

215-
# Note: Component.key matches its PublishableEntity.key
215+
# Note: Component.entity_ref matches its PublishableEntity.entity_ref
216216
collection_qs = content_api.get_collections(content_library.learning_package_id).filter(
217217
key__in=collection_keys
218218
)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,8 @@ def library_container_locator(
366366
if container_type_code not in LIBRARY_ALLOWED_CONTAINER_TYPES:
367367
raise ValueError(f"Unsupported container type for content libraries: {container!r}")
368368

369-
return LibraryContainerLocator(library_key, container_type=container_type_code, container_id=container.key)
369+
# TODO: verify whether container_id should use entity_ref (opaque) or container_code (local slug).
370+
return LibraryContainerLocator(library_key, container_type=container_type_code, container_id=container.entity_ref)
370371

371372

372373
def get_container_from_key(container_key: LibraryContainerLocator, include_deleted=False) -> Container:

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ def send_container_deleted_signal():
226226
# Fetch related collections and containers before soft-delete
227227
affected_collections = content_api.get_entity_collections(
228228
container.publishable_entity.learning_package_id,
229-
container.key,
229+
container.entity_ref,
230230
)
231231
affected_containers = get_containers_contains_item(container_key)
232232
# Get children containers or components to update their index data
@@ -291,7 +291,7 @@ def restore_container(container_key: LibraryContainerLocator) -> None:
291291

292292
affected_collections = content_api.get_entity_collections(
293293
container.publishable_entity.learning_package_id,
294-
container.key,
294+
container.entity_ref,
295295
)
296296

297297
content_api.set_draft_version(container.id, container.versioning.latest.pk)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ def patch(self, request: RestRequest, usage_key_str) -> Response:
272272
collection_keys = serializer.validated_data['collection_keys']
273273
api.set_library_item_collections(
274274
library_key=key.lib_key,
275-
entity_key=component.publishable_entity.key,
275+
entity_ref=component.publishable_entity.entity_ref,
276276
collection_keys=collection_keys,
277277
created_by=request.user.id,
278278
content_library=content_library,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ def patch(self, request: RestRequest, container_key: LibraryContainerLocator) ->
346346
collection_keys = serializer.validated_data['collection_keys']
347347
api.set_library_item_collections(
348348
library_key=container_key.lib_key,
349-
entity_key=container_key.container_id,
349+
entity_ref=container_key.container_id,
350350
collection_keys=collection_keys,
351351
created_by=request.user.id,
352352
content_library=content_library,

openedx/core/djangoapps/content_libraries/tasks.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def send_events_after_publish(publish_log_pk: int, library_key_str: str) -> None
142142
pass
143143
else:
144144
log.warning(
145-
f"PublishableEntity {record.entity.pk} / {record.entity.key} was modified during publish operation "
145+
f"PublishableEntity {record.entity.pk} / {record.entity.entity_ref} was modified during publish operation "
146146
"but is of unknown type."
147147
)
148148

@@ -246,13 +246,13 @@ def send_events_after_revert(draft_change_log_id: int, library_key_str: str) ->
246246
updated_container_keys.add(container_key)
247247
else:
248248
log.warning(
249-
f"PublishableEntity {record.entity.pk} / {record.entity.key} was modified during publish operation "
249+
f"PublishableEntity {record.entity.pk} / {record.entity.entity_ref} was modified during publish operation "
250250
"but is of unknown type."
251251
)
252252
# If any collections contain this entity, their item count may need to be updated, e.g. if this was a
253253
# newly created component in the collection and is now deleted, or this was deleted and is now re-added.
254254
for parent_collection in content_api.get_entity_collections(
255-
record.entity.learning_package_id, record.entity.key,
255+
record.entity.learning_package_id, record.entity.entity_ref,
256256
):
257257
collection_key = api.library_collection_locator(
258258
library_key=library_key,

0 commit comments

Comments
 (0)