Ticket #10411: update-fix-v2.diff

File update-fix-v2.diff, 16.3 KB (added by jbronn, 16 years ago)
  • django/db/models/sql/subqueries.py

     
    239239        """
    240240        from django.db.models.base import Model
    241241        for field, model, val in values_seq:
    242             # FIXME: Some sort of db_prep_* is probably more appropriate here.
    243             if field.rel and isinstance(val, Model):
    244                 val = val.pk
     242            if hasattr(val, 'prepare_database_save'):
     243                val = val.prepare_database_save(field)
     244            else:
     245                val = field.get_db_prep_save(val)
    245246
    246247            # Getting the placeholder for the field.
    247248            if hasattr(field, 'get_placeholder'):
  • django/db/models/base.py

     
    499499            setattr(self, cachename, obj)
    500500        return getattr(self, cachename)
    501501
     502    def prepare_database_save(self, unused):
     503        return self.pk
    502504
    503505
    504506############################################
  • django/db/models/expressions.py

     
    9090    def __ror__(self, other):
    9191        return self._combine(other, self.OR, True)
    9292
     93    def prepare_database_save(self, unused):
     94        return self
     95
    9396class F(ExpressionNode):
    9497    """
    9598    An expression representing the value of the given field.
  • django/contrib/gis/db/backend/adaptor.py

     
    88        self.srid = geom.srid
    99
    1010    def __eq__(self, other):
    11         return self.wkt == other.wkt and self.srid == other.srid 
     11        return self.wkt == other.wkt and self.srid == other.srid
    1212
    1313    def __str__(self):
    1414        return self.wkt
     15
     16    def prepare_database_save(self, unused):
     17        return self
  • django/contrib/gis/db/backend/postgis/adaptor.py

     
    3131        "Returns a properly quoted string for use in PostgreSQL/PostGIS."
    3232        # Want to use WKB, so wrap with psycopg2 Binary() to quote properly.
    3333        return "%s(%s, %s)" % (GEOM_FROM_WKB, Binary(self.wkb), self.srid or -1)
     34
     35    def prepare_database_save(self, unused):
     36        return self
  • django/contrib/gis/tests/geoapp/test_regress.py

     
     1import os, unittest
     2from django.contrib.gis.db.backend import SpatialBackend
     3from django.contrib.gis.tests.utils import no_mysql, no_oracle, no_postgis
     4from models import City
     5
     6class GeoRegressionTests(unittest.TestCase):
     7
     8    def test01_update(self):
     9        "Testing GeoQuerySet.update(), see #10411."
     10        pnt = City.objects.get(name='Pueblo').point
     11        bak = pnt.clone()
     12        pnt.y += 0.005
     13        pnt.x += 0.005
     14
     15        City.objects.filter(name='Pueblo').update(point=pnt)
     16        self.assertEqual(pnt, City.objects.get(name='Pueblo').point)
     17        City.objects.filter(name='Pueblo').update(point=bak)
     18        self.assertEqual(bak, City.objects.get(name='Pueblo').point)
  • django/contrib/gis/tests/geoapp/tests.py

     
    1313DISABLE = False
    1414
    1515class GeoModelTest(unittest.TestCase):
    16    
     16
    1717    def test01_initial_sql(self):
    1818        "Testing geographic initial SQL."
    1919        if DISABLE: return
     
    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
    2323            # in PLSQL, so we set up the larger geometries manually, rather
    24             # than relying on the initial SQL. 
     24            # than relying on the initial SQL.
    2525
    2626            # Routine for returning the path to the data files.
    2727            data_dir = os.path.join(os.path.dirname(__file__), 'sql')
     
    6565        new = Point(5, 23)
    6666        nullcity.point = new
    6767
    68         # Ensuring that the SRID is automatically set to that of the 
     68        # Ensuring that the SRID is automatically set to that of the
    6969        #  field after assignment, but before saving.
    7070        self.assertEqual(4326, nullcity.point.srid)
    7171        nullcity.save()
     
    9494
    9595        ns = State.objects.get(name='NullState')
    9696        self.assertEqual(ply, ns.poly)
    97        
     97
    9898        # Testing the `ogr` and `srs` lazy-geometry properties.
    9999        if gdal.HAS_GDAL:
    100100            self.assertEqual(True, isinstance(ns.poly.ogr, gdal.OGRGeometry))
     
    120120        qs = City.objects.all()
    121121        self.assertRaises(TypeError, qs.kml, 'name')
    122122
    123         # The reference KML depends on the version of PostGIS used 
     123        # The reference KML depends on the version of PostGIS used
    124124        # (the output stopped including altitude in 1.3.3).
    125125        major, minor1, minor2 = SpatialBackend.version
    126126        ref_kml1 = '<Point><coordinates>-104.609252,38.255001,0</coordinates></Point>'
     
    204204        self.assertRaises(TypeError, Country.objects.make_line)
    205205        # Reference query:
    206206        # SELECT AsText(ST_MakeLine(geoapp_city.point)) FROM geoapp_city;
    207         self.assertEqual(GEOSGeometry('LINESTRING(-95.363151 29.763374,-96.801611 32.782057,-97.521157 34.464642,174.783117 -41.315268,-104.609252 38.255001,-95.23506 38.971823,-87.650175 41.850385,-123.305196 48.462611)', srid=4326),
    208                         City.objects.make_line())
     207        ref_line = GEOSGeometry('LINESTRING(-95.363151 29.763374,-96.801611 32.782057,-97.521157 34.464642,174.783117 -41.315268,-104.609252 38.255001,-95.23506 38.971823,-87.650175 41.850385,-123.305196 48.462611)', srid=4326)
     208        self.assertEqual(ref_line, City.objects.make_line())
    209209
    210210    def test09_disjoint(self):
    211211        "Testing the `disjoint` lookup type."
     
    227227        if DISABLE: return
    228228        # Getting Texas, yes we were a country -- once ;)
    229229        texas = Country.objects.get(name='Texas')
    230        
     230
    231231        # Seeing what cities are in Texas, should get Houston and Dallas,
    232232        #  and Oklahoma City because 'contained' only checks on the
    233233        #  _bounding box_ of the Geometries.
     
    288288        # `ST_Intersects`, so contains is used instead.
    289289        nad_pnt = fromstr(nad_wkt, srid=nad_srid)
    290290        if SpatialBackend.oracle:
    291             tx = Country.objects.get(mpoly__contains=nad_pnt) 
     291            tx = Country.objects.get(mpoly__contains=nad_pnt)
    292292        else:
    293293            tx = Country.objects.get(mpoly__intersects=nad_pnt)
    294294        self.assertEqual('Texas', tx.name)
    295        
     295
    296296        # Creating San Antonio.  Remember the Alamo.
    297297        sa = City(name='San Antonio', point=nad_pnt)
    298298        sa.save()
    299        
     299
    300300        # Now verifying that San Antonio was transformed correctly
    301301        sa = City.objects.get(name='San Antonio')
    302302        self.assertAlmostEqual(wgs_pnt.x, sa.point.x, 6)
     
    321321        # Puerto Rico should be NULL (it's a commonwealth unincorporated territory)
    322322        self.assertEqual(1, len(nullqs))
    323323        self.assertEqual('Puerto Rico', nullqs[0].name)
    324        
     324
    325325        # The valid states should be Colorado & Kansas
    326326        self.assertEqual(2, len(validqs))
    327327        state_names = [s.name for s in validqs]
     
    338338        "Testing the 'left' and 'right' lookup types."
    339339        if DISABLE: return
    340340        # Left: A << B => true if xmax(A) < xmin(B)
    341         # Right: A >> B => true if xmin(A) > xmax(B) 
     341        # Right: A >> B => true if xmin(A) > xmax(B)
    342342        #  See: BOX2D_left() and BOX2D_right() in lwgeom_box2dfloat4.c in PostGIS source.
    343        
     343
    344344        # Getting the borders for Colorado & Kansas
    345345        co_border = State.objects.get(name='Colorado').poly
    346346        ks_border = State.objects.get(name='Kansas').poly
    347347
    348348        # Note: Wellington has an 'X' value of 174, so it will not be considered
    349349        #  to the left of CO.
    350        
     350
    351351        # These cities should be strictly to the right of the CO border.
    352         cities = ['Houston', 'Dallas', 'San Antonio', 'Oklahoma City', 
     352        cities = ['Houston', 'Dallas', 'San Antonio', 'Oklahoma City',
    353353                  'Lawrence', 'Chicago', 'Wellington']
    354354        qs = City.objects.filter(point__right=co_border)
    355355        self.assertEqual(7, len(qs))
     
    365365        #  to the left of CO.
    366366        vic = City.objects.get(point__left=co_border)
    367367        self.assertEqual('Victoria', vic.name)
    368        
     368
    369369        cities = ['Pueblo', 'Victoria']
    370370        qs = City.objects.filter(point__left=ks_border)
    371371        self.assertEqual(2, len(qs))
     
    383383    def test15_relate(self):
    384384        "Testing the 'relate' lookup type."
    385385        if DISABLE: return
    386         # To make things more interesting, we will have our Texas reference point in 
     386        # To make things more interesting, we will have our Texas reference point in
    387387        # different SRIDs.
    388388        pnt1 = fromstr('POINT (649287.0363174 4177429.4494686)', srid=2847)
    389389        pnt2 = fromstr('POINT(-98.4919715741052 29.4333344025053)', srid=4326)
    390390
    391         # Not passing in a geometry as first param shoud 
     391        # Not passing in a geometry as first param shoud
    392392        # raise a type error when initializing the GeoQuerySet
    393393        self.assertRaises(TypeError, Country.objects.filter, mpoly__relate=(23, 'foo'))
    394394        # Making sure the right exception is raised for the given
     
    440440        # Using `field_name` keyword argument in one query and specifying an
    441441        # order in the other (which should not be used because this is
    442442        # an aggregate method on a spatial column)
    443         u1 = qs.unionagg(field_name='point') 
     443        u1 = qs.unionagg(field_name='point')
    444444        u2 = qs.order_by('name').unionagg()
    445445        tol = 0.00001
    446446        if SpatialBackend.oracle:
     
    458458        Feature(name='Point', geom=Point(1, 1)).save()
    459459        Feature(name='LineString', geom=LineString((0, 0), (1, 1), (5, 5))).save()
    460460        Feature(name='Polygon', geom=Polygon(LinearRing((0, 0), (0, 5), (5, 5), (5, 0), (0, 0)))).save()
    461         Feature(name='GeometryCollection', 
    462                 geom=GeometryCollection(Point(2, 2), LineString((0, 0), (2, 2)), 
     461        Feature(name='GeometryCollection',
     462                geom=GeometryCollection(Point(2, 2), LineString((0, 0), (2, 2)),
    463463                                        Polygon(LinearRing((0, 0), (0, 5), (5, 5), (5, 0), (0, 0))))).save()
    464464
    465465        f_1 = Feature.objects.get(name='Point')
     
    474474        f_4 = Feature.objects.get(name='GeometryCollection')
    475475        self.assertEqual(True, isinstance(f_4.geom, GeometryCollection))
    476476        self.assertEqual(f_3.geom, f_4.geom[2])
    477    
     477
    478478    def test19_centroid(self):
    479479        "Testing the `centroid` GeoQuerySet method."
    480480        if DISABLE: return
     
    494494                   'Texas' : fromstr('POINT (-103.002434 36.500397)', srid=4326),
    495495                   }
    496496        elif SpatialBackend.postgis:
    497             # Using GEOSGeometry to compute the reference point on surface values 
     497            # Using GEOSGeometry to compute the reference point on surface values
    498498            # -- since PostGIS also uses GEOS these should be the same.
    499499            ref = {'New Zealand' : Country.objects.get(name='New Zealand').mpoly.point_on_surface,
    500500                   'Texas' : Country.objects.get(name='Texas').mpoly.point_on_surface
     
    533533        if DISABLE: return
    534534        # Both 'countries' only have two geometries.
    535535        for c in Country.objects.num_geom(): self.assertEqual(2, c.num_geom)
    536         for c in City.objects.filter(point__isnull=False).num_geom(): 
     536        for c in City.objects.filter(point__isnull=False).num_geom():
    537537            # Oracle will return 1 for the number of geometries on non-collections,
    538538            # whereas PostGIS will return None.
    539539            if SpatialBackend.postgis: self.assertEqual(None, c.num_geom)
     
    566566        # All transformation SQL will need to be performed on the
    567567        # _parent_ table.
    568568        qs = PennsylvaniaCity.objects.transform(32128)
    569        
     569
    570570        self.assertEqual(1, qs.count())
    571571        for pc in qs: self.assertEqual(32128, pc.point.srid)
    572572
    573573from test_feeds import GeoFeedTest
     574from test_regress import GeoRegressionTests
    574575from test_sitemaps import GeoSitemapTest
     576
    575577def suite():
    576578    s = unittest.TestSuite()
    577579    s.addTest(unittest.makeSuite(GeoModelTest))
    578580    s.addTest(unittest.makeSuite(GeoFeedTest))
    579581    s.addTest(unittest.makeSuite(GeoSitemapTest))
     582    s.addTest(unittest.makeSuite(GeoRegressionTests))
    580583    return s
  • django/contrib/gis/tests/geoapp/tests_mysql.py

     
    88from django.core.exceptions import ImproperlyConfigured
    99
    1010class GeoModelTest(unittest.TestCase):
    11    
     11
    1212    def test01_initial_sql(self):
    1313        "Testing geographic initial SQL."
    1414        # Ensuring that data was loaded from initial SQL.
     
    3838        new = Point(5, 23)
    3939        nullcity.point = new
    4040
    41         # Ensuring that the SRID is automatically set to that of the 
     41        # Ensuring that the SRID is automatically set to that of the
    4242        #  field after assignment, but before saving.
    4343        self.assertEqual(4326, nullcity.point.srid)
    4444        nullcity.save()
     
    6767
    6868        ns = State.objects.get(name='NullState')
    6969        self.assertEqual(ply, ns.poly)
    70        
     70
    7171        # Testing the `ogr` and `srs` lazy-geometry properties.
    7272        if gdal.HAS_GDAL:
    7373            self.assertEqual(True, isinstance(ns.poly.ogr, gdal.OGRGeometry))
     
    8888        "Testing the 'contained', 'contains', and 'bbcontains' lookup types."
    8989        # Getting Texas, yes we were a country -- once ;)
    9090        texas = Country.objects.get(name='Texas')
    91        
     91
    9292        # Seeing what cities are in Texas, should get Houston and Dallas,
    9393        #  and Oklahoma City because MySQL 'within' only checks on the
    9494        #  _bounding box_ of the Geometries.
     
    146146        f1 = Feature(name='Point', geom=Point(1, 1))
    147147        f2 = Feature(name='LineString', geom=LineString((0, 0), (1, 1), (5, 5)))
    148148        f3 = Feature(name='Polygon', geom=Polygon(LinearRing((0, 0), (0, 5), (5, 5), (5, 0), (0, 0))))
    149         f4 = Feature(name='GeometryCollection', 
    150                      geom=GeometryCollection(Point(2, 2), LineString((0, 0), (2, 2)), 
     149        f4 = Feature(name='GeometryCollection',
     150                     geom=GeometryCollection(Point(2, 2), LineString((0, 0), (2, 2)),
    151151                                             Polygon(LinearRing((0, 0), (0, 5), (5, 5), (5, 0), (0, 0)))))
    152152        f1.save()
    153153        f2.save()
     
    166166        f_4 = Feature.objects.get(name='GeometryCollection')
    167167        self.assertEqual(True, isinstance(f_4.geom, GeometryCollection))
    168168        self.assertEqual(f_3.geom, f_4.geom[2])
    169    
     169
    170170    def test07_mysql_limitations(self):
    171171        "Testing that union(), kml(), gml() raise exceptions."
    172172        self.assertRaises(ImproperlyConfigured, City.objects.union, Point(5, 23), field_name='point')
     
    174174        self.assertRaises(ImproperlyConfigured, Country.objects.all().gml, field_name='mpoly')
    175175
    176176from test_feeds import GeoFeedTest
     177from test_regress import GeoRegressionTests
    177178from test_sitemaps import GeoSitemapTest
     179
    178180def suite():
    179181    s = unittest.TestSuite()
    180182    s.addTest(unittest.makeSuite(GeoModelTest))
    181183    s.addTest(unittest.makeSuite(GeoFeedTest))
    182184    s.addTest(unittest.makeSuite(GeoSitemapTest))
     185    s.addTest(unittest.makeSuite(GeoRegressionTests))
    183186    return s
Back to Top