2008-08-09 04:59:02 +08:00
|
|
|
import datetime
|
|
|
|
import os
|
|
|
|
|
2009-12-18 06:06:41 +08:00
|
|
|
import django.utils.copycompat as copy
|
|
|
|
|
2008-08-09 04:59:02 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.db.models.fields import Field
|
|
|
|
from django.core.files.base import File, ContentFile
|
|
|
|
from django.core.files.storage import default_storage
|
|
|
|
from django.core.files.images import ImageFile, get_image_dimensions
|
|
|
|
from django.core.files.uploadedfile import UploadedFile
|
|
|
|
from django.utils.functional import curry
|
|
|
|
from django.db.models import signals
|
|
|
|
from django.utils.encoding import force_unicode, smart_str
|
|
|
|
from django.utils.translation import ugettext_lazy, ugettext as _
|
|
|
|
from django import forms
|
|
|
|
from django.db.models.loading import cache
|
|
|
|
|
|
|
|
class FieldFile(File):
|
|
|
|
def __init__(self, instance, field, name):
|
2009-05-08 23:08:09 +08:00
|
|
|
super(FieldFile, self).__init__(None, name)
|
2008-08-09 04:59:02 +08:00
|
|
|
self.instance = instance
|
|
|
|
self.field = field
|
|
|
|
self.storage = field.storage
|
2009-01-17 05:48:37 +08:00
|
|
|
self._committed = True
|
2008-08-09 04:59:02 +08:00
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
# Older code may be expecting FileField values to be simple strings.
|
|
|
|
# By overriding the == operator, it can remain backwards compatibility.
|
|
|
|
if hasattr(other, 'name'):
|
|
|
|
return self.name == other.name
|
|
|
|
return self.name == other
|
|
|
|
|
2008-12-16 12:52:55 +08:00
|
|
|
def __ne__(self, other):
|
|
|
|
return not self.__eq__(other)
|
|
|
|
|
2009-03-08 17:59:17 +08:00
|
|
|
def __hash__(self):
|
|
|
|
# Required because we defined a custom __eq__.
|
|
|
|
return hash(self.name)
|
|
|
|
|
2008-08-09 04:59:02 +08:00
|
|
|
# The standard File contains most of the necessary properties, but
|
|
|
|
# FieldFiles can be instantiated without a name, so that needs to
|
|
|
|
# be checked for here.
|
|
|
|
|
|
|
|
def _require_file(self):
|
|
|
|
if not self:
|
|
|
|
raise ValueError("The '%s' attribute has no file associated with it." % self.field.name)
|
|
|
|
|
|
|
|
def _get_file(self):
|
|
|
|
self._require_file()
|
2009-05-08 23:08:09 +08:00
|
|
|
if not hasattr(self, '_file') or self._file is None:
|
2008-08-09 04:59:02 +08:00
|
|
|
self._file = self.storage.open(self.name, 'rb')
|
|
|
|
return self._file
|
2009-05-08 23:08:09 +08:00
|
|
|
|
|
|
|
def _set_file(self, file):
|
|
|
|
self._file = file
|
|
|
|
|
|
|
|
def _del_file(self):
|
|
|
|
del self._file
|
|
|
|
|
|
|
|
file = property(_get_file, _set_file, _del_file)
|
2008-08-09 04:59:02 +08:00
|
|
|
|
|
|
|
def _get_path(self):
|
|
|
|
self._require_file()
|
|
|
|
return self.storage.path(self.name)
|
|
|
|
path = property(_get_path)
|
|
|
|
|
|
|
|
def _get_url(self):
|
|
|
|
self._require_file()
|
|
|
|
return self.storage.url(self.name)
|
|
|
|
url = property(_get_url)
|
|
|
|
|
2008-08-28 05:30:47 +08:00
|
|
|
def _get_size(self):
|
|
|
|
self._require_file()
|
2009-05-08 23:08:09 +08:00
|
|
|
if not self._committed:
|
2010-12-13 06:54:22 +08:00
|
|
|
return self.file.size
|
2008-08-28 05:30:47 +08:00
|
|
|
return self.storage.size(self.name)
|
|
|
|
size = property(_get_size)
|
|
|
|
|
2008-08-09 04:59:02 +08:00
|
|
|
def open(self, mode='rb'):
|
|
|
|
self._require_file()
|
2009-05-11 17:57:19 +08:00
|
|
|
self.file.open(mode)
|
2008-08-09 04:59:02 +08:00
|
|
|
# open() doesn't alter the file's contents, but it does reset the pointer
|
|
|
|
open.alters_data = True
|
|
|
|
|
|
|
|
# In addition to the standard File API, FieldFiles have extra methods
|
|
|
|
# to further manipulate the underlying file, as well as update the
|
|
|
|
# associated model instance.
|
|
|
|
|
|
|
|
def save(self, name, content, save=True):
|
|
|
|
name = self.field.generate_filename(self.instance, name)
|
2009-05-08 23:08:09 +08:00
|
|
|
self.name = self.storage.save(name, content)
|
2008-08-09 04:59:02 +08:00
|
|
|
setattr(self.instance, self.field.name, self.name)
|
|
|
|
|
|
|
|
# Update the filesize cache
|
2010-12-13 06:54:22 +08:00
|
|
|
self._size = content.size
|
2009-01-17 05:48:37 +08:00
|
|
|
self._committed = True
|
2008-08-09 04:59:02 +08:00
|
|
|
|
|
|
|
# Save the object because it has changed, unless save is False
|
|
|
|
if save:
|
|
|
|
self.instance.save()
|
|
|
|
save.alters_data = True
|
|
|
|
|
|
|
|
def delete(self, save=True):
|
2008-08-28 05:18:45 +08:00
|
|
|
# Only close the file if it's already open, which we know by the
|
|
|
|
# presence of self._file
|
|
|
|
if hasattr(self, '_file'):
|
|
|
|
self.close()
|
2009-05-08 23:08:09 +08:00
|
|
|
del self.file
|
|
|
|
|
2008-08-09 04:59:02 +08:00
|
|
|
self.storage.delete(self.name)
|
|
|
|
|
2009-05-08 23:08:09 +08:00
|
|
|
self.name = None
|
2008-08-09 04:59:02 +08:00
|
|
|
setattr(self.instance, self.field.name, self.name)
|
|
|
|
|
|
|
|
# Delete the filesize cache
|
|
|
|
if hasattr(self, '_size'):
|
|
|
|
del self._size
|
2009-01-17 05:48:37 +08:00
|
|
|
self._committed = False
|
2008-08-09 04:59:02 +08:00
|
|
|
|
|
|
|
if save:
|
|
|
|
self.instance.save()
|
|
|
|
delete.alters_data = True
|
|
|
|
|
2009-05-11 17:57:19 +08:00
|
|
|
def _get_closed(self):
|
|
|
|
file = getattr(self, '_file', None)
|
|
|
|
return file is None or file.closed
|
|
|
|
closed = property(_get_closed)
|
|
|
|
|
2009-05-08 23:08:09 +08:00
|
|
|
def close(self):
|
|
|
|
file = getattr(self, '_file', None)
|
|
|
|
if file is not None:
|
|
|
|
file.close()
|
|
|
|
|
2008-08-09 04:59:02 +08:00
|
|
|
def __getstate__(self):
|
|
|
|
# FieldFile needs access to its associated model field and an instance
|
|
|
|
# it's attached to in order to work properly, but the only necessary
|
|
|
|
# data to be pickled is the file's name itself. Everything else will
|
|
|
|
# be restored later, by FileDescriptor below.
|
2009-05-08 23:08:09 +08:00
|
|
|
return {'name': self.name, 'closed': False, '_committed': True, '_file': None}
|
2008-08-09 04:59:02 +08:00
|
|
|
|
|
|
|
class FileDescriptor(object):
|
2009-05-11 17:57:19 +08:00
|
|
|
"""
|
|
|
|
The descriptor for the file attribute on the model instance. Returns a
|
|
|
|
FieldFile when accessed so you can do stuff like::
|
2009-05-28 13:46:09 +08:00
|
|
|
|
2009-05-11 17:57:19 +08:00
|
|
|
>>> instance.file.size
|
2009-05-28 13:46:09 +08:00
|
|
|
|
2009-05-11 17:57:19 +08:00
|
|
|
Assigns a file object on assignment so you can do::
|
2009-05-28 13:46:09 +08:00
|
|
|
|
2009-05-11 17:57:19 +08:00
|
|
|
>>> instance.file = File(...)
|
2009-05-28 13:46:09 +08:00
|
|
|
|
2009-05-11 17:57:19 +08:00
|
|
|
"""
|
2008-08-09 04:59:02 +08:00
|
|
|
def __init__(self, field):
|
|
|
|
self.field = field
|
|
|
|
|
|
|
|
def __get__(self, instance=None, owner=None):
|
|
|
|
if instance is None:
|
2009-05-11 17:57:19 +08:00
|
|
|
raise AttributeError(
|
2009-05-28 13:46:09 +08:00
|
|
|
"The '%s' attribute can only be accessed from %s instances."
|
2009-05-11 17:57:19 +08:00
|
|
|
% (self.field.name, owner.__name__))
|
2009-05-28 13:46:09 +08:00
|
|
|
|
2009-05-11 17:57:19 +08:00
|
|
|
# This is slightly complicated, so worth an explanation.
|
|
|
|
# instance.file`needs to ultimately return some instance of `File`,
|
|
|
|
# probably a subclass. Additionally, this returned object needs to have
|
|
|
|
# the FieldFile API so that users can easily do things like
|
|
|
|
# instance.file.path and have that delegated to the file storage engine.
|
|
|
|
# Easy enough if we're strict about assignment in __set__, but if you
|
|
|
|
# peek below you can see that we're not. So depending on the current
|
|
|
|
# value of the field we have to dynamically construct some sort of
|
|
|
|
# "thing" to return.
|
2009-05-28 13:46:09 +08:00
|
|
|
|
|
|
|
# The instance dict contains whatever was originally assigned
|
2009-05-11 17:57:19 +08:00
|
|
|
# in __set__.
|
2008-08-09 04:59:02 +08:00
|
|
|
file = instance.__dict__[self.field.name]
|
2009-05-11 17:57:19 +08:00
|
|
|
|
|
|
|
# If this value is a string (instance.file = "path/to/file") or None
|
|
|
|
# then we simply wrap it with the appropriate attribute class according
|
|
|
|
# to the file field. [This is FieldFile for FileFields and
|
|
|
|
# ImageFieldFile for ImageFields; it's also conceivable that user
|
|
|
|
# subclasses might also want to subclass the attribute class]. This
|
|
|
|
# object understands how to convert a path to a file, and also how to
|
|
|
|
# handle None.
|
2009-01-17 05:48:37 +08:00
|
|
|
if isinstance(file, basestring) or file is None:
|
2009-05-11 17:57:19 +08:00
|
|
|
attr = self.field.attr_class(instance, self.field, file)
|
|
|
|
instance.__dict__[self.field.name] = attr
|
|
|
|
|
|
|
|
# Other types of files may be assigned as well, but they need to have
|
|
|
|
# the FieldFile interface added to the. Thus, we wrap any other type of
|
2009-05-28 13:46:09 +08:00
|
|
|
# File inside a FieldFile (well, the field's attr_class, which is
|
2009-05-11 17:57:19 +08:00
|
|
|
# usually FieldFile).
|
2009-01-17 05:48:37 +08:00
|
|
|
elif isinstance(file, File) and not isinstance(file, FieldFile):
|
2009-05-08 23:08:09 +08:00
|
|
|
file_copy = self.field.attr_class(instance, self.field, file.name)
|
|
|
|
file_copy.file = file
|
2009-01-17 05:48:37 +08:00
|
|
|
file_copy._committed = False
|
|
|
|
instance.__dict__[self.field.name] = file_copy
|
2009-05-28 13:46:09 +08:00
|
|
|
|
2009-05-11 17:57:19 +08:00
|
|
|
# Finally, because of the (some would say boneheaded) way pickle works,
|
|
|
|
# the underlying FieldFile might not actually itself have an associated
|
|
|
|
# file. So we need to reset the details of the FieldFile in those cases.
|
2009-01-17 05:48:37 +08:00
|
|
|
elif isinstance(file, FieldFile) and not hasattr(file, 'field'):
|
2008-08-09 04:59:02 +08:00
|
|
|
file.instance = instance
|
|
|
|
file.field = self.field
|
|
|
|
file.storage = self.field.storage
|
2009-05-28 13:46:09 +08:00
|
|
|
|
2009-05-11 17:57:19 +08:00
|
|
|
# That was fun, wasn't it?
|
2008-08-09 04:59:02 +08:00
|
|
|
return instance.__dict__[self.field.name]
|
|
|
|
|
|
|
|
def __set__(self, instance, value):
|
|
|
|
instance.__dict__[self.field.name] = value
|
|
|
|
|
|
|
|
class FileField(Field):
|
2009-05-11 17:57:19 +08:00
|
|
|
# The class to wrap instance attributes in. Accessing the file object off
|
|
|
|
# the instance will always return an instance of attr_class.
|
2008-08-09 04:59:02 +08:00
|
|
|
attr_class = FieldFile
|
2009-05-28 13:46:09 +08:00
|
|
|
|
2009-05-11 17:57:19 +08:00
|
|
|
# The descriptor to use for accessing the attribute off of the class.
|
|
|
|
descriptor_class = FileDescriptor
|
2008-08-09 04:59:02 +08:00
|
|
|
|
2009-12-17 02:13:34 +08:00
|
|
|
description = ugettext_lazy("File path")
|
|
|
|
|
2008-08-09 04:59:02 +08:00
|
|
|
def __init__(self, verbose_name=None, name=None, upload_to='', storage=None, **kwargs):
|
Removed oldforms, validators, and related code:
* Removed `Manipulator`, `AutomaticManipulator`, and related classes.
* Removed oldforms specific bits from model fields:
* Removed `validator_list` and `core` arguments from constructors.
* Removed the methods:
* `get_manipulator_field_names`
* `get_manipulator_field_objs`
* `get_manipulator_fields`
* `get_manipulator_new_data`
* `prepare_field_objs_and_params`
* `get_follow`
* Renamed `flatten_data` method to `value_to_string` for better alignment with its use by the serialization framework, which was the only remaining code using `flatten_data`.
* Removed oldforms methods from `django.db.models.Options` class: `get_followed_related_objects`, `get_data_holders`, `get_follow`, and `has_field_type`.
* Removed oldforms-admin specific options from `django.db.models.fields.related` classes: `num_in_admin`, `min_num_in_admin`, `max_num_in_admin`, `num_extra_on_change`, and `edit_inline`.
* Serialization framework
* `Serializer.get_string_value` now calls the model fields' renamed `value_to_string` methods.
* Removed a special-casing of `models.DateTimeField` in `core.serializers.base.Serializer.get_string_value` that's handled by `django.db.models.fields.DateTimeField.value_to_string`.
* Removed `django.core.validators`:
* Moved `ValidationError` exception to `django.core.exceptions`.
* For the couple places that were using validators, brought over the necessary code to maintain the same functionality.
* Introduced a SlugField form field for validation and to compliment the SlugField model field (refs #8040).
* Removed an oldforms-style model creation hack (refs #2160).
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8616 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-08-27 15:19:44 +08:00
|
|
|
for arg in ('primary_key', 'unique'):
|
2008-08-09 04:59:02 +08:00
|
|
|
if arg in kwargs:
|
|
|
|
raise TypeError("'%s' is not a valid argument for %s." % (arg, self.__class__))
|
|
|
|
|
|
|
|
self.storage = storage or default_storage
|
|
|
|
self.upload_to = upload_to
|
|
|
|
if callable(upload_to):
|
|
|
|
self.generate_filename = upload_to
|
|
|
|
|
|
|
|
kwargs['max_length'] = kwargs.get('max_length', 100)
|
|
|
|
super(FileField, self).__init__(verbose_name, name, **kwargs)
|
|
|
|
|
|
|
|
def get_internal_type(self):
|
|
|
|
return "FileField"
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def get_prep_lookup(self, lookup_type, value):
|
2008-08-09 04:59:02 +08:00
|
|
|
if hasattr(value, 'name'):
|
|
|
|
value = value.name
|
2009-12-22 23:18:51 +08:00
|
|
|
return super(FileField, self).get_prep_lookup(lookup_type, value)
|
2008-08-09 04:59:02 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def get_prep_value(self, value):
|
2008-08-09 04:59:02 +08:00
|
|
|
"Returns field's value prepared for saving into a database."
|
|
|
|
# Need to convert File objects provided via a form to unicode for database insertion
|
|
|
|
if value is None:
|
|
|
|
return None
|
|
|
|
return unicode(value)
|
|
|
|
|
2009-01-17 05:48:37 +08:00
|
|
|
def pre_save(self, model_instance, add):
|
|
|
|
"Returns field's value just before saving."
|
|
|
|
file = super(FileField, self).pre_save(model_instance, add)
|
|
|
|
if file and not file._committed:
|
|
|
|
# Commit the file to storage prior to saving the model
|
|
|
|
file.save(file.name, file, save=False)
|
|
|
|
return file
|
|
|
|
|
2008-08-09 04:59:02 +08:00
|
|
|
def contribute_to_class(self, cls, name):
|
|
|
|
super(FileField, self).contribute_to_class(cls, name)
|
2009-05-11 17:57:19 +08:00
|
|
|
setattr(cls, self.name, self.descriptor_class(self))
|
2008-08-09 04:59:02 +08:00
|
|
|
signals.post_delete.connect(self.delete_file, sender=cls)
|
|
|
|
|
|
|
|
def delete_file(self, instance, sender, **kwargs):
|
|
|
|
file = getattr(instance, self.attname)
|
|
|
|
# If no other object of this type references the file,
|
|
|
|
# and it's not the default value for future objects,
|
|
|
|
# delete it from the backend.
|
|
|
|
if file and file.name != self.default and \
|
|
|
|
not sender._default_manager.filter(**{self.name: file.name}):
|
|
|
|
file.delete(save=False)
|
|
|
|
elif file:
|
|
|
|
# Otherwise, just close the file, so it doesn't tie up resources.
|
|
|
|
file.close()
|
|
|
|
|
|
|
|
def get_directory_name(self):
|
|
|
|
return os.path.normpath(force_unicode(datetime.datetime.now().strftime(smart_str(self.upload_to))))
|
|
|
|
|
|
|
|
def get_filename(self, filename):
|
|
|
|
return os.path.normpath(self.storage.get_valid_name(os.path.basename(filename)))
|
|
|
|
|
|
|
|
def generate_filename(self, instance, filename):
|
|
|
|
return os.path.join(self.get_directory_name(), self.get_filename(filename))
|
|
|
|
|
2009-02-17 01:30:12 +08:00
|
|
|
def save_form_data(self, instance, data):
|
2010-10-01 10:02:58 +08:00
|
|
|
# Important: None means "no change", other false value means "clear"
|
|
|
|
# This subtle distinction (rather than a more explicit marker) is
|
|
|
|
# needed because we need to consume values that are also sane for a
|
|
|
|
# regular (non Model-) Form to find in its cleaned_data dictionary.
|
|
|
|
if data is not None:
|
|
|
|
# This value will be converted to unicode and stored in the
|
|
|
|
# database, so leaving False as-is is not acceptable.
|
|
|
|
if not data:
|
|
|
|
data = ''
|
2009-02-17 01:30:12 +08:00
|
|
|
setattr(instance, self.name, data)
|
|
|
|
|
2008-08-09 04:59:02 +08:00
|
|
|
def formfield(self, **kwargs):
|
2009-03-31 06:52:16 +08:00
|
|
|
defaults = {'form_class': forms.FileField, 'max_length': self.max_length}
|
2008-08-09 04:59:02 +08:00
|
|
|
# If a file has been provided previously, then the form doesn't require
|
|
|
|
# that a new file is provided this time.
|
|
|
|
# The code to mark the form field as not required is used by
|
|
|
|
# form_for_instance, but can probably be removed once form_for_instance
|
|
|
|
# is gone. ModelForm uses a different method to check for an existing file.
|
|
|
|
if 'initial' in kwargs:
|
|
|
|
defaults['required'] = False
|
|
|
|
defaults.update(kwargs)
|
|
|
|
return super(FileField, self).formfield(**defaults)
|
|
|
|
|
2009-05-11 17:57:19 +08:00
|
|
|
class ImageFileDescriptor(FileDescriptor):
|
|
|
|
"""
|
|
|
|
Just like the FileDescriptor, but for ImageFields. The only difference is
|
|
|
|
assigning the width/height to the width_field/height_field, if appropriate.
|
|
|
|
"""
|
|
|
|
def __set__(self, instance, value):
|
2009-05-28 13:46:09 +08:00
|
|
|
previous_file = instance.__dict__.get(self.field.name)
|
2009-05-11 17:57:19 +08:00
|
|
|
super(ImageFileDescriptor, self).__set__(instance, value)
|
2009-05-28 13:46:09 +08:00
|
|
|
|
|
|
|
# To prevent recalculating image dimensions when we are instantiating
|
|
|
|
# an object from the database (bug #11084), only update dimensions if
|
|
|
|
# the field had a value before this assignment. Since the default
|
|
|
|
# value for FileField subclasses is an instance of field.attr_class,
|
|
|
|
# previous_file will only be None when we are called from
|
|
|
|
# Model.__init__(). The ImageField.update_dimension_fields method
|
|
|
|
# hooked up to the post_init signal handles the Model.__init__() cases.
|
|
|
|
# Assignment happening outside of Model.__init__() will trigger the
|
|
|
|
# update right here.
|
|
|
|
if previous_file is not None:
|
|
|
|
self.field.update_dimension_fields(instance, force=True)
|
2008-08-09 04:59:02 +08:00
|
|
|
|
2009-05-11 17:57:19 +08:00
|
|
|
class ImageFieldFile(ImageFile, FieldFile):
|
2008-08-09 04:59:02 +08:00
|
|
|
def delete(self, save=True):
|
|
|
|
# Clear the image dimensions cache
|
|
|
|
if hasattr(self, '_dimensions_cache'):
|
|
|
|
del self._dimensions_cache
|
|
|
|
super(ImageFieldFile, self).delete(save)
|
|
|
|
|
|
|
|
class ImageField(FileField):
|
|
|
|
attr_class = ImageFieldFile
|
2009-05-11 17:57:19 +08:00
|
|
|
descriptor_class = ImageFileDescriptor
|
2009-12-17 02:13:34 +08:00
|
|
|
description = ugettext_lazy("File path")
|
2008-08-09 04:59:02 +08:00
|
|
|
|
|
|
|
def __init__(self, verbose_name=None, name=None, width_field=None, height_field=None, **kwargs):
|
|
|
|
self.width_field, self.height_field = width_field, height_field
|
|
|
|
FileField.__init__(self, verbose_name, name, **kwargs)
|
|
|
|
|
2009-05-28 13:46:09 +08:00
|
|
|
def contribute_to_class(self, cls, name):
|
|
|
|
super(ImageField, self).contribute_to_class(cls, name)
|
|
|
|
# Attach update_dimension_fields so that dimension fields declared
|
|
|
|
# after their corresponding image field don't stay cleared by
|
|
|
|
# Model.__init__, see bug #11196.
|
|
|
|
signals.post_init.connect(self.update_dimension_fields, sender=cls)
|
|
|
|
|
|
|
|
def update_dimension_fields(self, instance, force=False, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Updates field's width and height fields, if defined.
|
|
|
|
|
|
|
|
This method is hooked up to model's post_init signal to update
|
|
|
|
dimensions after instantiating a model instance. However, dimensions
|
|
|
|
won't be updated if the dimensions fields are already populated. This
|
|
|
|
avoids unnecessary recalculation when loading an object from the
|
|
|
|
database.
|
|
|
|
|
|
|
|
Dimensions can be forced to update with force=True, which is how
|
|
|
|
ImageFileDescriptor.__set__ calls this method.
|
|
|
|
"""
|
|
|
|
# Nothing to update if the field doesn't have have dimension fields.
|
|
|
|
has_dimension_fields = self.width_field or self.height_field
|
|
|
|
if not has_dimension_fields:
|
|
|
|
return
|
|
|
|
|
|
|
|
# getattr will call the ImageFileDescriptor's __get__ method, which
|
|
|
|
# coerces the assigned value into an instance of self.attr_class
|
|
|
|
# (ImageFieldFile in this case).
|
|
|
|
file = getattr(instance, self.attname)
|
|
|
|
|
|
|
|
# Nothing to update if we have no file and not being forced to update.
|
|
|
|
if not file and not force:
|
|
|
|
return
|
|
|
|
|
|
|
|
dimension_fields_filled = not(
|
|
|
|
(self.width_field and not getattr(instance, self.width_field))
|
|
|
|
or (self.height_field and not getattr(instance, self.height_field))
|
|
|
|
)
|
|
|
|
# When both dimension fields have values, we are most likely loading
|
|
|
|
# data from the database or updating an image field that already had
|
|
|
|
# an image stored. In the first case, we don't want to update the
|
|
|
|
# dimension fields because we are already getting their values from the
|
|
|
|
# database. In the second case, we do want to update the dimensions
|
|
|
|
# fields and will skip this return because force will be True since we
|
|
|
|
# were called from ImageFileDescriptor.__set__.
|
|
|
|
if dimension_fields_filled and not force:
|
|
|
|
return
|
|
|
|
|
|
|
|
# file should be an instance of ImageFieldFile or should be None.
|
|
|
|
if file:
|
|
|
|
width = file.width
|
|
|
|
height = file.height
|
|
|
|
else:
|
|
|
|
# No file, so clear dimensions fields.
|
|
|
|
width = None
|
|
|
|
height = None
|
|
|
|
|
|
|
|
# Update the width and height fields.
|
|
|
|
if self.width_field:
|
|
|
|
setattr(instance, self.width_field, width)
|
|
|
|
if self.height_field:
|
|
|
|
setattr(instance, self.height_field, height)
|
|
|
|
|
2008-08-09 04:59:02 +08:00
|
|
|
def formfield(self, **kwargs):
|
|
|
|
defaults = {'form_class': forms.ImageField}
|
|
|
|
defaults.update(kwargs)
|
|
|
|
return super(ImageField, self).formfield(**defaults)
|