Skip to content

Commit

Permalink
Keep track of whether all attachments have been submitted
Browse files Browse the repository at this point in the history
Fixes: #1049

Add new fields to the model instance to keep track of:

- How many attachments, if any are expected in a submission
- How many attachments have been actually received
- Whether all attachments have been received
  • Loading branch information
moshthepitt committed Aug 24, 2017
1 parent 54c8332 commit b811b0d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 14 deletions.
12 changes: 0 additions & 12 deletions onadata/apps/logger/models/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,18 +563,6 @@ def osm_data(self):
def deleted_at(self):
return None

@property
def total_media(self):
return self.xform_instance.total_media

@property
def media_count(self):
return self.xform_instance.media_count

@property
def media_all_received(self):
return self.xform_instance.media_all_received

def _set_parser(self):
if not hasattr(self, "_parser"):
self._parser = XFormInstanceParser(
Expand Down
68 changes: 66 additions & 2 deletions onadata/libs/tests/utils/test_logger_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_attachment_tracking(self):
xml_string = """
<data id="{}">
<meta>
<instanceID>uuid:UJ5jSMAJ1Jz4EszdgHy8n851AsKaqBPO5VN7</instanceID>
<instanceID>uuid:UJ5jSMAJ1Jz4EszdgHy8n851AsKaqBPO5</instanceID>
</meta>
<image1>1300221157303.jpg</image1>
<image2>1300375832136.jpg</image2>
Expand Down Expand Up @@ -115,7 +115,7 @@ def test_attachment_tracking_for_repeats(self):
xml_string = """
<data id="{}">
<meta>
<instanceID>uuid:UJ5jz4EszdgH8uhy8n851AsKaqBPO5VN7</instanceID>
<instanceID>uuid:UJ5jz4EszdgH8uhy8nss1AsKaqBPO5VN7</instanceID>
</meta>
<images>
<image1>1300221157303.jpg</image1>
Expand Down Expand Up @@ -157,3 +157,67 @@ def test_attachment_tracking_for_repeats(self):
self.assertEquals(instance2.json[MEDIA_COUNT], instance2.media_count)
self.assertEquals(instance2.json[MEDIA_ALL_RECEIVED],
instance2.media_all_received)

def test_attachment_tracking_for_nested_repeats(self):
"""
Test that when a submission with many attachments is made,
we keep track of the total number of attachments expected
and if we have received all of them
"""
md = """
| survey | | | |
| | type | name | label |
| | begin repeat | images | Photos |
| | begin repeat | g | G |
| | image | image1 | Photo |
| | end repeat | | |
| | end repeat | | |
"""
self._create_user_and_login()
self.xform = self._publish_md(md, self.user)

xml_string = """
<data id="{}">
<meta>
<instanceID>uuid:UJ5jz4EszdgH8uhy8n851AsKaqBPO5VN7</instanceID>
</meta>
<images>
<g><image1>1300221157303.jpg</image1></g>
</images>
<images>
<g><image1>1300375832136.jpg</image1></g>
</images>
</data>
""".format(self.xform.id_string)
file_path = "{}/apps/logger/tests/Health_2011_03_13."\
"xml_2011-03-15_20-30-28/1300221157303"\
".jpg".format(settings.PROJECT_ROOT)
media_file = django_file(path=file_path,
field_name="image1",
content_type="image/jpeg")
instance = create_instance(self.user.username,
StringIO(xml_string.strip()),
media_files=[media_file])
self.assertFalse(instance.json[MEDIA_ALL_RECEIVED])
self.assertEquals(instance.json[TOTAL_MEDIA], 2)
self.assertEquals(instance.json[MEDIA_COUNT], 1)
self.assertEquals(instance.json[TOTAL_MEDIA], instance.total_media)
self.assertEquals(instance.json[MEDIA_COUNT], instance.media_count)
self.assertEquals(instance.json[MEDIA_ALL_RECEIVED],
instance.media_all_received)
file2_path = "{}/apps/logger/tests/Water_2011_03_17_2011-03-17_16-29"\
"-59/1300375832136.jpg".format(settings.PROJECT_ROOT)
media2_file = django_file(path=file2_path,
field_name="image1",
content_type="image/jpeg")
create_instance(self.user.username,
StringIO(xml_string.strip()),
media_files=[media2_file])
instance2 = Instance.objects.get(pk=instance.pk)
self.assertTrue(instance2.json[MEDIA_ALL_RECEIVED])
self.assertEquals(instance2.json[TOTAL_MEDIA], 2)
self.assertEquals(instance2.json[MEDIA_COUNT], 2)
self.assertEquals(instance2.json[TOTAL_MEDIA], instance2.total_media)
self.assertEquals(instance2.json[MEDIA_COUNT], instance2.media_count)
self.assertEquals(instance2.json[MEDIA_ALL_RECEIVED],
instance2.media_all_received)
9 changes: 9 additions & 0 deletions onadata/libs/utils/logger_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,15 @@ def save_attachments(xform, instance, media_files):
extension=extension)
update_instance_attachment_tracking(instance)

instance.total_media = len([m for m in instance.get_dict().keys() if m in
xform.get_media_survey_xpaths()])
instance.media_count = instance.attachments.count()
instance.media_all_received = instance.media_count == \
instance.total_media
instance.save(update_fields=['total_media',
'media_count',
'media_all_received'])


def save_submission(xform, xml, media_files, new_uuid, submitted_by, status,
date_created_override):
Expand Down

0 comments on commit b811b0d

Please sign in to comment.