web/lib/django/db/models/fields/files.py
author ymh <ymh.work@gmail.com>
Wed, 02 Jun 2010 18:57:35 +0200
changeset 38 77b6da96e6f1
permissions -rw-r--r--
update django
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
import datetime
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
import os
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
import django.utils.copycompat as copy
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
from django.conf import settings
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
from django.db.models.fields import Field
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
from django.core.files.base import File, ContentFile
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
from django.core.files.storage import default_storage
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
from django.core.files.images import ImageFile, get_image_dimensions
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
from django.core.files.uploadedfile import UploadedFile
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
from django.utils.functional import curry
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
from django.db.models import signals
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
from django.utils.encoding import force_unicode, smart_str
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
from django.utils.translation import ugettext_lazy, ugettext as _
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
from django import forms
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
from django.db.models.loading import cache
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
class FieldFile(File):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
    def __init__(self, instance, field, name):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
        super(FieldFile, self).__init__(None, name)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
        self.instance = instance
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
        self.field = field
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
        self.storage = field.storage
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
        self._committed = True
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
    def __eq__(self, other):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
        # Older code may be expecting FileField values to be simple strings.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
        # By overriding the == operator, it can remain backwards compatibility.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
        if hasattr(other, 'name'):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
            return self.name == other.name
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
        return self.name == other
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
    def __ne__(self, other):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
        return not self.__eq__(other)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
    def __hash__(self):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
        # Required because we defined a custom __eq__.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
        return hash(self.name)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
    # The standard File contains most of the necessary properties, but
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
    # FieldFiles can be instantiated without a name, so that needs to
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
    # be checked for here.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
    def _require_file(self):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
        if not self:
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
            raise ValueError("The '%s' attribute has no file associated with it." % self.field.name)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
    def _get_file(self):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
        self._require_file()
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
        if not hasattr(self, '_file') or self._file is None:
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
            self._file = self.storage.open(self.name, 'rb')
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
        return self._file
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
    def _set_file(self, file):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
        self._file = file
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
    def _del_file(self):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
        del self._file
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
    file = property(_get_file, _set_file, _del_file)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
    def _get_path(self):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
        self._require_file()
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
        return self.storage.path(self.name)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
    path = property(_get_path)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
    def _get_url(self):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
        self._require_file()
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
        return self.storage.url(self.name)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
    url = property(_get_url)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
    def _get_size(self):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
        self._require_file()
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
        if not self._committed:
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
            return len(self.file)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
        return self.storage.size(self.name)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
    size = property(_get_size)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
    def open(self, mode='rb'):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
        self._require_file()
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
        self.file.open(mode)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
    # open() doesn't alter the file's contents, but it does reset the pointer
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
    open.alters_data = True
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
    # In addition to the standard File API, FieldFiles have extra methods
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
    # to further manipulate the underlying file, as well as update the
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
    # associated model instance.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
    def save(self, name, content, save=True):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
        name = self.field.generate_filename(self.instance, name)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
        self.name = self.storage.save(name, content)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
        setattr(self.instance, self.field.name, self.name)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
        # Update the filesize cache
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
        self._size = len(content)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
        self._committed = True
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
        # Save the object because it has changed, unless save is False
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
        if save:
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
            self.instance.save()
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
    save.alters_data = True
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
    def delete(self, save=True):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
        # Only close the file if it's already open, which we know by the
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
        # presence of self._file
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
        if hasattr(self, '_file'):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
            self.close()
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
            del self.file
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
        self.storage.delete(self.name)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
        self.name = None
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
        setattr(self.instance, self.field.name, self.name)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
        # Delete the filesize cache
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
        if hasattr(self, '_size'):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
            del self._size
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
        self._committed = False
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
        if save:
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
            self.instance.save()
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
    delete.alters_data = True
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
    def _get_closed(self):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
        file = getattr(self, '_file', None)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
        return file is None or file.closed
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
    closed = property(_get_closed)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
    def close(self):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
        file = getattr(self, '_file', None)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
        if file is not None:
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
            file.close()
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
    def __getstate__(self):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
        # FieldFile needs access to its associated model field and an instance
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
        # it's attached to in order to work properly, but the only necessary
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
        # data to be pickled is the file's name itself. Everything else will
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
        # be restored later, by FileDescriptor below.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
        return {'name': self.name, 'closed': False, '_committed': True, '_file': None}
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
class FileDescriptor(object):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
    """
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
    The descriptor for the file attribute on the model instance. Returns a
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
    FieldFile when accessed so you can do stuff like::
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
        >>> instance.file.size
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
    Assigns a file object on assignment so you can do::
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
        >>> instance.file = File(...)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
    """
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
    def __init__(self, field):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
        self.field = field
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
    def __get__(self, instance=None, owner=None):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
        if instance is None:
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
            raise AttributeError(
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
                "The '%s' attribute can only be accessed from %s instances."
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
                % (self.field.name, owner.__name__))
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
        # This is slightly complicated, so worth an explanation.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
        # instance.file`needs to ultimately return some instance of `File`,
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
        # probably a subclass. Additionally, this returned object needs to have
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
        # the FieldFile API so that users can easily do things like
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
        # instance.file.path and have that delegated to the file storage engine.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
        # Easy enough if we're strict about assignment in __set__, but if you
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
        # peek below you can see that we're not. So depending on the current
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
        # value of the field we have to dynamically construct some sort of
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
        # "thing" to return.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
        # The instance dict contains whatever was originally assigned
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
        # in __set__.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
        file = instance.__dict__[self.field.name]
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
        # If this value is a string (instance.file = "path/to/file") or None
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
        # then we simply wrap it with the appropriate attribute class according
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
        # to the file field. [This is FieldFile for FileFields and
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
        # ImageFieldFile for ImageFields; it's also conceivable that user
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
        # subclasses might also want to subclass the attribute class]. This
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
        # object understands how to convert a path to a file, and also how to
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
        # handle None.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
        if isinstance(file, basestring) or file is None:
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
            attr = self.field.attr_class(instance, self.field, file)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
            instance.__dict__[self.field.name] = attr
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
        # Other types of files may be assigned as well, but they need to have
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
        # the FieldFile interface added to the. Thus, we wrap any other type of
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
        # File inside a FieldFile (well, the field's attr_class, which is
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
        # usually FieldFile).
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
        elif isinstance(file, File) and not isinstance(file, FieldFile):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
            file_copy = self.field.attr_class(instance, self.field, file.name)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
            file_copy.file = file
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
            file_copy._committed = False
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
            instance.__dict__[self.field.name] = file_copy
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
        # Finally, because of the (some would say boneheaded) way pickle works,
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
        # the underlying FieldFile might not actually itself have an associated
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
        # file. So we need to reset the details of the FieldFile in those cases.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
        elif isinstance(file, FieldFile) and not hasattr(file, 'field'):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
            file.instance = instance
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
            file.field = self.field
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
            file.storage = self.field.storage
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
        # That was fun, wasn't it?
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
        return instance.__dict__[self.field.name]
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
    def __set__(self, instance, value):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
        instance.__dict__[self.field.name] = value
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
class FileField(Field):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
    # The class to wrap instance attributes in. Accessing the file object off
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
    # the instance will always return an instance of attr_class.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
    attr_class = FieldFile
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
    # The descriptor to use for accessing the attribute off of the class.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
    descriptor_class = FileDescriptor
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
    description = ugettext_lazy("File path")
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
    def __init__(self, verbose_name=None, name=None, upload_to='', storage=None, **kwargs):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
        for arg in ('primary_key', 'unique'):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
            if arg in kwargs:
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
                raise TypeError("'%s' is not a valid argument for %s." % (arg, self.__class__))
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
        self.storage = storage or default_storage
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
        self.upload_to = upload_to
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
        if callable(upload_to):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
            self.generate_filename = upload_to
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
        kwargs['max_length'] = kwargs.get('max_length', 100)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
        super(FileField, self).__init__(verbose_name, name, **kwargs)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
    def get_internal_type(self):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
        return "FileField"
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
    def get_prep_lookup(self, lookup_type, value):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
        if hasattr(value, 'name'):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
            value = value.name
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
        return super(FileField, self).get_prep_lookup(lookup_type, value)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
    def get_prep_value(self, value):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
        "Returns field's value prepared for saving into a database."
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
        # Need to convert File objects provided via a form to unicode for database insertion
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
        if value is None:
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
            return None
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
        return unicode(value)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
    def pre_save(self, model_instance, add):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
        "Returns field's value just before saving."
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
        file = super(FileField, self).pre_save(model_instance, add)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
        if file and not file._committed:
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
            # Commit the file to storage prior to saving the model
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
            file.save(file.name, file, save=False)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
        return file
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
    def contribute_to_class(self, cls, name):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
        super(FileField, self).contribute_to_class(cls, name)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
        setattr(cls, self.name, self.descriptor_class(self))
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
        signals.post_delete.connect(self.delete_file, sender=cls)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
    def delete_file(self, instance, sender, **kwargs):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
        file = getattr(instance, self.attname)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
        # If no other object of this type references the file,
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
        # and it's not the default value for future objects,
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
        # delete it from the backend.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
        if file and file.name != self.default and \
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
            not sender._default_manager.filter(**{self.name: file.name}):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
                file.delete(save=False)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
        elif file:
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
            # Otherwise, just close the file, so it doesn't tie up resources.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
            file.close()
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
    def get_directory_name(self):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
        return os.path.normpath(force_unicode(datetime.datetime.now().strftime(smart_str(self.upload_to))))
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
    def get_filename(self, filename):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
        return os.path.normpath(self.storage.get_valid_name(os.path.basename(filename)))
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
    def generate_filename(self, instance, filename):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
        return os.path.join(self.get_directory_name(), self.get_filename(filename))
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
    def save_form_data(self, instance, data):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
        if data:
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
            setattr(instance, self.name, data)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
    def formfield(self, **kwargs):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
        defaults = {'form_class': forms.FileField, 'max_length': self.max_length}
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
        # If a file has been provided previously, then the form doesn't require
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
        # that a new file is provided this time.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
        # The code to mark the form field as not required is used by
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
        # form_for_instance, but can probably be removed once form_for_instance
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
        # is gone. ModelForm uses a different method to check for an existing file.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
        if 'initial' in kwargs:
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
            defaults['required'] = False
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
        defaults.update(kwargs)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
        return super(FileField, self).formfield(**defaults)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
class ImageFileDescriptor(FileDescriptor):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
    """
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
    Just like the FileDescriptor, but for ImageFields. The only difference is
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
    assigning the width/height to the width_field/height_field, if appropriate.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
    """
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
    def __set__(self, instance, value):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
        previous_file = instance.__dict__.get(self.field.name)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
        super(ImageFileDescriptor, self).__set__(instance, value)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
        # To prevent recalculating image dimensions when we are instantiating
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
        # an object from the database (bug #11084), only update dimensions if
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
        # the field had a value before this assignment.  Since the default
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
        # value for FileField subclasses is an instance of field.attr_class,
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
        # previous_file will only be None when we are called from
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
        # Model.__init__().  The ImageField.update_dimension_fields method
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
        # hooked up to the post_init signal handles the Model.__init__() cases.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
        # Assignment happening outside of Model.__init__() will trigger the
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
        # update right here.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
        if previous_file is not None:
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
            self.field.update_dimension_fields(instance, force=True)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
class ImageFieldFile(ImageFile, FieldFile):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
    def delete(self, save=True):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
        # Clear the image dimensions cache
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
        if hasattr(self, '_dimensions_cache'):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
            del self._dimensions_cache
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
        super(ImageFieldFile, self).delete(save)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
class ImageField(FileField):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
    attr_class = ImageFieldFile
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
    descriptor_class = ImageFileDescriptor
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
    description = ugettext_lazy("File path")
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
    def __init__(self, verbose_name=None, name=None, width_field=None, height_field=None, **kwargs):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
        self.width_field, self.height_field = width_field, height_field
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
        FileField.__init__(self, verbose_name, name, **kwargs)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
    def contribute_to_class(self, cls, name):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
        super(ImageField, self).contribute_to_class(cls, name)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
        # Attach update_dimension_fields so that dimension fields declared
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
        # after their corresponding image field don't stay cleared by
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
        # Model.__init__, see bug #11196.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
        signals.post_init.connect(self.update_dimension_fields, sender=cls)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
    def update_dimension_fields(self, instance, force=False, *args, **kwargs):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
        """
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
        Updates field's width and height fields, if defined.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
        This method is hooked up to model's post_init signal to update
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
        dimensions after instantiating a model instance.  However, dimensions
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
        won't be updated if the dimensions fields are already populated.  This
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
        avoids unnecessary recalculation when loading an object from the
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
        database.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
        Dimensions can be forced to update with force=True, which is how
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
        ImageFileDescriptor.__set__ calls this method.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
        """
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
        # Nothing to update if the field doesn't have have dimension fields.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
        has_dimension_fields = self.width_field or self.height_field
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
        if not has_dimension_fields:
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
            return
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
        # getattr will call the ImageFileDescriptor's __get__ method, which
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
        # coerces the assigned value into an instance of self.attr_class
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
        # (ImageFieldFile in this case).
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
        file = getattr(instance, self.attname)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
        # Nothing to update if we have no file and not being forced to update.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
        if not file and not force:
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
            return
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
        dimension_fields_filled = not(
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
            (self.width_field and not getattr(instance, self.width_field))
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
            or (self.height_field and not getattr(instance, self.height_field))
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
        )
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
        # When both dimension fields have values, we are most likely loading
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
        # data from the database or updating an image field that already had
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
        # an image stored.  In the first case, we don't want to update the
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
        # dimension fields because we are already getting their values from the
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   379
        # database.  In the second case, we do want to update the dimensions
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
        # fields and will skip this return because force will be True since we
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   381
        # were called from ImageFileDescriptor.__set__.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
        if dimension_fields_filled and not force:
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
            return
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
        # file should be an instance of ImageFieldFile or should be None.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
        if file:
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
            width = file.width
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
            height = file.height
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
        else:
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
            # No file, so clear dimensions fields.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
            width = None
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
            height = None
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
        # Update the width and height fields.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
        if self.width_field:
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
            setattr(instance, self.width_field, width)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   397
        if self.height_field:
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   398
            setattr(instance, self.height_field, height)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   399
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   400
    def formfield(self, **kwargs):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
        defaults = {'form_class': forms.ImageField}
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   402
        defaults.update(kwargs)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   403
        return super(ImageField, self).formfield(**defaults)