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:
Justin Bronn 2010-02-24 21:20:02 +00:00
parent c8cd8b80aa
commit dc1ad69f30
1 changed files with 27 additions and 25 deletions

View File

@ -1,13 +1,11 @@
"""
The GeometryProxy object, allows for lazy-geometries. The proxy uses
Python descriptors for instantiating and setting Geometry objects
corresponding to geographic model fields.
The GeometryProxy object, allows for lazy-geometries. The proxy uses
Python descriptors for instantiating and setting Geometry objects
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):
"""
@ -23,6 +21,10 @@ class GeometryProxy(object):
class specified during initialization and the HEXEWKB value of the field.
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.
geom_value = obj.__dict__[self._field.attname]
@ -51,8 +53,8 @@ class GeometryProxy(object):
if isinstance(value, self._klass) and (str(value.geom_type).upper() == gtype or gtype == 'GEOMETRY'):
# Assigning the SRID to the geometry.
if value.srid is None: value.srid = self._field.srid
elif isinstance(value, (NoneType, StringType, UnicodeType)):
# Set with None, WKT, or HEX
elif value is None or isinstance(value, (basestring, buffer)):
# Set with None, WKT, HEX, or WKB
pass
else:
raise TypeError('cannot set %s GeometryProxy with value of type: %s' % (obj.__class__.__name__, type(value)))