Repository: django-cms/django-filer

Issue:
Image _width and _height are NULL after import from fixture
How to reproduce:

On a site with images

```
./manage.py dumpdata filer.Image > images.json
./manage.py loaddata images.json
```

check the database, in table ``filer_image`` fields ``_width`` and ``_height`` are NULL after the import, whereas the fixture contains the correct sizes.

One workaround is to reset the sizes with this simple script

```
from filer.models.imagemodels import Image

for img in Image.objects.all():
    img.file_data_changed()
    img.save()
```

Used versions:
Django==1.10.5
django-filer==1.2.6



Discussion:
After investigating this, I saw that the fields ``_width`` and ``_height`` (which contain valid data) are set  to ``None`` in https://github.com/divio/django-filer/blob/develop/filer/models/abstract.py#L69 while loading the fixture.

Why can't we just leave these values as is? Then this bug would not happen.

Resolving patch:
diff --git a/filer/models/abstract.py b/filer/models/abstract.py
--- a/filer/models/abstract.py
+++ b/filer/models/abstract.py
@@ -59,13 +59,15 @@ def matches_file_type(cls, iname, ifile, request):
     def file_data_changed(self, post_init=False):
         attrs_updated = super(BaseImage, self).file_data_changed(post_init=post_init)
         if attrs_updated:
-            # update image dimensions
             try:
-                self.file.seek(0)
-                self._width, self._height = PILImage.open(self.file).size
-                self.file.seek(0)
+                try:
+                    imgfile = self.file.file
+                except ValueError:
+                    imgfile = self.file_ptr.file
+                imgfile.seek(0)
+                self._width, self._height = PILImage.open(imgfile).size
+                imgfile.seek(0)
             except Exception:
-                # probably the image is missing. nevermind.
                 self._width, self._height = None, None
         return attrs_updated
 


Test patch:
diff --git a/filer/tests/dump.py b/filer/tests/dump.py
--- a/filer/tests/dump.py
+++ b/filer/tests/dump.py
@@ -73,6 +73,13 @@ def test_dump_load_data(self):
         # Initialize the test data
         create_folder_structure(1,1)
         fileobj = self.create_filer_file(Folder.objects.all()[0])
+
+        self.assertEqual(Image.objects.count(), 0)
+        image = self.create_filer_image()
+        image.save()
+        image_size = image._width, image._height
+        self.assertEqual(Image.objects.count(), 1)
+
         jdata = StringIO()
 
         # Dump the current data
@@ -93,10 +100,15 @@ def test_dump_load_data(self):
 
         # Database data is restored
         self.assertEqual(Folder.objects.all().count(), 1)
-        self.assertEqual(File.objects.all().count(), 1)
+        self.assertEqual(File.objects.all().count(), 2)
         self.assertEqual(File.objects.all()[0].original_filename, self.image_name)
+        self.assertEqual(Image.objects.count(), 1)
 
         fileobj = File.objects.all()[0]
+        image = Image.objects.all()[0]
+        self.assertEqual(image._width, image_size[0])
+        self.assertEqual(image._height, image_size[1])
+
         complete = os.path.join(fileobj.file.storage.location, fileobj.path)
         # Filesystem data is not
         self.assertFalse(os.path.exists(complete))
@@ -136,3 +148,4 @@ def test_dump_load_data_content(self):
             complete = os.path.join(fileobj.file.storage.location, fileobj.path)
             # Filesystem data too!
             self.assertTrue(os.path.exists(complete))
+