Skip to content

Commit

Permalink
Fix image resize
Browse files Browse the repository at this point in the history
  • Loading branch information
noliveleger committed May 27, 2024
1 parent a0faa19 commit 7233aae
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,31 @@ class Command(BaseCommand):
help = "Creates thumbnails for all form images and stores them"

def add_arguments(self, parser):
parser.add_argument('-u', '--username',
help="Username of the form user")
parser.add_argument(
'-u', '--username', help="Username of the form user"
)

parser.add_argument('-i', '--id_string',
help="id string of the form")
parser.add_argument('-i', '--id_string', help="id string of the form")

parser.add_argument('-f', '--force', action='store_false',
help="regenerate thumbnails if they exist.")
parser.add_argument(
'-f',
'--force',
action='store_false',
help="regenerate thumbnails if they exist.",
)

def handle(self, *args, **kwargs):
attachments_qs = Attachment.objects.select_related(
'instance', 'instance__xform')
'instance', 'instance__xform'
)
if kwargs.get('username'):
username = kwargs.get('username')
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
raise CommandError(
"Error: username %(username)s does not exist" %
{'username': username}
"Error: username %(username)s does not exist"
% {'username': username}
)
attachments_qs = attachments_qs.filter(instance__user=user)
if kwargs.get('id_string'):
Expand All @@ -48,33 +53,42 @@ def handle(self, *args, **kwargs):
xform = XForm.objects.get(id_string=id_string)
except XForm.DoesNotExist:
raise CommandError(
"Error: Form with id_string %(id_string)s does not exist" %
{'id_string': id_string}
"Error: Form with id_string %(id_string)s does not exist"
% {'id_string': id_string}
)
attachments_qs = attachments_qs.filter(instance__xform=xform)

for att in queryset_iterator(attachments_qs):
filename = att.media_file.name
full_path = get_path(filename,
settings.THUMB_CONF['small']['suffix'])
full_path = get_path(
filename, settings.THUMB_CONF['small']['suffix']
)
if kwargs.get('force') is not None:
for s in settings.THUMB_CONF.keys():
fp = get_path(filename,
settings.THUMB_CONF[s]['suffix'])
fp = get_path(filename, settings.THUMB_CONF[s]['suffix'])
if default_storage.exists(fp):
default_storage.delete(fp)

if not default_storage.exists(full_path):
try:
resize(filename)
if default_storage.exists(get_path(
if default_storage.exists(
get_path(
filename,
'%s' % settings.THUMB_CONF['small']['suffix'])):
print('Thumbnails created for %(file)s'
% {'file': filename})
'%s' % settings.THUMB_CONF['small']['suffix'],
)
):
print(
'Thumbnails created for %(file)s'
% {'file': filename}
)
else:
print('Problem with the file %(file)s'
% {'file': filename})
print(
'Problem with the file %(file)s'
% {'file': filename}
)
except (IOError, OSError) as e:
print('Error on %(filename)s: %(error)s'
% {'filename': filename, 'error': e})
print(
'Error on %(filename)s: %(error)s'
% {'filename': filename, 'error': e}
)
6 changes: 3 additions & 3 deletions kobo/apps/openrosa/libs/utils/image_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import requests
from django.conf import settings
from django.core.files.base import ContentFile
from django.core.files.storage import FileSystemStorage
from PIL import Image

from kobo.apps.openrosa.libs.utils.viewer_tools import get_path
from kpi.deployment_backends.kc_access.storage import (
default_kobocat_storage as default_storage,
)


def flat(*nums):
"""
Build a tuple of ints from float or integer arguments.
Expand Down Expand Up @@ -67,11 +69,9 @@ def _save_thumbnails(image, original_path, size, suffix):


def resize(filename):
is_local = default_storage.__class__.__name__ == 'FileSystemStorage'
image = None
original_path = None

if is_local:
if isinstance(default_storage, FileSystemStorage):
path = default_storage.path(filename)
image = Image.open(path)
original_path = path
Expand Down

0 comments on commit 7233aae

Please sign in to comment.