Skip to content

Commit

Permalink
Properly handle non-unique id_strings
Browse files Browse the repository at this point in the history
Amends #1864. Mongo and Postgres queries now respect that multiple users may
have forms with the same `id_string`
  • Loading branch information
jnm committed Nov 5, 2018
1 parent e0842b5 commit b5ea269
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions kpi/deployment_backends/kobocat_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,16 @@ def timestamp(self):
def xform_id_string(self):
return self.asset._deployment_data.get('backend_response', {}).get('id_string')

@property
def xform_id(self):
pk = self.asset._deployment_data.get('backend_response', {}).get('formid')
xform = _models.XForm.objects.filter(pk=pk).only(
'user__username', 'id_string').first()
if not (xform.user.username == self.asset.owner.username and
xform.id_string == self.xform_id_string):
raise Exception('Deployment links to an unexpected KoBoCAT XForm')
return pk

@property
def mongo_userform_id(self):
return '{}_{}'.format(self.asset.owner.username, self.xform_id_string)
Expand Down Expand Up @@ -524,7 +534,7 @@ def __get_submissions_in_json(self, instances_ids=[]):
:return: generator<JSON>
"""
query = {
"_xform_id_string": self.asset.uid,
"_userform_id": self.mongo_userform_id,
"_deleted_at": {"$exists": False}
}

Expand All @@ -547,8 +557,9 @@ def __get_submissions_in_xml(self, instances_ids=[]):
:return: list<XML>
"""
queryset = _models.Instance.objects.filter(
xform__id_string=self.asset.uid,
deleted_at=None)
xform_id=self.xform_id,
deleted_at=None
)

if len(instances_ids) > 0:
queryset = queryset.filter(id__in=instances_ids)
Expand Down

0 comments on commit b5ea269

Please sign in to comment.