Skip to content

Commit

Permalink
Use organisational name instead of id in topic definition
Browse files Browse the repository at this point in the history
Send message only after successful xls form update
  • Loading branch information
lincmba committed Jul 27, 2020
1 parent b1ae6c6 commit 01b6167
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def _create_submission_review(self):

return response.data

@patch('onadata.apps.logger.models.submission_review.send_message')
@patch('onadata.apps.api.viewsets.submission_review_viewset.send_message')
def test_submission_review_create(self, mock_send_message):
"""
Test we can create a submission review
Expand Down
11 changes: 6 additions & 5 deletions onadata/apps/api/viewsets/xform_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ def _try_update_xlsform(request, xform, owner):
serializer = XFormSerializer(
xform, context={'request': request})

# send form update notification
send_message(
instance_id=xform.id, target_id=xform.id,
target_type=XFORM, user=request.user or owner,
message_verb=FORM_UPDATED)

return Response(serializer.data, status=status.HTTP_200_OK)

return Response(survey, status=status.HTTP_400_BAD_REQUEST)
Expand Down Expand Up @@ -710,11 +716,6 @@ def partial_update(self, request, *args, **kwargs):
if request.FILES or set(['xls_url',
'dropbox_xls_url',
'text_xls_form']) & set(request.data):
# send form update notification
send_message(
instance_id=self.object.id, target_id=self.object.id,
target_type=XFORM, user=self.request.user,
message_verb=FORM_UPDATED)
return _try_update_xlsform(request, self.object, owner)

try:
Expand Down
3 changes: 1 addition & 2 deletions onadata/apps/messaging/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
"""
from __future__ import unicode_literals

from django.utils.module_loading import import_string

from actstream.models import Action
from django.utils.module_loading import import_string
from multidb.pinning import use_master


Expand Down
3 changes: 2 additions & 1 deletion onadata/apps/messaging/backends/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ def get_topic(self, instance):
}
if kwargs['target_name'] == XFORM:
xform = XForm.objects.get(id=instance.target_object_id)
kwargs['organization_username'] = xform.project.organization.id
kwargs[
'organization_username'] = xform.project.organization.username
kwargs['verb'] = VERB_TOPIC_DICT[instance.verb]
kwargs['project_id'] = xform.project.id
return ('/{topic_base}/organization/{organization_username}/'
Expand Down
5 changes: 4 additions & 1 deletion onadata/apps/messaging/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ def messaging_backends_handler(sender, **kwargs): # pylint: disable=W0613
backend = backends[name]['BACKEND']
backend_options = backends[name].get('OPTIONS')
if as_task:
call_backend_async.delay(backend, instance.id, backend_options)
# Sometimes the Action isn't created yet, hence
# the need to delay 2 seconds
call_backend_async.apply_async(
(backend, instance.id, backend_options), countdown=2)
else:
call_backend(backend, instance.id, backend_options)
5 changes: 2 additions & 3 deletions onadata/apps/messaging/tests/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
"""
from __future__ import unicode_literals

from actstream.models import Action
from django.test import TestCase
from django.test.utils import override_settings

from actstream.models import Action
from mock import patch

from onadata.apps.messaging.signals import messaging_backends_handler
Expand All @@ -26,7 +25,7 @@ class TestSignals(TestCase):
},
},
MESSAGING_ASYNC_NOTIFICATION=True)
@patch('onadata.apps.messaging.signals.call_backend_async.delay')
@patch('onadata.apps.messaging.signals.call_backend_async.apply_async')
def test_messaging_backends_handler_async(self, call_backend_async_mock):
"""
Test messaging backends handler function.
Expand Down
2 changes: 1 addition & 1 deletion onadata/apps/messaging/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_call_backend_async(self):
instance = _create_message(from_user, to_user, 'I love oov')

with self.assertRaises(NotImplementedError):
call_backend_async.delay(
call_backend_async.apply_async(
backend='onadata.apps.messaging.backends.base.BaseBackend',
instance_id=instance.id,
backend_options=None).get()
2 changes: 1 addition & 1 deletion onadata/libs/utils/logger_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def _get_instance(xml, new_uuid, submitted_by, status, xform, checksum):
# send notification on submission creation
send_message(
instance_id=instance.id, target_id=instance.xform.id,
target_type=XFORM, user=instance.user,
target_type=XFORM, user=instance.user or instance.xform.user,
message_verb=message_verb)
return instance

Expand Down

0 comments on commit 01b6167

Please sign in to comment.