cache_filename_format/cache_dir

5 views
Skip to first unread message

Peter Herndon

unread,
Sep 4, 2009, 4:57:38 PM9/4/09
to django-p...@googlegroups.com
Hi there,

I'm using ImageKit 0.3.2, and I'd like to be able to have variables in
the directory generated by the thumbnail pre_cache bits. That is, I'd
like to be able to drop in the same kind of %Y/%m/%d stuff you can have
in upload_to for regular files. Reason for this is that I've got, for
this first project, 300k files to create. I'd rather not have all 300k
thumbnails in one directory if at all possible.

I've looked at the code, and don't see any means of handling this in
ImageKit itself. On the other hand, the relevant code in Django itself
is a one-line method:

def get_directory_name(self):
return
os.path.normpath(force_unicode(datetime.datetime.now().strftime(smart_str(self.upload_to))))

Would a capability like this be useful to add, in addition to the
filename, specname stuff that's already there? If so, I'll take a stab
at it. Don't know how immediately, I'm headed to Djangocon on Sunday.
Perhaps while I'm there, though.

Thanks,

---Peter

Justin

unread,
Sep 4, 2009, 5:15:40 PM9/4/09
to Django Photologue
Hi Peter,

In the latest source cache_dir can be a callable just like upload_to.
It should take the following arguments: The object/instance, the path
of the original uploaded file and the cache filename (generated with
cache_filename_format) and return the full path the the new image
file.

Here's an example:

def get_cache_dir(obj, filepath, cache_filename):
return os.path.join('mycustompath', filepath, cache_filename)

One caveat: this function is called each time to find the image so if
you use a property of the object that could change in your function,
you run the risk of orphaning generated images.

Hope that helps,

- Justin

Justin

unread,
Sep 4, 2009, 5:18:13 PM9/4/09
to Django Photologue
To be clear, the path return from your cache_dir callable should still
be relative from MEDIA_ROOT.

Have fun at Djangocon!

- Justin

Peter Herndon

unread,
Sep 5, 2009, 4:53:55 PM9/5/09
to django-p...@googlegroups.com
Thanks very much, Justin, that indeed covers it.

Cheers,

---Peter
Reply all
Reply to author
Forward
0 new messages