-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: changed message object to dictionary when sending ACE_MESSAGE_S…
…ENT signal (#306)
- Loading branch information
1 parent
da4da56
commit d882dbd
Showing
5 changed files
with
101 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
""" | ||
Tests for the utils/signals module. | ||
""" | ||
from django.test import TestCase | ||
|
||
from edx_ace.utils.signals import make_serializable_object | ||
|
||
|
||
class TestMakeSerializableObject(TestCase): | ||
def test_primitive_types(self): | ||
self.assertEqual(make_serializable_object(42), 42) | ||
self.assertEqual(make_serializable_object(3.14), 3.14) | ||
self.assertEqual(make_serializable_object("string"), "string") | ||
self.assertEqual(make_serializable_object(True), True) | ||
self.assertEqual(make_serializable_object(None), None) | ||
|
||
def test_dict(self): | ||
input_dict = { | ||
"int": 1, | ||
"float": 2.0, | ||
"str": "test", | ||
"bool": False, | ||
"none": None, | ||
"list": [1, 2, 3], | ||
"nested_dict": {"key": "value"} | ||
} | ||
self.assertEqual(make_serializable_object(input_dict), input_dict) | ||
|
||
def test_list(self): | ||
input_list = [1, 2.0, "test", False, None, [1, 2, 3], {"key": "value"}] | ||
self.assertEqual(make_serializable_object(input_list), input_list) | ||
|
||
def test_non_serializable(self): | ||
class NonSerializable: | ||
pass | ||
|
||
obj = NonSerializable() | ||
self.assertEqual(make_serializable_object(obj), str(obj)) | ||
|
||
def test_non_serializable_list(self): | ||
class NonSerializable: | ||
pass | ||
|
||
obj = NonSerializable() | ||
obj2 = NonSerializable() | ||
obj_list = [obj, obj2] | ||
self.assertEqual(make_serializable_object(obj_list), [str(obj), str(obj2)]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
""" | ||
Utils for signals. | ||
""" | ||
from edx_ace.signals import ACE_MESSAGE_SENT | ||
|
||
|
||
def make_serializable_object(obj): | ||
""" | ||
Takes a dictionary/list and returns a dictionary/list with all the values converted | ||
to JSON serializable objects. | ||
""" | ||
try: | ||
if isinstance(obj, (int, float, str, bool)) or obj is None: | ||
return obj | ||
elif isinstance(obj, dict): | ||
return {key: make_serializable_object(value) for key, value in obj.items()} | ||
elif isinstance(obj, list): | ||
return [make_serializable_object(element) for element in obj] | ||
except Exception: # pylint: disable=broad-except | ||
pass | ||
return str(obj) | ||
|
||
|
||
def send_ace_message_sent_signal(channel, message): | ||
""" | ||
Creates dictionary from message, makes it JSON serializable and | ||
sends the ACE_MESSAGE_SENT signal. | ||
""" | ||
try: | ||
channel_name = channel.__class__.__name__ | ||
except AttributeError: | ||
channel_name = 'Other' | ||
data = { | ||
'name': message.name, | ||
'app_label': message.app_label, | ||
'recipient': { | ||
'email': getattr(message.recipient, 'email_address', ''), | ||
'user_id': getattr(message.recipient, 'lms_user_id', ''), | ||
}, | ||
'channel': channel_name, | ||
'context': message.context, | ||
'options': message.options, | ||
'uuid': str(message.uuid), | ||
'send_uuid': str(message.send_uuid), | ||
} | ||
ACE_MESSAGE_SENT.send(sender=channel, message=make_serializable_object(data)) |