Django

Code

Changeset 9336

Show
Ignore:
Timestamp:
11/05/08 11:51:11 (2 months ago)
Author:
jbronn
Message:

Fixed #9364 -- now uses the appropriate database table for inherited GeometryFields; now uses the SpatialBackend booleans in tests.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/contrib/gis/db/models/query.py

    r8219 r9336  
    606606        if aggregate: self.query.aggregate = True 
    607607 
    608         # Is this operation going to be on a related geographic field? 
    609         if not geo_field in self.model._meta.fields: 
     608        opts = self.model._meta 
     609        if not geo_field in opts.fields: 
     610            # Is this operation going to be on a related geographic field? 
    610611            # If so, it'll have to be added to the select related information 
    611612            # (e.g., if 'location__point' was given as the field name). 
     
    614615            rel_table, rel_col = self.query.related_select_cols[self.query.related_select_fields.index(geo_field)] 
    615616            return self.query._field_column(geo_field, rel_table) 
     617        elif not geo_field in opts.local_fields: 
     618            # This geographic field is inherited from another model, so we have to 
     619            # use the db table for the _parent_ model instead. 
     620            tmp_fld, parent_model, direct, m2m = opts.get_field_by_name(geo_field.name) 
     621            return self.query._field_column(geo_field, parent_model._meta.db_table) 
    616622        else: 
    617623            return self.query._field_column(geo_field) 
  • django/trunk/django/contrib/gis/tests/geoapp/models.py

    r8219 r9336  
    1717    def __unicode__(self): return self.name 
    1818 
     19# This is an inherited model from City 
     20class PennsylvaniaCity(City): 
     21    county = models.CharField(max_length=30) 
     22    objects = models.GeoManager() # TODO: This should be implicitly inherited. 
     23 
    1924class State(models.Model): 
    2025    name = models.CharField(max_length=30) 
  • django/trunk/django/contrib/gis/tests/geoapp/tests.py

    r8502 r9336  
    11import os, unittest 
    2 from models import Country, City, State, Feature, MinusOneSRID 
     2from models import Country, City, PennsylvaniaCity, State, Feature, MinusOneSRID 
    33from django.contrib.gis import gdal 
    44from django.contrib.gis.db.backend import SpatialBackend 
    55from django.contrib.gis.geos import * 
    66from django.contrib.gis.measure import Distance 
    7 from django.contrib.gis.tests.utils import no_oracle, no_postgis, oracle, postgis 
     7from django.contrib.gis.tests.utils import no_oracle, no_postgis 
    88 
    99# TODO: Some tests depend on the success/failure of previous tests, these should 
     
    1818        "Testing geographic initial SQL." 
    1919        if DISABLE: return 
    20         if oracle: 
     20        if SpatialBackend.oracle: 
    2121            # Oracle doesn't allow strings longer than 4000 characters 
    2222            # in SQL files, and I'm stumped on how to use Oracle BFILE's 
     
    3939 
    4040        # Oracle cannot handle NULL geometry values w/certain queries. 
    41         if oracle: n_state = 2 
     41        if SpatialBackend.oracle: n_state = 2 
    4242        else: n_state = 3 
    4343        self.assertEqual(n_state, State.objects.count()) 
     
    148148        ptown2 = City.objects.gml(precision=9).get(name='Pueblo') 
    149149 
    150         if oracle: 
     150        if SpatialBackend.oracle: 
    151151            # No precision parameter for Oracle :-/ 
    152152            import re 
     
    168168        # Asserting the result of the transform operation with the values in 
    169169        #  the pre-transformed points.  Oracle does not have the 3084 SRID. 
    170         if not oracle: 
     170        if not SpatialBackend.oracle: 
    171171            h = City.objects.transform(htown.srid).get(name='Houston') 
    172172            self.assertEqual(3084, h.point.srid) 
     
    215215        self.assertEqual(7, qs1.count()) 
    216216 
    217         if not postgis: 
     217        if not SpatialBackend.postgis: 
    218218            # TODO: Do NULL columns bork queries on PostGIS?  The following 
    219219            # error is encountered: 
     
    232232        #  and Oklahoma City because 'contained' only checks on the 
    233233        #  _bounding box_ of the Geometries. 
    234         if not oracle: 
     234        if not SpatialBackend.oracle: 
    235235            qs = City.objects.filter(point__contained=texas.mpoly) 
    236236            self.assertEqual(3, qs.count()) 
     
    260260 
    261261        # OK City is contained w/in bounding box of Texas. 
    262         if not oracle: 
     262        if not SpatialBackend.oracle: 
    263263            qs = Country.objects.filter(mpoly__bbcontains=okcity.point) 
    264264            self.assertEqual(1, len(qs)) 
     
    273273 
    274274        # Oracle doesn't have SRID 3084, using 41157. 
    275         if oracle: 
     275        if SpatialBackend.oracle: 
    276276            # San Antonio in 'Texas 4205, Southern Zone (1983, meters)' (SRID 41157) 
    277277            # Used the following Oracle SQL to get this value: 
     
    288288        # `ST_Intersects`, so contains is used instead. 
    289289        nad_pnt = fromstr(nad_wkt, srid=nad_srid) 
    290         if oracle: 
     290        if SpatialBackend.oracle: 
    291291            tx = Country.objects.get(mpoly__contains=nad_pnt)  
    292292        else: 
     
    330330 
    331331        # Saving another commonwealth w/a NULL geometry. 
    332         if not oracle: 
     332        if not SpatialBackend.oracle: 
    333333            # TODO: Fix saving w/NULL geometry on Oracle. 
    334334            State(name='Northern Mariana Islands', poly=None).save() 
     
    399399 
    400400        # Relate works differently for the different backends. 
    401         if postgis: 
     401        if SpatialBackend.postgis: 
    402402            contains_mask = 'T*T***FF*' 
    403403            within_mask = 'T*F**F***' 
    404404            intersects_mask = 'T********' 
    405         elif oracle: 
     405        elif SpatialBackend.oracle: 
    406406            contains_mask = 'contains' 
    407407            within_mask = 'inside' 
     
    418418 
    419419        # Testing intersection relation mask. 
    420         if not oracle: 
     420        if not SpatialBackend.oracle: 
    421421            self.assertEqual('Texas', Country.objects.get(mpoly__relate=(pnt1, intersects_mask)).name) 
    422422            self.assertEqual('Texas', Country.objects.get(mpoly__relate=(pnt2, intersects_mask)).name) 
     
    480480        if DISABLE: return 
    481481        qs = State.objects.exclude(poly__isnull=True).centroid() 
    482         if oracle: tol = 0.1 
     482        if SpatialBackend.oracle: tol = 0.1 
    483483        else: tol = 0.000000001 
    484484        for s in qs: 
     
    537537            # Oracle will return 1 for the number of geometries on non-collections, 
    538538            # whereas PostGIS will return None. 
    539             if postgis: self.assertEqual(None, c.num_geom) 
     539            if SpatialBackend.postgis: self.assertEqual(None, c.num_geom) 
    540540            else: self.assertEqual(1, c.num_geom) 
    541541 
     
    544544        if DISABLE: return 
    545545        for c in Country.objects.num_points(): self.assertEqual(c.mpoly.num_points, c.num_points) 
    546         if postgis: 
     546        if SpatialBackend.postgis: 
    547547            # Oracle cannot count vertices in Point geometries. 
    548548            for c in City.objects.num_points(): self.assertEqual(1, c.num_points) 
     
    558558            self.assertEqual(c.mpoly.sym_difference(geom), c.sym_difference) 
    559559            self.assertEqual(c.mpoly.union(geom), c.union) 
     560 
     561    def test26_inherited_geofields(self): 
     562        "Test GeoQuerySet methods on inherited Geometry fields." 
     563        # Creating a Pennsylvanian city. 
     564        mansfield = PennsylvaniaCity.objects.create(name='Mansfield', county='Tioga', point='POINT(-77.071445 41.823881)') 
     565 
     566        # All transformation SQL will need to be performed on the 
     567        # _parent_ table. 
     568        qs = PennsylvaniaCity.objects.transform(32128) 
     569         
     570        self.assertEqual(1, qs.count()) 
     571        for pc in qs: self.assertEqual(32128, pc.point.srid) 
    560572 
    561573from test_feeds import GeoFeedTest