Skip to content

Commit

Permalink
Use only the basename of the file and not the path to the file
Browse files Browse the repository at this point in the history
  • Loading branch information
ukanga committed Jan 23, 2018
1 parent 600b0bd commit 6d08eaa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
21 changes: 16 additions & 5 deletions onadata/apps/logger/management/commands/fix_attachments_counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
7 changes: 4 additions & 3 deletions onadata/libs/utils/logger_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down

0 comments on commit 6d08eaa

Please sign in to comment.