1
0
mirror of https://github.com/django/django.git synced 2025-07-05 02:09:13 +00:00

[1.1.X] 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.

Backport of r12584 from trunk.


git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12585 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Justin Bronn 2010-02-24 21:26:11 +00:00
parent 4006402570
commit 49194df8c3

View File

@ -1,13 +1,11 @@
""" """
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): class GeometryProxy(object):
def __init__(self, klass, field): def __init__(self, klass, field):
""" """
@ -23,6 +21,10 @@ class GeometryProxy(object):
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]
@ -51,8 +53,8 @@ class GeometryProxy(object):
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)))