From 0095376b9c24b7f4fd8f8b2f34959979388c6caf Mon Sep 17 00:00:00 2001 From: andrii-hantkovskyi <131773947+andrii-hantkovskyi@users.noreply.github.com> Date: Tue, 7 May 2024 20:09:43 +0300 Subject: [PATCH] refactor: [ACI-972] extract event bus event to another function (#2552) Co-authored-by: Andrii --- lms/djangoapps/grades/event_utils.py | 49 ++++++++++++ lms/djangoapps/grades/events.py | 88 +--------------------- lms/djangoapps/grades/tests/test_events.py | 4 +- 3 files changed, 54 insertions(+), 87 deletions(-) create mode 100644 lms/djangoapps/grades/event_utils.py diff --git a/lms/djangoapps/grades/event_utils.py b/lms/djangoapps/grades/event_utils.py new file mode 100644 index 000000000000..8e4df28c846b --- /dev/null +++ b/lms/djangoapps/grades/event_utils.py @@ -0,0 +1,49 @@ +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 + + +def emit_course_passing_status_update(user, course_id, is_passing): + 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, + ), + ) + ) diff --git a/lms/djangoapps/grades/events.py b/lms/djangoapps/grades/events.py index 538f3b34eef3..aada530cb7d6 100644 --- a/lms/djangoapps/grades/events.py +++ b/lms/djangoapps/grades/events.py @@ -6,15 +6,6 @@ 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 @@ -25,6 +16,7 @@ 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 @@ -199,44 +191,7 @@ def course_grade_now_passed(user, course_id): } ) - # produce to event bus - if hasattr(course_id, 'ccx'): - CCX_COURSE_PASSING_STATUS_UPDATED.send_event( - course_passing_status=CcxCoursePassingStatusData( - status=CcxCoursePassingStatusData.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( - status=CoursePassingStatusData.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, - ), - ) - ) + emit_course_passing_status_update(user, course_id, is_passing=True) def course_grade_now_failed(user, course_id): @@ -257,44 +212,7 @@ def course_grade_now_failed(user, course_id): } ) - # produce to event bus - if hasattr(course_id, 'ccx'): - CCX_COURSE_PASSING_STATUS_UPDATED.send_event( - course_passing_status=CcxCoursePassingStatusData( - status=CcxCoursePassingStatusData.FAILING, - 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( - status=CoursePassingStatusData.FAILING, - 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, - ), - ) - ) + 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): diff --git a/lms/djangoapps/grades/tests/test_events.py b/lms/djangoapps/grades/tests/test_events.py index 906b0d23ee00..aefe55fd423d 100644 --- a/lms/djangoapps/grades/tests/test_events.py +++ b/lms/djangoapps/grades/tests/test_events.py @@ -150,7 +150,7 @@ def test_course_passing_status_updated_emitted(self): "signal": COURSE_PASSING_STATUS_UPDATED, "sender": None, "course_passing_status": CoursePassingStatusData( - status=CoursePassingStatusData.PASSING, + is_passing=True, user=UserData( pii=UserPersonalData( username=self.user.username, @@ -217,7 +217,7 @@ def test_ccx_course_passing_status_updated_emitted(self): "signal": CCX_COURSE_PASSING_STATUS_UPDATED, "sender": None, "course_passing_status": CcxCoursePassingStatusData( - status=CcxCoursePassingStatusData.PASSING, + is_passing=True, user=UserData( pii=UserPersonalData( username=self.user.username,