Fixed #11353 -- `GeometryProxy` descriptor no longer chokes when accessed from a class rather than an instance, thanks yml and Tobu; removed unnecessary imports from `types` and cleaned up whitespace.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@12584 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
c8cd8b80aa
commit
dc1ad69f30
|
@ -1,42 +1,44 @@
|
||||||
"""
|
"""
|
||||||
The GeometryProxy object, allows for lazy-geometries. The proxy uses
|
The GeometryProxy object, allows for lazy-geometries. The proxy uses
|
||||||
Python descriptors for instantiating and setting Geometry objects
|
Python descriptors for instantiating and setting Geometry objects
|
||||||
corresponding to geographic model fields.
|
corresponding to geographic model fields.
|
||||||
|
|
||||||
Thanks to Robert Coup for providing this functionality (see #4322).
|
Thanks to Robert Coup for providing this functionality (see #4322).
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from types import NoneType, StringType, UnicodeType
|
class GeometryProxy(object):
|
||||||
|
def __init__(self, klass, field):
|
||||||
class GeometryProxy(object):
|
|
||||||
def __init__(self, klass, field):
|
|
||||||
"""
|
"""
|
||||||
Proxy initializes on the given Geometry class (not an instance) and
|
Proxy initializes on the given Geometry class (not an instance) and
|
||||||
the GeometryField.
|
the GeometryField.
|
||||||
"""
|
"""
|
||||||
self._field = field
|
self._field = field
|
||||||
self._klass = klass
|
self._klass = klass
|
||||||
|
|
||||||
def __get__(self, obj, type=None):
|
def __get__(self, obj, type=None):
|
||||||
"""
|
"""
|
||||||
This accessor retrieves the geometry, initializing it using the geometry
|
This accessor retrieves the geometry, initializing it using the geometry
|
||||||
class specified during initialization and the HEXEWKB value of the field.
|
class specified during initialization and the HEXEWKB value of the field.
|
||||||
Currently, only GEOS or OGR geometries are supported.
|
Currently, only GEOS or OGR geometries are supported.
|
||||||
"""
|
"""
|
||||||
|
if obj is None:
|
||||||
|
# Accessed on a class, not an instance
|
||||||
|
return self
|
||||||
|
|
||||||
# Getting the value of the field.
|
# Getting the value of the field.
|
||||||
geom_value = obj.__dict__[self._field.attname]
|
geom_value = obj.__dict__[self._field.attname]
|
||||||
|
|
||||||
if isinstance(geom_value, self._klass):
|
if isinstance(geom_value, self._klass):
|
||||||
geom = geom_value
|
geom = geom_value
|
||||||
elif (geom_value is None) or (geom_value==''):
|
elif (geom_value is None) or (geom_value==''):
|
||||||
geom = None
|
geom = None
|
||||||
else:
|
else:
|
||||||
# Otherwise, a Geometry object is built using the field's contents,
|
# Otherwise, a Geometry object is built using the field's contents,
|
||||||
# and the model's corresponding attribute is set.
|
# and the model's corresponding attribute is set.
|
||||||
geom = self._klass(geom_value)
|
geom = self._klass(geom_value)
|
||||||
setattr(obj, self._field.attname, geom)
|
setattr(obj, self._field.attname, geom)
|
||||||
return geom
|
return geom
|
||||||
|
|
||||||
def __set__(self, obj, value):
|
def __set__(self, obj, value):
|
||||||
"""
|
"""
|
||||||
This accessor sets the proxied geometry with the geometry class
|
This accessor sets the proxied geometry with the geometry class
|
||||||
|
@ -45,18 +47,18 @@ class GeometryProxy(object):
|
||||||
"""
|
"""
|
||||||
# The OGC Geometry type of the field.
|
# The OGC Geometry type of the field.
|
||||||
gtype = self._field.geom_type
|
gtype = self._field.geom_type
|
||||||
|
|
||||||
# The geometry type must match that of the field -- unless the
|
# The geometry type must match that of the field -- unless the
|
||||||
# general GeometryField is used.
|
# general GeometryField is used.
|
||||||
if isinstance(value, self._klass) and (str(value.geom_type).upper() == gtype or gtype == 'GEOMETRY'):
|
if isinstance(value, self._klass) and (str(value.geom_type).upper() == gtype or gtype == 'GEOMETRY'):
|
||||||
# Assigning the SRID to the geometry.
|
# Assigning the SRID to the geometry.
|
||||||
if value.srid is None: value.srid = self._field.srid
|
if value.srid is None: value.srid = self._field.srid
|
||||||
elif isinstance(value, (NoneType, StringType, UnicodeType)):
|
elif value is None or isinstance(value, (basestring, buffer)):
|
||||||
# Set with None, WKT, or HEX
|
# Set with None, WKT, HEX, or WKB
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
raise TypeError('cannot set %s GeometryProxy with value of type: %s' % (obj.__class__.__name__, type(value)))
|
raise TypeError('cannot set %s GeometryProxy with value of type: %s' % (obj.__class__.__name__, type(value)))
|
||||||
|
|
||||||
# Setting the objects dictionary with the value, and returning.
|
# Setting the objects dictionary with the value, and returning.
|
||||||
obj.__dict__[self._field.attname] = value
|
obj.__dict__[self._field.attname] = value
|
||||||
return value
|
return value
|
||||||
|
|
Loading…
Reference in New Issue