On Wed, 17 Sep 2008 14:42:40 -0400 (EDT), Faheem Mitha
<[EMAIL PROTECTED]> wrote:
>
> Hi,
>
> I wrote some unit tests for file upload. since I didn't want the files in
> the unit tests to be uploaded to the "official locaion", I changed the
> upload location by reassiging MEDIA_ROOT to something else, '/tmp' in this
> case.
>
> With an upgrade to Django 1.0, this method no longer works. The file
> upload appears to ignore the value of MEDIA_ROOT.
>
> Can anyone suggest another method? Apparently file upload is now more
> flexible - vide http://code.djangoproject.com/wiki/FileStorageRefactor.
> I'm not sure if it is possible to use this kind of functionality to do
> this. Suggestions welcome.
I'm following up on my own message. The following approach works:
*******************************************************
customstorage.py
*******************************************************
import os
from django.core.files.storage import FileSystemStorage
from django.utils._os import safe_join
from django.conf import settings
class FileUploadStorage(FileSystemStorage):
def path(self, name):
print "calling FileUploadStorage class."
try:
path = safe_join(settings.MEDIA_ROOT, name)
except ValueError:
raise SuspiciousOperation("Attempted access to '%s' denied." % name)
return os.path.normpath(path)
*******************************************************
Then in settings.py use
DEFAULT_FILE_STORAGE = "customstorage.FileUploadStorage"
Basically, overwrite the path in FileSystemStorage so it no longer
uses self.location, but instead looks up settings.MEDIA_ROOT when path
is called.
This is just a hack. I think it would be reasonable to make location
in FileSystemStorage a callable in similar fashion to the upload_to
argument in FileField. If I understand correctly, that would be make
this hack redundant. Comments appreciated - please cc me.
Faheem.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---