﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
26038	FileSystemStorage size() method does not use current MEDIA_ROOT setting value	Dave Voutila	nobody	"While writing some functional tests and using `@override_settings` to point to a different `MEDIA_ROOT` location on my filesystem containing test media files, getting a`FieldFile`'s `size` property resulted in `OSError` exceptions because of an attempt to read the file in the app's default (i.e. not overridden) `MEDIA_ROOT` location.

For instance, if I decorate my test class with like:

{{{#!python
@override_settings(
    MEDIA_ROOT = os.path.join(settings.INSTALL_ROOT, 'cl/assets/media/test/')
)
class FeedsFunctionalTest(StaticLiveServerTestCase):
...
}}}

My test fails during `setUp()` due to logic in the test class that attempts to read `size` on test model's `FieldFile` instance. (It's persisting the value in an index for downline use.) It appears the underlying `FileSystemStorage` class is getting and keeping the original `MEDIA_ROOT` before the `@override_settings` decorator gets to change my setting. It then uses that old value to build the path to call `os.path.getsize(self.path(name))`.

Right now the workaround I'm using is to override the `size()` method (not `path()` at the moment) like so:

{{{#!python
class MyDifferentFileSystemStorage(FileSystemStorage):

    def size(self, name):
            """"""
            Override the size method of FileSystemStorage to work around bug in
            Django 1.8 where MEDIA_ROOT is not used dynamically when building the
            underlying absolute path.
            """"""
            return os.path.getsize(os.path.join(settings.MEDIA_ROOT, name))
}}}

This looks like a bug to me as this negatively impacts using some test utils (namely `override_settings`) and functional testing in general. 

I'm experiencing this in v1.8, but looking at the code it appears to be the same logic in Django's ''master'' branch as well."	Bug	closed	File uploads/storage	dev	Normal	fixed	testing fieldfile		Ready for checkin	1	0	0	0	0	0
