Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix files endpoint for png images #1527

Merged
merged 9 commits into from
Jan 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions onadata/apps/api/tests/viewsets/test_abstract_viewset.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import json
import os
import re
import requests
from builtins import open
from tempfile import NamedTemporaryFile

import requests
from builtins import open
from django.conf import settings
from django.contrib.auth import authenticate
from django.contrib.auth.models import Permission, User
Expand Down
27 changes: 27 additions & 0 deletions onadata/apps/api/tests/viewsets/test_media_viewset.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import os

from mock import patch

from onadata.apps.api.tests.viewsets.test_abstract_viewset import \
TestAbstractViewSet
from onadata.apps.api.viewsets.media_viewset import MediaViewSet
from onadata.apps.logger.models import Attachment


def attachment_url(attachment, suffix=None):
Expand Down Expand Up @@ -75,3 +79,26 @@ def test_retrieve_view_no_filename_param(self):
request = self.factory.get('/', **self.extra)
response = self.retrieve_view(request, self.attachment.pk)
self.assertEqual(response.status_code, 404)

def test_retrieve_small_png(self):
"""Test retrieve png images"""
s = 'transport_2011-07-25_19-05-49_1'
media_file = "ona_png_image.png"

path = os.path.join(self.main_directory, 'fixtures',
'transportation', 'instances', s, media_file)
with open(path, 'rb') as f:
self._make_submission(os.path.join(
self.main_directory, 'fixtures',
'transportation', 'instances', s, s + '.xml'), media_file=f)
attachment = Attachment.objects.all().reverse()[0]
self.attachment = attachment
request = self.factory.get(
'/',
{'filename': self.attachment.media_file.name, 'suffix': 'small'},
**self.extra
)
response = self.retrieve_view(request, self.attachment.pk)
self.assertEqual(response.status_code, 302)
self.assertTrue(response['Location'],
attachment_url(self.attachment, 'small'))
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ def handle(self, *args, **options):
if not default_storage.exists(full_path):
try:
if default_storage.__class__ != fs.__class__:
resize(filename)
resize(filename, att.extension)
else:
resize_local_env(filename)
resize_local_env(filename, att.extension)
path = get_path(
filename, '%s' % THUMB_CONF['small']['suffix'])
if default_storage.exists(path):
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version='1.0' ?>
<transportation id="transportation_2011_07_25" version="2014111">
<transport>
<available_transportation_types_to_referral_facility>none
</available_transportation_types_to_referral_facility>
<loop_over_transport_types_frequency>
<ambulance/>
<bicycle/>
<boat_canoe/>
<bus/>
<donkey_mule_cart/>
<keke_pepe/>
<lorry/>
<motorbike/>
<taxi/>
<other/>
</loop_over_transport_types_frequency>
</transport>
<image1>ona_png_image.png</image1>
<meta>
<instanceID>uuid:5b2cc313-fc09-437e-8149-fcd32f695d42</instanceID>
</meta>
</transportation>
12 changes: 6 additions & 6 deletions onadata/libs/tests/utils/test_image_tools.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import os
import shutil
import requests
from httmock import urlmatch, HTTMock

import requests
from django.core.files.storage import get_storage_class
from httmock import urlmatch, HTTMock

from onadata.libs.utils.image_tools import resize
from onadata.apps.main.tests.test_base import TestBase
from onadata.apps.logger.models.attachment import Attachment
from onadata.apps.main.tests.test_base import TestBase
from onadata.libs.utils.image_tools import resize

storage = get_storage_class()()

Expand All @@ -24,7 +24,7 @@ class TestImageTools(TestBase):
def test_resize_exception_is_handled(self):
with HTTMock(image_url_mock):
with self.assertRaises(Exception) as io_error:
resize('test.jpg')
resize('test.jpg', 'jpg')

self.assertEqual(str(io_error.exception),
u'The image file couldn\'t be identified')
Expand All @@ -34,7 +34,7 @@ def test_resize(self):
self._submit_transport_instance_w_attachment()
attachment = Attachment.objects.first()
media_filename = attachment.media_file.name
resize(media_filename)
resize(media_filename, attachment.extension)
# small
path = os.path.join(
storage.path(''), media_filename[0:-4] + '-small.jpg')
Expand Down
25 changes: 15 additions & 10 deletions onadata/libs/utils/image_tools.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from tempfile import NamedTemporaryFile

from PIL import Image
from django.conf import settings
from django.core.files.base import ContentFile
from django.core.files.storage import get_storage_class

from PIL import Image

from onadata.libs.utils.viewer_tools import get_path


Expand Down Expand Up @@ -34,8 +33,8 @@ def get_dimensions(size, longest_side):
return flat(width, height)


def _save_thumbnails(image, path, size, suffix):
nm = NamedTemporaryFile(suffix='.%s' % settings.IMG_FILE_TYPE)
def _save_thumbnails(image, path, size, suffix, extension):
nm = NamedTemporaryFile(suffix='.%s' % extension)
default_storage = get_storage_class()()

try:
Expand All @@ -51,7 +50,9 @@ def _save_thumbnails(image, path, size, suffix):
nm.close()


def resize(filename):
def resize(filename, extension):
if extension == 'non':
extension = settings.DEFAULT_IMG_FILE_TYPE
default_storage = get_storage_class()()

try:
Expand All @@ -63,20 +64,23 @@ def resize(filename):
_save_thumbnails(
image, filename,
conf[key]['size'],
conf[key]['suffix'])
conf[key]['suffix'],
extension)
except IOError:
raise Exception("The image file couldn't be identified")


def resize_local_env(filename):
def resize_local_env(filename, extension):
if extension == 'non':
extension = settings.DEFAULT_IMG_FILE_TYPE
default_storage = get_storage_class()()
path = default_storage.path(filename)
image = Image.open(path)
conf = settings.THUMB_CONF

[_save_thumbnails(
image, path, conf[key]['size'],
conf[key]['suffix']) for key in settings.THUMB_ORDER]
conf[key]['suffix'], extension) for key in settings.THUMB_ORDER]


def image_url(attachment, suffix):
Expand All @@ -102,9 +106,10 @@ def image_url(attachment, suffix):
get_path(filename, size))
else:
if default_storage.__class__ != fs.__class__:
resize(filename)
resize(filename, extension=attachment.extension)
else:
resize_local_env(filename)
resize_local_env(filename,
extension=attachment.extension)

return image_url(attachment, suffix)
else:
Expand Down
2 changes: 1 addition & 1 deletion onadata/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ def configure_logging(logger, **kwargs):
}
# order of thumbnails from largest to smallest
THUMB_ORDER = ['large', 'medium', 'small']
IMG_FILE_TYPE = 'jpg'
DEFAULT_IMG_FILE_TYPE = 'jpg'

# celery
CELERY_RESULT_BACKEND = 'django-db'
Expand Down