diff --git a/onadata/apps/logger/management/commands/fix_attachments_counts.py b/onadata/apps/logger/management/commands/fix_attachments_counts.py index 8e6b035f2a..6ee8bbfc5d 100644 --- a/onadata/apps/logger/management/commands/fix_attachments_counts.py +++ b/onadata/apps/logger/management/commands/fix_attachments_counts.py @@ -2,11 +2,13 @@ """ Fix submission media count command. """ +import os from django.contrib.auth.models import User from django.core.management.base import BaseCommand, CommandError from django.utils.translation import ugettext as _ from django.utils.translation import ugettext_lazy +from multidb.pinning import use_master from onadata.apps.logger.models.attachment import get_original_filename from onadata.apps.logger.models.xform import XForm @@ -52,7 +54,8 @@ def update_attachment_tracking(instance): Takes an Instance object and updates attachment tracking fields """ for attachment in instance.attachments.all(): - attachment.name = get_original_filename(attachment.media_file.name) + attachment.name = os.path.basename( + get_original_filename(attachment.media_file.name)) attachment.save() instance.total_media = num_of_media(instance) @@ -87,10 +90,18 @@ def handle(self, *args, **options): except User.DoesNotExist: raise CommandError(_("The user '%s' does not exist.") % username) + self.process_attachments(user) + + @use_master + def process_attachments(self, user): + """ + Process attachments for submissions where media_all_received is False. + """ xforms = XForm.objects.filter(user=user, deleted_at__isnull=True) for xform in queryset_iterator(xforms): submissions = xform.instances.filter(media_all_received=False) - self.stdout.write("%s to process %s submissions" % ( - xform, submissions.count())) - for submission in queryset_iterator(submissions): - update_attachment_tracking(submission) + if submissions.count(): + self.stdout.write("%s to process %s submissions" % ( + xform, submissions.count())) + for submission in queryset_iterator(submissions): + update_attachment_tracking(submission) diff --git a/onadata/libs/utils/logger_tools.py b/onadata/libs/utils/logger_tools.py index d884daea34..773f83019e 100644 --- a/onadata/libs/utils/logger_tools.py +++ b/onadata/libs/utils/logger_tools.py @@ -214,6 +214,7 @@ def save_attachments(xform, instance, media_files): if extension == Attachment.OSM and not xform.instances_with_osm: xform.instances_with_osm = True xform.save() + filename = os.path.basename(f.name) # filename = os.path.join(upload_path, f.name) # attachments = Attachment.objects.filter(instance=instance, # media_file=filename) @@ -223,14 +224,14 @@ def save_attachments(xform, instance, media_files): # attachment.media_file = f # attachment.save() # else: - media_in_submission = (f.name in instance.get_expected_media() or - instance.xml.find(f.name) != -1) + media_in_submission = (filename in instance.get_expected_media() or + instance.xml.find(filename) != -1) if media_in_submission: Attachment.objects.get_or_create( instance=instance, media_file=f, mimetype=content_type, - name=f.name, + name=filename, extension=extension) update_attachment_tracking(instance)