1
0
mirror of https://github.com/django/django.git synced 2025-07-05 10:19:20 +00:00

[soc2009/multidb] Merged up to trunk r10865

git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/multidb@10887 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Alex Gaynor 2009-06-02 02:46:02 +00:00
parent 059dac0207
commit 4cd29f2520
17 changed files with 1188 additions and 470 deletions

File diff suppressed because it is too large Load Diff

View File

@ -143,7 +143,7 @@ class GoogleMap(object):
@property @property
def icons(self): def icons(self):
"Returns a sequence of GIcon objects in this map." "Returns a sequence of GIcon objects in this map."
return [marker.icon for marker in self.markers if marker.icon] return set([marker.icon for marker in self.markers if marker.icon])
class GoogleMapSet(GoogleMap): class GoogleMapSet(GoogleMap):
@ -221,6 +221,6 @@ class GoogleMapSet(GoogleMap):
@property @property
def icons(self): def icons(self):
"Returns a sequence of all icons in each map of the set." "Returns a sequence of all icons in each map of the set."
icons = [] icons = set()
for map in self.maps: icons.extend(map.icons) for map in self.maps: icons |= map.icons
return icons return icons

View File

@ -231,6 +231,14 @@ class GIcon(object):
self.iconanchor = iconanchor self.iconanchor = iconanchor
self.infowindowanchor = infowindowanchor self.infowindowanchor = infowindowanchor
def __cmp__(self, other):
return cmp(self.varname, other.varname)
def __hash__(self):
# XOR with hash of GIcon type so that hash('varname') won't
# equal hash(GIcon('varname')).
return hash(self.__class__) ^ hash(self.varname)
class GMarker(GOverlayBase): class GMarker(GOverlayBase):
""" """
A Python wrapper for the Google GMarker object. For more information A Python wrapper for the Google GMarker object. For more information

View File

@ -12,12 +12,12 @@ from django.contrib.gis.gdal.field import OFTDate, OFTDateTime, OFTInteger, OFTR
def mapping(data_source, geom_name='geom', layer_key=0, multi_geom=False): def mapping(data_source, geom_name='geom', layer_key=0, multi_geom=False):
""" """
Given a DataSource, generates a dictionary that may be used Given a DataSource, generates a dictionary that may be used
for invoking the LayerMapping utility. for invoking the LayerMapping utility.
Keyword Arguments: Keyword Arguments:
`geom_name` => The name of the geometry field to use for the model. `geom_name` => The name of the geometry field to use for the model.
`layer_key` => The key for specifying which layer in the DataSource to use; `layer_key` => The key for specifying which layer in the DataSource to use;
defaults to 0 (the first layer). May be an integer index or a string defaults to 0 (the first layer). May be an integer index or a string
identifier for the layer. identifier for the layer.
@ -31,7 +31,7 @@ def mapping(data_source, geom_name='geom', layer_key=0, multi_geom=False):
pass pass
else: else:
raise TypeError('Data source parameter must be a string or a DataSource object.') raise TypeError('Data source parameter must be a string or a DataSource object.')
# Creating the dictionary. # Creating the dictionary.
_mapping = {} _mapping = {}
@ -52,32 +52,32 @@ def ogrinspect(*args, **kwargs):
model name this function will generate a GeoDjango model. model name this function will generate a GeoDjango model.
Usage: Usage:
>>> from django.contrib.gis.utils import ogrinspect >>> from django.contrib.gis.utils import ogrinspect
>>> ogrinspect('/path/to/shapefile.shp','NewModel') >>> ogrinspect('/path/to/shapefile.shp','NewModel')
...will print model definition to stout ...will print model definition to stout
or put this in a python script and use to redirect the output to a new or put this in a python script and use to redirect the output to a new
model like: model like:
$ python generate_model.py > myapp/models.py $ python generate_model.py > myapp/models.py
# generate_model.py # generate_model.py
from django.contrib.gis.utils import ogrinspect from django.contrib.gis.utils import ogrinspect
shp_file = 'data/mapping_hacks/world_borders.shp' shp_file = 'data/mapping_hacks/world_borders.shp'
model_name = 'WorldBorders' model_name = 'WorldBorders'
print ogrinspect(shp_file, model_name, multi_geom=True, srid=4326, print ogrinspect(shp_file, model_name, multi_geom=True, srid=4326,
geom_name='shapes', blank=True) geom_name='shapes', blank=True)
Required Arguments Required Arguments
`datasource` => string or DataSource object to file pointer `datasource` => string or DataSource object to file pointer
`model name` => string of name of new model class to create `model name` => string of name of new model class to create
Optional Keyword Arguments Optional Keyword Arguments
`geom_name` => For specifying the model name for the Geometry Field. `geom_name` => For specifying the model name for the Geometry Field.
Otherwise will default to `geom` Otherwise will default to `geom`
`layer_key` => The key for specifying which layer in the DataSource to use; `layer_key` => The key for specifying which layer in the DataSource to use;
@ -86,24 +86,24 @@ def ogrinspect(*args, **kwargs):
`srid` => The SRID to use for the Geometry Field. If it can be determined, `srid` => The SRID to use for the Geometry Field. If it can be determined,
the SRID of the datasource is used. the SRID of the datasource is used.
`multi_geom` => Boolean (default: False) - specify as multigeometry. `multi_geom` => Boolean (default: False) - specify as multigeometry.
`name_field` => String - specifies a field name to return for the `name_field` => String - specifies a field name to return for the
`__unicode__` function (which will be generated if specified). `__unicode__` function (which will be generated if specified).
`imports` => Boolean (default: True) - set to False to omit the `imports` => Boolean (default: True) - set to False to omit the
`from django.contrib.gis.db import models` code from the `from django.contrib.gis.db import models` code from the
autogenerated models thus avoiding duplicated imports when building autogenerated models thus avoiding duplicated imports when building
more than one model by batching ogrinspect() more than one model by batching ogrinspect()
`decimal` => Boolean or sequence (default: False). When set to True `decimal` => Boolean or sequence (default: False). When set to True
all generated model fields corresponding to the `OFTReal` type will all generated model fields corresponding to the `OFTReal` type will
be `DecimalField` instead of `FloatField`. A sequence of specific be `DecimalField` instead of `FloatField`. A sequence of specific
field names to generate as `DecimalField` may also be used. field names to generate as `DecimalField` may also be used.
`blank` => Boolean or sequence (default: False). When set to True all `blank` => Boolean or sequence (default: False). When set to True all
generated model fields will have `blank=True`. If the user wants to generated model fields will have `blank=True`. If the user wants to
give specific fields to have blank, then a list/tuple of OGR field give specific fields to have blank, then a list/tuple of OGR field
names may be used. names may be used.
@ -111,13 +111,13 @@ def ogrinspect(*args, **kwargs):
model fields will have `null=True`. If the user wants to specify model fields will have `null=True`. If the user wants to specify
give specific fields to have null, then a list/tuple of OGR field give specific fields to have null, then a list/tuple of OGR field
names may be used. names may be used.
Note: This routine calls the _ogrinspect() helper to do the heavy lifting. Note: This routine calls the _ogrinspect() helper to do the heavy lifting.
""" """
return '\n'.join(s for s in _ogrinspect(*args, **kwargs)) return '\n'.join(s for s in _ogrinspect(*args, **kwargs))
def _ogrinspect(data_source, model_name, geom_name='geom', layer_key=0, srid=None, def _ogrinspect(data_source, model_name, geom_name='geom', layer_key=0, srid=None,
multi_geom=False, name_field=None, imports=True, multi_geom=False, name_field=None, imports=True,
decimal=False, blank=False, null=False): decimal=False, blank=False, null=False):
""" """
Helper routine for `ogrinspect` that generates GeoDjango models corresponding Helper routine for `ogrinspect` that generates GeoDjango models corresponding
@ -140,7 +140,7 @@ def _ogrinspect(data_source, model_name, geom_name='geom', layer_key=0, srid=Non
# keyword arguments. # keyword arguments.
def process_kwarg(kwarg): def process_kwarg(kwarg):
if isinstance(kwarg, (list, tuple)): if isinstance(kwarg, (list, tuple)):
return [s.lower() for s in kwarg] return [s.lower() for s in kwarg]
elif kwarg: elif kwarg:
return [s.lower() for s in ogr_fields] return [s.lower() for s in ogr_fields]
else: else:
@ -164,18 +164,18 @@ def _ogrinspect(data_source, model_name, geom_name='geom', layer_key=0, srid=Non
yield '' yield ''
yield 'class %s(models.Model):' % model_name yield 'class %s(models.Model):' % model_name
for field_name, width, precision, field_type in izip(ogr_fields, layer.field_widths, layer.field_precisions, layer.field_types): for field_name, width, precision, field_type in izip(ogr_fields, layer.field_widths, layer.field_precisions, layer.field_types):
# The model field name. # The model field name.
mfield = field_name.lower() mfield = field_name.lower()
if mfield[-1:] == '_': mfield += 'field' if mfield[-1:] == '_': mfield += 'field'
# Getting the keyword args string. # Getting the keyword args string.
kwargs_str = get_kwargs_str(field_name) kwargs_str = get_kwargs_str(field_name)
if field_type is OFTReal: if field_type is OFTReal:
# By default OFTReals are mapped to `FloatField`, however, they # By default OFTReals are mapped to `FloatField`, however, they
# may also be mapped to `DecimalField` if specified in the # may also be mapped to `DecimalField` if specified in the
# `decimal` keyword. # `decimal` keyword.
if field_name.lower() in decimal_fields: if field_name.lower() in decimal_fields:
yield ' %s = models.DecimalField(max_digits=%d, decimal_places=%d%s)' % (mfield, width, precision, kwargs_str) yield ' %s = models.DecimalField(max_digits=%d, decimal_places=%d%s)' % (mfield, width, precision, kwargs_str)
@ -192,8 +192,8 @@ def _ogrinspect(data_source, model_name, geom_name='geom', layer_key=0, srid=Non
elif field_type is OFTDate: elif field_type is OFTDate:
yield ' %s = models.TimeField(%s)' % (mfield, kwargs_str[2:]) yield ' %s = models.TimeField(%s)' % (mfield, kwargs_str[2:])
else: else:
raise TypeError('Unknown field type %s in %s' % (fld_type, mfield)) raise TypeError('Unknown field type %s in %s' % (field_type, mfield))
# TODO: Autodetection of multigeometry types (see #7218). # TODO: Autodetection of multigeometry types (see #7218).
gtype = layer.geom_type gtype = layer.geom_type
if multi_geom and gtype.num in (1, 2, 3): if multi_geom and gtype.num in (1, 2, 3):

View File

@ -142,13 +142,13 @@ class FileDescriptor(object):
""" """
The descriptor for the file attribute on the model instance. Returns a The descriptor for the file attribute on the model instance. Returns a
FieldFile when accessed so you can do stuff like:: FieldFile when accessed so you can do stuff like::
>>> instance.file.size >>> instance.file.size
Assigns a file object on assignment so you can do:: Assigns a file object on assignment so you can do::
>>> instance.file = File(...) >>> instance.file = File(...)
""" """
def __init__(self, field): def __init__(self, field):
self.field = field self.field = field
@ -156,9 +156,9 @@ class FileDescriptor(object):
def __get__(self, instance=None, owner=None): def __get__(self, instance=None, owner=None):
if instance is None: if instance is None:
raise AttributeError( raise AttributeError(
"The '%s' attribute can only be accessed from %s instances." "The '%s' attribute can only be accessed from %s instances."
% (self.field.name, owner.__name__)) % (self.field.name, owner.__name__))
# This is slightly complicated, so worth an explanation. # This is slightly complicated, so worth an explanation.
# instance.file`needs to ultimately return some instance of `File`, # instance.file`needs to ultimately return some instance of `File`,
# probably a subclass. Additionally, this returned object needs to have # probably a subclass. Additionally, this returned object needs to have
@ -168,8 +168,8 @@ class FileDescriptor(object):
# peek below you can see that we're not. So depending on the current # 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 # value of the field we have to dynamically construct some sort of
# "thing" to return. # "thing" to return.
# The instance dict contains whatever was originally assigned # The instance dict contains whatever was originally assigned
# in __set__. # in __set__.
file = instance.__dict__[self.field.name] file = instance.__dict__[self.field.name]
@ -186,14 +186,14 @@ class FileDescriptor(object):
# Other types of files may be assigned as well, but they need to have # 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 # the FieldFile interface added to the. Thus, we wrap any other type of
# File inside a FieldFile (well, the field's attr_class, which is # File inside a FieldFile (well, the field's attr_class, which is
# usually FieldFile). # usually FieldFile).
elif isinstance(file, File) and not isinstance(file, FieldFile): elif isinstance(file, File) and not isinstance(file, FieldFile):
file_copy = self.field.attr_class(instance, self.field, file.name) file_copy = self.field.attr_class(instance, self.field, file.name)
file_copy.file = file file_copy.file = file
file_copy._committed = False file_copy._committed = False
instance.__dict__[self.field.name] = file_copy instance.__dict__[self.field.name] = file_copy
# Finally, because of the (some would say boneheaded) way pickle works, # Finally, because of the (some would say boneheaded) way pickle works,
# the underlying FieldFile might not actually itself have an associated # the underlying FieldFile might not actually itself have an associated
# file. So we need to reset the details of the FieldFile in those cases. # file. So we need to reset the details of the FieldFile in those cases.
@ -201,7 +201,7 @@ class FileDescriptor(object):
file.instance = instance file.instance = instance
file.field = self.field file.field = self.field
file.storage = self.field.storage file.storage = self.field.storage
# That was fun, wasn't it? # That was fun, wasn't it?
return instance.__dict__[self.field.name] return instance.__dict__[self.field.name]
@ -212,7 +212,7 @@ class FileField(Field):
# The class to wrap instance attributes in. Accessing the file object off # The class to wrap instance attributes in. Accessing the file object off
# the instance will always return an instance of attr_class. # the instance will always return an instance of attr_class.
attr_class = FieldFile attr_class = FieldFile
# The descriptor to use for accessing the attribute off of the class. # The descriptor to use for accessing the attribute off of the class.
descriptor_class = FileDescriptor descriptor_class = FileDescriptor
@ -300,40 +300,20 @@ class ImageFileDescriptor(FileDescriptor):
assigning the width/height to the width_field/height_field, if appropriate. assigning the width/height to the width_field/height_field, if appropriate.
""" """
def __set__(self, instance, value): def __set__(self, instance, value):
previous_file = instance.__dict__.get(self.field.name)
super(ImageFileDescriptor, self).__set__(instance, value) super(ImageFileDescriptor, self).__set__(instance, value)
# The rest of this method deals with width/height fields, so we can # To prevent recalculating image dimensions when we are instantiating
# bail early if neither is used. # an object from the database (bug #11084), only update dimensions if
if not self.field.width_field and not self.field.height_field: # the field had a value before this assignment. Since the default
return # value for FileField subclasses is an instance of field.attr_class,
# previous_file will only be None when we are called from
# We need to call the descriptor's __get__ to coerce this assigned # Model.__init__(). The ImageField.update_dimension_fields method
# value into an instance of the right type (an ImageFieldFile, in this # hooked up to the post_init signal handles the Model.__init__() cases.
# case). # Assignment happening outside of Model.__init__() will trigger the
value = self.__get__(instance) # update right here.
if previous_file is not None:
if not value: self.field.update_dimension_fields(instance, force=True)
return
# Get the image dimensions, making sure to leave the file in the same
# state (opened or closed) that we got it in. However, we *don't* rewind
# the file pointer if the file is already open. This is in keeping with
# most Python standard library file operations that leave it up to the
# user code to reset file pointers after operations that move it.
from django.core.files.images import get_image_dimensions
close = value.closed
value.open()
try:
width, height = get_image_dimensions(value)
finally:
if close:
value.close()
# Update the width and height fields
if self.field.width_field:
setattr(value.instance, self.field.width_field, width)
if self.field.height_field:
setattr(value.instance, self.field.height_field, height)
class ImageFieldFile(ImageFile, FieldFile): class ImageFieldFile(ImageFile, FieldFile):
def delete(self, save=True): def delete(self, save=True):
@ -350,6 +330,69 @@ class ImageField(FileField):
self.width_field, self.height_field = width_field, height_field self.width_field, self.height_field = width_field, height_field
FileField.__init__(self, verbose_name, name, **kwargs) FileField.__init__(self, verbose_name, name, **kwargs)
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)
def formfield(self, **kwargs): def formfield(self, **kwargs):
defaults = {'form_class': forms.ImageField} defaults = {'form_class': forms.ImageField}
defaults.update(kwargs) defaults.update(kwargs)

View File

@ -689,7 +689,7 @@ class BaseQuery(object):
If 'with_aliases' is true, any column names that are duplicated If 'with_aliases' is true, any column names that are duplicated
(without the table names) are given unique aliases. This is needed in (without the table names) are given unique aliases. This is needed in
some cases to avoid ambiguitity with nested queries. some cases to avoid ambiguity with nested queries.
""" """
qn = self.quote_name_unless_alias qn = self.quote_name_unless_alias
qn2 = self.connection.ops.quote_name qn2 = self.connection.ops.quote_name
@ -1303,7 +1303,7 @@ class BaseQuery(object):
opts = self.model._meta opts = self.model._meta
root_alias = self.tables[0] root_alias = self.tables[0]
seen = {None: root_alias} seen = {None: root_alias}
# Skip all proxy to the root proxied model # Skip all proxy to the root proxied model
proxied_model = get_proxied_model(opts) proxied_model = get_proxied_model(opts)
@ -1732,7 +1732,7 @@ class BaseQuery(object):
raise MultiJoin(pos + 1) raise MultiJoin(pos + 1)
if model: if model:
# The field lives on a base class of the current model. # The field lives on a base class of the current model.
# Skip the chain of proxy to the concrete proxied model # Skip the chain of proxy to the concrete proxied model
proxied_model = get_proxied_model(opts) proxied_model = get_proxied_model(opts)
for int_model in opts.get_base_chain(model): for int_model in opts.get_base_chain(model):
@ -2362,7 +2362,7 @@ class BaseQuery(object):
return cursor return cursor
if result_type == SINGLE: if result_type == SINGLE:
if self.ordering_aliases: if self.ordering_aliases:
return cursor.fetchone()[:-len(results.ordering_aliases)] return cursor.fetchone()[:-len(self.ordering_aliases)]
return cursor.fetchone() return cursor.fetchone()
# The MULTI case. # The MULTI case.

View File

@ -584,7 +584,7 @@ class BaseModelFormSet(BaseFormSet):
else: else:
return ugettext("Please correct the duplicate data for %(field)s, " return ugettext("Please correct the duplicate data for %(field)s, "
"which must be unique.") % { "which must be unique.") % {
"field": get_text_list(unique_check, _("and")), "field": get_text_list(unique_check, unicode(_("and"))),
} }
def get_date_error_message(self, date_check): def get_date_error_message(self, date_check):

View File

@ -1175,8 +1175,9 @@ True
>>> instance.height >>> instance.height
16 16
# Delete the current file since this is not done by Django. # Delete the current file since this is not done by Django, but don't save
>>> instance.image.delete() # because the dimension fields are not null=True.
>>> instance.image.delete(save=False)
>>> f = ImageFileForm(data={'description': u'An image'}, files={'image': SimpleUploadedFile('test.png', image_data)}) >>> f = ImageFileForm(data={'description': u'An image'}, files={'image': SimpleUploadedFile('test.png', image_data)})
>>> f.is_valid() >>> f.is_valid()
@ -1207,9 +1208,9 @@ True
>>> instance.width >>> instance.width
16 16
# Delete the current image since this is not done by Django. # Delete the current file since this is not done by Django, but don't save
# because the dimension fields are not null=True.
>>> instance.image.delete() >>> instance.image.delete(save=False)
# Override the file by uploading a new one. # Override the file by uploading a new one.
@ -1224,8 +1225,9 @@ True
>>> instance.width >>> instance.width
48 48
# Delete the current file since this is not done by Django. # Delete the current file since this is not done by Django, but don't save
>>> instance.image.delete() # because the dimension fields are not null=True.
>>> instance.image.delete(save=False)
>>> instance.delete() >>> instance.delete()
>>> f = ImageFileForm(data={'description': u'Changed it'}, files={'image': SimpleUploadedFile('test2.png', image_data2)}) >>> f = ImageFileForm(data={'description': u'Changed it'}, files={'image': SimpleUploadedFile('test2.png', image_data2)})
@ -1239,8 +1241,9 @@ True
>>> instance.width >>> instance.width
48 48
# Delete the current file since this is not done by Django. # Delete the current file since this is not done by Django, but don't save
>>> instance.image.delete() # because the dimension fields are not null=True.
>>> instance.image.delete(save=False)
>>> instance.delete() >>> instance.delete()
# Test the non-required ImageField # Test the non-required ImageField

View File

@ -536,7 +536,7 @@ class DjangoAdminSettingsDirectory(AdminScriptTestCase):
args = ['startapp','settings_test'] args = ['startapp','settings_test']
out, err = self.run_django_admin(args,'settings') out, err = self.run_django_admin(args,'settings')
self.assertNoOutput(err) self.assertNoOutput(err)
self.assertTrue(os.path.exists(os.path.join(test_dir, 'settings_test'))) self.assert_(os.path.exists(os.path.join(test_dir, 'settings_test')))
shutil.rmtree(os.path.join(test_dir, 'settings_test')) shutil.rmtree(os.path.join(test_dir, 'settings_test'))
def test_builtin_command(self): def test_builtin_command(self):

View File

@ -1,93 +0,0 @@
import os
import tempfile
import shutil
from django.db import models
from django.core.files.storage import FileSystemStorage
from django.core.files.base import ContentFile
# Test for correct behavior of width_field/height_field.
# Of course, we can't run this without PIL.
try:
# Checking for the existence of Image is enough for CPython, but
# for PyPy, you need to check for the underlying modules
from PIL import Image, _imaging
except ImportError:
Image = None
# If we have PIL, do these tests
if Image:
temp_storage_dir = tempfile.mkdtemp()
temp_storage = FileSystemStorage(temp_storage_dir)
class Person(models.Model):
name = models.CharField(max_length=50)
mugshot = models.ImageField(storage=temp_storage, upload_to='tests',
height_field='mug_height',
width_field='mug_width')
mug_height = models.PositiveSmallIntegerField()
mug_width = models.PositiveSmallIntegerField()
__test__ = {'API_TESTS': """
>>> from django.core.files import File
>>> image_data = open(os.path.join(os.path.dirname(__file__), "test.png"), 'rb').read()
>>> p = Person(name="Joe")
>>> p.mugshot.save("mug", ContentFile(image_data))
>>> p.mugshot.width
16
>>> p.mugshot.height
16
>>> p.mug_height
16
>>> p.mug_width
16
# Bug #9786: Ensure '==' and '!=' work correctly.
>>> image_data = open(os.path.join(os.path.dirname(__file__), "test1.png"), 'rb').read()
>>> p1 = Person(name="Bob")
>>> p1.mugshot.save("mug", ContentFile(image_data))
>>> p2 = Person.objects.get(name="Joe")
>>> p.mugshot == p2.mugshot
True
>>> p.mugshot != p2.mugshot
False
>>> p.mugshot != p1.mugshot
True
Bug #9508: Similarly to the previous test, make sure hash() works as expected
(equal items must hash to the same value).
>>> hash(p.mugshot) == hash(p2.mugshot)
True
# Bug #8175: correctly delete files that have been removed off the file system.
>>> import os
>>> p2 = Person(name="Fred")
>>> p2.mugshot.save("shot", ContentFile(image_data))
>>> os.remove(p2.mugshot.path)
>>> p2.delete()
# Bug #8534: FileField.size should not leave the file open.
>>> p3 = Person(name="Joan")
>>> p3.mugshot.save("shot", ContentFile(image_data))
# Get a "clean" model instance
>>> p3 = Person.objects.get(name="Joan")
# It won't have an opened file.
>>> p3.mugshot.closed
True
# After asking for the size, the file should still be closed.
>>> _ = p3.mugshot.size
>>> p3.mugshot.closed
True
# Make sure that wrapping the file in a file still works
>>> p3.mugshot.file.open()
>>> p = Person.objects.create(name="Bob The Builder", mugshot=File(p3.mugshot.file))
>>> p.save()
>>> p3.mugshot.file.close()
# Delete all test files
>>> shutil.rmtree(temp_storage_dir)
"""}

View File

@ -161,9 +161,9 @@ class FileStoragePathParsing(TestCase):
self.storage.save('dotted.path/test', ContentFile("1")) self.storage.save('dotted.path/test', ContentFile("1"))
self.storage.save('dotted.path/test', ContentFile("2")) self.storage.save('dotted.path/test', ContentFile("2"))
self.assertFalse(os.path.exists(os.path.join(self.storage_dir, 'dotted_.path'))) self.failIf(os.path.exists(os.path.join(self.storage_dir, 'dotted_.path')))
self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/test'))) self.assert_(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/test')))
self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/test_'))) self.assert_(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/test_')))
def test_first_character_dot(self): def test_first_character_dot(self):
""" """
@ -173,13 +173,13 @@ class FileStoragePathParsing(TestCase):
self.storage.save('dotted.path/.test', ContentFile("1")) self.storage.save('dotted.path/.test', ContentFile("1"))
self.storage.save('dotted.path/.test', ContentFile("2")) self.storage.save('dotted.path/.test', ContentFile("2"))
self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/.test'))) self.assert_(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/.test')))
# Before 2.6, a leading dot was treated as an extension, and so # Before 2.6, a leading dot was treated as an extension, and so
# underscore gets added to beginning instead of end. # underscore gets added to beginning instead of end.
if sys.version_info < (2, 6): if sys.version_info < (2, 6):
self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/_.test'))) self.assert_(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/_.test')))
else: else:
self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/.test_'))) self.assert_(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/.test_')))
if Image is not None: if Image is not None:
class DimensionClosingBug(TestCase): class DimensionClosingBug(TestCase):

Binary file not shown.

After

Width:  |  Height:  |  Size: 87 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 87 B

View File

@ -0,0 +1,420 @@
import os
import shutil
from django.core.files import File
from django.core.files.base import ContentFile
from django.core.files.images import ImageFile
from django.test import TestCase
from models import Image, Person, PersonWithHeight, PersonWithHeightAndWidth, \
PersonDimensionsFirst, PersonTwoImages, TestImageFieldFile
# If PIL available, do these tests.
if Image:
from models import temp_storage_dir
class ImageFieldTestMixin(object):
"""
Mixin class to provide common functionality to ImageField test classes.
"""
# Person model to use for tests.
PersonModel = PersonWithHeightAndWidth
# File class to use for file instances.
File = ImageFile
def setUp(self):
"""
Creates a pristine temp directory (or deletes and recreates if it
already exists) that the model uses as its storage directory.
Sets up two ImageFile instances for use in tests.
"""
if os.path.exists(temp_storage_dir):
shutil.rmtree(temp_storage_dir)
os.mkdir(temp_storage_dir)
file_path1 = os.path.join(os.path.dirname(__file__), "4x8.png")
self.file1 = self.File(open(file_path1, 'rb'))
file_path2 = os.path.join(os.path.dirname(__file__), "8x4.png")
self.file2 = self.File(open(file_path2, 'rb'))
def tearDown(self):
"""
Removes temp directory and all its contents.
"""
shutil.rmtree(temp_storage_dir)
def check_dimensions(self, instance, width, height,
field_name='mugshot'):
"""
Asserts that the given width and height values match both the
field's height and width attributes and the height and width fields
(if defined) the image field is caching to.
Note, this method will check for dimension fields named by adding
"_width" or "_height" to the name of the ImageField. So, the
models used in these tests must have their fields named
accordingly.
By default, we check the field named "mugshot", but this can be
specified by passing the field_name parameter.
"""
field = getattr(instance, field_name)
# Check height/width attributes of field.
if width is None and height is None:
self.assertRaises(ValueError, getattr, field, 'width')
self.assertRaises(ValueError, getattr, field, 'height')
else:
self.assertEqual(field.width, width)
self.assertEqual(field.height, height)
# Check height/width fields of model, if defined.
width_field_name = field_name + '_width'
if hasattr(instance, width_field_name):
self.assertEqual(getattr(instance, width_field_name), width)
height_field_name = field_name + '_height'
if hasattr(instance, height_field_name):
self.assertEqual(getattr(instance, height_field_name), height)
class ImageFieldTests(ImageFieldTestMixin, TestCase):
"""
Tests for ImageField that don't need to be run with each of the
different test model classes.
"""
def test_equal_notequal_hash(self):
"""
Bug #9786: Ensure '==' and '!=' work correctly.
Bug #9508: make sure hash() works as expected (equal items must
hash to the same value).
"""
# Create two Persons with different mugshots.
p1 = self.PersonModel(name="Joe")
p1.mugshot.save("mug", self.file1)
p2 = self.PersonModel(name="Bob")
p2.mugshot.save("mug", self.file2)
self.assertEqual(p1.mugshot == p2.mugshot, False)
self.assertEqual(p1.mugshot != p2.mugshot, True)
# Test again with an instance fetched from the db.
p1_db = self.PersonModel.objects.get(name="Joe")
self.assertEqual(p1_db.mugshot == p2.mugshot, False)
self.assertEqual(p1_db.mugshot != p2.mugshot, True)
# Instance from db should match the local instance.
self.assertEqual(p1_db.mugshot == p1.mugshot, True)
self.assertEqual(hash(p1_db.mugshot), hash(p1.mugshot))
self.assertEqual(p1_db.mugshot != p1.mugshot, False)
def test_instantiate_missing(self):
"""
If the underlying file is unavailable, still create instantiate the
object without error.
"""
p = self.PersonModel(name="Joan")
p.mugshot.save("shot", self.file1)
p = self.PersonModel.objects.get(name="Joan")
path = p.mugshot.path
shutil.move(path, path + '.moved')
p2 = self.PersonModel.objects.get(name="Joan")
def test_delete_when_missing(self):
"""
Bug #8175: correctly delete an object where the file no longer
exists on the file system.
"""
p = self.PersonModel(name="Fred")
p.mugshot.save("shot", self.file1)
os.remove(p.mugshot.path)
p.delete()
def test_size_method(self):
"""
Bug #8534: FileField.size should not leave the file open.
"""
p = self.PersonModel(name="Joan")
p.mugshot.save("shot", self.file1)
# Get a "clean" model instance
p = self.PersonModel.objects.get(name="Joan")
# It won't have an opened file.
self.assertEqual(p.mugshot.closed, True)
# After asking for the size, the file should still be closed.
_ = p.mugshot.size
self.assertEqual(p.mugshot.closed, True)
def test_pickle(self):
"""
Tests that ImageField can be pickled, unpickled, and that the
image of the unpickled version is the same as the original.
"""
import pickle
p = Person(name="Joe")
p.mugshot.save("mug", self.file1)
dump = pickle.dumps(p)
p2 = Person(name="Bob")
p2.mugshot = self.file1
loaded_p = pickle.loads(dump)
self.assertEqual(p.mugshot, loaded_p.mugshot)
class ImageFieldTwoDimensionsTests(ImageFieldTestMixin, TestCase):
"""
Tests behavior of an ImageField and its dimensions fields.
"""
def test_constructor(self):
"""
Tests assigning an image field through the model's constructor.
"""
p = self.PersonModel(name='Joe', mugshot=self.file1)
self.check_dimensions(p, 4, 8)
p.save()
self.check_dimensions(p, 4, 8)
def test_image_after_constructor(self):
"""
Tests behavior when image is not passed in constructor.
"""
p = self.PersonModel(name='Joe')
# TestImageField value will default to being an instance of its
# attr_class, a TestImageFieldFile, with name == None, which will
# cause it to evaluate as False.
self.assertEqual(isinstance(p.mugshot, TestImageFieldFile), True)
self.assertEqual(bool(p.mugshot), False)
# Test setting a fresh created model instance.
p = self.PersonModel(name='Joe')
p.mugshot = self.file1
self.check_dimensions(p, 4, 8)
def test_create(self):
"""
Tests assigning an image in Manager.create().
"""
p = self.PersonModel.objects.create(name='Joe', mugshot=self.file1)
self.check_dimensions(p, 4, 8)
def test_default_value(self):
"""
Tests that the default value for an ImageField is an instance of
the field's attr_class (TestImageFieldFile in this case) with no
name (name set to None).
"""
p = self.PersonModel()
self.assertEqual(isinstance(p.mugshot, TestImageFieldFile), True)
self.assertEqual(bool(p.mugshot), False)
def test_assignment_to_None(self):
"""
Tests that assigning ImageField to None clears dimensions.
"""
p = self.PersonModel(name='Joe', mugshot=self.file1)
self.check_dimensions(p, 4, 8)
# If image assigned to None, dimension fields should be cleared.
p.mugshot = None
self.check_dimensions(p, None, None)
p.mugshot = self.file2
self.check_dimensions(p, 8, 4)
def test_field_save_and_delete_methods(self):
"""
Tests assignment using the field's save method and deletion using
the field's delete method.
"""
p = self.PersonModel(name='Joe')
p.mugshot.save("mug", self.file1)
self.check_dimensions(p, 4, 8)
# A new file should update dimensions.
p.mugshot.save("mug", self.file2)
self.check_dimensions(p, 8, 4)
# Field and dimensions should be cleared after a delete.
p.mugshot.delete(save=False)
self.assertEqual(p.mugshot, None)
self.check_dimensions(p, None, None)
def test_dimensions(self):
"""
Checks that dimensions are updated correctly in various situations.
"""
p = self.PersonModel(name='Joe')
# Dimensions should get set if file is saved.
p.mugshot.save("mug", self.file1)
self.check_dimensions(p, 4, 8)
# Test dimensions after fetching from database.
p = self.PersonModel.objects.get(name='Joe')
# Bug 11084: Dimensions should not get recalculated if file is
# coming from the database. We test this by checking if the file
# was opened.
self.assertEqual(p.mugshot.was_opened, False)
self.check_dimensions(p, 4, 8)
# After checking dimensions on the image field, the file will have
# opened.
self.assertEqual(p.mugshot.was_opened, True)
# Dimensions should now be cached, and if we reset was_opened and
# check dimensions again, the file should not have opened.
p.mugshot.was_opened = False
self.check_dimensions(p, 4, 8)
self.assertEqual(p.mugshot.was_opened, False)
# If we assign a new image to the instance, the dimensions should
# update.
p.mugshot = self.file2
self.check_dimensions(p, 8, 4)
# Dimensions were recalculated, and hence file should have opened.
self.assertEqual(p.mugshot.was_opened, True)
class ImageFieldNoDimensionsTests(ImageFieldTwoDimensionsTests):
"""
Tests behavior of an ImageField with no dimension fields.
"""
PersonModel = Person
class ImageFieldOneDimensionTests(ImageFieldTwoDimensionsTests):
"""
Tests behavior of an ImageField with one dimensions field.
"""
PersonModel = PersonWithHeight
class ImageFieldDimensionsFirstTests(ImageFieldTwoDimensionsTests):
"""
Tests behavior of an ImageField where the dimensions fields are
defined before the ImageField.
"""
PersonModel = PersonDimensionsFirst
class ImageFieldUsingFileTests(ImageFieldTwoDimensionsTests):
"""
Tests behavior of an ImageField when assigning it a File instance
rather than an ImageFile instance.
"""
PersonModel = PersonDimensionsFirst
File = File
class TwoImageFieldTests(ImageFieldTestMixin, TestCase):
"""
Tests a model with two ImageFields.
"""
PersonModel = PersonTwoImages
def test_constructor(self):
p = self.PersonModel(mugshot=self.file1, headshot=self.file2)
self.check_dimensions(p, 4, 8, 'mugshot')
self.check_dimensions(p, 8, 4, 'headshot')
p.save()
self.check_dimensions(p, 4, 8, 'mugshot')
self.check_dimensions(p, 8, 4, 'headshot')
def test_create(self):
p = self.PersonModel.objects.create(mugshot=self.file1,
headshot=self.file2)
self.check_dimensions(p, 4, 8)
self.check_dimensions(p, 8, 4, 'headshot')
def test_assignment(self):
p = self.PersonModel()
self.check_dimensions(p, None, None, 'mugshot')
self.check_dimensions(p, None, None, 'headshot')
p.mugshot = self.file1
self.check_dimensions(p, 4, 8, 'mugshot')
self.check_dimensions(p, None, None, 'headshot')
p.headshot = self.file2
self.check_dimensions(p, 4, 8, 'mugshot')
self.check_dimensions(p, 8, 4, 'headshot')
# Clear the ImageFields one at a time.
p.mugshot = None
self.check_dimensions(p, None, None, 'mugshot')
self.check_dimensions(p, 8, 4, 'headshot')
p.headshot = None
self.check_dimensions(p, None, None, 'mugshot')
self.check_dimensions(p, None, None, 'headshot')
def test_field_save_and_delete_methods(self):
p = self.PersonModel(name='Joe')
p.mugshot.save("mug", self.file1)
self.check_dimensions(p, 4, 8, 'mugshot')
self.check_dimensions(p, None, None, 'headshot')
p.headshot.save("head", self.file2)
self.check_dimensions(p, 4, 8, 'mugshot')
self.check_dimensions(p, 8, 4, 'headshot')
# We can use save=True when deleting the image field with null=True
# dimension fields and the other field has an image.
p.headshot.delete(save=True)
self.check_dimensions(p, 4, 8, 'mugshot')
self.check_dimensions(p, None, None, 'headshot')
p.mugshot.delete(save=False)
self.check_dimensions(p, None, None, 'mugshot')
self.check_dimensions(p, None, None, 'headshot')
def test_dimensions(self):
"""
Checks that dimensions are updated correctly in various situations.
"""
p = self.PersonModel(name='Joe')
# Dimensions should get set for the saved file.
p.mugshot.save("mug", self.file1)
p.headshot.save("head", self.file2)
self.check_dimensions(p, 4, 8, 'mugshot')
self.check_dimensions(p, 8, 4, 'headshot')
# Test dimensions after fetching from database.
p = self.PersonModel.objects.get(name='Joe')
# Bug 11084: Dimensions should not get recalculated if file is
# coming from the database. We test this by checking if the file
# was opened.
self.assertEqual(p.mugshot.was_opened, False)
self.assertEqual(p.headshot.was_opened, False)
self.check_dimensions(p, 4, 8,'mugshot')
self.check_dimensions(p, 8, 4, 'headshot')
# After checking dimensions on the image fields, the files will
# have been opened.
self.assertEqual(p.mugshot.was_opened, True)
self.assertEqual(p.headshot.was_opened, True)
# Dimensions should now be cached, and if we reset was_opened and
# check dimensions again, the file should not have opened.
p.mugshot.was_opened = False
p.headshot.was_opened = False
self.check_dimensions(p, 4, 8,'mugshot')
self.check_dimensions(p, 8, 4, 'headshot')
self.assertEqual(p.mugshot.was_opened, False)
self.assertEqual(p.headshot.was_opened, False)
# If we assign a new image to the instance, the dimensions should
# update.
p.mugshot = self.file2
p.headshot = self.file1
self.check_dimensions(p, 8, 4, 'mugshot')
self.check_dimensions(p, 4, 8, 'headshot')
# Dimensions were recalculated, and hence file should have opened.
self.assertEqual(p.mugshot.was_opened, True)
self.assertEqual(p.headshot.was_opened, True)

View File

@ -1,10 +1,23 @@
from django.db import models import os
import tempfile
try: try:
import decimal import decimal
except ImportError: except ImportError:
from django.utils import _decimal as decimal # Python 2.3 fallback from django.utils import _decimal as decimal # Python 2.3 fallback
try:
# Checking for the existence of Image is enough for CPython, but for PyPy,
# you need to check for the underlying modules.
from PIL import Image, _imaging
except ImportError:
Image = None
from django.core.files.storage import FileSystemStorage
from django.db import models
from django.db.models.fields.files import ImageFieldFile, ImageField
class Foo(models.Model): class Foo(models.Model):
a = models.CharField(max_length=10) a = models.CharField(max_length=10)
d = models.DecimalField(max_digits=5, decimal_places=3) d = models.DecimalField(max_digits=5, decimal_places=3)
@ -31,9 +44,97 @@ class Whiz(models.Model):
(0,'Other'), (0,'Other'),
) )
c = models.IntegerField(choices=CHOICES, null=True) c = models.IntegerField(choices=CHOICES, null=True)
class BigD(models.Model): class BigD(models.Model):
d = models.DecimalField(max_digits=38, decimal_places=30) d = models.DecimalField(max_digits=38, decimal_places=30)
class BigS(models.Model): class BigS(models.Model):
s = models.SlugField(max_length=255) s = models.SlugField(max_length=255)
###############################################################################
# ImageField
# If PIL available, do these tests.
if Image:
class TestImageFieldFile(ImageFieldFile):
"""
Custom Field File class that records whether or not the underlying file
was opened.
"""
def __init__(self, *args, **kwargs):
self.was_opened = False
super(TestImageFieldFile, self).__init__(*args,**kwargs)
def open(self):
self.was_opened = True
super(TestImageFieldFile, self).open()
class TestImageField(ImageField):
attr_class = TestImageFieldFile
# Set up a temp directory for file storage.
temp_storage_dir = tempfile.mkdtemp()
temp_storage = FileSystemStorage(temp_storage_dir)
temp_upload_to_dir = os.path.join(temp_storage.location, 'tests')
class Person(models.Model):
"""
Model that defines an ImageField with no dimension fields.
"""
name = models.CharField(max_length=50)
mugshot = TestImageField(storage=temp_storage, upload_to='tests')
class PersonWithHeight(models.Model):
"""
Model that defines an ImageField with only one dimension field.
"""
name = models.CharField(max_length=50)
mugshot = TestImageField(storage=temp_storage, upload_to='tests',
height_field='mugshot_height')
mugshot_height = models.PositiveSmallIntegerField()
class PersonWithHeightAndWidth(models.Model):
"""
Model that defines height and width fields after the ImageField.
"""
name = models.CharField(max_length=50)
mugshot = TestImageField(storage=temp_storage, upload_to='tests',
height_field='mugshot_height',
width_field='mugshot_width')
mugshot_height = models.PositiveSmallIntegerField()
mugshot_width = models.PositiveSmallIntegerField()
class PersonDimensionsFirst(models.Model):
"""
Model that defines height and width fields before the ImageField.
"""
name = models.CharField(max_length=50)
mugshot_height = models.PositiveSmallIntegerField()
mugshot_width = models.PositiveSmallIntegerField()
mugshot = TestImageField(storage=temp_storage, upload_to='tests',
height_field='mugshot_height',
width_field='mugshot_width')
class PersonTwoImages(models.Model):
"""
Model that:
* Defines two ImageFields
* Defines the height/width fields before the ImageFields
* Has a nullalble ImageField
"""
name = models.CharField(max_length=50)
mugshot_height = models.PositiveSmallIntegerField()
mugshot_width = models.PositiveSmallIntegerField()
mugshot = TestImageField(storage=temp_storage, upload_to='tests',
height_field='mugshot_height',
width_field='mugshot_width')
headshot_height = models.PositiveSmallIntegerField(
blank=True, null=True)
headshot_width = models.PositiveSmallIntegerField(
blank=True, null=True)
headshot = TestImageField(blank=True, null=True,
storage=temp_storage, upload_to='tests',
height_field='headshot_height',
width_field='headshot_width')
###############################################################################

View File

@ -6,13 +6,26 @@ from django import forms
from django.db import models from django.db import models
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from models import Foo, Bar, Whiz, BigD, BigS from models import Foo, Bar, Whiz, BigD, BigS, Image
try: try:
from decimal import Decimal from decimal import Decimal
except ImportError: except ImportError:
from django.utils._decimal import Decimal from django.utils._decimal import Decimal
# If PIL available, do these tests.
if Image:
from imagefield import \
ImageFieldTests, \
ImageFieldTwoDimensionsTests, \
ImageFieldNoDimensionsTests, \
ImageFieldOneDimensionTests, \
ImageFieldDimensionsFirstTests, \
ImageFieldUsingFileTests, \
TwoImageFieldTests
class DecimalFieldTests(django.test.TestCase): class DecimalFieldTests(django.test.TestCase):
def test_to_python(self): def test_to_python(self):
f = models.DecimalField(max_digits=4, decimal_places=2) f = models.DecimalField(max_digits=4, decimal_places=2)
@ -131,4 +144,3 @@ class SlugFieldTests(django.test.TestCase):
bs = BigS.objects.create(s = 'slug'*50) bs = BigS.objects.create(s = 'slug'*50)
bs = BigS.objects.get(pk=bs.pk) bs = BigS.objects.get(pk=bs.pk)
self.assertEqual(bs.s, 'slug'*50) self.assertEqual(bs.s, 'slug'*50)