Skip to content

Commit

Permalink
Add custom message support
Browse files Browse the repository at this point in the history
Signed-off-by: Kipchirchir Sigei <[email protected]>
  • Loading branch information
KipSigei committed Feb 9, 2023
1 parent df94f56 commit 2a6237b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
11 changes: 11 additions & 0 deletions onadata/apps/messaging/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from django.utils.translation import gettext as _
from rest_framework import exceptions, serializers

from onadata.apps.logger.models import Instance
from onadata.apps.messaging.constants import MESSAGE, MESSAGE_VERBS
from onadata.apps.messaging.utils import TargetDoesNotExist, get_target

Expand Down Expand Up @@ -151,6 +152,7 @@ def send_message(
target_type: str,
user: User,
message_verb: str,
custom_message: dict = None
):
"""
Send a message.
Expand Down Expand Up @@ -180,12 +182,21 @@ def send_message(
ids = instance_id
while len(ids) > 0:
data["message"] = json.dumps({"id": ids[:message_id_limit]})
if custom_message:
message_dict = json.loads(data["message"])
message_dict.update(custom_message)
data["message"] = json.dumps(message_dict)
message = MessageSerializer(data=data, context={"request": request})
del ids[:message_id_limit]
if message.is_valid():
message.save()
else:
data["message"] = json.dumps({"id": instance_id})
if custom_message:
# update default message dict with extra fields
message_dict = json.loads(data["message"])
message_dict.update(custom_message)
data["message"] = json.dumps(message_dict)
message = MessageSerializer(data=data, context={"request": request})
if message.is_valid():
message.save()
44 changes: 43 additions & 1 deletion onadata/apps/messaging/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
"""
Tests messaging app utils
"""
import json
from django.http.request import HttpRequest
from django.test.utils import override_settings
from unittest.mock import patch

from onadata.apps.main.tests.test_base import TestBase
from onadata.apps.messaging.serializers import send_message
from onadata.apps.messaging.constants import SUBMISSION_DELETED
from onadata.apps.messaging.constants import (
SUBMISSION_DELETED, FORM_RENAMED
)


class TestMessagingUtils(TestBase):
Expand All @@ -32,3 +36,41 @@ def is_valid():
self.xform.id, 'xform', self.user, SUBMISSION_DELETED)
self.assertTrue(message_serializer_mock.called)
self.assertEqual(message_serializer_mock.call_count, 2)

@patch('onadata.apps.messaging.serializers.MessageSerializer')
def test_custom_message(self, message_serializer_mock):
def is_valid():
return True
message_serializer_mock.is_valid.side_effect = is_valid
self._create_user_and_login()
self._publish_transportation_form()
instance_id = [1]
custom_message = {
"old_title": "first title",
"new_title": "second title"
}
send_message(
instance_id,
self.xform.id,
'xform',
self.user,
FORM_RENAMED,
custom_message
)
self.assertTrue(message_serializer_mock.called)
self.assertEqual(message_serializer_mock.call_count, 1)
request = HttpRequest()
data = {
"target_id": self.xform.id,
"target_type": 'xform',
"verb": FORM_RENAMED,
"message": json.dumps({
"id": [self.xform.id],
"old_title": "first title",
"new_title": "second title"
})
}
message_serializer_mock.called_with(
data=data, context={"request": request}
)

0 comments on commit 2a6237b

Please sign in to comment.