Skip to content

Commit a8c8047

Browse files
fix: ValidationError when trying to add components to a collection (#38579)
If a component exists in multiple libraries with the same code/ref/block_id, it will trigger a validation error when trying to add it to a collection.
1 parent f110ff3 commit a8c8047

1 file changed

Lines changed: 4 additions & 6 deletions

File tree

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def update_library_collection_items(
125125
assert content_library.library_key == library_key
126126

127127
# Fetch the Component.entity_ref values for the provided UsageKeys.
128-
item_refs = []
128+
entity_ids: list[PublishableEntity.ID] = []
129129
for opaque_key in opaque_keys:
130130
if isinstance(opaque_key, LibraryContainerLocator):
131131
try:
@@ -136,7 +136,7 @@ def update_library_collection_items(
136136
except Collection.DoesNotExist as exc:
137137
raise ContentLibraryContainerNotFound(opaque_key) from exc
138138

139-
item_refs.append(container.entity_ref)
139+
entity_ids.append(container.id)
140140
elif isinstance(opaque_key, UsageKeyV2):
141141
# Parse the block_family from the key to use as namespace.
142142
block_type = BlockTypeKey.from_string(str(opaque_key))
@@ -150,14 +150,12 @@ def update_library_collection_items(
150150
except Component.DoesNotExist as exc:
151151
raise ContentLibraryBlockNotFound(opaque_key) from exc
152152

153-
item_refs.append(component.entity_ref)
153+
entity_ids.append(component.id)
154154
else:
155155
# This should never happen, but just in case.
156156
raise ValueError(f"Invalid opaque_key: {opaque_key}")
157157

158-
entities_qset = PublishableEntity.objects.filter(
159-
entity_ref__in=item_refs,
160-
)
158+
entities_qset = content_api.get_publishable_entities(content_library.learning_package_id).filter(id__in=entity_ids)
161159

162160
if remove:
163161
collection = content_api.remove_from_collection(

0 commit comments

Comments
 (0)