Skip to content

Commit

Permalink
feat: Add events emitting for Collections
Browse files Browse the repository at this point in the history
  • Loading branch information
yusuf-musleh committed Aug 26, 2024
1 parent 743291c commit 3da44d5
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 5 deletions.
12 changes: 12 additions & 0 deletions docs/hooks/events.rst
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,15 @@ Content Authoring Events
* - `CONTENT_OBJECT_TAGS_CHANGED <https://github.com/openedx/openedx-events/blob/c0eb4ba1a3d7d066d58e5c87920b8ccb0645f769/openedx_events/content_authoring/signals.py#L207>`_
- org.openedx.content_authoring.content.object.tags.changed.v1
- 2024-03-31

* - `LIBRARY_COLLECTION_CREATED <https://github.com/openedx/openedx-events/blob/main/openedx_events/content_authoring/signals.py#L219>`_
- org.openedx.content_authoring.content.library.collection.created.v1
- 2024-08-23

* - `LIBRARY_COLLECTION_UPDATED <https://github.com/openedx/openedx-events/blob/main/openedx_events/content_authoring/signals.py#L230>`_
- org.openedx.content_authoring.content.library.collection.updated.v1
- 2024-08-23

* - `LIBRARY_COLLECTION_DELETED <https://github.com/openedx/openedx-events/blob/main/openedx_events/content_authoring/signals.py#L241>`_
- org.openedx.content_authoring.content.library.collection.deleted.v1
- 2024-08-23
1 change: 1 addition & 0 deletions openedx/core/djangoapps/content_libraries/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ def ready(self):
Import signal handler's module to ensure they are registered.
"""
from . import signal_handlers # pylint: disable=unused-import
from .collections import handlers # pylint: disable=unused-import
Empty file.
57 changes: 57 additions & 0 deletions openedx/core/djangoapps/content_libraries/collections/handlers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""
Signal handlers for Content Library Collections.
"""

import logging

from django.dispatch import receiver
from openedx_events.content_authoring.data import LibraryCollectionData
from openedx_events.content_authoring.signals import (
LIBRARY_COLLECTION_CREATED,
LIBRARY_COLLECTION_UPDATED,
LIBRARY_COLLECTION_DELETED,
)


log = logging.getLogger(__name__)


@receiver(LIBRARY_COLLECTION_CREATED)
def library_collection_created_handler(**kwargs):
"""
Content Library Collection Created signal handler
"""
library_collection_data = kwargs.get("library_collection", None)
if not library_collection_data or not isinstance(library_collection_data, LibraryCollectionData):
log.error("Received null or incorrect data for event")
return

log.info("Received Collection Created Signal")

# TODO: Implement handler logic


@receiver(LIBRARY_COLLECTION_UPDATED)
def library_collection_updated_handler(**kwargs):
"""
Content Library Collection Updated signal handler
"""
library_collection_data = kwargs.get("library_collection", None)
if not library_collection_data or not isinstance(library_collection_data, LibraryCollectionData):
log.error("Received null or incorrect data for event")
return

log.info("Received Collection Updated Signal")


@receiver(LIBRARY_COLLECTION_DELETED)
def library_collection_deleted_handler(**kwargs):
"""
Content Library Collection Deleted signal handler
"""
library_collection_data = kwargs.get("library_collection", None)
if not library_collection_data or not isinstance(library_collection_data, LibraryCollectionData):
log.error("Received null or incorrect data for event")
return

log.info("Received Collection Deleted Signal")
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@

from opaque_keys.edx.locator import LibraryLocatorV2

from openedx_events.content_authoring.data import LibraryCollectionData
from openedx_events.content_authoring.signals import (
LIBRARY_COLLECTION_CREATED,
LIBRARY_COLLECTION_UPDATED,
)

from openedx.core.djangoapps.content_libraries import api, permissions
from openedx.core.djangoapps.content_libraries.serializers import (
ContentLibraryCollectionSerializer,
Expand Down Expand Up @@ -104,6 +110,15 @@ def create(self, request, lib_key_str):
create_serializer.validated_data["description"]
)
serializer = self.get_serializer(collection)

# Emit event for library content collection creation
LIBRARY_COLLECTION_CREATED.send_event(
library_collection=LibraryCollectionData(
library_key=library_key,
collection_id=collection.id
)
)

return Response(serializer.data)

def partial_update(self, request, lib_key_str, pk=None):
Expand All @@ -126,6 +141,15 @@ def partial_update(self, request, lib_key_str, pk=None):
update_serializer.is_valid(raise_exception=True)
updated_collection = authoring_api.update_collection(pk, **update_serializer.validated_data)
serializer = self.get_serializer(updated_collection)

# Emit event for library content collection updated
LIBRARY_COLLECTION_UPDATED.send_event(
library_collection=LibraryCollectionData(
library_key=library_key,
collection_id=collection.id
)
)

return Response(serializer.data)

def destroy(self, request, lib_key_str, pk=None):
Expand All @@ -134,4 +158,6 @@ def destroy(self, request, lib_key_str, pk=None):
Note: (currently not allowed)
"""
# TODO: Implement the deletion logic and emit event signal

return Response(None, status=HTTP_405_METHOD_NOT_ALLOWED)
2 changes: 1 addition & 1 deletion requirements/edx/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ openedx-django-require==2.1.0
# via -r requirements/edx/kernel.in
openedx-django-wiki==2.1.0
# via -r requirements/edx/kernel.in
openedx-events==9.11.0
openedx-events @ git+https://github.com/open-craft/openedx-events.git@8101eb46d15717e7d5390af0901b8314a2c7da25
# via
# -r requirements/edx/kernel.in
# edx-event-bus-kafka
Expand Down
2 changes: 1 addition & 1 deletion requirements/edx/development.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1358,7 +1358,7 @@ openedx-django-wiki==2.1.0
# via
# -r requirements/edx/doc.txt
# -r requirements/edx/testing.txt
openedx-events==9.11.0
openedx-events @ git+https://github.com/open-craft/openedx-events.git@8101eb46d15717e7d5390af0901b8314a2c7da25
# via
# -r requirements/edx/doc.txt
# -r requirements/edx/testing.txt
Expand Down
2 changes: 1 addition & 1 deletion requirements/edx/doc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ openedx-django-require==2.1.0
# via -r requirements/edx/base.txt
openedx-django-wiki==2.1.0
# via -r requirements/edx/base.txt
openedx-events==9.11.0
openedx-events @ git+https://github.com/open-craft/openedx-events.git@8101eb46d15717e7d5390af0901b8314a2c7da25
# via
# -r requirements/edx/base.txt
# edx-event-bus-kafka
Expand Down
4 changes: 3 additions & 1 deletion requirements/edx/kernel.in
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ olxcleaner
openedx-atlas # CLI tool to manage translations
openedx-calc # Library supporting mathematical calculations for Open edX
openedx-django-require
openedx-events # Open edX Events from Hooks Extension Framework (OEP-50)
# openedx-events # Open edX Events from Hooks Extension Framework (OEP-50)
# TODO: Remove this once new version released
openedx-events @ git+https://github.com/open-craft/openedx-events.git@8101eb46d15717e7d5390af0901b8314a2c7da25
openedx-filters # Open edX Filters from Hooks Extension Framework (OEP-50)
openedx-learning # Open edX Learning core (experimental)
openedx-mongodbproxy
Expand Down
2 changes: 1 addition & 1 deletion requirements/edx/testing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,7 @@ openedx-django-require==2.1.0
# via -r requirements/edx/base.txt
openedx-django-wiki==2.1.0
# via -r requirements/edx/base.txt
openedx-events==9.11.0
openedx-events @ git+https://github.com/open-craft/openedx-events.git@8101eb46d15717e7d5390af0901b8314a2c7da25
# via
# -r requirements/edx/base.txt
# edx-event-bus-kafka
Expand Down

0 comments on commit 3da44d5

Please sign in to comment.