Skip to content

Commit

Permalink
chore: add changes from openedx/master (#2556)
Browse files Browse the repository at this point in the history
* refactor: add protected utility to send events and update docs

* fix: update code because of quality check

* docs: add docstrings for tests

* fix: correct course key for ccx event

* style: [ACI-972] fix lint errors

---------

Co-authored-by: Glib Glugovskiy <[email protected]>
Co-authored-by: Glib Glugovskiy <[email protected]>
Co-authored-by: Andrii <[email protected]>
  • Loading branch information
4 people authored May 14, 2024
1 parent 0095376 commit df3baf8
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 64 deletions.
3 changes: 2 additions & 1 deletion cms/envs/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2711,6 +2711,7 @@
def _should_send_learning_badge_events(settings):
return settings.FEATURES['BADGES_ENABLED']


# .. setting_name: EVENT_BUS_PRODUCER_CONFIG
# .. setting_default: all events disabled
# .. setting_description: Dictionary of event_types mapped to dictionaries of topic to topic-related configuration.
Expand All @@ -2730,7 +2731,7 @@ def _should_send_learning_badge_events(settings):
},
"org.openedx.learning.ccx.course.passing.status.updated.v1": {
"learning-badges-lifecycle": {
"event_key_field": "course_passing_status.course.course_key",
"event_key_field": "course_passing_status.course.ccx_course_key",
"enabled": _should_send_learning_badge_events,
},
},
Expand Down
49 changes: 0 additions & 49 deletions lms/djangoapps/grades/event_utils.py

This file was deleted.

66 changes: 59 additions & 7 deletions lms/djangoapps/grades/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
from crum import get_current_user
from django.conf import settings
from eventtracking import tracker
from openedx_events.learning.data import (
CcxCourseData,
CcxCoursePassingStatusData,
CourseData,
CoursePassingStatusData,
UserData,
UserPersonalData
)
from openedx_events.learning.signals import CCX_COURSE_PASSING_STATUS_UPDATED, COURSE_PASSING_STATUS_UPDATED

from common.djangoapps.course_modes.models import CourseMode
from common.djangoapps.student.models import CourseEnrollment
Expand All @@ -16,7 +25,6 @@
get_event_transaction_type,
set_event_transaction_type
)
from lms.djangoapps.grades.event_utils import emit_course_passing_status_update
from lms.djangoapps.grades.signals.signals import SCHEDULE_FOLLOW_UP_SEGMENT_EVENT_FOR_COURSE_PASSED_FIRST_TIME
from openedx.core.djangoapps.content.course_overviews.models import CourseOverview
from openedx.features.enterprise_support.context import get_enterprise_event_context
Expand Down Expand Up @@ -175,8 +183,8 @@ def course_grade_passed_first_time(user_id, course_id):

def course_grade_now_passed(user, course_id):
"""
Emits an edx.course.grade.now_passed event
with data from the course and user passed now .
Emits an edx.course.grade.now_passed and passing status updated events
with data from the course and user passed now.
"""
event_name = COURSE_GRADE_NOW_PASSED_EVENT_TYPE
context = contexts.course_context_from_course_id(course_id)
Expand All @@ -191,13 +199,13 @@ def course_grade_now_passed(user, course_id):
}
)

emit_course_passing_status_update(user, course_id, is_passing=True)
_emit_course_passing_status_update(user, course_id, is_passing=True)


def course_grade_now_failed(user, course_id):
"""
Emits an edx.course.grade.now_failed event
with data from the course and user failed now .
Emits an edx.course.grade.now_failed and passing status updated events
with data from the course and user failed now.
"""
event_name = COURSE_GRADE_NOW_FAILED_EVENT_TYPE
context = contexts.course_context_from_course_id(course_id)
Expand All @@ -212,7 +220,7 @@ def course_grade_now_failed(user, course_id):
}
)

emit_course_passing_status_update(user, course_id, is_passing=False)
_emit_course_passing_status_update(user, course_id, is_passing=False)


def fire_segment_event_on_course_grade_passed_first_time(user_id, course_locator):
Expand Down Expand Up @@ -263,3 +271,47 @@ def fire_segment_event_on_course_grade_passed_first_time(user_id, course_locator
)

log.info("Segment event fired for passed learners. Event: [{}], Data: [{}]".format(event_name, event_properties))


def _emit_course_passing_status_update(user, course_id, is_passing):
"""
Emit course passing status event according to the course type.
The status of event is determined by is_passing parameter.
"""
if hasattr(course_id, 'ccx'):
CCX_COURSE_PASSING_STATUS_UPDATED.send_event(
course_passing_status=CcxCoursePassingStatusData(
is_passing=is_passing,
user=UserData(
pii=UserPersonalData(
username=user.username,
email=user.email,
name=user.get_full_name(),
),
id=user.id,
is_active=user.is_active,
),
course=CcxCourseData(
ccx_course_key=course_id,
master_course_key=course_id.to_course_locator(),
),
)
)
else:
COURSE_PASSING_STATUS_UPDATED.send_event(
course_passing_status=CoursePassingStatusData(
is_passing=is_passing,
user=UserData(
pii=UserPersonalData(
username=user.username,
email=user.email,
name=user.get_full_name(),
),
id=user.id,
is_active=user.is_active,
),
course=CourseData(
course_key=course_id,
),
)
)
24 changes: 18 additions & 6 deletions lms/djangoapps/grades/tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ def test_persistent_grade_event_emitted(self):
)


class CoursePassingStatusEventsTest(SharedModuleStoreTestCase, OpenEdxEventsTestMixin): # pylint: disable=missing-class-docstring
class CoursePassingStatusEventsTest(SharedModuleStoreTestCase, OpenEdxEventsTestMixin):
"""
Tests for Open edX passing status update event.
"""
ENABLED_OPENEDX_EVENTS = [
"org.openedx.learning.course.passing.status.updated.v1",
]
Expand All @@ -124,19 +127,22 @@ def setUpClass(cls):
super().setUpClass()
cls.start_events_isolation()

def setUp(self): # pylint: disable=arguments-differ
def setUp(self):
super().setUp()
self.course = CourseFactory.create()
self.user = UserFactory.create()
self.receiver_called = False

def _event_receiver_side_effect(self, **kwargs): # pylint: disable=unused-argument
def _event_receiver_side_effect(self, **kwargs):
"""
Used show that the Open edX Event was called by the Django signal handler.
"""
self.receiver_called = True

def test_course_passing_status_updated_emitted(self):
"""
Test whether passing status updated event is sent after the grade is being updated for a user.
"""
event_receiver = mock.Mock(side_effect=self._event_receiver_side_effect)
COURSE_PASSING_STATUS_UPDATED.connect(event_receiver)
grade_factory = CourseGradeFactory()
Expand Down Expand Up @@ -169,9 +175,12 @@ def test_course_passing_status_updated_emitted(self):
)


class CCXCoursePassingStatusEventsTest( # pylint: disable=missing-class-docstring
class CCXCoursePassingStatusEventsTest(
SharedModuleStoreTestCase, OpenEdxEventsTestMixin
):
"""
Tests for Open edX passing status update event in a CCX course.
"""
ENABLED_OPENEDX_EVENTS = [
"org.openedx.learning.ccx.course.passing.status.updated.v1",
]
Expand All @@ -184,7 +193,7 @@ def setUpClass(cls):
super().setUpClass()
cls.start_events_isolation()

def setUp(self): # pylint: disable=arguments-differ
def setUp(self):
super().setUp()
self.course = CourseFactory.create()
self.user = UserFactory.create()
Expand All @@ -197,13 +206,16 @@ def setUp(self): # pylint: disable=arguments-differ

self.receiver_called = False

def _event_receiver_side_effect(self, **kwargs): # pylint: disable=unused-argument
def _event_receiver_side_effect(self, **kwargs):
"""
Used show that the Open edX Event was called by the Django signal handler.
"""
self.receiver_called = True

def test_ccx_course_passing_status_updated_emitted(self):
"""
Test whether passing status updated event is sent after the grade is being updated in CCX course.
"""
event_receiver = mock.Mock(side_effect=self._event_receiver_side_effect)
CCX_COURSE_PASSING_STATUS_UPDATED.connect(event_receiver)
grade_factory = CourseGradeFactory()
Expand Down
2 changes: 1 addition & 1 deletion lms/envs/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5298,7 +5298,7 @@ def _should_send_learning_badge_events(settings):
},
"org.openedx.learning.ccx.course.passing.status.updated.v1": {
"learning-badges-lifecycle": {
"event_key_field": "course_passing_status.course.course_key",
"event_key_field": "course_passing_status.course.ccx_course_key",
"enabled": _should_send_learning_badge_events,
},
},
Expand Down

0 comments on commit df3baf8

Please sign in to comment.