Ticket #17687: pull-112.patch

File pull-112.patch, 4.2 KB (added by nosa_manuel, 13 years ago)

From Github pull request #112

  • new file django/contrib/gis/tests/layermap/routers.py

    From 1a0e0fd28e953136d63752363d4782e3883a833b Mon Sep 17 00:00:00 2001
    From: Noah Seger <nosamanuel@gmail.com>
    Date: Tue, 14 Feb 2012 17:33:13 -0600
    Subject: [PATCH] Use router by default to get LayerMapping DB
    
    ---
     django/contrib/gis/tests/layermap/routers.py |   12 ++++++++++++
     django/contrib/gis/tests/layermap/tests.py   |   19 +++++++++++++++++++
     django/contrib/gis/utils/layermapping.py     |   11 +++++++----
     3 files changed, 38 insertions(+), 4 deletions(-)
     create mode 100644 django/contrib/gis/tests/layermap/routers.py
    
    diff --git a/django/contrib/gis/tests/layermap/routers.py b/django/contrib/gis/tests/layermap/routers.py
    new file mode 100644
    index 0000000..3f071ea
    - +  
     1class OtherRouter(object):
     2    def db_for_read(self, model, **hints):
     3        return 'other'
     4
     5    def db_for_write(self, model, **hints):
     6        return self.db_for_read(model, **hints)
     7
     8    def allow_relation(self, obj1, obj2, **hints):
     9        return None
     10
     11    def allow_syncdb(self, db, model):
     12        return True
  • django/contrib/gis/tests/layermap/tests.py

    diff --git a/django/contrib/gis/tests/layermap/tests.py b/django/contrib/gis/tests/layermap/tests.py
    index ef0e15e..cc5d2ca 100644
    a b  
    66
    77from django.utils.unittest import TestCase
    88
     9from django.db.utils import ConnectionRouter
    910from django.contrib.gis.gdal import DataSource
    1011from django.contrib.gis.tests.utils import mysql
    1112from django.contrib.gis.utils.layermapping import LayerMapping, LayerMapError, InvalidDecimal, MissingForeignKey
     
    2627NUMS   = [1, 2, 1, 19, 1] # Number of polygons for each.
    2728STATES = ['Texas', 'Texas', 'Texas', 'Hawaii', 'Colorado']
    2829
     30OTHER_ROUTER = 'django.contrib.gis.tests.layermap.routers.OtherRouter'
     31
    2932class LayerMapTest(TestCase):
    3033
    3134    def test01_init(self):
    def test07_invalid_layer(self):  
    278281        lm = LayerMapping(Invalid, invalid_shp, invalid_mapping,
    279282                          source_srs=4326)
    280283        lm.save(silent=True)
     284
     285
     286class LayerMapRouterTest(TestCase):
     287
     288    def setUp(self):
     289        from django.contrib.gis.utils import layermapping
     290        self._original_router = layermapping.router
     291        layermapping.router = ConnectionRouter([OTHER_ROUTER])
     292
     293    def tearDown(self):
     294        from django.contrib.gis.utils import layermapping
     295        layermapping.router = self._original_router
     296
     297    def test01_layermapping_default_db(self):
     298        lm = LayerMapping(City, city_shp, city_mapping)
     299        self.assertEqual(lm.using, 'other')
  • django/contrib/gis/utils/layermapping.py

    diff --git a/django/contrib/gis/utils/layermapping.py b/django/contrib/gis/utils/layermapping.py
    index 154617f..0d6d59d 100644
    a b  
    99import sys
    1010from decimal import Decimal
    1111from django.core.exceptions import ObjectDoesNotExist
    12 from django.db import connections, DEFAULT_DB_ALIAS
     12from django.db import connections, router
    1313from django.contrib.gis.db.models import GeometryField
    1414from django.contrib.gis.gdal import (CoordTransform, DataSource,
    1515    OGRException, OGRGeometry, OGRGeomType, SpatialReference)
    class LayerMapping(object):  
    6666    def __init__(self, model, data, mapping, layer=0,
    6767                 source_srs=None, encoding=None,
    6868                 transaction_mode='commit_on_success',
    69                  transform=True, unique=None, using=DEFAULT_DB_ALIAS):
     69                 transform=True, unique=None, using=None):
    7070        """
    7171        A LayerMapping object is initialized using the given Model (not an instance),
    7272        a DataSource (or string path to an OGR-supported data file), and a mapping
    def __init__(self, model, data, mapping, layer=0,  
    8080            self.ds = data
    8181        self.layer = self.ds[layer]
    8282
    83         self.using = using
    84         self.spatial_backend = connections[using].ops
     83        if using is not None:
     84            self.using = using
     85        else:
     86            self.using = router.db_for_write(model)
     87        self.spatial_backend = connections[self.using].ops
    8588
    8689        # Setting the mapping & model attributes.
    8790        self.mapping = mapping
Back to Top