2020 LIBRARY_COLLECTION_DELETED ,
2121 LIBRARY_COLLECTION_UPDATED ,
2222)
23- from openedx_learning .api .authoring import get_component , get_components
24- from openedx_learning .api .authoring_models import Collection , CollectionPublishableEntity , Component , PublishableEntity
23+ from openedx_learning .api .authoring import get_components , get_containers
24+ from openedx_learning .api .authoring_models import Collection , CollectionPublishableEntity , PublishableEntity
2525
2626from lms .djangoapps .grades .api import signals as grades_signals
2727
28- from .api import library_component_usage_key
2928from .models import ContentLibrary , LtiGradedResource
3029
3130log = logging .getLogger (__name__ )
@@ -118,8 +117,8 @@ def library_collection_deleted(sender, instance, **kwargs):
118117 )
119118
120119
121- def _library_collection_component_changed (
122- component : Component ,
120+ def _library_collection_entity_changed (
121+ publishable_entity : PublishableEntity ,
123122 library_key : LibraryLocatorV2 | None = None ,
124123) -> None :
125124 """
@@ -128,23 +127,19 @@ def _library_collection_component_changed(
128127 if not library_key :
129128 try :
130129 library = ContentLibrary .objects .get (
131- learning_package_id = component .learning_package_id ,
130+ learning_package_id = publishable_entity .learning_package_id ,
132131 )
133132 except ContentLibrary .DoesNotExist :
134- log .error ("{component } is not associated with a content library." )
133+ log .error ("{publishable_entity } is not associated with a content library." )
135134 return
136135
137136 library_key = library .library_key
138137
139138 assert library_key
140139
141- usage_key = library_component_usage_key (
142- library_key ,
143- component ,
144- )
145140 CONTENT_OBJECT_ASSOCIATIONS_CHANGED .send_event (
146141 content_object = ContentObjectChangedData (
147- object_id = str (usage_key ),
142+ object_id = str (publishable_entity . key ),
148143 changes = ["collections" ],
149144 ),
150145 )
@@ -156,9 +151,7 @@ def library_collection_entity_saved(sender, instance, created, **kwargs):
156151 Sends a CONTENT_OBJECT_ASSOCIATIONS_CHANGED event for components added to a collection.
157152 """
158153 if created :
159- # Component.pk matches its entity.pk
160- component = get_component (instance .entity_id )
161- _library_collection_component_changed (component )
154+ _library_collection_entity_changed (instance .entity )
162155
163156
164157@receiver (post_delete , sender = CollectionPublishableEntity , dispatch_uid = "library_collection_entity_deleted" )
@@ -168,9 +161,7 @@ def library_collection_entity_deleted(sender, instance, **kwargs):
168161 """
169162 # Only trigger component updates if CollectionPublishableEntity was cascade deleted due to deletion of a collection.
170163 if isinstance (kwargs .get ('origin' ), Collection ):
171- # Component.pk matches its entity.pk
172- component = get_component (instance .entity_id )
173- _library_collection_component_changed (component )
164+ _library_collection_entity_changed (instance .entity )
174165
175166
176167@receiver (m2m_changed , sender = CollectionPublishableEntity , dispatch_uid = "library_collection_entities_changed" )
@@ -190,15 +181,17 @@ def library_collection_entities_changed(sender, instance, action, pk_set, **kwar
190181 return
191182
192183 if isinstance (instance , PublishableEntity ):
193- _library_collection_component_changed (instance . component , library .library_key )
184+ _library_collection_entity_changed (instance , library .library_key )
194185 return
195186
196187 # When action=="post_clear", pk_set==None
197188 # Since the collection instance now has an empty entities set,
198189 # we don't know which ones were removed, so we need to update associations for all library components.
199190 components = get_components (instance .learning_package_id )
191+ containers = get_containers (instance .learning_package_id )
200192 if pk_set :
201193 components = components .filter (pk__in = pk_set )
194+ containers = containers .filter (pk__in = pk_set )
202195
203- for component in components .all ():
204- _library_collection_component_changed ( component , library .library_key )
196+ for entity in [ components .all (), containers . all ()] :
197+ _library_collection_entity_changed ( entity . publishable_entity , library .library_key )
0 commit comments