Skip to content

Commit 845375f

Browse files
feat: update content libraries API to use upstream entity changed events
1 parent 4007eb5 commit 845375f

9 files changed

Lines changed: 426 additions & 498 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 (
@@ -249,29 +238,6 @@ def set_library_block_olx(usage_key: LibraryUsageLocatorV2, new_olx_str: str) ->
249238
created=now,
250239
)
251240

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

277243

@@ -348,16 +314,6 @@ def create_library_block(
348314
_create_component_for_block(content_library, usage_key, user_id, can_stand_alone)
349315

350316
# Now return the metadata about the new block:
351-
352-
# .. event_implemented_name: LIBRARY_BLOCK_CREATED
353-
# .. event_type: org.openedx.content_authoring.library_block.created.v1
354-
LIBRARY_BLOCK_CREATED.send_event(
355-
library_block=LibraryBlockData(
356-
library_key=content_library.library_key,
357-
usage_key=usage_key
358-
)
359-
)
360-
361317
return get_library_block(usage_key)
362318

363319

@@ -490,16 +446,6 @@ def _import_staged_block(
490446
key=filename,
491447
)
492448

493-
# Emit library block created event
494-
# .. event_implemented_name: LIBRARY_BLOCK_CREATED
495-
# .. event_type: org.openedx.content_authoring.library_block.created.v1
496-
transaction.on_commit(lambda: LIBRARY_BLOCK_CREATED.send_event(
497-
library_block=LibraryBlockData(
498-
library_key=content_library.library_key,
499-
usage_key=usage_key
500-
)
501-
))
502-
503449
# Now return the metadata about the new block
504450
return get_library_block(usage_key)
505451

@@ -701,16 +647,6 @@ def delete_library_block(
701647
"""
702648
library_key = usage_key.context_key
703649

704-
def send_block_deleted_signal():
705-
# .. event_implemented_name: LIBRARY_BLOCK_DELETED
706-
# .. event_type: org.openedx.content_authoring.library_block.deleted.v1
707-
LIBRARY_BLOCK_DELETED.send_event(
708-
library_block=LibraryBlockData(
709-
library_key=library_key,
710-
usage_key=usage_key
711-
)
712-
)
713-
714650
try:
715651
component = get_component_from_usage_key(usage_key)
716652
except Component.DoesNotExist:
@@ -719,78 +655,29 @@ def send_block_deleted_signal():
719655
# (an intermediate error occurred).
720656
# In that case, we keep the index updated by removing the entry,
721657
# but still raise the error so the caller knows the component did not exist.
722-
send_block_deleted_signal()
723-
raise
724658

725-
affected_containers = get_containers_contains_item(usage_key)
659+
# .. event_implemented_name: LIBRARY_BLOCK_DELETED
660+
# .. event_type: org.openedx.content_authoring.library_block.deleted.v1
661+
LIBRARY_BLOCK_DELETED.send_event(
662+
library_block=LibraryBlockData(library_key=library_key, usage_key=usage_key)
663+
)
664+
raise
726665

727666
content_api.soft_delete_draft(component.id, deleted_by=user_id)
728667

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

746669
def restore_library_block(usage_key: LibraryUsageLocatorV2, user_id: int | None = None) -> None:
747670
"""
748671
Restore the specified library block.
749672
"""
750673
component = get_component_from_usage_key(usage_key)
751-
library_key = usage_key.context_key
752-
753674
# Set draft version back to the latest available component version id.
754675
content_api.set_draft_version(
755676
component.id,
756677
component.versioning.latest.pk,
757678
set_by=user_id,
758679
)
759680

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

795682
def get_library_block_static_asset_files(usage_key: LibraryUsageLocatorV2) -> list[LibraryXBlockStaticFile]:
796683
"""
@@ -876,16 +763,6 @@ def add_library_block_static_asset_file(
876763
created=datetime.now(tz=timezone.utc), # noqa: UP017
877764
created_by=user.id if user else None,
878765
)
879-
transaction.on_commit(
880-
# .. event_implemented_name: LIBRARY_BLOCK_UPDATED
881-
# .. event_type: org.openedx.content_authoring.library_block.updated.v1
882-
lambda: LIBRARY_BLOCK_UPDATED.send_event(
883-
library_block=LibraryBlockData(
884-
library_key=usage_key.context_key,
885-
usage_key=usage_key,
886-
)
887-
)
888-
)
889766

890767
# Now figure out the URL for the newly created asset...
891768
site_root_url = get_xblock_app_config().get_site_root_url()
@@ -924,16 +801,6 @@ def delete_library_block_static_asset_file(usage_key, file_path, user=None):
924801
created=now,
925802
created_by=user.id if user else None,
926803
)
927-
transaction.on_commit(
928-
# .. event_implemented_name: LIBRARY_BLOCK_UPDATED
929-
# .. event_type: org.openedx.content_authoring.library_block.updated.v1
930-
lambda: LIBRARY_BLOCK_UPDATED.send_event(
931-
library_block=LibraryBlockData(
932-
library_key=usage_key.context_key,
933-
usage_key=usage_key,
934-
)
935-
)
936-
)
937804

938805

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

0 commit comments

Comments
 (0)