Fixed #5619 -- Return the same path in get_FOO_filename() before and after a
model is saved (i.e. make sure the upload prefix is prepended in both cases). Patch from wreese@gmail.com. Tests from Leah Culver. git-svn-id: http://code.djangoproject.com/svn/django/trunk@7986 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
c09351aec6
commit
a103786971
|
@ -459,13 +459,13 @@ class Model(object):
|
||||||
|
|
||||||
def _get_FIELD_filename(self, field):
|
def _get_FIELD_filename(self, field):
|
||||||
if getattr(self, field.attname): # Value is not blank.
|
if getattr(self, field.attname): # Value is not blank.
|
||||||
return os.path.normpath(os.path.join(settings.MEDIA_ROOT, getattr(self, field.attname)))
|
return os.path.normpath(os.path.join(settings.MEDIA_ROOT, field.get_filename(getattr(self, field.attname))))
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
def _get_FIELD_url(self, field):
|
def _get_FIELD_url(self, field):
|
||||||
if getattr(self, field.attname): # Value is not blank.
|
if getattr(self, field.attname): # Value is not blank.
|
||||||
import urlparse
|
import urlparse
|
||||||
return urlparse.urljoin(settings.MEDIA_URL, getattr(self, field.attname)).replace('\\', '/')
|
return urlparse.urljoin(settings.MEDIA_URL, field.get_filename(getattr(self, field.attname))).replace('\\', '/')
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
def _get_FIELD_size(self, field):
|
def _get_FIELD_size(self, field):
|
||||||
|
@ -494,7 +494,7 @@ class Model(object):
|
||||||
elif isinstance(raw_field, basestring):
|
elif isinstance(raw_field, basestring):
|
||||||
import warnings
|
import warnings
|
||||||
warnings.warn(
|
warnings.warn(
|
||||||
message = "Representing uploaded files as dictionaries is deprecated. Use django.core.files.uploadedfile.SimpleUploadedFile instead.",
|
message = "Representing uploaded files as strings is deprecated. Use django.core.files.uploadedfile.SimpleUploadedFile instead.",
|
||||||
category = DeprecationWarning,
|
category = DeprecationWarning,
|
||||||
stacklevel = 2
|
stacklevel = 2
|
||||||
)
|
)
|
||||||
|
|
|
@ -12,6 +12,8 @@ import tempfile
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
|
TEMP_DIR = tempfile.gettempdir()
|
||||||
|
|
||||||
ARTICLE_STATUS = (
|
ARTICLE_STATUS = (
|
||||||
(1, 'Draft'),
|
(1, 'Draft'),
|
||||||
(2, 'Pending'),
|
(2, 'Pending'),
|
||||||
|
@ -60,7 +62,7 @@ class PhoneNumber(models.Model):
|
||||||
|
|
||||||
class TextFile(models.Model):
|
class TextFile(models.Model):
|
||||||
description = models.CharField(max_length=20)
|
description = models.CharField(max_length=20)
|
||||||
file = models.FileField(upload_to=tempfile.gettempdir())
|
file = models.FileField(upload_to=TEMP_DIR)
|
||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return self.description
|
return self.description
|
||||||
|
@ -71,9 +73,9 @@ class ImageFile(models.Model):
|
||||||
# If PIL is available, try testing PIL.
|
# If PIL is available, try testing PIL.
|
||||||
# Otherwise, it's equivalent to TextFile above.
|
# Otherwise, it's equivalent to TextFile above.
|
||||||
import Image
|
import Image
|
||||||
image = models.ImageField(upload_to=tempfile.gettempdir())
|
image = models.ImageField(upload_to=TEMP_DIR)
|
||||||
except ImportError:
|
except ImportError:
|
||||||
image = models.FileField(upload_to=tempfile.gettempdir())
|
image = models.FileField(upload_to=TEMP_DIR)
|
||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return self.description
|
return self.description
|
||||||
|
@ -784,6 +786,24 @@ u'Assistance'
|
||||||
|
|
||||||
# FileField ###################################################################
|
# FileField ###################################################################
|
||||||
|
|
||||||
|
# File instance methods. Tests fix for #5619.
|
||||||
|
|
||||||
|
>>> instance = TextFile(description='nothing', file='name')
|
||||||
|
>>> expected = '%s/name' % TEMP_DIR
|
||||||
|
>>> instance.get_file_filename() == expected
|
||||||
|
True
|
||||||
|
>>> instance.get_file_url() == expected
|
||||||
|
True
|
||||||
|
>>> instance.save_file_file(instance.file, SimpleUploadedFile(instance.file, 'some text'))
|
||||||
|
>>> instance.get_file_filename() == expected
|
||||||
|
True
|
||||||
|
>>> instance.get_file_url() == expected
|
||||||
|
True
|
||||||
|
|
||||||
|
>>> os.unlink(instance.get_file_filename())
|
||||||
|
|
||||||
|
# File forms.
|
||||||
|
|
||||||
>>> class TextFileForm(ModelForm):
|
>>> class TextFileForm(ModelForm):
|
||||||
... class Meta:
|
... class Meta:
|
||||||
... model = TextFile
|
... model = TextFile
|
||||||
|
|
Loading…
Reference in New Issue