matthewwithanm / django-imagekit

Automated image processing for Django. Currently v4.0
http://django-imagekit.rtfd.org/
BSD 3-Clause "New" or "Revised" License
2.26k stars 276 forks source link

IMAGEKIT_SPEC_CACHEFILE_NAMER is not working #492

Closed altostratous closed 5 years ago

altostratous commented 5 years ago

In Short

Setting IMAGEKIT_SPEC_CACHEFILE_NAMER to a callable in the settings file does not make imagekit actually call my function.

Expected Behaviour

Setting IMAGEKIT_SPEC_CACHEFILE_NAMER to a callable would lead to calling my function.

Description and Steps to Reproduce

I am about to use size/quality suffixed image names for the ImageSpecFields. I figured out that I can specify IMAGEKIT_SPEC_CACHEFILE_NAMER for such a purpose.

I found the source code of the default function here and changed it to the following:

import os
from django.conf import settings
from pilkit.utils import suggest_extension

def source_name_as_path(generator):
    """
    A namer that, given the following source file name::
        photos/thumbnails/bulldog.jpg
    will generate a name like this::
        /path/to/generated/images/photos/thumbnails/bulldog/bulldog80x25.jpg
    where "/path/to/generated/images/" is the value specified by the
    ``IMAGEKIT_CACHEFILE_DIR`` setting.
    """
    source_filename = getattr(generator.source, 'name', None)

    if source_filename is None or os.path.isabs(source_filename):
        # Generally, we put the file right in the cache file directory.
        directory = settings.IMAGEKIT_CACHEFILE_DIR
    else:
        # For source files with relative names (like Django media files),
        # use the source's name to create the new filename.
        directory = os.path.join(settings.IMAGEKIT_CACHEFILE_DIR,
                           os.path.splitext(source_filename)[0])

    ext = suggest_extension(source_filename or '', generator.format)
    return os.path.normpath(os.path.join(directory,
                                         '{}x{}{}'.format(*generator.size, ext)))

but my code is not, simply called. I know that the *generator.size part might not work but I was about to test the setting and check if what attributes the generator have at the place the function is called. I'm using python 3.6.8 and here's my requiremnets.txt and settings.py if it matters:

# requirements.txt
Django==2.1.7
django-imagekit==4.0.2
...
# settings.py
from ynutils.imagekit_naming import source_name_as_path
...
IMAGEKIT_SPEC_CACHEFILE_NAMER = source_name_as_path

Regrads

altostratous commented 5 years ago

I found the problem by deepening a little through the imagekit code. It expected the setting to be the python path to the namer function. So after I changed it from pointing to the imported function to the path to that function it worked like a charm.

So I'm closing this issue.