Ticket #10411: update_fix.diff

File update_fix.diff, 6.3 KB (added by jbronn, 15 years ago)

A patch of last resort. Need something more elegant.

  • django/contrib/gis/db/models/sql/__init__.py

     
    11from django.contrib.gis.db.models.sql.conversion import AreaField, DistanceField, GeomField
    2 from django.contrib.gis.db.models.sql.query import GeoQuery
     2from django.contrib.gis.db.models.sql.query import GeoQuery, GeoUpdateQuery
    33from django.contrib.gis.db.models.sql.where import GeoWhereNode
  • django/contrib/gis/db/models/sql/query.py

     
    306306            # Otherwise, check by the given field name -- which may be
    307307            # a lookup to a _related_ geographic field.
    308308            return GeoWhereNode._check_geo_field(self.model._meta, field_name)
     309
     310class GeoUpdateQuery(sql.subqueries.UpdateQuery):
     311
     312    def add_update_fields(self, values_seq):
     313        """
     314        Overloaded so that get_db_prep_save() is called on values
     315        before going to `add_update_fields`.
     316        """
     317        for i, row in enumerate(values_seq):
     318            fld, model, val = row
     319            if isinstance(fld, GeometryField):
     320                values_seq[i] = (fld, model, fld.get_db_prep_save(val))
     321        return super(GeoUpdateQuery, self).add_update_fields(values_seq)
  • django/contrib/gis/db/models/query.py

     
    11from django.core.exceptions import ImproperlyConfigured
    2 from django.db import connection
     2from django.db import connection, transaction
    33from django.db.models.query import sql, QuerySet, Q
    44
    55from django.contrib.gis.db.backend import SpatialBackend
    66from django.contrib.gis.db.models import aggregates
    77from django.contrib.gis.db.models.fields import GeometryField, PointField
    8 from django.contrib.gis.db.models.sql import AreaField, DistanceField, GeomField, GeoQuery, GeoWhereNode
     8from django.contrib.gis.db.models.sql import AreaField, DistanceField, GeomField, GeoQuery, GeoUpdateQuery, GeoWhereNode
    99from django.contrib.gis.measure import Area, Distance
    1010from django.contrib.gis.models import get_srid_info
    1111
     
    2828        super(GeoQuerySet, self).__init__(model=model, query=query)
    2929        self.query = query or GeoQuery(self.model, connection)
    3030
     31    #### Overloaded methods ####
     32    def update(self, **kwargs):
     33        """
     34        Updates all elements in the current QuerySet, setting all the given
     35        fields to the appropriate values.
     36
     37        Overloaded to use GeoUpdateQuery instead of UpdateQuery.
     38        """
     39        assert self.query.can_filter(), \
     40                "Cannot update a query once a slice has been taken."
     41        query = self.query.clone(GeoUpdateQuery)
     42        query.add_update_values(kwargs)
     43        rows = query.execute_sql(None)
     44        transaction.commit_unless_managed()
     45        self._result_cache = None
     46        return rows
     47    update.alters_data = True
     48
     49    #### Routines unique to GeoQuerySet ####
     50
    3151    def area(self, tolerance=0.05, **kwargs):
    3252        """
    3353        Returns the area of the geographic field in an `area` attribute on
  • django/contrib/gis/db/backend/postgis/creation.py

     
    140140    # Closing the connection
    141141    connection.close()
    142142    settings.DATABASE_NAME = db_name
     143    settings.DATABASE_SUPPORTS_TRANSACTIONS = connection.creation._rollback_works()
    143144
    144145    # Syncing the database
    145146    call_command('syncdb', verbosity=verbosity, interactive=interactive)
  • django/contrib/gis/tests/geoapp/test_regress.py

     
     1import os, unittest
     2from django.test import TestCase
     3from django.contrib.gis.db.backend import SpatialBackend
     4from django.contrib.gis.tests.utils import no_mysql, no_oracle, no_postgis
     5from models import City
     6
     7class GeoRegressionTests(TestCase):
     8
     9    def test01_update(self):
     10        "Testing GeoQuerySet.update(), see #10411."
     11        pnt = City.objects.get(name='Pueblo').point
     12        bak = pnt.clone()
     13        pnt.y += 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)
     19       
     20
  • django/contrib/gis/tests/geoapp/tests.py

     
    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()
     579    s.addTest(unittest.makeSuite(GeoRegressionTests))
    577580    s.addTest(unittest.makeSuite(GeoModelTest))
    578581    s.addTest(unittest.makeSuite(GeoFeedTest))
    579582    s.addTest(unittest.makeSuite(GeoSitemapTest))
  • django/contrib/gis/tests/geoapp/tests_mysql.py

     
    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()
     182    s.addTest(unittest.makeSuite(GeoRegressionTests))
    180183    s.addTest(unittest.makeSuite(GeoModelTest))
    181184    s.addTest(unittest.makeSuite(GeoFeedTest))
    182185    s.addTest(unittest.makeSuite(GeoSitemapTest))
Back to Top