Skip to content

Commit

Permalink
refactor: use django signals to trigger LIBRARY_COLLECTION events
Browse files Browse the repository at this point in the history
  • Loading branch information
pomegranited committed Sep 20, 2024
1 parent cf3673e commit 21393f1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 33 deletions.
33 changes: 2 additions & 31 deletions openedx/core/djangoapps/content_libraries/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
ContentLibraryData,
ContentObjectChangedData,
LibraryBlockData,
LibraryCollectionData,
)
from openedx_events.content_authoring.signals import (
CONTENT_OBJECT_ASSOCIATIONS_CHANGED,
Expand All @@ -91,8 +90,6 @@
LIBRARY_BLOCK_CREATED,
LIBRARY_BLOCK_DELETED,
LIBRARY_BLOCK_UPDATED,
LIBRARY_COLLECTION_CREATED,
LIBRARY_COLLECTION_UPDATED,
)
from openedx_learning.api import authoring as authoring_api
from openedx_learning.api.authoring_models import Collection, Component, MediaType, LearningPackage, PublishableEntity
Expand Down Expand Up @@ -1103,8 +1100,7 @@ def create_library_collection(
content_library: ContentLibrary | None = None,
) -> Collection:
"""
Creates a Collection in the given ContentLibrary,
and emits a LIBRARY_COLLECTION_CREATED event.
Creates a Collection in the given ContentLibrary.
If you've already fetched a ContentLibrary for the given library_key, pass it in here to avoid refetching.
"""
Expand All @@ -1125,14 +1121,6 @@ def create_library_collection(
except IntegrityError as err:
raise LibraryCollectionAlreadyExists from err

# Emit event for library collection created
LIBRARY_COLLECTION_CREATED.send_event(
library_collection=LibraryCollectionData(
library_key=library_key,
collection_key=collection.key,
)
)

return collection


Expand All @@ -1146,8 +1134,7 @@ def update_library_collection(
content_library: ContentLibrary | None = None,
) -> Collection:
"""
Creates a Collection in the given ContentLibrary,
and emits a LIBRARY_COLLECTION_CREATED event.
Updates a Collection in the given ContentLibrary.
"""
if not content_library:
content_library = ContentLibrary.objects.get_by_key(library_key) # type: ignore[attr-defined]
Expand All @@ -1165,14 +1152,6 @@ def update_library_collection(
except Collection.DoesNotExist as exc:
raise ContentLibraryCollectionNotFound from exc

# Emit event for library collection updated
LIBRARY_COLLECTION_UPDATED.send_event(
library_collection=LibraryCollectionData(
library_key=library_key,
collection_key=collection.key,
)
)

return collection


Expand Down Expand Up @@ -1243,14 +1222,6 @@ def update_library_collection_components(
created_by=created_by,
)

# Emit event for library collection updated
LIBRARY_COLLECTION_UPDATED.send_event(
library_collection=LibraryCollectionData(
library_key=library_key,
collection_key=collection.key,
)
)

# Emit a CONTENT_OBJECT_ASSOCIATIONS_CHANGED event for each of the objects added/removed
for usage_key in usage_keys:
CONTENT_OBJECT_ASSOCIATIONS_CHANGED.send_event(
Expand Down
42 changes: 40 additions & 2 deletions openedx/core/djangoapps/content_libraries/signal_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,23 @@
import logging

from django.conf import settings
from django.db.models.signals import post_save
from django.dispatch import receiver

from lms.djangoapps.grades.api import signals as grades_signals
from opaque_keys import InvalidKeyError # lint-amnesty, pylint: disable=wrong-import-order
from opaque_keys.edx.locator import LibraryUsageLocatorV2 # lint-amnesty, pylint: disable=wrong-import-order
from openedx_events.content_authoring.data import (
LibraryCollectionData,
)
from openedx_events.content_authoring.signals import (
LIBRARY_COLLECTION_CREATED,
LIBRARY_COLLECTION_UPDATED,
)
from openedx_learning.api.authoring_models import Collection

from lms.djangoapps.grades.api import signals as grades_signals

from .models import LtiGradedResource
from .models import ContentLibrary, LtiGradedResource


log = logging.getLogger(__name__)
Expand Down Expand Up @@ -55,3 +65,31 @@ def score_changed_handler(sender, **kwargs): # pylint: disable=unused-argument
resource.update_score(weighted_earned, weighted_possible, modified)
log.info("LTI 1.3: Score Signal: Grade upgraded: resource; %s",
resource)


@receiver(post_save, sender=Collection, dispatch_uid="library_collection_saved")
def library_collection_saved(sender, instance, created, **kwargs):
"""
Raises LIBRARY_COLLECTION_CREATED if the Collection is new,
or LIBRARY_COLLECTION_UPDATED if updated an existing Collection.
"""
try:
library = ContentLibrary.objects.get(learning_package_id=instance.learning_package_id)
except ContentLibrary.DoesNotExist:
log.error("{instance} is not associated with a content library.")
return

if created:
LIBRARY_COLLECTION_CREATED.send_event(
library_collection=LibraryCollectionData(
library_key=library.library_key,
collection_key=instance.key,
)
)
else:
LIBRARY_COLLECTION_UPDATED.send_event(
library_collection=LibraryCollectionData(
library_key=library.library_key,
collection_key=instance.key,
)
)

0 comments on commit 21393f1

Please sign in to comment.