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
-
|
+
|
|
| 1 | class 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 |
diff --git a/django/contrib/gis/tests/layermap/tests.py b/django/contrib/gis/tests/layermap/tests.py
index ef0e15e..cc5d2ca 100644
a
|
b
|
|
6 | 6 | |
7 | 7 | from django.utils.unittest import TestCase |
8 | 8 | |
| 9 | from django.db.utils import ConnectionRouter |
9 | 10 | from django.contrib.gis.gdal import DataSource |
10 | 11 | from django.contrib.gis.tests.utils import mysql |
11 | 12 | from django.contrib.gis.utils.layermapping import LayerMapping, LayerMapError, InvalidDecimal, MissingForeignKey |
… |
… |
|
26 | 27 | NUMS = [1, 2, 1, 19, 1] # Number of polygons for each. |
27 | 28 | STATES = ['Texas', 'Texas', 'Texas', 'Hawaii', 'Colorado'] |
28 | 29 | |
| 30 | OTHER_ROUTER = 'django.contrib.gis.tests.layermap.routers.OtherRouter' |
| 31 | |
29 | 32 | class LayerMapTest(TestCase): |
30 | 33 | |
31 | 34 | def test01_init(self): |
… |
… |
def test07_invalid_layer(self):
|
278 | 281 | lm = LayerMapping(Invalid, invalid_shp, invalid_mapping, |
279 | 282 | source_srs=4326) |
280 | 283 | lm.save(silent=True) |
| 284 | |
| 285 | |
| 286 | class 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') |
diff --git a/django/contrib/gis/utils/layermapping.py b/django/contrib/gis/utils/layermapping.py
index 154617f..0d6d59d 100644
a
|
b
|
|
9 | 9 | import sys |
10 | 10 | from decimal import Decimal |
11 | 11 | from django.core.exceptions import ObjectDoesNotExist |
12 | | from django.db import connections, DEFAULT_DB_ALIAS |
| 12 | from django.db import connections, router |
13 | 13 | from django.contrib.gis.db.models import GeometryField |
14 | 14 | from django.contrib.gis.gdal import (CoordTransform, DataSource, |
15 | 15 | OGRException, OGRGeometry, OGRGeomType, SpatialReference) |
… |
… |
class LayerMapping(object):
|
66 | 66 | def __init__(self, model, data, mapping, layer=0, |
67 | 67 | source_srs=None, encoding=None, |
68 | 68 | transaction_mode='commit_on_success', |
69 | | transform=True, unique=None, using=DEFAULT_DB_ALIAS): |
| 69 | transform=True, unique=None, using=None): |
70 | 70 | """ |
71 | 71 | A LayerMapping object is initialized using the given Model (not an instance), |
72 | 72 | a DataSource (or string path to an OGR-supported data file), and a mapping |
… |
… |
def __init__(self, model, data, mapping, layer=0,
|
80 | 80 | self.ds = data |
81 | 81 | self.layer = self.ds[layer] |
82 | 82 | |
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 |
85 | 88 | |
86 | 89 | # Setting the mapping & model attributes. |
87 | 90 | self.mapping = mapping |