Skip to content

Commit 955f772

Browse files
feat: update content libraries API to use upstream entity changed events
1 parent 0834b1b commit 955f772

9 files changed

Lines changed: 436 additions & 529 deletions

File tree

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

Lines changed: 8 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,8 @@
2525
from opaque_keys.edx.locator import LibraryContainerLocator, LibraryLocatorV2, LibraryUsageLocatorV2
2626
from openedx_content import api as content_api
2727
from openedx_content.models_api import Collection, Component, ComponentVersion, Container, LearningPackage, MediaType
28-
from openedx_events.content_authoring.data import (
29-
ContentObjectChangedData,
30-
LibraryBlockData,
31-
LibraryContainerData,
32-
)
33-
from openedx_events.content_authoring.signals import (
34-
CONTENT_OBJECT_ASSOCIATIONS_CHANGED,
35-
LIBRARY_BLOCK_CREATED,
36-
LIBRARY_BLOCK_DELETED,
37-
LIBRARY_BLOCK_UPDATED,
38-
LIBRARY_CONTAINER_UPDATED,
39-
)
28+
from openedx_events.content_authoring.data import LibraryBlockData
29+
from openedx_events.content_authoring.signals import LIBRARY_BLOCK_DELETED
4030
from xblock.core import XBlock
4131

4232
from openedx.core.djangoapps.content_staging.data import StagedContentID
@@ -55,7 +45,6 @@
5545
ContainerMetadata,
5646
create_container,
5747
get_container,
58-
get_containers_contains_item,
5948
update_container_children,
6049
)
6150
from .exceptions import (
@@ -252,29 +241,6 @@ def set_library_block_olx(usage_key: LibraryUsageLocatorV2, new_olx_str: str) ->
252241
created=now,
253242
)
254243

255-
# .. event_implemented_name: LIBRARY_BLOCK_UPDATED
256-
# .. event_type: org.openedx.content_authoring.library_block.updated.v1
257-
transaction.on_commit(lambda: LIBRARY_BLOCK_UPDATED.send_event(
258-
library_block=LibraryBlockData(
259-
library_key=usage_key.context_key,
260-
usage_key=usage_key
261-
)
262-
))
263-
264-
# For each container, trigger LIBRARY_CONTAINER_UPDATED signal and set background=True to trigger
265-
# container indexing asynchronously.
266-
affected_containers = get_containers_contains_item(usage_key)
267-
for container in affected_containers:
268-
# .. event_implemented_name: LIBRARY_CONTAINER_UPDATED
269-
# .. event_type: org.openedx.content_authoring.content_library.container.updated.v1
270-
container_key = container.container_key
271-
transaction.on_commit(lambda ck=container_key: LIBRARY_CONTAINER_UPDATED.send_event( # type: ignore[misc]
272-
library_container=LibraryContainerData(
273-
container_key=ck,
274-
background=True,
275-
)
276-
))
277-
278244
return new_component_version
279245

280246

@@ -351,16 +317,6 @@ def create_library_block(
351317
_create_component_for_block(content_library, usage_key, user_id, can_stand_alone)
352318

353319
# Now return the metadata about the new block:
354-
355-
# .. event_implemented_name: LIBRARY_BLOCK_CREATED
356-
# .. event_type: org.openedx.content_authoring.library_block.created.v1
357-
LIBRARY_BLOCK_CREATED.send_event(
358-
library_block=LibraryBlockData(
359-
library_key=content_library.library_key,
360-
usage_key=usage_key
361-
)
362-
)
363-
364320
return get_library_block(usage_key)
365321

366322

@@ -493,16 +449,6 @@ def _import_staged_block(
493449
path=filename,
494450
)
495451

496-
# Emit library block created event
497-
# .. event_implemented_name: LIBRARY_BLOCK_CREATED
498-
# .. event_type: org.openedx.content_authoring.library_block.created.v1
499-
transaction.on_commit(lambda: LIBRARY_BLOCK_CREATED.send_event(
500-
library_block=LibraryBlockData(
501-
library_key=content_library.library_key,
502-
usage_key=usage_key
503-
)
504-
))
505-
506452
# Now return the metadata about the new block
507453
return get_library_block(usage_key)
508454

@@ -704,16 +650,6 @@ def delete_library_block(
704650
"""
705651
library_key = usage_key.context_key
706652

707-
def send_block_deleted_signal():
708-
# .. event_implemented_name: LIBRARY_BLOCK_DELETED
709-
# .. event_type: org.openedx.content_authoring.library_block.deleted.v1
710-
LIBRARY_BLOCK_DELETED.send_event(
711-
library_block=LibraryBlockData(
712-
library_key=library_key,
713-
usage_key=usage_key
714-
)
715-
)
716-
717653
try:
718654
component = get_component_from_usage_key(usage_key)
719655
except Component.DoesNotExist:
@@ -722,78 +658,29 @@ def send_block_deleted_signal():
722658
# (an intermediate error occurred).
723659
# In that case, we keep the index updated by removing the entry,
724660
# but still raise the error so the caller knows the component did not exist.
725-
send_block_deleted_signal()
726-
raise
727661

728-
affected_containers = get_containers_contains_item(usage_key)
662+
# .. event_implemented_name: LIBRARY_BLOCK_DELETED
663+
# .. event_type: org.openedx.content_authoring.library_block.deleted.v1
664+
LIBRARY_BLOCK_DELETED.send_event(
665+
library_block=LibraryBlockData(library_key=library_key, usage_key=usage_key)
666+
)
667+
raise
729668

730669
content_api.soft_delete_draft(component.id, deleted_by=user_id)
731670

732-
send_block_deleted_signal()
733-
734-
# For each container, trigger LIBRARY_CONTAINER_UPDATED signal and set background=True to trigger
735-
# container indexing asynchronously.
736-
#
737-
# To update the components count in containers
738-
for container in affected_containers:
739-
# .. event_implemented_name: LIBRARY_CONTAINER_UPDATED
740-
# .. event_type: org.openedx.content_authoring.content_library.container.updated.v1
741-
LIBRARY_CONTAINER_UPDATED.send_event(
742-
library_container=LibraryContainerData(
743-
container_key=container.container_key,
744-
background=True,
745-
)
746-
)
747-
748671

749672
def restore_library_block(usage_key: LibraryUsageLocatorV2, user_id: int | None = None) -> None:
750673
"""
751674
Restore the specified library block.
752675
"""
753676
component = get_component_from_usage_key(usage_key)
754-
library_key = usage_key.context_key
755-
756677
# Set draft version back to the latest available component version id.
757678
content_api.set_draft_version(
758679
component.id,
759680
component.versioning.latest.pk,
760681
set_by=user_id,
761682
)
762683

763-
# .. event_implemented_name: LIBRARY_BLOCK_CREATED
764-
# .. event_type: org.openedx.content_authoring.library_block.created.v1
765-
LIBRARY_BLOCK_CREATED.send_event(
766-
library_block=LibraryBlockData(
767-
library_key=library_key,
768-
usage_key=usage_key
769-
)
770-
)
771-
772-
# Add tags and collections back to index
773-
# .. event_implemented_name: CONTENT_OBJECT_ASSOCIATIONS_CHANGED
774-
# .. event_type: org.openedx.content_authoring.content.object.associations.changed.v1
775-
CONTENT_OBJECT_ASSOCIATIONS_CHANGED.send_event(
776-
content_object=ContentObjectChangedData(
777-
object_id=str(usage_key),
778-
changes=["collections", "tags", "units"],
779-
),
780-
)
781-
782-
# For each container, trigger LIBRARY_CONTAINER_UPDATED signal and set background=True to trigger
783-
# container indexing asynchronously.
784-
#
785-
# To update the components count in containers
786-
affected_containers = get_containers_contains_item(usage_key)
787-
for container in affected_containers:
788-
# .. event_implemented_name: LIBRARY_CONTAINER_UPDATED
789-
# .. event_type: org.openedx.content_authoring.content_library.container.updated.v1
790-
LIBRARY_CONTAINER_UPDATED.send_event(
791-
library_container=LibraryContainerData(
792-
container_key=container.container_key,
793-
background=True,
794-
)
795-
)
796-
797684

798685
def get_library_block_static_asset_files(usage_key: LibraryUsageLocatorV2) -> list[LibraryXBlockStaticFile]:
799686
"""
@@ -879,16 +766,6 @@ def add_library_block_static_asset_file(
879766
created=datetime.now(tz=timezone.utc), # noqa: UP017
880767
created_by=user.id if user else None,
881768
)
882-
transaction.on_commit(
883-
# .. event_implemented_name: LIBRARY_BLOCK_UPDATED
884-
# .. event_type: org.openedx.content_authoring.library_block.updated.v1
885-
lambda: LIBRARY_BLOCK_UPDATED.send_event(
886-
library_block=LibraryBlockData(
887-
library_key=usage_key.context_key,
888-
usage_key=usage_key,
889-
)
890-
)
891-
)
892769

893770
# Now figure out the URL for the newly created asset...
894771
site_root_url = get_xblock_app_config().get_site_root_url()
@@ -927,16 +804,6 @@ def delete_library_block_static_asset_file(usage_key, file_path, user=None):
927804
created=now,
928805
created_by=user.id if user else None,
929806
)
930-
transaction.on_commit(
931-
# .. event_implemented_name: LIBRARY_BLOCK_UPDATED
932-
# .. event_type: org.openedx.content_authoring.library_block.updated.v1
933-
lambda: LIBRARY_BLOCK_UPDATED.send_event(
934-
library_block=LibraryBlockData(
935-
library_key=usage_key.context_key,
936-
usage_key=usage_key,
937-
)
938-
)
939-
)
940807

941808

942809
def publish_component_changes(usage_key: LibraryUsageLocatorV2, user_id: int):

0 commit comments

Comments
 (0)