Skip to content

Commit

Permalink
feat: add course created event signals
Browse files Browse the repository at this point in the history
  • Loading branch information
JuanDavidBuitrago committed Nov 30, 2023
1 parent c2428f8 commit 9bb85f6
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 4 deletions.
11 changes: 11 additions & 0 deletions lms/djangoapps/ccx/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
from django.shortcuts import redirect
from django.urls import reverse
from django.utils.translation import gettext as _
from django.utils.timezone import timezone
from django.views.decorators.cache import cache_control
from django.views.decorators.csrf import ensure_csrf_cookie
from opaque_keys.edx.keys import CourseKey
from six import StringIO

from openedx_events.content_authoring.data import CourseData
from openedx_events.content_authoring.signals import COURSE_CREATED
from common.djangoapps.edxmako.shortcuts import render_to_response
from common.djangoapps.student.models import CourseEnrollment
from common.djangoapps.student.roles import CourseCcxCoachRole
Expand Down Expand Up @@ -301,6 +304,14 @@ def create_ccx(request, course, ccx=None):
user=request.user,
)

# .. event_implemented_name: COURSE_CREATED
COURSE_CREATED.send_event(
time=datetime.datetime.now(tz=timezone.utc),
course=CourseData(
course_key=ccx_id,
)
)

return redirect(url)


Expand Down
2 changes: 1 addition & 1 deletion requirements/constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,4 @@ pytz==2022.2.1

# openedx-events>0.13.0 causes test failures due to breaking changes
# These broken tests will be fixed in a separate PR
openedx-events==0.13.0
openedx-events==8.3.0
2 changes: 1 addition & 1 deletion requirements/edx/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ openedx-calc==3.0.1
# via -r requirements/edx/base.in
openedx-django-pyfs==3.2.1
# via xblock
openedx-events==0.13.0
openedx-events==8.3.0
# via
# -c requirements/edx/../constraints.txt
# -r requirements/edx/base.in
Expand Down
2 changes: 1 addition & 1 deletion requirements/edx/development.txt
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,7 @@ openedx-django-pyfs==3.2.1
# via
# -r requirements/edx/testing.txt
# xblock
openedx-events==0.13.0
openedx-events==8.3.0
# via
# -c requirements/edx/../constraints.txt
# -r requirements/edx/testing.txt
Expand Down
2 changes: 1 addition & 1 deletion requirements/edx/testing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ openedx-django-pyfs==3.2.1
# via
# -r requirements/edx/base.txt
# xblock
openedx-events==0.13.0
openedx-events==8.3.0
# via
# -c requirements/edx/../constraints.txt
# -r requirements/edx/base.txt
Expand Down
12 changes: 12 additions & 0 deletions xmodule/modulestore/mixed.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
from opaque_keys.edx.keys import CourseKey
from opaque_keys.edx.locator import LibraryLocator

from openedx_events.content_authoring.data import CourseData
from openedx_events.content_authoring.signals import COURSE_CREATED

from django.utils.timezone import datetime, timezone
from xmodule.assetstore import AssetMetadata

from . import XMODULE_FIELDS_WITH_USAGE_KEYS, ModuleStoreWriteBase
Expand Down Expand Up @@ -667,6 +671,14 @@ def create_course(self, org, course, run, user_id, **kwargs): # lint-amnesty, p
# add new course to the mapping
self.mappings[course_key] = store

# .. event_implemented_name: COURSE_CREATED
COURSE_CREATED.send_event(
time=datetime.now(timezone.utc),
course=CourseData(
course_key=course_key,
)
)

return course

@strip_key
Expand Down
27 changes: 27 additions & 0 deletions xmodule/modulestore/tests/test_mixed_modulestore.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from unittest.mock import Mock, call, patch

import ddt
from openedx_events.content_authoring.signals import COURSE_CREATED
import pymongo
import pytest
# Mixed modulestore depends on django, so we'll manually configure some django settings
Expand Down Expand Up @@ -109,6 +110,9 @@ class CommonMixedModuleStoreSetup(CourseComparisonTest):
],
'xblock_mixins': modulestore_options['xblock_mixins'],
}
ENABLED_OPENEDX_EVENTS = [
"org.openedx.content_authoring.course.created.v1",
]

def setUp(self):
"""
Expand Down Expand Up @@ -724,6 +728,29 @@ def test_publish_automatically_after_delete_unit(self, default_ms):
self.store.delete_item(vertical.location, self.user_id)
assert not self._has_changes(sequential.location)

@ddt.data(ModuleStoreEnum.Type.mongo, ModuleStoreEnum.Type.split)
def test_course_create_event(self, default_ms):
"""
Check that COURSE_CREATED event is sent when a course is created.
"""
self.initdb(default_ms)
event_receiver = Mock()
COURSE_CREATED.connect(event_receiver)

test_course = self.store.create_course('test_org', 'test_course', 'test_run', self.user_id)

event_receiver.assert_called_once()
self.assertDictContainsSubset(
{
"signal": COURSE_CREATED,
"sender": None,
"course": CourseData(
course_key=test_course.id,
),
},
event_receiver.call_args.kwargs,
)

def setup_has_changes(self, default_ms):
"""
Common set up for has_changes tests below.
Expand Down

0 comments on commit 9bb85f6

Please sign in to comment.