Ticket #5361: filestorage.8.diff

File filestorage.8.diff, 63.3 KB (added by Marty Alchin, 17 years ago)

Fixed a few things, added tests and hopefully the patch will work this time

  • django/core/filestorage/base.py

     
     1from StringIO import StringIO
     2
     3from django.utils.text import get_valid_filename
     4
     5class Backend(object):
     6    def get_valid_filename(self, filename):
     7        return get_valid_filename(filename)
     8
     9    def get_available_filename(self, filename):
     10        # If the filename already exists, keep adding an underscore to the name
     11        # of the file until the filename doesn't exist.
     12        while self.file_exists(filename):
     13            try:
     14                dot_index = filename.rindex('.')
     15            except ValueError: # filename has no dot
     16                filename += '_'
     17            else:
     18                filename = filename[:dot_index] + '_' + filename[dot_index:]
     19        return filename
     20
     21class RemoteFile(StringIO):
     22    """Sends files to a remote backend automatically, when necessary."""
     23
     24    def __init__(self, data, mode, writer):
     25        self._mode = mode
     26        self._write_to_backend = writer
     27        self._is_dirty = False
     28        StringIO.__init__(self, data)
     29
     30    def write(self, data):
     31        if 'w' not in self._mode:
     32            raise AttributeError, "File was opened for read-only access."
     33        StringIO.write(self, data)
     34        self._is_dirty = True
     35
     36    def close(self):
     37        if self._is_dirty:
     38            self._write_to_backend(self.getvalue())
     39        StringIO.close(self)
  • django/core/filestorage/filesystem.py

     
     1import os
     2import urlparse
     3
     4from django.conf import settings
     5from django.utils.encoding import force_unicode, smart_str
     6from django.core.filestorage.base import Backend
     7from django.utils.text import force_unicode
     8
     9class FileSystemBackend(Backend):
     10    """Standard filesystem storage"""
     11
     12    def __init__(self, location=settings.MEDIA_ROOT, base_url=settings.MEDIA_URL):
     13        self.location = os.path.abspath(location)
     14        self.base_url = base_url
     15
     16    def get_absolute_path(self, filename):
     17        return os.path.normpath(os.path.join(self.location, filename))
     18
     19    def get_filesize(self, filename):
     20        return os.path.getsize(self.get_absolute_path(filename))
     21
     22    def get_absolute_url(self, filename):
     23        return urlparse.urljoin(self.base_url, filename).replace('\\', '/')
     24
     25    def open_file(self, filename, mode='rb'):
     26        return open(self.get_absolute_path(filename), mode)
     27
     28    def file_exists(self, filename):
     29        return os.path.exists(self.get_absolute_path(filename))
     30
     31    def save_file(self, filename, raw_contents):
     32        try: # Create the destination directory if it doesn't exist.
     33            os.makedirs(os.path.join(self.location, os.path.dirname(filename)))
     34        except OSError: # Directory probably already exists.
     35            pass
     36        filename = self.get_available_filename(filename)
     37
     38        # Write the file to disk.
     39        fp = self.open_file(filename, 'wb')
     40        fp.write(raw_contents)
     41        fp.close()
     42
     43        # Store filenames with forward slashes, even on Windows
     44        return force_unicode(filename.replace('\\', '/'))
     45
     46    def delete_file(self, filename):
     47        file_name = self.get_absolute_path(filename)
     48        # If the file exists, delete it from the filesystem.
     49        if os.path.exists(file_name):
     50            os.remove(file_name)
  • django/core/filestorage/s3.py

     
     1from mimetypes import guess_type
     2from StringIO import StringIO
     3import urlparse
     4import os
     5
     6from django.core.exceptions import ImproperlyConfigured
     7from django.core.filestorage.base import Backend, RemoteFile
     8from django.utils.functional import curry
     9
     10ACCESS_KEY_NAME = 'AWS_ACCESS_KEY_ID'
     11SECRET_KEY_NAME = 'AWS_SECRET_ACCESS_KEY'
     12
     13try:
     14    from boto.s3.connection import S3Connection
     15except ImportError:
     16    raise ImproperlyConfigured, "Could not load boto's S3 bindings."
     17
     18class S3Backend(Backend):
     19    """Amazon Simple Storage Service"""
     20
     21    def __init__(self, bucket, access_key=None, secret_key=None, acl='public-read'):
     22        self.bucket = bucket
     23
     24        if not access_key and not secret_key:
     25             access_key, secret_key = self._get_access_keys()
     26
     27        self.connection = S3Connection(access_key, secret_key)
     28        self.generator = S3.QueryStringAuthGenerator(access_key, secret_key)
     29
     30    def _get_access_keys(self):
     31        access_key = getattr(settings, ACCESS_KEY_NAME, None)
     32        secret_key = getattr(settings, SECRET_KEY_NAME, None)
     33        if (access_key or secret_key) and (not access_key or not secret_key):
     34            access_key = os.environ.get(ACCESS_KEY_NAME)
     35            secret_key = os.environ.get(SECRET_KEY_NAME)
     36
     37        if access_key and secret_key:
     38            # Both were provided, so use them
     39            return access_key, secret_key
     40
     41        return None, None
     42
     43    def _get_connection(self):
     44        return S3Connection(*self._get_access_keys())
     45
     46    def _put_file(self, filename, raw_contents):
     47        content_type = guess_type(filename)[0] or "application/x-octet-stream"
     48        headers = {'x-amz-acl':  self.acl, 'Content-Type': content_type}
     49        self.connection.put(self.bucket, self.filename, raw_contents, headers)
     50
     51    def get_filename(self, filename):
     52        return urlparse.urljoin('/%s/' % self.bucket, filename)
     53
     54    def get_absolute_url(self, filename):
     55        return self.generator.make_bare_url(self.bucket, filename)
     56
     57    def get_filesize(self, filename):
     58        response = self.connection.make_request('HEAD', self.bucket, filename)
     59        return int(response.getheader('Content-Length'))
     60
     61    def open_file(self, filename, mode='rb'):
     62        response = self.connection.get(self.bucket, filename)
     63        writer = curry(self._put_file, filename)
     64        return RemoteFile(self, response.object.data, mode, writer)
     65
     66    def file_exists(self, filename):
     67        response = self.connection.make_request('HEAD', self.bucket, filename)
     68        return response.status == 200
     69
     70    def save_file(self, filename, raw_contents):
     71        filename = self.get_available_filename(filename)
     72        self._put_file(filename, raw_contents)
     73        return filename
     74
     75    def delete_file(self, filename):
     76        self.connection.delete(self.bucket, self.key)
  • django/core/filestorage/base.py

     
     1from StringIO import StringIO
     2
     3from django.utils.text import get_valid_filename
     4
     5class Backend(object):
     6    def get_valid_filename(self, filename):
     7        return get_valid_filename(filename)
     8
     9    def get_available_filename(self, filename):
     10        # If the filename already exists, keep adding an underscore to the name
     11        # of the file until the filename doesn't exist.
     12        while self.file_exists(filename):
     13            try:
     14                dot_index = filename.rindex('.')
     15            except ValueError: # filename has no dot
     16                filename += '_'
     17            else:
     18                filename = filename[:dot_index] + '_' + filename[dot_index:]
     19        return filename
     20
     21class RemoteFile(StringIO):
     22    """Sends files to a remote backend automatically, when necessary."""
     23
     24    def __init__(self, data, mode, writer):
     25        self._mode = mode
     26        self._write_to_backend = writer
     27        self._is_dirty = False
     28        StringIO.__init__(self, data)
     29
     30    def write(self, data):
     31        if 'w' not in self._mode:
     32            raise AttributeError, "File was opened for read-only access."
     33        StringIO.write(self, data)
     34        self._is_dirty = True
     35
     36    def close(self):
     37        if self._is_dirty:
     38            self._write_to_backend(self.getvalue())
     39        StringIO.close(self)
  • django/core/filestorage/filesystem.py

     
     1import os
     2import urlparse
     3
     4from django.conf import settings
     5from django.utils.encoding import force_unicode, smart_str
     6from django.core.filestorage.base import Backend
     7from django.utils.text import force_unicode
     8
     9class FileSystemBackend(Backend):
     10    """Standard filesystem storage"""
     11
     12    def __init__(self, location=settings.MEDIA_ROOT, base_url=settings.MEDIA_URL):
     13        self.location = os.path.abspath(location)
     14        self.base_url = base_url
     15
     16    def get_absolute_path(self, filename):
     17        return os.path.normpath(os.path.join(self.location, filename))
     18
     19    def get_filesize(self, filename):
     20        return os.path.getsize(self.get_absolute_path(filename))
     21
     22    def get_absolute_url(self, filename):
     23        return urlparse.urljoin(self.base_url, filename).replace('\\', '/')
     24
     25    def open_file(self, filename, mode='rb'):
     26        return open(self.get_absolute_path(filename), mode)
     27
     28    def file_exists(self, filename):
     29        return os.path.exists(self.get_absolute_path(filename))
     30
     31    def save_file(self, filename, raw_contents):
     32        try: # Create the destination directory if it doesn't exist.
     33            os.makedirs(os.path.join(self.location, os.path.dirname(filename)))
     34        except OSError: # Directory probably already exists.
     35            pass
     36        filename = self.get_available_filename(filename)
     37
     38        # Write the file to disk.
     39        fp = self.open_file(filename, 'wb')
     40        fp.write(raw_contents)
     41        fp.close()
     42
     43        # Store filenames with forward slashes, even on Windows
     44        return force_unicode(filename.replace('\\', '/'))
     45
     46    def delete_file(self, filename):
     47        file_name = self.get_absolute_path(filename)
     48        # If the file exists, delete it from the filesystem.
     49        if os.path.exists(file_name):
     50            os.remove(file_name)
  • django/db/models/__init__.py

     
    88from django.db.models.base import Model, AdminOptions
    99from django.db.models.fields import *
    1010from django.db.models.fields.subclassing import SubfieldBase
     11from django.db.models.fields.files import FileField, ImageField
    1112from django.db.models.fields.related import ForeignKey, OneToOneField, ManyToManyField, ManyToOneRel, ManyToManyRel, OneToOneRel, TABULAR, STACKED
    1213from django.db.models import signals
    1314from django.utils.functional import curry
  • django/db/models/base.py

     
    22import django.db.models.manager
    33from django.core import validators
    44from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
    5 from django.db.models.fields import AutoField, ImageField, FieldDoesNotExist
     5from django.db.models.fields import AutoField, FieldDoesNotExist
    66from django.db.models.fields.related import OneToOneRel, ManyToOneRel
    77from django.db.models.query import delete_objects
    88from django.db.models.options import Options, AdminOptions
     
    1818import types
    1919import sys
    2020import os
     21from warnings import warn
    2122
    2223class ModelBase(type):
    2324    "Metaclass for all models"
     
    368369        return getattr(self, cachename)
    369370
    370371    def _get_FIELD_filename(self, field):
    371         if getattr(self, field.attname): # value is not blank
    372             return os.path.join(settings.MEDIA_ROOT, getattr(self, field.attname))
    373         return ''
     372        warn("Use instance.%s.get_absolute_path()." % field.attname, DeprecationWarning)
     373        try:
     374            return getattr(self, field.attname).get_absolute_path()
     375        except ValueError:
     376            # For backward compatibility
     377            return settings.MEDIA_ROOT
    374378
    375379    def _get_FIELD_url(self, field):
    376         if getattr(self, field.attname): # value is not blank
    377             import urlparse
    378             return urlparse.urljoin(settings.MEDIA_URL, getattr(self, field.attname)).replace('\\', '/')
    379         return ''
     380        warn("Use instance.%s.get_absolute_url()." % field.attname, DeprecationWarning)
     381        try:
     382            return getattr(self, field.attname).get_absolute_url()
     383        except ValueError:
     384            # For backward compatibility
     385            return settings.MEDIA_URL
    380386
    381387    def _get_FIELD_size(self, field):
    382         return os.path.getsize(self._get_FIELD_filename(field))
     388        warn("Use instance.%s.get_filesize()." % field.attname, DeprecationWarning)
     389        return getattr(self, field.attname).get_filesize()
    383390
    384391    def _save_FIELD_file(self, field, filename, raw_contents, save=True):
    385         directory = field.get_directory_name()
    386         try: # Create the date-based directory if it doesn't exist.
    387             os.makedirs(os.path.join(settings.MEDIA_ROOT, directory))
    388         except OSError: # Directory probably already exists.
    389             pass
    390         filename = field.get_filename(filename)
     392        warn("Use instance.%s.save_file()." % field.attname, DeprecationWarning)
     393        return getattr(self, field.attname).save_file(filename, raw_contents, save)
    391394
    392         # If the filename already exists, keep adding an underscore to the name of
    393         # the file until the filename doesn't exist.
    394         while os.path.exists(os.path.join(settings.MEDIA_ROOT, filename)):
    395             try:
    396                 dot_index = filename.rindex('.')
    397             except ValueError: # filename has no dot
    398                 filename += '_'
    399             else:
    400                 filename = filename[:dot_index] + '_' + filename[dot_index:]
    401 
    402         # Write the file to disk.
    403         setattr(self, field.attname, filename)
    404 
    405         full_filename = self._get_FIELD_filename(field)
    406         fp = open(full_filename, 'wb')
    407         fp.write(raw_contents)
    408         fp.close()
    409 
    410         # Save the width and/or height, if applicable.
    411         if isinstance(field, ImageField) and (field.width_field or field.height_field):
    412             from django.utils.images import get_image_dimensions
    413             width, height = get_image_dimensions(full_filename)
    414             if field.width_field:
    415                 setattr(self, field.width_field, width)
    416             if field.height_field:
    417                 setattr(self, field.height_field, height)
    418 
    419         # Save the object because it has changed unless save is False
    420         if save:
    421             self.save()
    422 
    423     _save_FIELD_file.alters_data = True
    424 
    425395    def _get_FIELD_width(self, field):
    426         return self._get_image_dimensions(field)[0]
     396        warn("Use instance.%s.get_width()." % field.attname, DeprecationWarning)
     397        return getattr(self, field.attname).get_width()
    427398
    428399    def _get_FIELD_height(self, field):
    429         return self._get_image_dimensions(field)[1]
     400        warn("Use instance.%s.get_height()." % field.attname, DeprecationWarning)
     401        return getattr(self, field.attname).get_height()
    430402
    431     def _get_image_dimensions(self, field):
    432         cachename = "__%s_dimensions_cache" % field.name
    433         if not hasattr(self, cachename):
    434             from django.utils.images import get_image_dimensions
    435             filename = self._get_FIELD_filename(field)
    436             setattr(self, cachename, get_image_dimensions(filename))
    437         return getattr(self, cachename)
    438 
    439403############################################
    440404# HELPER FUNCTIONS (CURRIED MODEL METHODS) #
    441405############################################
  • django/db/models/fields/__init__.py

     
    11import datetime
    2 import os
    32import time
    43try:
    54    import decimal
     
    276275        name_prefix is a prefix to prepend to the "field_name" argument.
    277276        rel is a boolean specifying whether this field is in a related context.
    278277        """
     278        from django.db.models.fields import files
     279
    279280        field_objs, params = self.prepare_field_objs_and_params(manipulator, name_prefix)
    280281
    281282        # Add the "unique" validator(s).
     
    307308        # If this field is in a related context, check whether any other fields
    308309        # in the related object have core=True. If so, add a validator --
    309310        # RequiredIfOtherFieldsGiven -- to this FormField.
    310         if rel and not self.blank and not isinstance(self, AutoField) and not isinstance(self, FileField):
     311        if rel and not self.blank and not isinstance(self, AutoField) and not isinstance(self, files.FileField):
    311312            # First, get the core fields, if any.
    312313            core_field_names = []
    313314            for f in opts.fields:
     
    707708        defaults.update(kwargs)
    708709        return super(EmailField, self).formfield(**defaults)
    709710
    710 class FileField(Field):
    711     def __init__(self, verbose_name=None, name=None, upload_to='', **kwargs):
    712         self.upload_to = upload_to
    713         kwargs['max_length'] = kwargs.get('max_length', 100)
    714         Field.__init__(self, verbose_name, name, **kwargs)
    715 
    716     def get_db_prep_save(self, value):
    717         "Returns field's value prepared for saving into a database."
    718         # Need to convert UploadedFile objects provided via a form to unicode for database insertion
    719         if value is None:
    720             return None
    721         return unicode(value)
    722 
    723     def get_manipulator_fields(self, opts, manipulator, change, name_prefix='', rel=False, follow=True):
    724         field_list = Field.get_manipulator_fields(self, opts, manipulator, change, name_prefix, rel, follow)
    725         if not self.blank:
    726             if rel:
    727                 # This validator makes sure FileFields work in a related context.
    728                 class RequiredFileField(object):
    729                     def __init__(self, other_field_names, other_file_field_name):
    730                         self.other_field_names = other_field_names
    731                         self.other_file_field_name = other_file_field_name
    732                         self.always_test = True
    733                     def __call__(self, field_data, all_data):
    734                         if not all_data.get(self.other_file_field_name, False):
    735                             c = validators.RequiredIfOtherFieldsGiven(self.other_field_names, ugettext_lazy("This field is required."))
    736                             c(field_data, all_data)
    737                 # First, get the core fields, if any.
    738                 core_field_names = []
    739                 for f in opts.fields:
    740                     if f.core and f != self:
    741                         core_field_names.extend(f.get_manipulator_field_names(name_prefix))
    742                 # Now, if there are any, add the validator to this FormField.
    743                 if core_field_names:
    744                     field_list[0].validator_list.append(RequiredFileField(core_field_names, field_list[1].field_name))
    745             else:
    746                 v = validators.RequiredIfOtherFieldNotGiven(field_list[1].field_name, ugettext_lazy("This field is required."))
    747                 v.always_test = True
    748                 field_list[0].validator_list.append(v)
    749                 field_list[0].is_required = field_list[1].is_required = False
    750 
    751         # If the raw path is passed in, validate it's under the MEDIA_ROOT.
    752         def isWithinMediaRoot(field_data, all_data):
    753             f = os.path.abspath(os.path.join(settings.MEDIA_ROOT, field_data))
    754             if not f.startswith(os.path.abspath(os.path.normpath(settings.MEDIA_ROOT))):
    755                 raise validators.ValidationError, _("Enter a valid filename.")
    756         field_list[1].validator_list.append(isWithinMediaRoot)
    757         return field_list
    758 
    759     def contribute_to_class(self, cls, name):
    760         super(FileField, self).contribute_to_class(cls, name)
    761         setattr(cls, 'get_%s_filename' % self.name, curry(cls._get_FIELD_filename, field=self))
    762         setattr(cls, 'get_%s_url' % self.name, curry(cls._get_FIELD_url, field=self))
    763         setattr(cls, 'get_%s_size' % self.name, curry(cls._get_FIELD_size, field=self))
    764         setattr(cls, 'save_%s_file' % self.name, lambda instance, filename, raw_contents, save=True: instance._save_FIELD_file(self, filename, raw_contents, save))
    765         dispatcher.connect(self.delete_file, signal=signals.post_delete, sender=cls)
    766 
    767     def delete_file(self, instance):
    768         if getattr(instance, self.attname):
    769             file_name = getattr(instance, 'get_%s_filename' % self.name)()
    770             # If the file exists and no other object of this type references it,
    771             # delete it from the filesystem.
    772             if os.path.exists(file_name) and \
    773                 not instance.__class__._default_manager.filter(**{'%s__exact' % self.name: getattr(instance, self.attname)}):
    774                 os.remove(file_name)
    775 
    776     def get_manipulator_field_objs(self):
    777         return [oldforms.FileUploadField, oldforms.HiddenField]
    778 
    779     def get_manipulator_field_names(self, name_prefix):
    780         return [name_prefix + self.name + '_file', name_prefix + self.name]
    781 
    782     def save_file(self, new_data, new_object, original_object, change, rel, save=True):
    783         upload_field_name = self.get_manipulator_field_names('')[0]
    784         if new_data.get(upload_field_name, False):
    785             func = getattr(new_object, 'save_%s_file' % self.name)
    786             if rel:
    787                 func(new_data[upload_field_name][0]["filename"], new_data[upload_field_name][0]["content"], save)
    788             else:
    789                 func(new_data[upload_field_name]["filename"], new_data[upload_field_name]["content"], save)
    790 
    791     def get_directory_name(self):
    792         return os.path.normpath(force_unicode(datetime.datetime.now().strftime(smart_str(self.upload_to))))
    793 
    794     def get_filename(self, filename):
    795         from django.utils.text import get_valid_filename
    796         f = os.path.join(self.get_directory_name(), get_valid_filename(os.path.basename(filename)))
    797         return os.path.normpath(f)
    798 
    799     def save_form_data(self, instance, data):
    800         if data:
    801             getattr(instance, "save_%s_file" % self.name)(data.filename, data.content, save=False)
    802 
    803     def formfield(self, **kwargs):
    804         defaults = {'form_class': forms.FileField}
    805         # If a file has been provided previously, then the form doesn't require
    806         # that a new file is provided this time.
    807         if 'initial' in kwargs:
    808             defaults['required'] = False
    809         defaults.update(kwargs)
    810         return super(FileField, self).formfield(**defaults)
    811 
    812711class FilePathField(Field):
    813712    def __init__(self, verbose_name=None, name=None, path='', match=None, recursive=False, **kwargs):
    814713        self.path, self.match, self.recursive = path, match, recursive
     
    829728        defaults.update(kwargs)
    830729        return super(FloatField, self).formfield(**defaults)
    831730
    832 class ImageField(FileField):
    833     def __init__(self, verbose_name=None, name=None, width_field=None, height_field=None, **kwargs):
    834         self.width_field, self.height_field = width_field, height_field
    835         FileField.__init__(self, verbose_name, name, **kwargs)
    836 
    837     def get_manipulator_field_objs(self):
    838         return [oldforms.ImageUploadField, oldforms.HiddenField]
    839 
    840     def contribute_to_class(self, cls, name):
    841         super(ImageField, self).contribute_to_class(cls, name)
    842         # Add get_BLAH_width and get_BLAH_height methods, but only if the
    843         # image field doesn't have width and height cache fields.
    844         if not self.width_field:
    845             setattr(cls, 'get_%s_width' % self.name, curry(cls._get_FIELD_width, field=self))
    846         if not self.height_field:
    847             setattr(cls, 'get_%s_height' % self.name, curry(cls._get_FIELD_height, field=self))
    848 
    849     def save_file(self, new_data, new_object, original_object, change, rel, save=True):
    850         FileField.save_file(self, new_data, new_object, original_object, change, rel, save)
    851         # If the image has height and/or width field(s) and they haven't
    852         # changed, set the width and/or height field(s) back to their original
    853         # values.
    854         if change and (self.width_field or self.height_field) and save:
    855             if self.width_field:
    856                 setattr(new_object, self.width_field, getattr(original_object, self.width_field))
    857             if self.height_field:
    858                 setattr(new_object, self.height_field, getattr(original_object, self.height_field))
    859             new_object.save()
    860 
    861     def formfield(self, **kwargs):
    862         defaults = {'form_class': forms.ImageField}
    863         defaults.update(kwargs)
    864         return super(ImageField, self).formfield(**defaults)
    865 
    866731class IntegerField(Field):
    867732    empty_strings_allowed = False
    868733    def get_manipulator_field_objs(self):
  • django/db/models/fields/files.py

     
     1import datetime
     2import os
     3
     4from django.db.models.fields import Field
     5from django.core.filestorage.filesystem import FileSystemBackend
     6from django.utils.functional import curry
     7from django.dispatch import dispatcher
     8from django.db.models import signals
     9from django.utils.encoding import force_unicode, smart_str
     10from django.utils.translation import ugettext_lazy, ugettext as _
     11from django import oldforms
     12from django import newforms as forms
     13from django.core import validators
     14
     15class File(object):
     16    def __init__(self, obj, field, filename):
     17        self.obj = obj
     18        self.field = field
     19        self.backend = field.backend
     20        self.filename = filename or None
     21
     22    def __str__(self):
     23        return self.filename or ''
     24
     25    def __repr__(self):
     26        return smart_str(u'<%s: %s>' % (self.__class__.__name__, unicode(self)))
     27
     28    def __nonzero__(self):
     29        return not not self.filename
     30
     31    def __eq__(self, other):
     32        return True
     33        return self.filename == other
     34
     35    def get_absolute_path(self):
     36        if not self:
     37            raise ValueError, "The '%s' attribute has no file associated with it." % self.field.name
     38        return self.backend.get_absolute_path(self.filename)
     39
     40    def get_absolute_url(self):
     41        if not self:
     42            raise ValueError, "The '%s' attribute has no file associated with it." % self.field.name
     43        return self.backend.get_absolute_url(self.filename)
     44
     45    def get_filesize(self):
     46        if not self:
     47            raise ValueError, "The '%s' attribute has no file associated with it." % self.field.name
     48        if not hasattr(self, '_filesize'):
     49            self._filesize = self.backend.get_filesize(self.filename)
     50        return self._filesize
     51
     52    def open_file(self, mode='rb'):
     53        if not self:
     54            raise ValueError, "The '%s' attribute has no file associated with it." % self.field.name
     55        return self.backend.open_file(self.filename, mode)
     56
     57    def save_file(self, filename, raw_contents, save=True):
     58        filename = self.field.generate_filename(self.obj, filename)
     59        self.filename = self.backend.save_file(filename, raw_contents)
     60        self._has_file = True
     61
     62        # Update the filesize cache
     63        self._filesize = len(raw_contents)
     64
     65        # Save the object because it has changed, unless save is False
     66        if save:
     67            self.obj.save()
     68
     69    def delete_file(self, save=True):
     70        if not self:
     71            raise ValueError, "The '%s' attribute has no file associated with it." % self.field.name
     72        self.backend.delete_file(self.filename)
     73
     74        self.filename = None
     75
     76        # Delete the filesize cache
     77        if hasattr(self, '_filesize'):
     78            del self._filesize
     79
     80        if save:
     81            self.obj.save()
     82
     83class FileProxy(object):
     84    def __init__(self, field):
     85        self.field = field
     86
     87    def __get__(self, instance=None, owner=None):
     88        if instance is None:
     89            raise AttributeError, "%s can only be accessed from %s instances." % (self.field.name, self.owner.__name__)
     90        return instance.__dict__[self.field.name]
     91
     92    def __set__(self, instance, value):
     93        attr = self.field.attr_class(instance, self.field, value)
     94        instance.__dict__[self.field.name] = attr
     95
     96class FileField(Field):
     97    attr_class = File
     98
     99    def __init__(self, verbose_name=None, name=None, upload_to='', backend=None, **kwargs):
     100        for arg in ('core', 'primary_key', 'unique'):
     101            if arg in kwargs:
     102                raise TypeError, "__init__() got an unexpected keyword argument '%s'" % arg
     103        self.backend = backend or FileSystemBackend()
     104
     105        self.upload_to = upload_to
     106        if callable(upload_to):
     107            self.generate_filename = upload_to
     108
     109        kwargs['max_length'] = kwargs.get('max_length', 100)
     110        super(FileField, self).__init__(verbose_name, name, **kwargs)
     111
     112    def get_db_prep_lookup(self, lookup_type, value):
     113        if hasattr(value, 'filename'):
     114            value = value.filename
     115        return super(FileField, self).get_db_prep_lookup(lookup_type, value)
     116
     117    def get_db_prep_save(self, value):
     118        "Returns field's value prepared for saving into a database."
     119        # Need to convert UploadedFile objects provided via a form to unicode for database insertion
     120        if value is None:
     121            return None
     122        return unicode(value.filename)
     123
     124    def get_manipulator_fields(self, opts, manipulator, change, name_prefix='', rel=False, follow=True):
     125        field_list = Field.get_manipulator_fields(self, opts, manipulator, change, name_prefix, rel, follow)
     126        if not self.blank:
     127            if rel:
     128                # This validator makes sure FileFields work in a related context.
     129                class RequiredFileField(object):
     130                    def __init__(self, other_field_names, other_file_field_name):
     131                        self.other_field_names = other_field_names
     132                        self.other_file_field_name = other_file_field_name
     133                        self.always_test = True
     134                    def __call__(self, field_data, all_data):
     135                        if not all_data.get(self.other_file_field_name, False):
     136                            c = validators.RequiredIfOtherFieldsGiven(self.other_field_names, ugettext_lazy("This field is required."))
     137                            c(field_data, all_data)
     138                # First, get the core fields, if any.
     139                core_field_names = []
     140                for f in opts.fields:
     141                    if f.core and f != self:
     142                        core_field_names.extend(f.get_manipulator_field_names(name_prefix))
     143                # Now, if there are any, add the validator to this FormField.
     144                if core_field_names:
     145                    field_list[0].validator_list.append(RequiredFileField(core_field_names, field_list[1].field_name))
     146            else:
     147                v = validators.RequiredIfOtherFieldNotGiven(field_list[1].field_name, ugettext_lazy("This field is required."))
     148                v.always_test = True
     149                field_list[0].validator_list.append(v)
     150                field_list[0].is_required = field_list[1].is_required = False
     151
     152        # If the raw path is passed in, validate it's under the MEDIA_ROOT.
     153        def isWithinMediaRoot(field_data, all_data):
     154            f = os.path.abspath(os.path.join(settings.MEDIA_ROOT, field_data))
     155            if not f.startswith(os.path.abspath(os.path.normpath(settings.MEDIA_ROOT))):
     156                raise validators.ValidationError, _("Enter a valid filename.")
     157        field_list[1].validator_list.append(isWithinMediaRoot)
     158        return field_list
     159
     160    def contribute_to_class(self, cls, name):
     161        super(FileField, self).contribute_to_class(cls, name)
     162        setattr(cls, self.name, FileProxy(self))
     163        setattr(cls, 'get_%s_filename' % self.name, curry(cls._get_FIELD_filename, field=self))
     164        setattr(cls, 'get_%s_url' % self.name, curry(cls._get_FIELD_url, field=self))
     165        setattr(cls, 'get_%s_size' % self.name, curry(cls._get_FIELD_size, field=self))
     166        setattr(cls, 'save_%s_file' % self.name, lambda instance, filename, raw_contents, save=True: instance._save_FIELD_file(self, filename, raw_contents, save))
     167        dispatcher.connect(self.delete_file, signal=signals.post_delete, sender=cls)
     168
     169    def delete_file(self, instance, sender):
     170        filename = getattr(instance, self.attname).filename
     171        # If no other object of this type references the file,
     172        # delete it from the backend.
     173        if not sender._default_manager.filter(**{self.name: filename}):
     174            self.backend.delete_file(filename)
     175
     176    def get_manipulator_field_objs(self):
     177        return [oldforms.FileUploadField, oldforms.HiddenField]
     178
     179    def get_manipulator_field_names(self, name_prefix):
     180        return [name_prefix + self.name + '_file', name_prefix + self.name]
     181
     182    def save_file(self, new_data, new_object, original_object, change, rel, save=True):
     183        upload_field_name = self.get_manipulator_field_names('')[0]
     184        if new_data.get(upload_field_name, False):
     185            if rel:
     186                field = new_data[upload_field_name][0]
     187            else:
     188                field = new_data[upload_field_name]
     189            filename = self.get_filename(field["filename"])
     190            getattr(new_object, self.attname).save_file(filename, field["content"], save)
     191
     192    def get_directory_name(self):
     193        return os.path.normpath(force_unicode(datetime.datetime.now().strftime(smart_str(self.upload_to))))
     194
     195    def get_filename(self, filename):
     196        return os.path.normpath(self.backend.get_valid_filename(os.path.basename(filename)))
     197
     198    def generate_filename(self, obj, filename):
     199        return os.path.join(self.get_directory_name(), self.get_filename(filename))
     200
     201    def save_form_data(self, instance, data):
     202        if data:
     203            getattr(instance, self.attname).save_file(data.filename, data.content, save=False)
     204
     205    def formfield(self, **kwargs):
     206        defaults = {'form_class': forms.FileField}
     207        # If a file has been provided previously, then the form doesn't require
     208        # that a new file is provided this time.
     209        if 'initial' in kwargs:
     210            defaults['required'] = False
     211        defaults.update(kwargs)
     212        return super(FileField, self).formfield(**defaults)
     213
     214class ImageFile(File):
     215    def get_width(self):
     216        return self._get_image_dimensions()[0]
     217
     218    def get_height(self):
     219        return self._get_image_dimensions()[1]
     220
     221    def _get_image_dimensions(self):
     222        if not hasattr(self, '_dimensions_cache'):
     223            from django.utils.images import get_image_dimensions
     224            self._dimensions_cache = get_image_dimensions(self.open_file())
     225        return self._dimensions_cache
     226
     227    def save_file(self, filename, raw_contents, save=True):
     228        super(ImageFile, self).save_file(filename, raw_contents, save)
     229       
     230        # Update the cache for image dimensions
     231        from django.utils.images import get_image_dimensions
     232        from cStringIO import StringIO
     233        self._dimensions_cache = get_image_dimensions(StringIO(raw_contents))
     234
     235    def delete_file(self, save=True):
     236        # Clear the image dimensions cache
     237        del self._dimensions_cache
     238
     239        super(ImageFile, self).delete_file(save)
     240
     241class ImageField(FileField):
     242    attr_class = ImageFile
     243
     244    def __init__(self, verbose_name=None, name=None, width_field=None, height_field=None, **kwargs):
     245        self.width_field, self.height_field = width_field, height_field
     246        FileField.__init__(self, verbose_name, name, **kwargs)
     247
     248    def get_manipulator_field_objs(self):
     249        return [oldforms.ImageUploadField, oldforms.HiddenField]
     250
     251    def contribute_to_class(self, cls, name):
     252        super(ImageField, self).contribute_to_class(cls, name)
     253        # Add get_BLAH_width and get_BLAH_height methods, but only if the
     254        # image field doesn't have width and height cache fields.
     255        if not self.width_field:
     256            setattr(cls, 'get_%s_width' % self.name, curry(cls._get_FIELD_width, field=self))
     257        if not self.height_field:
     258            setattr(cls, 'get_%s_height' % self.name, curry(cls._get_FIELD_height, field=self))
     259
     260    def save_file(self, new_data, new_object, original_object, change, rel, save=True):
     261        # If the image has height and/or width field(s) and they haven't
     262        # changed, set the width and/or height field(s) back to their original
     263        # values.
     264        if self.width_field or self.height_field:
     265            if original_object and not change:
     266                if self.width_field:
     267                    setattr(new_object, self.width_field, getattr(original_object, self.width_field))
     268                if self.height_field:
     269                    setattr(new_object, self.height_field, getattr(original_object, self.height_field))
     270            else:
     271                from cStringIO import StringIO
     272                from django.utils.images import get_image_dimensions
     273
     274                upload_field_name = self.get_manipulator_field_names('')[0]
     275                if rel:
     276                    field = new_data[upload_field_name][0]
     277                else:
     278                    field = new_data[upload_field_name]
     279
     280                # Get the width and height from the raw content to avoid extra
     281                # unnecessary trips to the file backend.
     282                width, height = get_image_dimensions(StringIO(field["content"]))
     283
     284                if self.width_field:
     285                    setattr(new_object, self.width_field, width)
     286                if self.height_field:
     287                    setattr(new_object, self.height_field, height)
     288        FileField.save_file(self, new_data, new_object, original_object, change, rel, save)
     289
     290    def formfield(self, **kwargs):
     291        defaults = {'form_class': forms.ImageField}
     292        defaults.update(kwargs)
     293        return super(ImageField, self).formfield(**defaults)
  • django/db/models/manipulators.py

     
    11from django.core.exceptions import ObjectDoesNotExist
    22from django import oldforms
    33from django.core import validators
    4 from django.db.models.fields import FileField, AutoField
     4from django.db.models.fields import AutoField
     5from django.db.models.fields.files import FileField
    56from django.dispatch import dispatcher
    67from django.db.models import signals
    78from django.utils.functional import curry
  • django/utils/images.py

     
    66
    77import ImageFile
    88
    9 def get_image_dimensions(path):
    10     """Returns the (width, height) of an image at a given path."""
     9def get_image_dimensions(file_or_path):
     10    """Returns the (width, height) of an image, given an open file or a path."""
    1111    p = ImageFile.Parser()
    12     fp = open(path, 'rb')
     12    if hasattr(file_or_path, 'read'):
     13        fp = file_or_path
     14    else:
     15        fp = open(file_or_path, 'rb')
    1316    while 1:
    1417        data = fp.read(1024)
    1518        if not data:
  • docs/db-api.txt

     
    18711871get_FOO_filename()
    18721872------------------
    18731873
     1874**Deprecated in Django development version. See `managing files`_ for the new,
     1875preferred method for dealing with files.**
     1876
    18741877For every ``FileField``, the object will have a ``get_FOO_filename()`` method,
    18751878where ``FOO`` is the name of the field. This returns the full filesystem path
    18761879to the file, according to your ``MEDIA_ROOT`` setting.
     
    18811884get_FOO_url()
    18821885-------------
    18831886
     1887**Deprecated in Django development version. See `managing files`_ for the new,
     1888preferred method for dealing with files.**
     1889
    18841890For every ``FileField``, the object will have a ``get_FOO_url()`` method,
    18851891where ``FOO`` is the name of the field. This returns the full URL to the file,
    18861892according to your ``MEDIA_URL`` setting. If the value is blank, this method
     
    18891895get_FOO_size()
    18901896--------------
    18911897
     1898**Deprecated in Django development version. See `managing files`_ for the new,
     1899preferred method for dealing with files.**
     1900
    18921901For every ``FileField``, the object will have a ``get_FOO_size()`` method,
    18931902where ``FOO`` is the name of the field. This returns the size of the file, in
    18941903bytes. (Behind the scenes, it uses ``os.path.getsize``.)
     
    18961905save_FOO_file(filename, raw_contents)
    18971906-------------------------------------
    18981907
     1908**Deprecated in Django development version. See `managing files`_ for the new,
     1909preferred method for dealing with files.**
     1910
    18991911For every ``FileField``, the object will have a ``save_FOO_file()`` method,
    19001912where ``FOO`` is the name of the field. This saves the given file to the
    19011913filesystem, using the given filename. If a file with the given filename already
     
    19051917get_FOO_height() and get_FOO_width()
    19061918------------------------------------
    19071919
     1920**Deprecated in Django development version. See `managing files`_ for the new,
     1921preferred method for dealing with files.**
     1922
    19081923For every ``ImageField``, the object will have ``get_FOO_height()`` and
    19091924``get_FOO_width()`` methods, where ``FOO`` is the name of the field. This
    19101925returns the height (or width) of the image, as an integer, in pixels.
    19111926
     1927.. _`managing files`: ../files/
     1928
    19121929Shortcuts
    19131930=========
    19141931
  • docs/files.txt

     
     1==============
     2Managing files
     3==============
     4
     5When dealing with files, Django provides a number of features to make this task
     6easier and more portable. A backend protocol is available to allow files to be
     7stored in a variety of locations, and a special object is provided to allow
     8models to make use of this protocol, without having to worry about which storage
     9system is being used.
     10
     11Using files in models
     12=====================
     13
     14When accessing a ``FileField`` attached to a model, a special object provides
     15access to the file and information about it.
     16
     17get_absolute_path()
     18-------------------
     19
     20Returns the absolute path to the file's location on a local filesystem. For
     21backends which do not store files locally, this will return `None`.
     22
     23get_absolute_url()
     24------------------
     25
     26Provides a URL where the content of the file can be retrieved. Therefore,
     27returned from this method is suitable for use as the destination of a link to
     28the file.
     29
     30get_filesize()
     31--------------
     32
     33Returns the size of the file, as an integer.
     34
     35open_file(mode='rb')
     36--------------------
     37
     38Returns an open file object, providing read or write access to the file's
     39contents. The ``mode`` argument allows the same values as Python's standard
     40``open()`` function.
     41
     42save_file(filename, raw_contents, save=True)
     43--------------------------------------------
     44
     45Saves a new file with the filename and contents provided. This will not replace
     46the existing file, but will create a new file and update the object to point to
     47it. The optional ``save`` argument dictates whether the model instance will be
     48saved to the database immediately.
     49
     50get_width() and get_height()
     51----------------------------
     52
     53When using an ``ImageField``, these two methods will be available, providing
     54easy access to the dimensions of the image.
     55
     56Example
     57-------
     58
     59Consider the following model, using an ``ImageField`` to store a product photo::
     60
     61    class Product(models.Model):
     62        name = models.CharField(maxlength=255)
     63        price = models.DecimalField(max_digits=5, decimal_places=2)
     64        photo = models.ImageField(upload_to='product_photos')
     65
     66Your views can then use the ``photo`` attribute with the functions described
     67above, as follows::
     68
     69    >>> car = Product.object.get(name="'57 Chevy")
     70    >>> car.photo.get_absolute_url()
     71    '/products/photo/123.jpg'
     72    >>> car.photo.get_width(), car.photo.get_height()
     73    (800, 600)
     74
     75Using a storage backend with FileField
     76======================================
     77
     78When using a storage backend, supply whatever options are appropriate for
     79that backend when creating a new object. Details on the requirements for the
     80included backends can be found below. Then pass that object as the ``backend``
     81argument to a ``FileField``.
     82
     83If using the ``FileSystemBackend``, it is not necessary to create a backend
     84object explicitly. Simply supplying the ``upload_to`` argument will create the
     85backend object automatically.
     86
     87See the ```FileField`` documentation`_ for more information on using the field.
     88
     89.. _FileField documentation: ../model-api/#filefield
     90
     91For example, the following code will explicitly use the ``FileSystemBackend``::
     92
     93    from django.db import models
     94    from django.core.filestorage.filesystem import FileSystemBackend
     95   
     96    fs = FileSystemBackend(location='product_photos')
     97   
     98    class Product(models.Model):
     99        name = models.CharField(maxlength=255)
     100        price = models.DecimalField(max_digits=5, decimal_places=2)
     101        photo = models.ImageField(backend=fs)
     102
     103Using a storage backend on its own
     104==================================
     105
     106Storage backends may also be used directly, without being attached to a model.
     107Simply use the following API on any instantiated backend to access files without
     108having to worry about the underlying storage mechanism.
     109       
     110file_exists(filename)
     111---------------------
     112
     113Returns ``True`` or ``False, indicating whether there is already a file present
     114at the location referenced by``filename``.
     115
     116open_file(filename, mode='rb')
     117------------------------------
     118
     119Returns an open file, or file-like, object to provide access to the contents of
     120the file referenced by ``filename``. The ``mode`` argument allows the same
     121values as Python's standard ``open()`` function.
     122
     123get_filesize(filename)
     124----------------------
     125
     126Returns the total size of the file referenced by ``filename``, as an integer.
     127
     128get_absolute_url(filename)
     129--------------------------
     130
     131Returns the URL where the contents of the file referenced by ``filename`` can
     132be accessed.
     133
     134save_file(filename, raw_contents)
     135---------------------------------
     136
     137Saves a new file using the backend-specific storage mechanism, preferably using
     138the name specified. If there already exists a file at the location referenced by
     139``filename``, this may modify the filename as necessary to locate one that is
     140available. Once the file is saved, this method will return the filename where
     141the file was actually stored.
     142
     143delete_file(filename)
     144---------------------
     145
     146Deletes the file referenced by ``filename``. If the file does not already exist,
     147this method will simply return without raising an exception.
     148
     149Available backends
     150==================
     151
     152Only a few storage backends are supplied in the official Django distribution,
     153but more may be available elsewhere. If you'd like to use a different backend
     154than those listed below, see the documentation included with the backend.
     155
     156django.core.filestorage.filesystem.FileSystemBackend
     157----------------------------------------------------
     158
     159This backend stores files on the system's standard filesystem.
     160
     161    ======================  ===================================================
     162    Argument                Description
     163    ======================  ===================================================
     164    ``location``            Required. A local filesystem path that will be
     165                            appended to the ``MEDIA_ROOT`` setting to determine
     166                            the output of the ``get_<fieldname>_url()`` helper
     167                            function.
     168    ``media_root``          Required. Absolute path to the directory that holds
     169                            the files for this backend. If omitted, it will be
     170                            set to the value of your ``MEDIA_ROOT`` setting.
     171    ``media_url``           Optional. URL that serves the files stored in this
     172                            backend. If omitted, it will default to the value
     173                            of your ``MEDIA_URL`` setting.
     174    ======================  ===================================================
     175
     176django.core.filestorage.s3.S3Backend
     177------------------------------------
     178
     179This allows files to be stored using Amazon's `Simple Storage Service`_, using
     180the `boto`_ library to interface with Amazon. It requires at least two arguments
     181in order to connect to your S3 account, with two others available to customize
     182how you'd like it to behave.
     183
     184    ======================  ===================================================
     185    Argument                Description
     186    ======================  ===================================================
     187    ``access_key``          Required. The Access Key ID associated with your S3
     188                            account.
     189    ``secret_key``          Required. The Secret Access Key associated with the
     190                            provided Access Key ID.
     191    ``bucket``              Required. The name of the bucket where files using
     192                            this backend will be stored.
     193    ``acl``                 Optional. The name of one of Amazon's built-in
     194                            `canned access policies`_. If omitted, it will
     195                            default to ``'public-read'``.
     196
     197.. _Simple Storage Service: http://aws.amazon.com/s3
     198.. _boto: http://code.google.com/p/boto/
     199.. _canned access policies: http://docs.amazonwebservices.com/AmazonS3/2006-03-01/RESTAccessPolicy.html
     200
     201Writing a storage backend
     202=========================
     203
     204While the default filesystem storage is suitable for most needs, there are many
     205other storage mechanisms that may be used, and situations that will require
     206special processing. In order to use Django in these environments, it's fairly
     207simple to write a new storage backend, creating a wrapper around whatever
     208libraries are used to access your files, or simply customizing method calls on
     209an existing backend.
     210
     211A backend must implement the methods described above, but the built-in backends
     212also define two other methods to assist in the process. When writing a custom
     213backend from scratch, these methods are available on the provided ``Backend``
     214class, living at ``django.core.filestorage.base``, so subclassing it will allow
     215these methods to be available on the custom backend as well. When extending an
     216existing backend, overriding these methods allow a great deal of customization.
     217
     218get_valid_filename(filename)
     219----------------------------
     220
     221Returns a filename suitable for use with the underlying storage system. The
     222``filename`` argument passed to this method is the original filename sent to the
     223server, after having any path information removed. Override this to customize
     224how non-standard characters are converted to safe filenames.
     225
     226The code provided on ``Backend`` retains only alpha-numeric characters, periods
     227and underscores from the original filename, removing everything else.
     228
     229get_available_filename(filename)
     230--------------------------------
     231
     232Returns a filename that is available in the storage mechanism, possibly taking
     233the provided filename into account. The ``filename`` argument passed to this
     234method will have already cleaned to a filename valid for the storage system,
     235according to the ``get_valid_filename()`` method described above.
     236
     237The code provided on ``Backend`` simply appends underscores to the filename
     238until it finds one that's available in the destination directory.
     239
     240Opening remote files
     241--------------------
     242
     243When accessing a file stored at a remote location, the object returned by
     244``open_file()`` should function like a standard `file object`_, but to keep
     245network traffic to a minimum, writes to the remote storage system should only
     246occur if actually necessary. To make this task easier, Django provides a class
     247to automate this process.
     248
     249Living at ``django.core.filestorage.base``, the ``RemoteFile`` class simulates
     250a standard Python `file object`_, but can write changes to a remote storage
     251system when application using a function provided by the storage backend.
     252Creating an instance of this object requires three arguments, which are
     253desribed below.
     254
     255    ======================  ===================================================
     256    Argument                Description
     257    ======================  ===================================================
     258    ``data``                The raw content of the file.
     259    ``mode``                The access mode that was passed to the
     260                            ``open_file()`` method.
     261    ``writer``              A function that will be used to write the contents
     262                            to the backend-specific storage mechanism. The
     263                            function provided here will need to take a single
     264                            argument, which will be the raw content to be
     265                            written to the file.
     266    ======================  ===================================================
     267
     268.. _file object: http://docs.python.org/lib/bltin-file-objects.html
     269
  • docs/model-api.txt

     
    230230``FileField``
    231231~~~~~~~~~~~~~
    232232
    233 A file-upload field. Has one **required** argument:
     233A file-upload field. **Requires** exactly one of the following two arguments:
    234234
    235235    ======================  ===================================================
    236236    Argument                Description
    237237    ======================  ===================================================
    238238    ``upload_to``           A local filesystem path that will be appended to
    239239                            your ``MEDIA_ROOT`` setting to determine the
    240                             output of the ``get_<fieldname>_url()`` helper
    241                             function.
     240                            final storage destination. If this argument is
     241                            supplied, the storage backend will default to
     242                            ``FileSystemBackend``.
     243    ``backend``             **New in Django development version**
     244
     245                            A storage backend object, which handles the storage
     246                            and retrieval of your files. See `managing files`_
     247                            for details on how to provide this object.
    242248    ======================  ===================================================
    243249
    244 This path may contain `strftime formatting`_, which will be replaced by the
    245 date/time of the file upload (so that uploaded files don't fill up the given
    246 directory).
     250.. _managing files: ../files/
    247251
     252The ``upload_to`` path may contain `strftime formatting`_, which will be
     253replaced by the date/time of the file upload (so that uploaded files don't fill
     254up the given directory).
     255
    248256The admin represents this field as an ``<input type="file">`` (a file-upload
    249257widget).
    250258
    251 Using a ``FileField`` or an ``ImageField`` (see below) in a model takes a few
    252 steps:
     259Using a ``FileField`` or an ``ImageField`` (see below) in a model without a
     260specified backend takes a few steps:
    253261
    254262    1. In your settings file, you'll need to define ``MEDIA_ROOT`` as the
    255263       full path to a directory where you'd like Django to store uploaded
  • tests/modeltests/files/models.py

     
     1"""
     242. Storing files according to a custom backend
     3
     4FileField and its variations can take a "backend" argument to specify how and
     5where files should be stored.
     6"""
     7
     8import tempfile
     9
     10from django.db import models
     11from django.core.filestorage.filesystem import FileSystemBackend
     12
     13temp_dir = tempfile.gettempdir()
     14
     15class CustomBackend(FileSystemBackend):
     16    def get_available_filename(self, filename):
     17        # Append numbers to duplicate files rather than underscores, like Trac
     18
     19        parts = filename.split('.')
     20        basename, ext = parts[0], parts[1:]
     21        number = 2
     22
     23        while self.file_exists(filename):
     24            filename = '.'.join([basename, str(number)] + ext)
     25            number += 1
     26
     27        return filename
     28
     29standard_backend = FileSystemBackend(location=temp_dir)
     30custom_backend = CustomBackend(location=temp_dir)
     31
     32class Storage(models.Model):
     33    normal = models.FileField(backend=standard_backend, upload_to='tests')
     34    custom = models.FileField(backend=custom_backend, upload_to='tests')
     35
     36__test__ = {'API_TESTS':"""
     37# An object without a file has limited functionality
     38
     39>>> obj1 = Storage()
     40>>> obj1.normal
     41<File: >
     42>>> obj1.normal.get_filesize()
     43Traceback (most recent call last):
     44...
     45ValueError: The 'normal' attribute has no file associated with it.
     46
     47# Saving a file enables full functionality
     48
     49>>> obj1.normal.save_file('django_test.txt', 'content')
     50>>> obj1.normal
     51<File: tests/django_test.txt>
     52>>> obj1.normal.get_filesize()
     537
     54>>> obj1.normal.open_file().read()
     55'content'
     56
     57# Save another file with the same name
     58
     59>>> obj2 = Storage()
     60>>> obj2.normal.save_file('django_test.txt', 'more content')
     61>>> obj2.normal
     62<File: tests/django_test_.txt>
     63>>> obj2.normal.get_filesize()
     6412
     65
     66# Custom backends can behave differently
     67
     68>>> obj1.custom.save_file('django_test.txt', 'trac-style filenames')
     69>>> obj1.custom
     70<File: tests/django_test.2.txt>
     71>>> obj2.custom.save_file('django_test.txt', 'another file')
     72>>> obj2.custom
     73<File: tests/django_test.3.txt>
     74
     75# Clean up the temporary files
     76
     77>>> obj1.normal.delete_file()
     78>>> obj1.custom.delete_file()
     79>>> obj2.normal.delete_file()
     80>>> obj2.custom.delete_file()
     81"""}
  • tests/modeltests/files/models.py

     
     1"""
     242. Storing files according to a custom backend
     3
     4FileField and its variations can take a "backend" argument to specify how and
     5where files should be stored.
     6"""
     7
     8import tempfile
     9
     10from django.db import models
     11from django.core.filestorage.filesystem import FileSystemBackend
     12
     13temp_dir = tempfile.gettempdir()
     14
     15class CustomBackend(FileSystemBackend):
     16    def get_available_filename(self, filename):
     17        # Append numbers to duplicate files rather than underscores, like Trac
     18
     19        parts = filename.split('.')
     20        basename, ext = parts[0], parts[1:]
     21        number = 2
     22
     23        while self.file_exists(filename):
     24            filename = '.'.join([basename, str(number)] + ext)
     25            number += 1
     26
     27        return filename
     28
     29standard_backend = FileSystemBackend(location=temp_dir)
     30custom_backend = CustomBackend(location=temp_dir)
     31
     32class Storage(models.Model):
     33    normal = models.FileField(backend=standard_backend, upload_to='tests')
     34    custom = models.FileField(backend=custom_backend, upload_to='tests')
     35
     36__test__ = {'API_TESTS':"""
     37# An object without a file has limited functionality
     38
     39>>> obj1 = Storage()
     40>>> obj1.normal
     41<File: >
     42>>> obj1.normal.get_filesize()
     43Traceback (most recent call last):
     44...
     45ValueError: The 'normal' attribute has no file associated with it.
     46
     47# Saving a file enables full functionality
     48
     49>>> obj1.normal.save_file('django_test.txt', 'content')
     50>>> obj1.normal
     51<File: tests/django_test.txt>
     52>>> obj1.normal.get_filesize()
     537
     54>>> obj1.normal.open_file().read()
     55'content'
     56
     57# Save another file with the same name
     58
     59>>> obj2 = Storage()
     60>>> obj2.normal.save_file('django_test.txt', 'more content')
     61>>> obj2.normal
     62<File: tests/django_test_.txt>
     63>>> obj2.normal.get_filesize()
     6412
     65
     66# Custom backends can behave differently
     67
     68>>> obj1.custom.save_file('django_test.txt', 'trac-style filenames')
     69>>> obj1.custom
     70<File: tests/django_test.2.txt>
     71>>> obj2.custom.save_file('django_test.txt', 'another file')
     72>>> obj2.custom
     73<File: tests/django_test.3.txt>
     74
     75# Clean up the temporary files
     76
     77>>> obj1.normal.delete_file()
     78>>> obj1.custom.delete_file()
     79>>> obj2.normal.delete_file()
     80>>> obj2.custom.delete_file()
     81"""}
  • tests/regressiontests/bug639/tests.py

     
    3939        Make sure to delete the "uploaded" file to avoid clogging /tmp.
    4040        """
    4141        p = Photo.objects.get()
    42         os.unlink(p.get_image_filename())
    43  No newline at end of file
     42        os.unlink(p.image.get_absolute_path())
     43 No newline at end of file
  • tests/regressiontests/serializers_regress/models.py

     
    158158class EmailPKData(models.Model):
    159159    data = models.EmailField(primary_key=True)
    160160
    161 class FilePKData(models.Model):
    162     data = models.FileField(primary_key=True, upload_to='/foo/bar')
     161# class FilePKData(models.Model):
     162#    data = models.FileField(primary_key=True, upload_to='/foo/bar')
    163163
    164164class FilePathPKData(models.Model):
    165165    data = models.FilePathField(primary_key=True)
  • tests/regressiontests/serializers_regress/tests.py

     
    223223#     (pk_obj, 620, DatePKData, datetime.date(2006,6,16)),
    224224#     (pk_obj, 630, DateTimePKData, datetime.datetime(2006,6,16,10,42,37)),
    225225    (pk_obj, 640, EmailPKData, "hovercraft@example.com"),
    226     (pk_obj, 650, FilePKData, 'file:///foo/bar/whiz.txt'),
     226#     (pk_obj, 650, FilePKData, 'file:///foo/bar/whiz.txt'),
    227227    (pk_obj, 660, FilePathPKData, "/foo/bar/whiz.txt"),
    228228    (pk_obj, 670, DecimalPKData, decimal.Decimal('12.345')),
    229229    (pk_obj, 671, DecimalPKData, decimal.Decimal('-12.345')),
Back to Top