diff --git a/onadata/apps/restservice/management/__init__.py b/onadata/apps/restservice/management/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/onadata/apps/restservice/management/commands/__init__.py b/onadata/apps/restservice/management/commands/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/onadata/apps/restservice/management/commands/update_kpi_hooks_endpoint.py b/onadata/apps/restservice/management/commands/update_kpi_hooks_endpoint.py new file mode 100644 index 000000000..2f582986a --- /dev/null +++ b/onadata/apps/restservice/management/commands/update_kpi_hooks_endpoint.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python +# vim: ai ts=4 sts=4 et sw=4 coding=utf-8 +from django.core.management.base import BaseCommand +from django.utils.translation import ugettext as _, ugettext_lazy + +from onadata.apps.restservice.models import RestService + + +class Command(BaseCommand): + """ + A faster method is available with PostgreSQL: + UPDATE restservice_restservice + SET service_url = REGEXP_REPLACE( + service_url, + '/assets/([^/]*)/submissions/', + '/assets/\1/hook-signal/' + ) + WHERE service_url LIKE '/assets/%'; + """ + + help = ugettext_lazy("Updates KPI rest service endpoint") + + def handle(self, *args, **kwargs): + + rest_services = RestService.objects.filter(name="kpi_hook").all() + for rest_service in rest_services: + service_url = rest_service.service_url + if service_url.endswith("/submissions/"): + service_url = service_url.replace("/submissions/", "/hook-signal/") + rest_service.service_url = service_url + rest_service.save(update_fields=["service_url"]) + + print("Done!") diff --git a/onadata/apps/restservice/signals.py b/onadata/apps/restservice/signals.py index 0e4633fcf..7d8552fbe 100644 --- a/onadata/apps/restservice/signals.py +++ b/onadata/apps/restservice/signals.py @@ -20,11 +20,11 @@ def save_kpi_hook_service(sender, instance, **kwargs): # Only register the service if it hasn't been created yet. if kpi_hook_service is None: kpi_hook_service = RestService( - service_url="/assets/{}/submissions/".format(instance.id_string), + service_url="/assets/{}/hook-signal/".format(instance.id_string), xform=instance, name=SERVICE_KPI_HOOK[0] ) kpi_hook_service.save() elif kpi_hook_service is not None: # Only delete the service if it already exists. - kpi_hook_service.delete() \ No newline at end of file + kpi_hook_service.delete()