#26038: FileSystemStorage size() method does not use current MEDIA_ROOT setting
value
--------------------------------------+-------------------------------
Reporter: voutilad | Owner: nobody
Type: Bug | Status: new
Component: File uploads/storage | Version: 1.8
Severity: Normal | Keywords: testing fieldfile
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+-------------------------------
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/26038>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/051.66de94a890536cb8d40a9bd6c43e2320%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.