Skip to content

Commit

Permalink
Add attachments_count property to Instance model.
Browse files Browse the repository at this point in the history
Refactor update_attachment_tracking to use the Instance.attachments_count property.
  • Loading branch information
ukanga committed Jan 23, 2018
1 parent a72bf25 commit 108a64a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
6 changes: 6 additions & 0 deletions onadata/apps/logger/models/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,12 @@ def num_of_media(self):

return self._num_of_media

@property
def attachments_count(self):
return self.attachments.filter(
name__in=self.get_expected_media()
).distinct('name').order_by('name').count()

def save(self, *args, **kwargs):
force = kwargs.get('force')

Expand Down
9 changes: 3 additions & 6 deletions onadata/libs/utils/logger_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,9 @@ def update_attachment_tracking(instance):
Takes an Instance object and updates attachment tracking fields
"""
instance.total_media = instance.num_of_media
instance.media_count = instance.attachments.distinct('name')\
.order_by('name').count()
instance.media_all_received = instance.media_count == \
instance.total_media
instance.save(update_fields=['total_media',
'media_count',
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'])


Expand Down

0 comments on commit 108a64a

Please sign in to comment.