Commits

Alex Ulianytskyi committed a894fc2

Изменены/добавлены функции для эскизов картинок.

Comments (0)

Files changed (3)

django_utils/decorators.py

             new_filename.strip('/ ')
         if field is not None:
             image = getattr(instance, field)
-            if image.storage.exists(new_filename):
-                image.storage.delete(new_filename)
+            if image:
+                image.delete()
         return new_filename
     return wrapped
 

django_utils/image_storage.py

         if options.has_key('image_size'):
             self.image_size = options.get('image_size')
         super(ImageStorage, self).__init__(location, base_url)
-
+    # FIXME: The 'image' attribute has no file associated with it.
     def _save(self, name, content):
-        super(ImageStorage, self)._save(name, content)
+        new_name = super(ImageStorage, self)._save(name, content)
         if self.do_thumbnail:
-            self._thumbnail(name, self.thumbnail_size, self.thumbnail_suffix)
+            self._thumbnail(new_name, self.thumbnail_size, self.thumbnail_suffix)
         if self.do_resize:
-            self._thumbnail(name, self.image_size)
-        return name
-    def get_thumbnail_url(self, name):
+            self._thumbnail(new_name, self.image_size)
+        return new_name
+    def _format_thumnail_str(self, name, ext):
+        if name:
+            return u'%s%s%s' % (name, self.thumbnail_suffix, ext)
+        else:
+            return u''
+    def thumbnail_name(self, name):
+        _name, ext = os.path.splitext(name)
+        return self._format_thumnail_str(_name, ext)
+    def thumbnail_path(self, name):
+        _name, ext = os.path.splitext(self.path(name))
+        return self._format_thumnail_str(_name, ext)
+    def thumbnail_delete(self, name):
+        return self.delete(self.thumbnail_name(name))
+    def thumbnail_url(self, name):
         _name, ext = os.path.splitext(self.url(name))
-        if _name is not u'':
-            return u'%s%s%s' % (_name, self.thumbnail_suffix, ext)
-        else:
-            return u''
+        return self._format_thumnail_str(_name, ext)

django_utils/models.py

 from django.db.models.fields.files import ImageField
 from django.db.models.fields.files import ImageFieldFile
 
+def missing_attr(func):
+    def wrpapped(*args, **kwargs):
+        try:
+            res = func(*args, **kwargs)
+        except AttributeError:
+            res = None
+        return res
+    return wrpapped
+
 class ImageWithThumbFieldFile(ImageFieldFile):
-    def get_thumbnail_url(self):
-        if hasattr(self.storage, 'get_thumbnail_url'):
-            return self.storage.get_thumbnail_url(self.name)
-        else:
-            return u''
-
+    @missing_attr
+    def thumbnail_name(self):
+        return self.storage.thumbnail_name(self.name)
+    @missing_attr
+    def thumbnail_path(self):
+        return self.storage.thumbnail_path(self.name)
+    @missing_attr
+    def thumbnail_delete(self):
+        return self.storage.thumbnail_delete(self.name)
+    @missing_attr
+    def thumbnail_url(self):
+        return self.storage.thumbnail_url(self.name)
+    def delete(self, save=True):
+        if self.thumbnail_name():
+            self.thumbnail_delete()
+        super(ImageWithThumbFieldFile, self).delete(save)
 class ImageWithThumbField(ImageField):
     attr_class = ImageWithThumbFieldFile