worldcompany / djangoembed

rich media consuming and providing with django
http://djangoembed.readthedocs.org
MIT License
138 stars 38 forks source link

Remove ImageField limitation on photo resource providers #27

Closed shagi closed 12 years ago

shagi commented 12 years ago

Currently the resize method of DjangoProvider raises an exception if the image field is not a ImageFieldFile. However this requirement is used only to get images heigth and width, and in the same function is already the code to get those fields with PIL.

With this patch any FileField-like field could be used; sorl.thumbnail.fields.ImageField for example. (I pasted the patch directly since didn't find a way to attach files).

diff --git a/oembed/providers.py b/oembed/providers.py
index 58f4399..faf8d4c 100644
--- a/oembed/providers.py
+++ b/oembed/providers.py
@@ -459,13 +459,11 @@ class DjangoProvider(BaseProvider):
         the url to the new image and the new width & height
         (http://path-to-new-image, 300, 300)
         """
-        if not isinstance(image_field, ImageFieldFile):
-            raise ValueError('image_field must be an instance of django.db.models.fields.files.ImageFieldFile')
-        
-        if image_field.field.width_field and image_field.field.height_field:
-            # use model fields
-            current_width = getattr(image_field.instance, image_field.field.width_field)
-            current_height = getattr(image_field.instance, image_field.field.height_field)
+        if isinstance(image_field, ImageFieldFile):
+            if image_field.field.width_field and image_field.field.height_field:
+                # use model fields
+                current_width = getattr(image_field.instance, image_field.field.width_field)
+                current_height = getattr(image_field.instance, image_field.field.height_field)
         else:
             # use PIL
             try: