Django

Code

Changeset 7986

Show
Ignore:
Timestamp:
07/19/08 13:35:11 (3 months ago)
Author:
mtredinnick
Message:

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.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/models/base.py

    r7977 r7986  
    460460    def _get_FIELD_filename(self, field): 
    461461        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)))) 
    463463        return '' 
    464464 
     
    466466        if getattr(self, field.attname): # Value is not blank. 
    467467            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('\\', '/') 
    469469        return '' 
    470470 
     
    495495            import warnings 
    496496            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.", 
    498498                category = DeprecationWarning, 
    499499                stacklevel = 2 
  • django/trunk/tests/modeltests/model_forms/models.py

    r7971 r7986  
    1212 
    1313from django.db import models 
     14 
     15TEMP_DIR = tempfile.gettempdir() 
    1416 
    1517ARTICLE_STATUS = ( 
     
    6163class TextFile(models.Model): 
    6264    description = models.CharField(max_length=20) 
    63     file = models.FileField(upload_to=tempfile.gettempdir()
     65    file = models.FileField(upload_to=TEMP_DIR
    6466 
    6567    def __unicode__(self): 
     
    7274        # Otherwise, it's equivalent to TextFile above. 
    7375        import Image 
    74         image = models.ImageField(upload_to=tempfile.gettempdir()
     76        image = models.ImageField(upload_to=TEMP_DIR
    7577    except ImportError: 
    76         image = models.FileField(upload_to=tempfile.gettempdir()
     78        image = models.FileField(upload_to=TEMP_DIR
    7779 
    7880    def __unicode__(self): 
     
    785787# FileField ################################################################### 
    786788 
     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 
     794True 
     795>>> instance.get_file_url() == expected 
     796True 
     797>>> instance.save_file_file(instance.file, SimpleUploadedFile(instance.file, 'some text')) 
     798>>> instance.get_file_filename() == expected 
     799True 
     800>>> instance.get_file_url() == expected 
     801True 
     802 
     803>>> os.unlink(instance.get_file_filename()) 
     804 
     805# File forms. 
     806 
    787807>>> class TextFileForm(ModelForm): 
    788808...     class Meta: