1. RJ Ryan
  2. sorl-thumbnail

Commits

smil...@18c32c52-eb3f-0410-93bd-f15eff1430f4  committed 3c1fd60

Adding a `thumbs_dir` option to DjangoThumbnail and small fix to img_tag

  • Participants
  • Parent commits f732973
  • Branches smiley-refactor

Comments (0)

Files changed (1)

File thumbnail/main.py

View file
  • Ignore whitespace
 
 class DjangoThumbnail(Thumbnail):
     # filename_format is passed the following string options:
-    #   `filename`  -- original source filename
-    #   `basename`  -- source filename, minus its extensions if it was '.jpg'
-    #   `directory` -- source directory (relative to self.root)
-    #   `thumb_dir` -- optional thumbnail directory option
-    #   `quality`   -- the quality of the image
-    #   `options`   -- a list of any non-default settings, each starting with an
-    #                  underscore
-    filename_format = '%(thumb_dir)s%(directory)s/%(basename)s_%(x)sx%(y)s%(options)s_q%(quality)s.jpg'
+    #   `filename`   -- original source filename
+    #   `basename`   -- source filename, minus its extensions if it was '.jpg'
+    #   `directory`  -- source directory (relative to self.root)
+    #   `thumbs_dir` -- optional thumbnail directory option (starting with a /
+    #   `quality`    -- the quality of the image
+    #   `options`    -- a list of any non-default settings, each starting with
+    #                   an underscore
+    #   `x` and `y`  -- thumbnail size (not guaranteed to match actual image
+    #                   dimensions)
+    filename_format = '%(thumbs_dir)s%(directory)s/%(basename)s_%(x)sx%(y)s%(options)s_q%(quality)s.jpg'
     root = settings.MEDIA_ROOT
     base_url = settings.MEDIA_URL
 
-    def __init__(self, relative_source_path, *args, **kwargs):
+    def __init__(self, relative_source_path, thumbs_dir='thumbs', *args,
+                 **kwargs):
+        self.thumbs_dir = thumbs_dir
         self.relative_source_path = relative_source_path
         source_path = self._absolute_path(relative_source_path)
         super(DjangoThumbnail, self).__init__(source_path, '',
             basename = source_filename
 
         options = ['_%s' % option for option in OPTIONS if getattr(self, option)]
+        thumbs_dir = self.thumbs_dir or ''
+        if thumbs_dir and not thumbs_dir.endswith('/'):
+            thumbs_dir = '%s/' % thumbs_dir
         filename_opts = dict(
+            thumbs_dir=thumbs_dir,
             filename=source_filename,
             basename=basename,
             directory=source_dir,
         attributes['src'] = self.url
         html_attributes = [' %s="%s"' % (k, conditional_escape(v))\
                            for k, v in attributes.items()]
-        return mark_safe('<img%s />' % html_attributes)
+        return mark_safe('<img%s />' % ''.join(html_attributes))