Skip to content

Commit

Permalink
Merge pull request kobotoolbox#2512 from kobotoolbox/2497-cannot-dele…
Browse files Browse the repository at this point in the history
…te-asset-with-rest

Asset cannot be deleted if it has active REST Services
  • Loading branch information
jnm authored Apr 17, 2020
2 parents 4bbb5bd + 92e7a79 commit fb99d80
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
2 changes: 1 addition & 1 deletion kobo/settings/testing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# coding: utf-8
from .base import *

# For tests, don't use KoBoCat's DB
# For tests, don't use KoBoCAT's DB
DATABASES = {
'default': dj_database_url.config(default='sqlite:///%s/db.sqlite3' % BASE_DIR),
}
Expand Down
41 changes: 29 additions & 12 deletions kpi/deployment_backends/kobocat_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ def set_active(self, active):
'downloadable': bool(active)
}
json_response = self._kobocat_request('PATCH', url, data=payload)
assert(json_response['downloadable'] == bool(active))
assert json_response['downloadable'] == bool(active)
self.store_data({
'active': json_response['downloadable'],
'backend_response': json_response,
Expand All @@ -317,7 +317,7 @@ def set_active(self, active):
def set_asset_uid(self, force=False):
"""
Link KoBoCAT `XForm` back to its corresponding KPI `Asset` by
populating the `kpi_asset_uid` field (use KoBoCat proxy to PATCH XForm).
populating the `kpi_asset_uid` field (use KoBoCAT proxy to PATCH XForm).
Useful when a form is created from the legacy upload form.
Store results in self.asset._deployment_data
Expand Down Expand Up @@ -347,7 +347,7 @@ def set_asset_uid(self, force=False):
def set_has_kpi_hooks(self):
"""
PATCH `has_kpi_hooks` boolean of survey.
It lets `kc` know whether it needs to ping `kpi`
It lets KoBoCAT know whether it needs to ping KPI
each time a submission comes in.
Store results in self.asset._deployment_data
Expand All @@ -359,11 +359,25 @@ def set_has_kpi_hooks(self):
'has_kpi_hooks': has_active_hooks,
'kpi_asset_uid': self.asset.uid
}
json_response = self._kobocat_request('PATCH', url, data=payload)
assert(json_response['has_kpi_hooks'] == has_active_hooks)
self.store_data({
'backend_response': json_response,
})

try:
json_response = self._kobocat_request('PATCH', url, data=payload)
except KobocatDeploymentException as e:
if (
has_active_hooks is False
and hasattr(e, 'response')
and e.response.status_code == status.HTTP_404_NOT_FOUND
):
# It's okay if we're trying to unset the active hooks flag and
# the KoBoCAT project is already gone. See #2497
pass
else:
raise
else:
assert json_response['has_kpi_hooks'] == has_active_hooks
self.store_data({
'backend_response': json_response,
})

def delete(self):
"""
Expand All @@ -373,7 +387,10 @@ def delete(self):
try:
self._kobocat_request('DELETE', url)
except KobocatDeploymentException as e:
if hasattr(e, 'response') and e.response.status_code == 404:
if (
hasattr(e, 'response')
and e.response.status_code == status.HTTP_404_NOT_FOUND
):
# The KC project is already gone!
pass
else:
Expand All @@ -382,7 +399,7 @@ def delete(self):

def delete_submission(self, pk, user):
"""
Deletes submission through `KoBoCat` proxy
Deletes submission through KoBoCAT proxy
:param pk: int
:param user: User
:return: dict
Expand All @@ -395,7 +412,7 @@ def delete_submission(self, pk, user):

def delete_submissions(self, data, user):
"""
Deletes submissions through `KoBoCat` proxy
Deletes submissions through KoBoCAT proxy
:param user: User
:return: dict
"""
Expand Down Expand Up @@ -729,7 +746,7 @@ def __prepare_as_drf_response_signature(requests_response):
except ValueError as e:
if not requests_response.status_code == status.HTTP_204_NO_CONTENT:
prepared_drf_response['data'] = {
'detail': _('KoBoCat returned an unexpected response: {}'.format(str(e)))
'detail': _('KoBoCAT returned an unexpected response: {}'.format(str(e)))
}

return prepared_drf_response
Expand Down
2 changes: 1 addition & 1 deletion kpi/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def tag_uid_post_save(sender, instance, created, raw, **kwargs):
TagUid.objects.get_or_create(tag=instance)


@receiver([post_save, post_delete], sender=Hook)
@receiver(post_save, sender=Hook)
def update_kc_xform_has_kpi_hooks(sender, instance, **kwargs):
"""
Updates `kc.XForm` instance as soon as Asset.Hook list is updated.
Expand Down
2 changes: 1 addition & 1 deletion kpi/utils/mongo_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class MongoHelper:
(re.compile(base64_encodestring('.').strip()), '.'),
]

# Match KoBoCat's variables of ParsedInstance class
# Match KoBoCAT's variables of ParsedInstance class
USERFORM_ID = '_userform_id'
DEFAULT_BATCHSIZE = 1000

Expand Down

0 comments on commit fb99d80

Please sign in to comment.