Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RestAPI hooks : allow post hook for submit, edit, delete, validation status change #916

Open
wants to merge 2 commits into
base: beta
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion onadata/apps/api/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
check_and_set_form_by_id,
check_and_set_form_by_id_string,
)
from onadata.libs.utils.common_tags import HOOK_EVENT

DECIMAL_PRECISION = 2

Expand Down Expand Up @@ -158,6 +159,8 @@ def add_validation_status_to_instance(
instance.validation_status = validation_status
instance.save()
success = instance.parsed_instance.update_mongo(asynchronous=False)
if success:
instance.parsed_instance.call_service_event(HOOK_EVENT['ON_VALIDATION_STATUS_CHANGE'])

return success

Expand Down Expand Up @@ -185,7 +188,10 @@ def get_validation_status(validation_status_uid, asset, username):
def remove_validation_status_from_instance(instance):
instance.validation_status = {}
instance.save()
return instance.parsed_instance.update_mongo(asynchronous=False)
success = instance.parsed_instance.update_mongo(asynchronous=False)
if success:
instance.parsed_instance.call_service_event(HOOK_EVENT['ON_VALIDATION_STATUS_CHANGE'])
return success


def get_media_file_response(
Expand Down
4 changes: 3 additions & 1 deletion onadata/apps/restservice/services/kpi_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.conf import settings
from onadata.apps.restservice.RestServiceInterface import RestServiceInterface
from onadata.apps.logger.models import Instance
from onadata.libs.utils.common_tags import HOOK_EVENT


class ServiceDefinition(RestServiceInterface):
Expand All @@ -16,7 +17,8 @@ def send(self, endpoint, data):

# Will be used internally by KPI to fetch data with KoBoCatBackend
post_data = {
'submission_id': data.get('instance_id')
'submission_id': data.get('instance_id'),
'event': data.get('event') or HOOK_EVENT['ON_SUBMIT']
}
headers = {'Content-Type': 'application/json'}

Expand Down
6 changes: 4 additions & 2 deletions onadata/apps/restservice/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# coding: utf-8
from onadata.apps.restservice.models import RestService
from onadata.apps.restservice.tasks import service_definition_task
from onadata.libs.utils.common_tags import HOOK_EVENT


def call_service(parsed_instance):
def call_service(parsed_instance, event=HOOK_EVENT['ON_SUBMIT']):
# lookup service
instance = parsed_instance.instance
rest_services = RestService.objects.filter(xform=instance.xform)
Expand All @@ -19,6 +20,7 @@ def call_service(parsed_instance):
"instance_uuid": instance.uuid,
"instance_id": instance.id,
"xml": parsed_instance.instance.xml,
"json": parsed_instance.to_dict_for_mongo()
"json": parsed_instance.to_dict_for_mongo(),
"event": event
}
service_definition_task.delay(rest_service.pk, data)
15 changes: 12 additions & 3 deletions onadata/apps/viewer/models/parsed_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
TAGS,
NOTES,
SUBMITTED_BY,
VALIDATION_STATUS
VALIDATION_STATUS,
HOOK_EVENT
)
from onadata.libs.utils.decorators import apply_form_field_names
from onadata.libs.utils.model_tools import queryset_iterator
Expand Down Expand Up @@ -304,6 +305,9 @@ def update_mongo(self, asynchronous=True):

return True

def call_service_event(self, event='on_submit'):
call_service(self, event)

@staticmethod
def bulk_update_validation_statuses(query, validation_status):
return xform_instances.update(
Expand Down Expand Up @@ -372,8 +376,8 @@ def save(self, asynchronous=False, *args, **kwargs):
# Signal has been removed because of a race condition.
# Rest Services were called before data was saved in DB.
success = self.update_mongo(asynchronous)
if success and created:
call_service(self)
if success:
self.call_service_event(HOOK_EVENT['ON_SUBMIT'] if created else HOOK_EVENT['ON_EDIT'])
return success

def add_note(self, note):
Expand Down Expand Up @@ -414,9 +418,14 @@ def _get_attachments_from_instance(instance):
return attachments


def _send_remove_event(sender, **kwargs):
instance = kwargs.get('instance')
instance.call_service_event(HOOK_EVENT['ON_DELETE'])

def _remove_from_mongo(sender, **kwargs):
instance_id = kwargs.get('instance').instance.id
xform_instances.delete_one({'_id': instance_id})


pre_delete.connect(_send_remove_event, sender=ParsedInstance)
pre_delete.connect(_remove_from_mongo, sender=ParsedInstance)
8 changes: 8 additions & 0 deletions onadata/libs/utils/common_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,11 @@
NESTED_RESERVED_ATTRIBUTES = [
VALIDATION_STATUS,
]


HOOK_EVENT = {
'ON_SUBMIT': "on_submit",
'ON_EDIT': 'on_edit',
'ON_DELETE': 'on_delete',
'ON_VALIDATION_STATUS_CHANGE': 'on_validation_status_change'
}