Ticket #5619: patch-5619-with-tests.diff

File patch-5619-with-tests.diff, 2.7 KB (added by Leah Culver, 16 years ago)

added tests for previous patch

  • django/db/models/base.py

     
    462462
    463463    def _get_FIELD_filename(self, field):
    464464        if getattr(self, field.attname): # Value is not blank.
    465             return os.path.normpath(os.path.join(settings.MEDIA_ROOT, getattr(self, field.attname)))
     465            return os.path.normpath(os.path.join(settings.MEDIA_ROOT, field.get_filename(getattr(self, field.attname))))
    466466        return ''
    467467
    468468    def _get_FIELD_url(self, field):
    469469        if getattr(self, field.attname): # Value is not blank.
    470470            import urlparse
    471             return urlparse.urljoin(settings.MEDIA_URL, getattr(self, field.attname)).replace('\\', '/')
     471            return urlparse.urljoin(settings.MEDIA_URL, field.get_filename(getattr(self, field.attname))).replace('\\', '/')
    472472        return ''
    473473
    474474    def _get_FIELD_size(self, field):
  • tests/modeltests/model_forms/models.py

     
    1212
    1313from django.db import models
    1414
     15TEMP_DIR = tempfile.gettempdir()
     16
    1517ARTICLE_STATUS = (
    1618    (1, 'Draft'),
    1719    (2, 'Pending'),
     
    6062
    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):
    6668        return self.description
     
    7173        # If PIL is available, try testing PIL.
    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):
    7981        return self.description
     
    784786
    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, '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:
    789809...         model = TextFile
Back to Top