Login

Custom filename example with sorl-thumbnail

Author:
sajal
Posted:
February 16, 2011
Language:
Python
Version:
1.2
Score:
2 (after 2 ratings)

usage :- put it in python path and refer to it from settings.py

THUMBNAIL_BACKEND = 'full.import.path.to.SEOThumbnailBackend'

Took me a bit to figure it out since i couldn't find an existing example code for it.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from sorl.thumbnail.base import ThumbnailBackend
from os.path import basename
from django.conf import settings
from sorl.thumbnail.helpers import tokey, serialize

class SEOThumbnailBackend(ThumbnailBackend):
    def _get_thumbnail_filename(self, source, geometry_string, options):
        key = tokey(source.key, geometry_string, serialize(options))
        path = '%s/%s/%s' % (key[:2], key[2:4], key)
        return '%s%s/%s' % (settings.THUMBNAIL_PREFIX, path, basename(source.name))

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.