Skip to content

Commit

Permalink
Merge pull request #523 from kobotoolbox/new-hook-signal-endpoint
Browse files Browse the repository at this point in the history
Changed 'kpi' endpoint for hook signal
  • Loading branch information
jnm authored Jun 25, 2019
2 parents 1208aaa + ef7d4fd commit ab53168
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -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!")
4 changes: 2 additions & 2 deletions onadata/apps/restservice/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
kpi_hook_service.delete()

0 comments on commit ab53168

Please sign in to comment.