Skip to content

Commit

Permalink
feat: incorporate changes in 8.7.0 and add signal hander
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelRoytman committed Oct 2, 2023
1 parent 2c8cd5a commit 1403167
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 23 deletions.
3 changes: 2 additions & 1 deletion edx_exams/apps/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ def update_attempt_status(attempt_id, to_status):
emit_exam_attempt_submitted_event(
attempt_obj.user,
course_key,
usage_key
usage_key,
attempt_obj.exam.exam_type
)

attempt_obj.status = to_status
Expand Down
18 changes: 18 additions & 0 deletions edx_exams/apps/core/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
Core Application Configuration
"""

from django.apps import AppConfig


class CoreConfig(AppConfig):
"""
Application configuration for core application.
"""
name = 'edx_exams.apps.core'

def ready(self):
"""
Connect handlers to signals.
"""
from .signals import handlers # pylint: disable=unused-import,import-outside-toplevel
Empty file.
20 changes: 20 additions & 0 deletions edx_exams/apps/core/signals/handlers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""
Signal handlers for the edx-exams application.
"""
from django.dispatch import receiver

from openedx_events.event_bus import get_producer
from openedx_events.learning.signals import EXAM_ATTEMPT_SUBMITTED

@receiver(EXAM_ATTEMPT_SUBMITTED)
def listen_for_exam_attempt_submitted(sender, signal, **kwargs): # pylint: disable=unused-argument
"""
Publish EXAM_ATTEMPT_SUBMITTED signals onto the event bus.
"""
get_producer().send(
signal=EXAM_ATTEMPT_SUBMITTED,
topic='exam-attempt-submitted',
event_key_field='exam_attempt.course_key',
event_data={'exam_attempt': kwargs['exam_attempt']},
event_metadata=kwargs['metadata'],
)
32 changes: 22 additions & 10 deletions edx_exams/apps/core/signals/signals.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
"""
Signal definitions and functions to send those signals for the edx-exams application.
"""

from openedx_events.learning.data import ExamAttemptData, UserData, UserPersonalData
from openedx_events.learning.signals import EXAM_ATTEMPT_SUBMITTED


def emit_exam_attempt_submitted_event(user, course_key, usage_key):
def emit_exam_attempt_submitted_event(user, course_key, usage_key, exam_type):
"""
Emit the EXAM_ATTEMPT_SUBMITTED Open edX event.
"""
name=user.full_name or ''
user_data = UserData(
id=user.id,
is_active=user.is_active,
pii=UserPersonalData(
username=user.username,
email=user.email,
name=name
)
)

# .. event_implemented_name: EXAM_ATTEMPT_SUBMITTED
EXAM_ATTEMPT_SUBMITTED.send_event(
exam_attempt=ExamAttemptData(
student_user=UserData(
id=user.id,
is_active=user.is_active,
pii=UserPersonalData(
username=user.username,
email=user.email,
name=user.full_name
)
),
student_user=user_data,
course_key=course_key,
usage_key=usage_key,
exam_type=exam_type,
requesting_user=user_data
)
)
26 changes: 14 additions & 12 deletions edx_exams/apps/core/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,25 +307,27 @@ def test_submit_attempt_event_emitted(self, mock_event_send):
update_attempt_status(self.exam_attempt.id, ExamAttemptStatus.submitted)
self.assertEqual(mock_event_send.call_count, 1)

usage_key = UsageKey.from_string(self.exam.content_id)
user_data = UserData(
id=self.user.id,
is_active=self.user.is_active,
pii=UserPersonalData(
username=self.user.username,
email=self.user.email,
name=self.user.full_name
)
)
course_key = CourseKey.from_string(self.exam.course_id)
usage_key = UsageKey.from_string(self.exam.content_id)

expected_data = ExamAttemptData(
student_user=UserData(
id=self.user.id,
is_active=self.user.is_active,
pii=UserPersonalData(
username=self.user.username,
email=self.user.email,
name=self.user.full_name
)
),
student_user=user_data,
course_key=course_key,
usage_key=usage_key
usage_key=usage_key,
exam_type=self.exam.exam_type,
requesting_user=user_data,
)
mock_event_send.assert_called_with(exam_attempt=expected_data)


def test_illegal_start(self):
"""
Test that an already started exam cannot be started
Expand Down

0 comments on commit 1403167

Please sign in to comment.