Skip to content

Commit

Permalink
fix stale data sent to rest services when editing submission
Browse files Browse the repository at this point in the history
The data sent to rest services can be stale if the instance is not yet saved to the database when a submission is edited
  • Loading branch information
kelvin-muchiri committed Aug 15, 2024
1 parent 7bb0020 commit 42fbdd3
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 27 deletions.
4 changes: 1 addition & 3 deletions onadata/apps/restservice/services/generic_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ class ServiceDefinition(RestServiceInterface): # pylint: disable=too-few-public
def send(self, url, data=None):
"""Post submisison JSON data to an external service that accepts a JSON post."""
if data:
# We use Instance.get_full_dict() instead of Instance.json because
# when asynchronous processing is enabled, the json may not be upto date
post_data = json.dumps(data.get_full_dict())
post_data = json.dumps(data.json)
headers = {"Content-Type": "application/json"}
try:
requests.post(
Expand Down
4 changes: 1 addition & 3 deletions onadata/apps/restservice/services/textit.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ def send(self, url, data=None):
:param data:
:return:
"""
# We use Instance.get_full_dict() instead of Instance.json because
# when asynchronous processing is enabled, the json may not be upto date
extra_data = self.clean_keys_of_slashes(data.get_full_dict())
extra_data = self.clean_keys_of_slashes(data.json)
data_value = MetaData.textit(data.xform)

if data_value:
Expand Down
25 changes: 5 additions & 20 deletions onadata/apps/restservice/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,9 @@
RestService signals module
"""
import django.dispatch
from django.conf import settings
from multidb.pinning import use_master

from onadata.apps.restservice.tasks import call_service_async
from onadata.apps.restservice.utils import call_service
from onadata.apps.logger.models.instance import Instance

ASYNC_POST_SUBMISSION_PROCESSING_ENABLED = getattr(
settings, "ASYNC_POST_SUBMISSION_PROCESSING_ENABLED", False
)

# pylint: disable=invalid-name
trigger_webhook = django.dispatch.Signal()
Expand All @@ -22,19 +15,11 @@ def call_webhooks(sender, **kwargs): # pylint: disable=unused-argument
"""
Call webhooks signal.
"""
instance_id = kwargs["instance"].pk
if ASYNC_POST_SUBMISSION_PROCESSING_ENABLED:
call_service_async.apply_async(args=[instance_id], countdown=1)
else:
with use_master:
try:
instance = Instance.objects.get(pk=instance_id)
except Instance.DoesNotExist:
# if the instance has already been removed we do not send it to the
# service
pass
else:
call_service(instance)
instance = kwargs["instance"]

call_service_async.apply_async(
args=[instance.pk, instance.get_full_dict()], countdown=1
)


trigger_webhook.connect(call_webhooks, dispatch_uid="call_webhooks")
8 changes: 7 additions & 1 deletion onadata/apps/restservice/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


@app.task()
def call_service_async(instance_pk):
def call_service_async(instance_pk, latest_json=None):
"""Async function that calls call_service()."""
# load the parsed instance

Expand All @@ -19,4 +19,10 @@ def call_service_async(instance_pk):
# service
pass
else:
if latest_json is None:
instance.json = instance.get_full_dict()

else:
instance.json = latest_json

call_service(instance)

0 comments on commit 42fbdd3

Please sign in to comment.