Ticket #5361: filestorage.15.diff

File filestorage.15.diff, 63.9 KB (added by Marty Alchin, 17 years ago)

Takes david's corrections into account and updates to r7397

  • django/conf/global_settings.py

     
    216216# Path to the "jing" executable -- needed to validate XMLFields
    217217JING_PATH = "/usr/bin/jing"
    218218
     219# Default file storage mechanism that holds media.
     220DEFAULT_FILE_STORAGE = 'django.core.filestorage.filesystem.FileSystemStorage'
     221
    219222# Absolute path to the directory that holds media.
    220223# Example: "/home/media/media.lawrence.com/"
    221224MEDIA_ROOT = ''
  • django/core/filestorage/__init__.py

    Property changes on: django\core\filestorage
    ___________________________________________________________________
    Name: svn:ignore
       + *.pyc
    
    
     
     1from django.conf import settings
     2
     3def get_storage(import_path):
     4    try:
     5        dot = import_path.rindex('.')
     6    except ValueError:
     7        raise exceptions.ImproperlyConfigured("%s isn't a storage module." % import_path)
     8    module, classname = import_path[:dot], import_path[dot+1:]
     9    try:
     10        mod = __import__(module, {}, {}, [''])
     11    except ImportError, e:
     12        raise exceptions.ImproperlyConfigured('Error importing storage module %s: "%s"' % (module, e))
     13    try:
     14        storage_class = getattr(mod, classname)
     15    except AttributeError:
     16        raise exceptions.ImproperlyConfigured('Storage module "%s" does not define a "%s" class.' % (module, classname))
     17    return storage_class()
     18
     19storage = get_storage(settings.DEFAULT_FILE_STORAGE)
  • django/core/filestorage/base.py

     
     1from StringIO import StringIO
     2
     3from django.utils.text import get_valid_filename
     4
     5class Storage(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.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 remote storage automatically, when necessary."""
     23
     24    def __init__(self, data, mode, writer):
     25        self._mode = mode
     26        self._write_to_storage = 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_storage(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 Storage
     7from django.utils.text import force_unicode
     8
     9class FileSystemStorage(Storage):
     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 path(self, filename):
     17        return os.path.normpath(os.path.join(self.location, filename))
     18
     19    def filesize(self, filename):
     20        return os.path.getsize(self.path(filename))
     21
     22    def url(self, filename):
     23        return urlparse.urljoin(self.base_url, filename).replace('\\', '/')
     24
     25    def exists(self, filename):
     26        return os.path.exists(self.path(filename))
     27
     28    def open(self, filename, mode='rb'):
     29        return open(self.path(filename), mode)
     30
     31    def save(self, filename, raw_contents):
     32        directory = os.path.join(self.location, os.path.dirname(filename))
     33        if not os.path.exists(directory):
     34            os.makedirs(directory)
     35        elif not os.path.isdir(directory):
     36            raise IOError("%s exists and is not a directory." % directory)
     37
     38        filename = self.get_available_filename(filename)
     39
     40        # Write the file to disk.
     41        fp = self.open(filename, 'wb')
     42        fp.write(raw_contents)
     43        fp.close()
     44
     45        # Store filenames with forward slashes, even on Windows
     46        return force_unicode(filename.replace('\\', '/'))
     47
     48    def delete(self, filename):
     49        file_name = self.path(filename)
     50        # If the file exists, delete it from the filesystem.
     51        if os.path.exists(file_name):
     52            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"
     
    371372        return getattr(self, cachename)
    372373
    373374    def _get_FIELD_filename(self, field):
    374         if getattr(self, field.attname): # value is not blank
    375             return os.path.join(settings.MEDIA_ROOT, getattr(self, field.attname))
    376         return ''
     375        warn("instance.get_%s_filename() is deprecated. Use instance.%s.path() instead." % \
     376            (field.attname, field.attname), DeprecationWarning)
     377        try:
     378            return getattr(self, field.attname).path()
     379        except ValueError:
     380            # For backward compatibility
     381            return settings.MEDIA_ROOT
    377382
    378383    def _get_FIELD_url(self, field):
    379         if getattr(self, field.attname): # value is not blank
    380             import urlparse
    381             return urlparse.urljoin(settings.MEDIA_URL, getattr(self, field.attname)).replace('\\', '/')
    382         return ''
     384        warn("instance.get_%s_url() is deprecated. Use instance.%s.url() instead." % \
     385            (field.attname, field.attname), DeprecationWarning)
     386        try:
     387            return getattr(self, field.attname).url()
     388        except ValueError:
     389            # For backward compatibility
     390            return settings.MEDIA_URL
    383391
    384392    def _get_FIELD_size(self, field):
    385         return os.path.getsize(self._get_FIELD_filename(field))
     393        warn("instance.get_%s_size() is deprecated. Use instance.%s.filesize() instead." % \
     394            (field.attname, field.attname), DeprecationWarning)
     395        return getattr(self, field.attname).filesize()
    386396
    387397    def _save_FIELD_file(self, field, filename, raw_contents, save=True):
    388         directory = field.get_directory_name()
    389         try: # Create the date-based directory if it doesn't exist.
    390             os.makedirs(os.path.join(settings.MEDIA_ROOT, directory))
    391         except OSError: # Directory probably already exists.
    392             pass
    393         filename = field.get_filename(filename)
     398        warn("instance.save_%s_file() is deprecated. Use instance.%s.save() instead." % \
     399            (field.attname, field.attname), DeprecationWarning)
     400        return getattr(self, field.attname).save(filename, raw_contents, save)
    394401
    395         # If the filename already exists, keep adding an underscore to the name of
    396         # the file until the filename doesn't exist.
    397         while os.path.exists(os.path.join(settings.MEDIA_ROOT, filename)):
    398             try:
    399                 dot_index = filename.rindex('.')
    400             except ValueError: # filename has no dot
    401                 filename += '_'
    402             else:
    403                 filename = filename[:dot_index] + '_' + filename[dot_index:]
    404 
    405         # Write the file to disk.
    406         setattr(self, field.attname, filename)
    407 
    408         full_filename = self._get_FIELD_filename(field)
    409         fp = open(full_filename, 'wb')
    410         fp.write(raw_contents)
    411         fp.close()
    412 
    413         # Save the width and/or height, if applicable.
    414         if isinstance(field, ImageField) and (field.width_field or field.height_field):
    415             from django.utils.images import get_image_dimensions
    416             width, height = get_image_dimensions(full_filename)
    417             if field.width_field:
    418                 setattr(self, field.width_field, width)
    419             if field.height_field:
    420                 setattr(self, field.height_field, height)
    421 
    422         # Save the object because it has changed unless save is False
    423         if save:
    424             self.save()
    425 
    426     _save_FIELD_file.alters_data = True
    427 
    428402    def _get_FIELD_width(self, field):
    429         return self._get_image_dimensions(field)[0]
     403        warn("instance.get_%s_width() is deprecated. Use instance.%s.width() instead." % \
     404            (field.attname, field.attname), DeprecationWarning)
     405        return getattr(self, field.attname).width()
    430406
    431407    def _get_FIELD_height(self, field):
    432         return self._get_image_dimensions(field)[1]
     408        warn("instance.get_%s_height() is deprecated. Use instance.%s.height() instead." % \
     409            (field.attname, field.attname), DeprecationWarning)
     410        return getattr(self, field.attname).height()
    433411
    434     def _get_image_dimensions(self, field):
    435         cachename = "__%s_dimensions_cache" % field.name
    436         if not hasattr(self, cachename):
    437             from django.utils.images import get_image_dimensions
    438             filename = self._get_FIELD_filename(field)
    439             setattr(self, cachename, get_image_dimensions(filename))
    440         return getattr(self, cachename)
    441 
    442412############################################
    443413# HELPER FUNCTIONS (CURRIED MODEL METHODS) #
    444414############################################
  • django/db/models/fields/__init__.py

     
    11import datetime
    2 import os
    32import time
    43try:
    54    import decimal
     
    285284        name_prefix is a prefix to prepend to the "field_name" argument.
    286285        rel is a boolean specifying whether this field is in a related context.
    287286        """
     287        from django.db.models.fields import files
     288
    288289        field_objs, params = self.prepare_field_objs_and_params(manipulator, name_prefix)
    289290
    290291        # Add the "unique" validator(s).
     
    316317        # If this field is in a related context, check whether any other fields
    317318        # in the related object have core=True. If so, add a validator --
    318319        # RequiredIfOtherFieldsGiven -- to this FormField.
    319         if rel and not self.blank and not isinstance(self, AutoField) and not isinstance(self, FileField):
     320        if rel and not self.blank and not isinstance(self, AutoField) and not isinstance(self, files.FileField):
    320321            # First, get the core fields, if any.
    321322            core_field_names = []
    322323            for f in opts.fields:
     
    728729        defaults.update(kwargs)
    729730        return super(EmailField, self).formfield(**defaults)
    730731
    731 class FileField(Field):
    732     def __init__(self, verbose_name=None, name=None, upload_to='', **kwargs):
    733         self.upload_to = upload_to
    734         kwargs['max_length'] = kwargs.get('max_length', 100)
    735         Field.__init__(self, verbose_name, name, **kwargs)
    736 
    737     def get_internal_type(self):
    738         return "FileField"
    739 
    740     def get_db_prep_save(self, value):
    741         "Returns field's value prepared for saving into a database."
    742         # Need to convert UploadedFile objects provided via a form to unicode for database insertion
    743         if value is None:
    744             return None
    745         return unicode(value)
    746 
    747     def get_manipulator_fields(self, opts, manipulator, change, name_prefix='', rel=False, follow=True):
    748         field_list = Field.get_manipulator_fields(self, opts, manipulator, change, name_prefix, rel, follow)
    749         if not self.blank:
    750             if rel:
    751                 # This validator makes sure FileFields work in a related context.
    752                 class RequiredFileField(object):
    753                     def __init__(self, other_field_names, other_file_field_name):
    754                         self.other_field_names = other_field_names
    755                         self.other_file_field_name = other_file_field_name
    756                         self.always_test = True
    757                     def __call__(self, field_data, all_data):
    758                         if not all_data.get(self.other_file_field_name, False):
    759                             c = validators.RequiredIfOtherFieldsGiven(self.other_field_names, ugettext_lazy("This field is required."))
    760                             c(field_data, all_data)
    761                 # First, get the core fields, if any.
    762                 core_field_names = []
    763                 for f in opts.fields:
    764                     if f.core and f != self:
    765                         core_field_names.extend(f.get_manipulator_field_names(name_prefix))
    766                 # Now, if there are any, add the validator to this FormField.
    767                 if core_field_names:
    768                     field_list[0].validator_list.append(RequiredFileField(core_field_names, field_list[1].field_name))
    769             else:
    770                 v = validators.RequiredIfOtherFieldNotGiven(field_list[1].field_name, ugettext_lazy("This field is required."))
    771                 v.always_test = True
    772                 field_list[0].validator_list.append(v)
    773                 field_list[0].is_required = field_list[1].is_required = False
    774 
    775         # If the raw path is passed in, validate it's under the MEDIA_ROOT.
    776         def isWithinMediaRoot(field_data, all_data):
    777             f = os.path.abspath(os.path.join(settings.MEDIA_ROOT, field_data))
    778             if not f.startswith(os.path.abspath(os.path.normpath(settings.MEDIA_ROOT))):
    779                 raise validators.ValidationError, _("Enter a valid filename.")
    780         field_list[1].validator_list.append(isWithinMediaRoot)
    781         return field_list
    782 
    783     def contribute_to_class(self, cls, name):
    784         super(FileField, self).contribute_to_class(cls, name)
    785         setattr(cls, 'get_%s_filename' % self.name, curry(cls._get_FIELD_filename, field=self))
    786         setattr(cls, 'get_%s_url' % self.name, curry(cls._get_FIELD_url, field=self))
    787         setattr(cls, 'get_%s_size' % self.name, curry(cls._get_FIELD_size, field=self))
    788         setattr(cls, 'save_%s_file' % self.name, lambda instance, filename, raw_contents, save=True: instance._save_FIELD_file(self, filename, raw_contents, save))
    789         dispatcher.connect(self.delete_file, signal=signals.post_delete, sender=cls)
    790 
    791     def delete_file(self, instance):
    792         if getattr(instance, self.attname):
    793             file_name = getattr(instance, 'get_%s_filename' % self.name)()
    794             # If the file exists and no other object of this type references it,
    795             # delete it from the filesystem.
    796             if os.path.exists(file_name) and \
    797                 not instance.__class__._default_manager.filter(**{'%s__exact' % self.name: getattr(instance, self.attname)}):
    798                 os.remove(file_name)
    799 
    800     def get_manipulator_field_objs(self):
    801         return [oldforms.FileUploadField, oldforms.HiddenField]
    802 
    803     def get_manipulator_field_names(self, name_prefix):
    804         return [name_prefix + self.name + '_file', name_prefix + self.name]
    805 
    806     def save_file(self, new_data, new_object, original_object, change, rel, save=True):
    807         upload_field_name = self.get_manipulator_field_names('')[0]
    808         if new_data.get(upload_field_name, False):
    809             func = getattr(new_object, 'save_%s_file' % self.name)
    810             if rel:
    811                 func(new_data[upload_field_name][0]["filename"], new_data[upload_field_name][0]["content"], save)
    812             else:
    813                 func(new_data[upload_field_name]["filename"], new_data[upload_field_name]["content"], save)
    814 
    815     def get_directory_name(self):
    816         return os.path.normpath(force_unicode(datetime.datetime.now().strftime(smart_str(self.upload_to))))
    817 
    818     def get_filename(self, filename):
    819         from django.utils.text import get_valid_filename
    820         f = os.path.join(self.get_directory_name(), get_valid_filename(os.path.basename(filename)))
    821         return os.path.normpath(f)
    822 
    823     def save_form_data(self, instance, data):
    824         from django.newforms.fields import UploadedFile
    825         if data and isinstance(data, UploadedFile):
    826             getattr(instance, "save_%s_file" % self.name)(data.filename, data.content, save=False)
    827 
    828     def formfield(self, **kwargs):
    829         defaults = {'form_class': forms.FileField}
    830         # If a file has been provided previously, then the form doesn't require
    831         # that a new file is provided this time.
    832         # The code to mark the form field as not required is used by
    833         # form_for_instance, but can probably be removed once form_for_instance
    834         # is gone. ModelForm uses a different method to check for an existing file.
    835         if 'initial' in kwargs:
    836             defaults['required'] = False
    837         defaults.update(kwargs)
    838         return super(FileField, self).formfield(**defaults)
    839 
    840732class FilePathField(Field):
    841733    def __init__(self, verbose_name=None, name=None, path='', match=None, recursive=False, **kwargs):
    842734        self.path, self.match, self.recursive = path, match, recursive
     
    873765        defaults.update(kwargs)
    874766        return super(FloatField, self).formfield(**defaults)
    875767
    876 class ImageField(FileField):
    877     def __init__(self, verbose_name=None, name=None, width_field=None, height_field=None, **kwargs):
    878         self.width_field, self.height_field = width_field, height_field
    879         FileField.__init__(self, verbose_name, name, **kwargs)
    880 
    881     def get_manipulator_field_objs(self):
    882         return [oldforms.ImageUploadField, oldforms.HiddenField]
    883 
    884     def contribute_to_class(self, cls, name):
    885         super(ImageField, self).contribute_to_class(cls, name)
    886         # Add get_BLAH_width and get_BLAH_height methods, but only if the
    887         # image field doesn't have width and height cache fields.
    888         if not self.width_field:
    889             setattr(cls, 'get_%s_width' % self.name, curry(cls._get_FIELD_width, field=self))
    890         if not self.height_field:
    891             setattr(cls, 'get_%s_height' % self.name, curry(cls._get_FIELD_height, field=self))
    892 
    893     def get_internal_type(self):
    894         return "ImageField"
    895 
    896     def save_file(self, new_data, new_object, original_object, change, rel, save=True):
    897         FileField.save_file(self, new_data, new_object, original_object, change, rel, save)
    898         # If the image has height and/or width field(s) and they haven't
    899         # changed, set the width and/or height field(s) back to their original
    900         # values.
    901         if change and (self.width_field or self.height_field) and save:
    902             if self.width_field:
    903                 setattr(new_object, self.width_field, getattr(original_object, self.width_field))
    904             if self.height_field:
    905                 setattr(new_object, self.height_field, getattr(original_object, self.height_field))
    906             new_object.save()
    907 
    908     def formfield(self, **kwargs):
    909         defaults = {'form_class': forms.ImageField}
    910         defaults.update(kwargs)
    911         return super(ImageField, self).formfield(**defaults)
    912 
    913768class IntegerField(Field):
    914769    empty_strings_allowed = False
    915770    def get_manipulator_field_objs(self):
  • django/db/models/fields/files.py

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

     
    580580       instance, not a ``HandField``). So if your ``__unicode__()`` method
    581581       automatically converts to the string form of your Python object, you can
    582582       save yourself a lot of work.
     583
     584Writing a ``FileField`` subclass
     585=================================
     586
     587In addition to the above methods, fields that deal with files have a few other
     588special requirements which must be taken into account. The majority of the
     589mechanics provided by ``FileField``, such as controlling database storage and
     590retrieval, can remain unchanged, leaving subclasses to deal with the challenge
     591of supporting a particular type of file.
     592
     593Django provides a ``File`` class, which is used as a proxy to the file's
     594contents and operations. This can be subclassed to customzie hwo the file is
     595accessed, and what methods are available. It lives at
     596``django.db.models.fields.files``, and its default behavior is explained in the
     597`file documentation`_.
     598
     599Once a subclass of ``File`` is created, the new ``FileField`` subclass must be
     600told to use it. To do so, simply assign the new ``File`` subclass to the special
     601``attr_class`` attribute of the ``FileField`` subclass.
     602
     603.. _file documentation: ../files/
     604
     605A few suggestions
     606------------------
     607
     608In addition to the above details, there are a few guidelines which can greatly
     609improve the efficiency and readability of the field's code.
     610
     611    1. The source for Django's own ``ImageField`` (in
     612       ``django/db/models/fields/files.py``) is a great example of how to
     613       subclass ``FileField`` to support a particular type of file, as it
     614       incorporates all of the techniques described above.
     615
     616    2. Cache file attributes wherever possible. Since files may be stored in
     617       remote storage systems, retrieving them may cost extra time, or even
     618       money, that isn't always necessary. Once a file is retrieved to obtain
     619       some data about its content, cache as much of that data as possible to
     620       reduce the number of times the file must be retrieved on subsequent
     621       calls for that information.
  • docs/db-api.txt

     
    18671867get_FOO_filename()
    18681868------------------
    18691869
     1870**Deprecated in Django development version. See `managing files`_ for the new,
     1871preferred method for dealing with files.**
     1872
    18701873For every ``FileField``, the object will have a ``get_FOO_filename()`` method,
    18711874where ``FOO`` is the name of the field. This returns the full filesystem path
    18721875to the file, according to your ``MEDIA_ROOT`` setting.
     
    18771880get_FOO_url()
    18781881-------------
    18791882
     1883**Deprecated in Django development version. See `managing files`_ for the new,
     1884preferred method for dealing with files.**
     1885
    18801886For every ``FileField``, the object will have a ``get_FOO_url()`` method,
    18811887where ``FOO`` is the name of the field. This returns the full URL to the file,
    18821888according to your ``MEDIA_URL`` setting. If the value is blank, this method
     
    18851891get_FOO_size()
    18861892--------------
    18871893
     1894**Deprecated in Django development version. See `managing files`_ for the new,
     1895preferred method for dealing with files.**
     1896
    18881897For every ``FileField``, the object will have a ``get_FOO_size()`` method,
    18891898where ``FOO`` is the name of the field. This returns the size of the file, in
    18901899bytes. (Behind the scenes, it uses ``os.path.getsize``.)
     
    18921901save_FOO_file(filename, raw_contents)
    18931902-------------------------------------
    18941903
     1904**Deprecated in Django development version. See `managing files`_ for the new,
     1905preferred method for dealing with files.**
     1906
    18951907For every ``FileField``, the object will have a ``save_FOO_file()`` method,
    18961908where ``FOO`` is the name of the field. This saves the given file to the
    18971909filesystem, using the given filename. If a file with the given filename already
     
    19011913get_FOO_height() and get_FOO_width()
    19021914------------------------------------
    19031915
     1916**Deprecated in Django development version. See `managing files`_ for the new,
     1917preferred method for dealing with files.**
     1918
    19041919For every ``ImageField``, the object will have ``get_FOO_height()`` and
    19051920``get_FOO_width()`` methods, where ``FOO`` is the name of the field. This
    19061921returns the height (or width) of the image, as an integer, in pixels.
    19071922
     1923.. _`managing files`: ../files/
     1924
    19081925Shortcuts
    19091926=========
    19101927
  • docs/files.txt

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

     
    230230``FileField``
    231231~~~~~~~~~~~~~
    232232
    233 A file-upload field. Has one **required** argument:
     233A file-upload field. Has two special arguments, of which the first is
     234**required**:
    234235
    235236    ======================  ===================================================
    236237    Argument                Description
    237238    ======================  ===================================================
    238     ``upload_to``           A local filesystem path that will be appended to
    239                             your ``MEDIA_ROOT`` setting to determine the
    240                             output of the ``get_<fieldname>_url()`` helper
    241                             function.
     239    ``upload_to``           Required. A filesystem-style path that will be
     240                            prepended to the filename before being committed to
     241                            the final storage destination.
     242
     243                            **New in Django development version**
     244
     245                            This may also be a callable, such as a function,
     246                            which will be called to obtain the upload path,
     247                            including the filename. See below for details.
     248
     249    ``storage``             **New in Django development version**
     250
     251                            Optional. A storage object, which handles the
     252                            storage and retrieval of your files. See `managing
     253                            files`_ for details on how to provide this object.
    242254    ======================  ===================================================
    243255
    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).
     256.. _managing files: ../files/
    247257
     258The ``upload_to`` path may contain `strftime formatting`_, which will be
     259replaced by the date/time of the file upload (so that uploaded files don't fill
     260up the given directory).
     261
     262**New in Django development version**
     263
     264If a callable is provided for the ``upload_to`` argument, that callable must be
     265able to accept two arguments, and return a Unix-style path (with forward
     266slashes) to be passed along to the storage system. The two arguments that will
     267be passed are:
     268
     269    ======================  ===================================================
     270    Argument                Description
     271    ======================  ===================================================
     272    ``instance``            An instance of the model where the ``FileField`` is
     273                            defined. More specifically, this is the particular
     274                            instance where the current file is being attached.
     275                           
     276                            **Note**: In most cases, this object will not have
     277                            been saved to the database yet, so if it uses the
     278                            default ``AutoField``, *it might not yet have a
     279                            value for its primary key field*.
     280
     281    ``filename``            The filename that was originally given to the file.
     282                            This may or may not be taken into account when
     283                            determining the final destination path.
     284    ======================  ===================================================
     285
    248286The admin represents this field as an ``<input type="file">`` (a file-upload
    249287widget).
    250288
    251 Using a ``FileField`` or an ``ImageField`` (see below) in a model takes a few
    252 steps:
     289Using a ``FileField`` or an ``ImageField`` (see below) in a model without a
     290specified storage system takes a few steps:
    253291
    254292    1. In your settings file, you'll need to define ``MEDIA_ROOT`` as the
    255293       full path to a directory where you'd like Django to store uploaded
  • docs/settings.txt

     
    409409isn't manually specified. Used with ``DEFAULT_CHARSET`` to construct the
    410410``Content-Type`` header.
    411411
     412DEFAULT_FILE_STORAGE
     413--------------------
     414
     415Default: ``'django.core.filestorage.filesystem.FileSystemStorage'``
     416
     417Default file storage class to be used for any file-related operations that don't
     418specify a particular storage system. See the `file documentation`_ for details.
     419
     420.. _file documentation: ../files/
     421
    412422DEFAULT_FROM_EMAIL
    413423------------------
    414424
  • tests/modeltests/files/__init__.py

    Property changes on: tests\modeltests\files
    ___________________________________________________________________
    Name: svn:ignore
       + *.pyc
    
    
     
     1
  • tests/modeltests/files/models.py

     
     1"""
     242. Storing files according to a custom storage system
     3
     4FileField and its variations can take a "storage" 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 FileSystemStorage
     12from django.core.cache import cache
     13
     14temp_dir = tempfile.gettempdir()
     15
     16class CustomStorage(FileSystemStorage):
     17    def get_available_filename(self, filename):
     18        # Append numbers to duplicate files rather than underscores, like Trac
     19
     20        parts = filename.split('.')
     21        basename, ext = parts[0], parts[1:]
     22        number = 2
     23
     24        while self.exists(filename):
     25            filename = '.'.join([basename, str(number)] + ext)
     26            number += 1
     27
     28        return filename
     29
     30standard_storage = FileSystemStorage(location=temp_dir)
     31custom_storage = CustomStorage(location=temp_dir)
     32
     33class Storage(models.Model):
     34    def custom_upload_to(self, filename):
     35        return 'foo'
     36
     37    normal = models.FileField(storage=standard_storage, upload_to='tests')
     38    custom = models.FileField(storage=custom_storage, upload_to='tests')
     39    upload = models.FileField(upload_to=custom_upload_to)
     40
     41__test__ = {'API_TESTS':"""
     42# An object without a file has limited functionality
     43
     44>>> obj1 = Storage()
     45>>> obj1.normal
     46<File: None>
     47>>> obj1.normal.filesize()
     48Traceback (most recent call last):
     49...
     50ValueError: The 'normal' attribute has no file associated with it.
     51
     52# Saving a file enables full functionality
     53
     54>>> obj1.normal.save('django_test.txt', 'content')
     55>>> obj1.normal
     56<File: tests/django_test.txt>
     57>>> obj1.normal.filesize()
     587
     59>>> obj1.normal.open().read()
     60'content'
     61
     62# Save another file with the same name
     63
     64>>> obj2 = Storage()
     65>>> obj2.normal.save('django_test.txt', 'more content')
     66>>> obj2.normal
     67<File: tests/django_test_.txt>
     68>>> obj2.normal.filesize()
     6912
     70
     71# Custom storage systems can behave differently
     72
     73>>> obj1.custom.save('django_test.txt', 'trac-style filenames')
     74>>> obj1.custom
     75<File: tests/django_test.2.txt>
     76>>> obj2.custom.save('django_test.txt', 'another file')
     77>>> obj2.custom
     78<File: tests/django_test.3.txt>
     79
     80# Push the objects into the cache to make sure they pickle properly
     81
     82>>> cache.set('obj1', obj1)
     83>>> cache.set('obj2', obj2)
     84>>> cache.get('obj1').custom
     85<File: tests/django_test.2.txt>
     86>>> cache.get('obj2').custom
     87<File: tests/django_test.3.txt>
     88
     89# Clean up the temporary files
     90
     91>>> obj1.normal.delete()
     92>>> obj1.custom.delete()
     93>>> obj2.normal.delete()
     94>>> obj2.custom.delete()
     95"""}
  • tests/modeltests/model_forms/models.py

     
    799799<class 'django.newforms.fields.UploadedFile'>
    800800>>> instance = f.save()
    801801>>> instance.file
    802 u'...test1.txt'
     802<File: .../test1.txt>
    803803
    804804# Edit an instance that already has the file defined in the model. This will not
    805805# save the file again, but leave it exactly as it is.
     
    808808>>> f.is_valid()
    809809True
    810810>>> f.cleaned_data['file']
    811 u'...test1.txt'
     811<File: .../test1.txt>
    812812>>> instance = f.save()
    813813>>> instance.file
    814 u'...test1.txt'
     814<File: .../test1.txt>
    815815
    816816# Delete the current file since this is not done by Django.
    817817
    818 >>> os.unlink(instance.get_file_filename())
     818>>> instance.file.delete()
    819819
    820820# Override the file by uploading a new one.
    821821
     
    824824True
    825825>>> instance = f.save()
    826826>>> instance.file
    827 u'...test2.txt'
     827<File: .../test2.txt>
    828828
    829829>>> instance.delete()
    830830
     
    836836True
    837837>>> instance = f.save()
    838838>>> instance.file
    839 ''
     839<File: None>
    840840
    841841>>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': {'filename': 'test3.txt', 'content': 'hello world'}}, instance=instance)
    842842>>> f.is_valid()
    843843True
    844844>>> instance = f.save()
    845845>>> instance.file
    846 u'...test3.txt'
     846<File: .../test3.txt>
    847847>>> instance.delete()
    848848
    849849# ImageField ###################################################################
     
    865865<class 'django.newforms.fields.UploadedFile'>
    866866>>> instance = f.save()
    867867>>> instance.image
    868 u'...test.png'
     868<File: .../test.png>
    869869
    870870# Edit an instance that already has the image defined in the model. This will not
    871871# save the image again, but leave it exactly as it is.
     
    874874>>> f.is_valid()
    875875True
    876876>>> f.cleaned_data['image']
    877 u'...test.png'
     877<File: .../test.png>
    878878>>> instance = f.save()
    879879>>> instance.image
    880 u'...test.png'
     880<File: .../test.png>
    881881
    882882# Delete the current image since this is not done by Django.
    883883
    884 >>> os.unlink(instance.get_image_filename())
     884>>> instance.image.delete()
    885885
    886886# Override the file by uploading a new one.
    887887
     
    890890True
    891891>>> instance = f.save()
    892892>>> instance.image
    893 u'...test2.png'
     893<File: .../test2.png>
    894894
    895895>>> instance.delete()
    896896
     
    902902True
    903903>>> instance = f.save()
    904904>>> instance.image
    905 ''
     905<File: None>
    906906
    907907>>> f = ImageFileForm(data={'description': u'And a final one'}, files={'image': {'filename': 'test3.png', 'content': image_data}}, instance=instance)
    908908>>> f.is_valid()
    909909True
    910910>>> instance = f.save()
    911911>>> instance.image
    912 u'...test3.png'
     912<File: .../test3.png>
    913913>>> instance.delete()
    914914
    915915"""}
  • 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        p.image.delete()
  • 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

     
    125125    (data_obj, 41, EmailData, None),
    126126    (data_obj, 42, EmailData, ""),
    127127    (data_obj, 50, FileData, 'file:///foo/bar/whiz.txt'),
    128     (data_obj, 51, FileData, None),
     128#     (data_obj, 51, FileData, None),
    129129    (data_obj, 52, FileData, ""),
    130130    (data_obj, 60, FilePathData, "/foo/bar/whiz.txt"),
    131131    (data_obj, 61, FilePathData, None),
     
    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