Ticket #5361: filestorage.15.diff
File filestorage.15.diff, 63.9 KB (added by , 17 years ago) |
---|
-
django/conf/global_settings.py
216 216 # Path to the "jing" executable -- needed to validate XMLFields 217 217 JING_PATH = "/usr/bin/jing" 218 218 219 # Default file storage mechanism that holds media. 220 DEFAULT_FILE_STORAGE = 'django.core.filestorage.filesystem.FileSystemStorage' 221 219 222 # Absolute path to the directory that holds media. 220 223 # Example: "/home/media/media.lawrence.com/" 221 224 MEDIA_ROOT = '' -
django/core/filestorage/__init__.py
Property changes on: django\core\filestorage ___________________________________________________________________ Name: svn:ignore + *.pyc
1 from django.conf import settings 2 3 def 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 19 storage = get_storage(settings.DEFAULT_FILE_STORAGE) -
django/core/filestorage/base.py
1 from StringIO import StringIO 2 3 from django.utils.text import get_valid_filename 4 5 class 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 21 class 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
1 import os 2 import urlparse 3 4 from django.conf import settings 5 from django.utils.encoding import force_unicode, smart_str 6 from django.core.filestorage.base import Storage 7 from django.utils.text import force_unicode 8 9 class 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
8 8 from django.db.models.base import Model, AdminOptions 9 9 from django.db.models.fields import * 10 10 from django.db.models.fields.subclassing import SubfieldBase 11 from django.db.models.fields.files import FileField, ImageField 11 12 from django.db.models.fields.related import ForeignKey, OneToOneField, ManyToManyField, ManyToOneRel, ManyToManyRel, OneToOneRel, TABULAR, STACKED 12 13 from django.db.models import signals 13 14 from django.utils.functional import curry -
django/db/models/base.py
2 2 import django.db.models.manager 3 3 from django.core import validators 4 4 from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned 5 from django.db.models.fields import AutoField, ImageField,FieldDoesNotExist5 from django.db.models.fields import AutoField, FieldDoesNotExist 6 6 from django.db.models.fields.related import OneToOneRel, ManyToOneRel 7 7 from django.db.models.query import delete_objects 8 8 from django.db.models.options import Options, AdminOptions … … 18 18 import types 19 19 import sys 20 20 import os 21 from warnings import warn 21 22 22 23 class ModelBase(type): 23 24 "Metaclass for all models" … … 371 372 return getattr(self, cachename) 372 373 373 374 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 377 382 378 383 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 383 391 384 392 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() 386 396 387 397 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) 394 401 395 # If the filename already exists, keep adding an underscore to the name of396 # 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 dot401 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_dimensions416 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 False423 if save:424 self.save()425 426 _save_FIELD_file.alters_data = True427 428 402 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() 430 406 431 407 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() 433 411 434 def _get_image_dimensions(self, field):435 cachename = "__%s_dimensions_cache" % field.name436 if not hasattr(self, cachename):437 from django.utils.images import get_image_dimensions438 filename = self._get_FIELD_filename(field)439 setattr(self, cachename, get_image_dimensions(filename))440 return getattr(self, cachename)441 442 412 ############################################ 443 413 # HELPER FUNCTIONS (CURRIED MODEL METHODS) # 444 414 ############################################ -
django/db/models/fields/__init__.py
1 1 import datetime 2 import os3 2 import time 4 3 try: 5 4 import decimal … … 285 284 name_prefix is a prefix to prepend to the "field_name" argument. 286 285 rel is a boolean specifying whether this field is in a related context. 287 286 """ 287 from django.db.models.fields import files 288 288 289 field_objs, params = self.prepare_field_objs_and_params(manipulator, name_prefix) 289 290 290 291 # Add the "unique" validator(s). … … 316 317 # If this field is in a related context, check whether any other fields 317 318 # in the related object have core=True. If so, add a validator -- 318 319 # 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): 320 321 # First, get the core fields, if any. 321 322 core_field_names = [] 322 323 for f in opts.fields: … … 728 729 defaults.update(kwargs) 729 730 return super(EmailField, self).formfield(**defaults) 730 731 731 class FileField(Field):732 def __init__(self, verbose_name=None, name=None, upload_to='', **kwargs):733 self.upload_to = upload_to734 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 insertion743 if value is None:744 return None745 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_names755 self.other_file_field_name = other_file_field_name756 self.always_test = True757 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 = True772 field_list[0].validator_list.append(v)773 field_list[0].is_required = field_list[1].is_required = False774 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_list782 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_filename820 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 UploadedFile825 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 require831 # that a new file is provided this time.832 # The code to mark the form field as not required is used by833 # form_for_instance, but can probably be removed once form_for_instance834 # is gone. ModelForm uses a different method to check for an existing file.835 if 'initial' in kwargs:836 defaults['required'] = False837 defaults.update(kwargs)838 return super(FileField, self).formfield(**defaults)839 840 732 class FilePathField(Field): 841 733 def __init__(self, verbose_name=None, name=None, path='', match=None, recursive=False, **kwargs): 842 734 self.path, self.match, self.recursive = path, match, recursive … … 873 765 defaults.update(kwargs) 874 766 return super(FloatField, self).formfield(**defaults) 875 767 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_field879 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 the887 # 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't899 # changed, set the width and/or height field(s) back to their original900 # 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 913 768 class IntegerField(Field): 914 769 empty_strings_allowed = False 915 770 def get_manipulator_field_objs(self): -
django/db/models/fields/files.py
1 import datetime 2 import os 3 4 from django.conf import settings 5 from django.db.models.fields import Field 6 from django.core.filestorage import storage as default_storage 7 from django.utils.functional import curry 8 from django.dispatch import dispatcher 9 from django.db.models import signals 10 from django.utils.encoding import force_unicode, smart_str 11 from django.utils.translation import ugettext_lazy, ugettext as _ 12 from django import oldforms 13 from django import newforms as forms 14 from django.core import validators 15 16 class 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 85 class 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 97 class 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 222 class 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 249 class 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
1 1 from django.core.exceptions import ObjectDoesNotExist 2 2 from django import oldforms 3 3 from django.core import validators 4 from django.db.models.fields import FileField, AutoField 4 from django.db.models.fields import AutoField 5 from django.db.models.fields.files import FileField 5 6 from django.dispatch import dispatcher 6 7 from django.db.models import signals 7 8 from django.utils.functional import curry -
django/utils/images.py
6 6 7 7 import ImageFile 8 8 9 def get_image_dimensions( path):10 """Returns the (width, height) of an image at a givenpath."""9 def get_image_dimensions(file_or_path): 10 """Returns the (width, height) of an image, given an open file or a path.""" 11 11 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') 13 16 while 1: 14 17 data = fp.read(1024) 15 18 if not data: -
docs/custom_model_fields.txt
580 580 instance, not a ``HandField``). So if your ``__unicode__()`` method 581 581 automatically converts to the string form of your Python object, you can 582 582 save yourself a lot of work. 583 584 Writing a ``FileField`` subclass 585 ================================= 586 587 In addition to the above methods, fields that deal with files have a few other 588 special requirements which must be taken into account. The majority of the 589 mechanics provided by ``FileField``, such as controlling database storage and 590 retrieval, can remain unchanged, leaving subclasses to deal with the challenge 591 of supporting a particular type of file. 592 593 Django provides a ``File`` class, which is used as a proxy to the file's 594 contents and operations. This can be subclassed to customzie hwo the file is 595 accessed, 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 599 Once a subclass of ``File`` is created, the new ``FileField`` subclass must be 600 told 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 605 A few suggestions 606 ------------------ 607 608 In addition to the above details, there are a few guidelines which can greatly 609 improve 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
1867 1867 get_FOO_filename() 1868 1868 ------------------ 1869 1869 1870 **Deprecated in Django development version. See `managing files`_ for the new, 1871 preferred method for dealing with files.** 1872 1870 1873 For every ``FileField``, the object will have a ``get_FOO_filename()`` method, 1871 1874 where ``FOO`` is the name of the field. This returns the full filesystem path 1872 1875 to the file, according to your ``MEDIA_ROOT`` setting. … … 1877 1880 get_FOO_url() 1878 1881 ------------- 1879 1882 1883 **Deprecated in Django development version. See `managing files`_ for the new, 1884 preferred method for dealing with files.** 1885 1880 1886 For every ``FileField``, the object will have a ``get_FOO_url()`` method, 1881 1887 where ``FOO`` is the name of the field. This returns the full URL to the file, 1882 1888 according to your ``MEDIA_URL`` setting. If the value is blank, this method … … 1885 1891 get_FOO_size() 1886 1892 -------------- 1887 1893 1894 **Deprecated in Django development version. See `managing files`_ for the new, 1895 preferred method for dealing with files.** 1896 1888 1897 For every ``FileField``, the object will have a ``get_FOO_size()`` method, 1889 1898 where ``FOO`` is the name of the field. This returns the size of the file, in 1890 1899 bytes. (Behind the scenes, it uses ``os.path.getsize``.) … … 1892 1901 save_FOO_file(filename, raw_contents) 1893 1902 ------------------------------------- 1894 1903 1904 **Deprecated in Django development version. See `managing files`_ for the new, 1905 preferred method for dealing with files.** 1906 1895 1907 For every ``FileField``, the object will have a ``save_FOO_file()`` method, 1896 1908 where ``FOO`` is the name of the field. This saves the given file to the 1897 1909 filesystem, using the given filename. If a file with the given filename already … … 1901 1913 get_FOO_height() and get_FOO_width() 1902 1914 ------------------------------------ 1903 1915 1916 **Deprecated in Django development version. See `managing files`_ for the new, 1917 preferred method for dealing with files.** 1918 1904 1919 For every ``ImageField``, the object will have ``get_FOO_height()`` and 1905 1920 ``get_FOO_width()`` methods, where ``FOO`` is the name of the field. This 1906 1921 returns the height (or width) of the image, as an integer, in pixels. 1907 1922 1923 .. _`managing files`: ../files/ 1924 1908 1925 Shortcuts 1909 1926 ========= 1910 1927 -
docs/files.txt
1 ============== 2 Managing files 3 ============== 4 5 **New in Django development version** 6 7 When dealing with files, Django provides a number of features to make this task 8 easier and more portable. A storage protocol is available to allow files to be 9 stored in a variety of locations, and a special object is provided to allow 10 models to make use of this protocol, without having to worry about which storage 11 system is being used. 12 13 Using files in models 14 ===================== 15 16 When accessing a ``FileField`` attached to a model, a special object provides 17 access to the file and information about it. 18 19 Example 20 ------- 21 22 Consider 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 29 Your views can then use the ``photo`` attribute with the functions described 30 above, 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 43 Returns the absolute path to the file's location on a local filesystem. For 44 storage systems which do not store files locally, this will return `None`. 45 46 ``url()`` 47 --------- 48 49 Provides a URL where the content of the file can be retrieved. Therefore, 50 returned from this method is suitable for use as the destination of a link to 51 the file. 52 53 ``filesize()`` 54 -------------- 55 56 Returns the size of the file, as an integer. 57 58 ``open(mode='rb')`` 59 ------------------- 60 61 Returns an open file object, providing read or write access to the file's 62 contents. The ``mode`` argument allows the same values as Python's standard 63 ``open()`` function. 64 65 ``save(filename, raw_contents, save=True)`` 66 ------------------------------------------- 67 68 Saves a new file with the filename and contents provided. This will not replace 69 the existing file, but will create a new file and update the object to point to 70 it. The optional ``save`` argument dictates whether the model instance will be 71 saved to the database immediately. 72 73 ``width() and height()`` 74 ------------------------ 75 76 When using an ``ImageField``, these two methods will be available, providing 77 easy access to the dimensions of the image. 78 79 Using a storage system with FileField 80 ===================================== 81 82 When using a storage system, supply whatever options are appropriate for 83 that system when creating a new object. Then pass that object as the ``storage`` 84 argument to a ``FileField``. Details on the requirements for the included 85 storage system can be found below. 86 87 If using the default storage system, it is not necessary to create a storage 88 object explicitly. In this case, the ``FileField`` will use the one referenced 89 by the `DEFAULT_FILE_STORAGE setting`_. 90 91 See 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 96 For 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 108 Using a storage system on its own 109 ================================= 110 111 Storage systems may also be used directly, without being attached to a model. 112 Simply use the following API on any instantiated storage system to access files 113 without having to worry about the underlying mechanism. 114 115 In addition to explicit storage mechanisms, the file storage module, 116 ``django.core.filestorage``, exports a ``storage`` object that's automatically 117 created from the ``DEFAULT_FILE_STORAGE`` setting:: 118 119 >>> from django.core.filestorage import storage 120 121 With a functional storage system on hand, managing files is quite simple, with a 122 few 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 138 Returns ``True`` or ``False, indicating whether there is already a file present 139 at the location referenced by``filename``. 140 141 ``open(filename, mode='rb')`` 142 ----------------------------- 143 144 Returns an open file, or file-like, object to provide access to the contents of 145 the file referenced by ``filename``. The ``mode`` argument allows the same 146 values as Python's standard ``open()`` function. 147 148 ``filesize(filename)`` 149 ---------------------- 150 151 Returns the total size of the file referenced by ``filename``, as an integer. 152 153 ``url(filename)`` 154 ----------------- 155 156 Returns the URL where the contents of the file referenced by ``filename`` can 157 be accessed. 158 159 ``save(filename, raw_contents)`` 160 -------------------------------- 161 162 Saves a new file using the storage system, preferably with the name specified. 163 If there already exists a file at the location referenced by ``filename``, this 164 may modify the filename as necessary to locate one that is available. Once the 165 file is saved, this method will return the filename where the file was actually 166 stored. 167 168 ``delete(filename)`` 169 -------------------- 170 171 Deletes the file referenced by ``filename``. If the file does not already exist, 172 this method will simply return without raising an exception. 173 174 Available storage systems 175 ========================= 176 177 Only one storage system is supplied in the official Django distribution, but 178 more may be available elsewhere. If you'd like to use a different storage system 179 than the one listed below, see the documentation included with it. 180 181 ``django.core.filestorage.filesystem.FileSystemStorage`` 182 -------------------------------------------------------- 183 184 This 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 197 Writing a storage system 198 ======================== 199 200 While the default filesystem storage is suitable for most needs, there are many 201 other storage mechanisms that may be used, and situations that will require 202 special processing. In order to use Django in these environments, it's fairly 203 simple to write a new storage system, creating a wrapper around whatever 204 libraries are used to access your files, or simply customizing method calls on 205 an existing storage class. 206 207 If a storage system requires any configuration options to determine how it 208 should access the underlying storage mechanism or cusotmize its behavior in 209 other ways, those options should be specified in a particular way. Because the 210 default storage system is specified as a string, Django must be able to 211 instantiate it without any arguments, and any required arguments should be 212 specified as global settings, which can be referenced from the storage system. 213 For 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 222 All storage systems must implement the methods described above, but Django also 223 uses two other methods to assist in the process. When writing a custom class, 224 these methods may be inherited from the built-in ``Storage`` class, living at 225 ``django.core.filestorage.base``. When extending an existing storage class, they 226 can be overriden to allow a great deal of customization. 227 228 ``get_valid_filename(filename)`` 229 -------------------------------- 230 231 Returns 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 233 server, after having any path information removed. Override this to customize 234 how non-standard characters are converted to safe filenames. 235 236 The code provided on ``Storage`` retains only alpha-numeric characters, periods 237 and underscores from the original filename, removing everything else. 238 239 ``get_available_filename(filename)`` 240 ------------------------------------ 241 242 Returns a filename that is available in the storage mechanism, possibly taking 243 the provided filename into account. The ``filename`` argument passed to this 244 method will have already cleaned to a filename valid for the storage system, 245 according to the ``get_valid_filename()`` method described above. 246 247 The code provided on ``Storage`` simply appends underscores to the filename 248 until it finds one that's available in the destination directory. 249 250 Opening remote files 251 -------------------- 252 253 When accessing a file stored at a remote location, the object returned by 254 ``open()`` should function like a standard `file object`_, but to keep 255 network traffic to a minimum, writes to the remote storage system should only 256 occur if actually necessary. To make this task easier, Django provides a class 257 to automate this process. 258 259 Living at ``django.core.filestorage.base``, the ``RemoteFile`` class simulates 260 a standard Python `file object`_, but can write changes to a remote storage 261 system when application using a function provided by the storage system. 262 Creating an instance of this object requires three arguments, which are 263 described 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
230 230 ``FileField`` 231 231 ~~~~~~~~~~~~~ 232 232 233 A file-upload field. Has one **required** argument: 233 A file-upload field. Has two special arguments, of which the first is 234 **required**: 234 235 235 236 ====================== =================================================== 236 237 Argument Description 237 238 ====================== =================================================== 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. 242 254 ====================== =================================================== 243 255 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/ 247 257 258 The ``upload_to`` path may contain `strftime formatting`_, which will be 259 replaced by the date/time of the file upload (so that uploaded files don't fill 260 up the given directory). 261 262 **New in Django development version** 263 264 If a callable is provided for the ``upload_to`` argument, that callable must be 265 able to accept two arguments, and return a Unix-style path (with forward 266 slashes) to be passed along to the storage system. The two arguments that will 267 be 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 248 286 The admin represents this field as an ``<input type="file">`` (a file-upload 249 287 widget). 250 288 251 Using a ``FileField`` or an ``ImageField`` (see below) in a model takes a few252 s teps:289 Using a ``FileField`` or an ``ImageField`` (see below) in a model without a 290 specified storage system takes a few steps: 253 291 254 292 1. In your settings file, you'll need to define ``MEDIA_ROOT`` as the 255 293 full path to a directory where you'd like Django to store uploaded -
docs/settings.txt
409 409 isn't manually specified. Used with ``DEFAULT_CHARSET`` to construct the 410 410 ``Content-Type`` header. 411 411 412 DEFAULT_FILE_STORAGE 413 -------------------- 414 415 Default: ``'django.core.filestorage.filesystem.FileSystemStorage'`` 416 417 Default file storage class to be used for any file-related operations that don't 418 specify a particular storage system. See the `file documentation`_ for details. 419 420 .. _file documentation: ../files/ 421 412 422 DEFAULT_FROM_EMAIL 413 423 ------------------ 414 424 -
tests/modeltests/files/__init__.py
Property changes on: tests\modeltests\files ___________________________________________________________________ Name: svn:ignore + *.pyc
1 -
tests/modeltests/files/models.py
1 """ 2 42. Storing files according to a custom storage system 3 4 FileField and its variations can take a "storage" argument to specify how and 5 where files should be stored. 6 """ 7 8 import tempfile 9 10 from django.db import models 11 from django.core.filestorage.filesystem import FileSystemStorage 12 from django.core.cache import cache 13 14 temp_dir = tempfile.gettempdir() 15 16 class 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 30 standard_storage = FileSystemStorage(location=temp_dir) 31 custom_storage = CustomStorage(location=temp_dir) 32 33 class 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() 48 Traceback (most recent call last): 49 ... 50 ValueError: 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() 58 7 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() 69 12 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
799 799 <class 'django.newforms.fields.UploadedFile'> 800 800 >>> instance = f.save() 801 801 >>> instance.file 802 u'...test1.txt' 802 <File: .../test1.txt> 803 803 804 804 # Edit an instance that already has the file defined in the model. This will not 805 805 # save the file again, but leave it exactly as it is. … … 808 808 >>> f.is_valid() 809 809 True 810 810 >>> f.cleaned_data['file'] 811 u'...test1.txt' 811 <File: .../test1.txt> 812 812 >>> instance = f.save() 813 813 >>> instance.file 814 u'...test1.txt' 814 <File: .../test1.txt> 815 815 816 816 # Delete the current file since this is not done by Django. 817 817 818 >>> os.unlink(instance.get_file_filename())818 >>> instance.file.delete() 819 819 820 820 # Override the file by uploading a new one. 821 821 … … 824 824 True 825 825 >>> instance = f.save() 826 826 >>> instance.file 827 u'...test2.txt' 827 <File: .../test2.txt> 828 828 829 829 >>> instance.delete() 830 830 … … 836 836 True 837 837 >>> instance = f.save() 838 838 >>> instance.file 839 '' 839 <File: None> 840 840 841 841 >>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': {'filename': 'test3.txt', 'content': 'hello world'}}, instance=instance) 842 842 >>> f.is_valid() 843 843 True 844 844 >>> instance = f.save() 845 845 >>> instance.file 846 u'...test3.txt' 846 <File: .../test3.txt> 847 847 >>> instance.delete() 848 848 849 849 # ImageField ################################################################### … … 865 865 <class 'django.newforms.fields.UploadedFile'> 866 866 >>> instance = f.save() 867 867 >>> instance.image 868 u'...test.png' 868 <File: .../test.png> 869 869 870 870 # Edit an instance that already has the image defined in the model. This will not 871 871 # save the image again, but leave it exactly as it is. … … 874 874 >>> f.is_valid() 875 875 True 876 876 >>> f.cleaned_data['image'] 877 u'...test.png' 877 <File: .../test.png> 878 878 >>> instance = f.save() 879 879 >>> instance.image 880 u'...test.png' 880 <File: .../test.png> 881 881 882 882 # Delete the current image since this is not done by Django. 883 883 884 >>> os.unlink(instance.get_image_filename())884 >>> instance.image.delete() 885 885 886 886 # Override the file by uploading a new one. 887 887 … … 890 890 True 891 891 >>> instance = f.save() 892 892 >>> instance.image 893 u'...test2.png' 893 <File: .../test2.png> 894 894 895 895 >>> instance.delete() 896 896 … … 902 902 True 903 903 >>> instance = f.save() 904 904 >>> instance.image 905 '' 905 <File: None> 906 906 907 907 >>> f = ImageFileForm(data={'description': u'And a final one'}, files={'image': {'filename': 'test3.png', 'content': image_data}}, instance=instance) 908 908 >>> f.is_valid() 909 909 True 910 910 >>> instance = f.save() 911 911 >>> instance.image 912 u'...test3.png' 912 <File: .../test3.png> 913 913 >>> instance.delete() 914 914 915 915 """} -
tests/regressiontests/bug639/tests.py
39 39 Make sure to delete the "uploaded" file to avoid clogging /tmp. 40 40 """ 41 41 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
158 158 class EmailPKData(models.Model): 159 159 data = models.EmailField(primary_key=True) 160 160 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') 163 163 164 164 class FilePathPKData(models.Model): 165 165 data = models.FilePathField(primary_key=True) -
tests/regressiontests/serializers_regress/tests.py
125 125 (data_obj, 41, EmailData, None), 126 126 (data_obj, 42, EmailData, ""), 127 127 (data_obj, 50, FileData, 'file:///foo/bar/whiz.txt'), 128 (data_obj, 51, FileData, None),128 # (data_obj, 51, FileData, None), 129 129 (data_obj, 52, FileData, ""), 130 130 (data_obj, 60, FilePathData, "/foo/bar/whiz.txt"), 131 131 (data_obj, 61, FilePathData, None), … … 223 223 # (pk_obj, 620, DatePKData, datetime.date(2006,6,16)), 224 224 # (pk_obj, 630, DateTimePKData, datetime.datetime(2006,6,16,10,42,37)), 225 225 (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'), 227 227 (pk_obj, 660, FilePathPKData, "/foo/bar/whiz.txt"), 228 228 (pk_obj, 670, DecimalPKData, decimal.Decimal('12.345')), 229 229 (pk_obj, 671, DecimalPKData, decimal.Decimal('-12.345')),