Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit a1037869716796a1075a964112aa380d86ed485f 1 parent c09351a
Malcolm Tredinnick authored
6  django/db/models/base.py
@@ -459,13 +459,13 @@ def _get_next_or_previous_in_order(self, is_next):
459 459
 
460 460
     def _get_FIELD_filename(self, field):
461 461
         if getattr(self, field.attname): # Value is not blank.
462  
-            return os.path.normpath(os.path.join(settings.MEDIA_ROOT, getattr(self, field.attname)))
  462
+            return os.path.normpath(os.path.join(settings.MEDIA_ROOT, field.get_filename(getattr(self, field.attname))))
463 463
         return ''
464 464
 
465 465
     def _get_FIELD_url(self, field):
466 466
         if getattr(self, field.attname): # Value is not blank.
467 467
             import urlparse
468  
-            return urlparse.urljoin(settings.MEDIA_URL, getattr(self, field.attname)).replace('\\', '/')
  468
+            return urlparse.urljoin(settings.MEDIA_URL, field.get_filename(getattr(self, field.attname))).replace('\\', '/')
469 469
         return ''
470 470
 
471 471
     def _get_FIELD_size(self, field):
@@ -494,7 +494,7 @@ def _save_FIELD_file(self, field, filename, raw_field, save=True):
494 494
         elif isinstance(raw_field, basestring):
495 495
             import warnings
496 496
             warnings.warn(
497  
-                message = "Representing uploaded files as dictionaries is deprecated. Use django.core.files.uploadedfile.SimpleUploadedFile instead.",
  497
+                message = "Representing uploaded files as strings is deprecated. Use django.core.files.uploadedfile.SimpleUploadedFile instead.",
498 498
                 category = DeprecationWarning,
499 499
                 stacklevel = 2
500 500
             )
26  tests/modeltests/model_forms/models.py
@@ -12,6 +12,8 @@
12 12
 
13 13
 from django.db import models
14 14
 
  15
+TEMP_DIR = tempfile.gettempdir()
  16
+
15 17
 ARTICLE_STATUS = (
16 18
     (1, 'Draft'),
17 19
     (2, 'Pending'),
@@ -60,7 +62,7 @@ def __unicode__(self):
60 62
 
61 63
 class TextFile(models.Model):
62 64
     description = models.CharField(max_length=20)
63  
-    file = models.FileField(upload_to=tempfile.gettempdir())
  65
+    file = models.FileField(upload_to=TEMP_DIR)
64 66
 
65 67
     def __unicode__(self):
66 68
         return self.description
@@ -71,9 +73,9 @@ class ImageFile(models.Model):
71 73
         # If PIL is available, try testing PIL.
72 74
         # Otherwise, it's equivalent to TextFile above.
73 75
         import Image
74  
-        image = models.ImageField(upload_to=tempfile.gettempdir())
  76
+        image = models.ImageField(upload_to=TEMP_DIR)
75 77
     except ImportError:
76  
-        image = models.FileField(upload_to=tempfile.gettempdir())
  78
+        image = models.FileField(upload_to=TEMP_DIR)
77 79
 
78 80
     def __unicode__(self):
79 81
         return self.description
@@ -784,6 +786,24 @@ def __unicode__(self):
784 786
 
785 787
 # FileField ###################################################################
786 788
 
  789
+# File instance methods. Tests fix for #5619.
  790
+
  791
+>>> instance = TextFile(description='nothing', file='name')
  792
+>>> expected = '%s/name' % TEMP_DIR
  793
+>>> instance.get_file_filename() == expected
  794
+True
  795
+>>> instance.get_file_url() == expected
  796
+True
  797
+>>> instance.save_file_file(instance.file, SimpleUploadedFile(instance.file, 'some text'))
  798
+>>> instance.get_file_filename() == expected
  799
+True
  800
+>>> instance.get_file_url() == expected
  801
+True
  802
+
  803
+>>> os.unlink(instance.get_file_filename())
  804
+
  805
+# File forms.
  806
+
787 807
 >>> class TextFileForm(ModelForm):
788 808
 ...     class Meta:
789 809
 ...         model = TextFile

0 notes on commit a103786

Please sign in to comment.
Something went wrong with that request. Please try again.